-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.py
More file actions
291 lines (261 loc) · 8.72 KB
/
Copy pathcore.py
File metadata and controls
291 lines (261 loc) · 8.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# import default libs
import sys, os.path
import importlib
import re
import json
# import logging
import urllib.request
import webbrowser as vivaldi
from types import SimpleNamespace as Namespace # from argparse import Namespace
from base64 import b64decode,b64encode
# import additional libs
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP as PKCS1_OAEP
from PyInquirer import style_from_dict, Token, prompt
# import configs
import config
## AICC is a company that has been working from time immemorial
# when the RAM in the PC was barely enough for the consoled chrome
# so among the deadly sins in their corporate RFC is
## comingsoon
#
class MessageList(list):
def __init__(self):
self = list()
def add(self, name, text, date):
self.append({
'name': name,
'text': text,
'date': date
})
#
class Roster(dict):
def __init__(self):
self = dict()
def add(self, name, public_key):
self[name] = public_key
# GLOBALS
SELFNAME = ''
CONTACTS = Roster()
# system magic
def json2obj(data): return json.loads(data, object_hook=lambda bazat: Namespace(**bazat))
def reread_config():
importlib.reload(config)
def token_exist():
return True if (config.access_token!="") else False
def token_set():
if not input("Open settings? (y/N): ").lower().strip()[:1] == "y": sys.exit(1)
vivaldi.open('https://github.com/settings/tokens/')
access_token = input('Paste your token: ')
token_pattern = "^[a-z0-9]{40}$"
if (re.compile(token_pattern).match(access_token)):
# half-kostyle
with open('config.py', 'a') as cfg:
cfg.write(f'\naccess_token = \'{access_token}\'')
print('Token successfully saved')
else:
print('Token\'s format is incorrect. ')
token_set()
def selfkey_exist():
private_file = './keys/private.pem'
return os.path.isfile(private_file)
def selfkey_generate():
if not input("Generate keypare? (y/N): ").lower().strip()[:1] == "y": sys.exit(1)
#
key = RSA.generate(2048)
private_key = key.export_key()
public_key = key.publickey().export_key()
open(config.private_file, "wb").write(private_key)
open(config.public_file, "wb").write(public_key)
#
print('RSA keypare successfully generated')
def selfkey_publish():
public_key = open(config.public_file, "r").read()
PostPublic(config.issue_id['pub_keys'], config.access_token, public_key)
# github api add comment to enc_msgs issue
def PostPublic(pub_keys, access_token, public_key):
url = f'https://api.github.com/repos/IDIDIR/IPM/issues/{pub_keys}/comments?access_token={access_token}'
msg = json.dumps({"body": public_key})
msg = msg.encode('utf-8') # string to bytes
print(msg)
req = urllib.request.Request(url, msg)
try: res = urllib.request.urlopen(req)
except urllib.error.URLError as e:
print(e.reason)
return 'oke'
def roster_init():
global SELFNAME
SELFNAME = GetUsername(config.access_token).login # return False
eContacts = GetContacts(config.issue_id['pub_keys'], config.access_token) # return False
# javascriptostailovie zamashki onelove
{ CONTACTS.add(eContact.user.login, eContact.body) for eContact in eContacts }
# print(CONTACTS)
#
print('Your contacts was loaded')
return True
# github api get username
def GetUsername(access_token):
url = f'https://api.github.com/user?access_token={access_token}'
req = urllib.request.Request(url)
try: res = urllib.request.urlopen(req)
except urllib.error.URLError as e:
print(e.reason)
return json2obj(res.read())
# tmp = commentList(req.read())
# github api load comment in pub_keys issue
def GetContacts(pub_keys, access_token):
url = f'https://api.github.com/repos/IDIDIR/IPM/issues/{pub_keys}/comments?access_token={access_token}'
req = urllib.request.Request(url)
try: res = urllib.request.urlopen(req)
except urllib.error.URLError as e:
print(e.reason)
return json2obj(res.read())
# tmp = commentList(req.read())
def CreateMessage():
receiver = ChooseContact(CONTACTS)
while receiver is False:
print('Invalid username, check case and vse ostal\'noe')
receiver = ChooseContact(CONTACTS)
# message header
header = f'@{receiver}\n'
# send public_2 over GitHub (recipient)
message = input('Private message text: ')
# secure encode
eMessage = Encrypt(message,receiver)
# github api write comment
link = PostMessage(config.issue_id['enc_msgs'], config.access_token, header + eMessage)
print(link)
def LoadMessages():
sender = ChooseContact(CONTACTS)
while sender is False:
print('Invalid username, check case and vse ostal\'noe')
sender = ChooseContact(CONTACTS)
# message header
header = f'@{SELFNAME}\n'
# select contact
# github api parse comment
# need pagination fix
AllEMessages = GetMessages(config.issue_id['enc_msgs'], config.access_token)
MyEMessages = []
# decode in loop
MyMessages = MessageList()
for eMessage in AllEMessages:
if (eMessage.user.login == sender) & (header in eMessage.body):
eMessage.body = eMessage.body.replace(header, '')
MyEMessages.append(eMessage)
# javascriptostailovie zamashki onelove
{ MyMessages.add(eMessage.user.login, Decrypt(eMessage.body), eMessage.created_at) for eMessage in MyEMessages }
#
ShowMessages(MyMessages, sender)
#
def ShowMessages(messages, sender):
print(f'From: {sender}')
{ print(f'{message["date"]}\t{message["text"]}') for message in messages }
# user-frendly interface for select contact
def ChooseContact(contacts):
# kostyle
contacts.update({'back': ''})
registred_contacts = {
'type': 'list',
'name': 'contacts',
'message': 'Select your contact:',
'choices': contacts
}
answers = prompt(registred_contacts)
if(answers['contacts'] == 'back'): main() #
return answers['contacts']
# github api load comment in enc_msgs issue
def GetMessages(enc_msgs, access_token):
# need pagination fix
url = f'https://api.github.com/repos/IDIDIR/IPM/issues/{enc_msgs}/comments?page=0&per_page=65535&access_token={access_token}'
req = urllib.request.Request(url)
try: res = urllib.request.urlopen(req)
except urllib.error.URLError as e:
print(e.reason)
return json2obj(res.read())
# tmp = commentList(req.read())
# github api add comment to enc_msgs issue
def PostMessage(enc_msgs, access_token, eMessage):
url = f'https://api.github.com/repos/IDIDIR/IPM/issues/{enc_msgs}/comments?access_token={access_token}'
msg = json.dumps({"body": eMessage})
msg = msg.encode('utf-8') # string to bytes
req = urllib.request.Request(url, msg)
try: res = urllib.request.urlopen(req)
except urllib.error.URLError as e:
print(e.reason)
return 'oke'
# encrypt single message with public_2 (sender)
def Encrypt(message,receiver):
# more corrected load public, no read local
# public_file = "./keys/public.pem"
# public_key = open(public_file,"r").read()
public_key = CONTACTS[receiver]
public_key = RSA.importKey(public_key)
public_key = PKCS1_OAEP.new(public_key)
eMessage = message.encode('utf-8') # 0
eMessage = public_key.encrypt(eMessage) # 1
eMessage = b64encode(eMessage) # 2
eMessage = eMessage.decode('utf-8') # ¯\_(ツ)_/¯
return eMessage
# decrypt single message with private_2 (recipient)
def Decrypt(eMessage):
private_key = config.private_file
private_file = open(private_key,"r").read()
private_key = RSA.importKey(private_file)
private_key = PKCS1_OAEP.new(private_key)
try:
message = b64decode(eMessage) # 2
message = private_key.decrypt(message) # 1
message = message.decode('utf-8') # 0
except:
message = f'! {eMessage}'
return message
# user-frendly interface for select actions
def ChooseMenuItem():
menu_items = {
'type': 'list',
'name': 'items',
'message': 'Select menu item:',
'choices': ['Post', 'Load', 'Exit', 'Quit', 'Close', 'Kill']
}
answers = prompt(menu_items)
return answers['items']
# general algorithm
def main():
if not (True): pass
#
elif not (token_exist()):
print('Access token don\'t found.\nBro, please:\n0. Open settings of "Personal access tokens"\n1. Generate new token\n2. Check "write:discussion", "public_repo"\n3. Copy&Paste below')
token_set()
reread_config()
main()
elif not (selfkey_exist()):
print('Selfkeys don\'t found.')
selfkey_generate()
reread_config()
selfkey_publish()
main()
elif not (roster_init()):
print('Contacts haven\'t been loaded.')
input()
main()
else:
mode = ChooseMenuItem()
# switch case break foo bazat lorem ipsum
if (mode == 'Post'): CreateMessage()
if (mode == 'Load'): LoadMessages()
if (mode == 'Exit'): sys.exit(1)
if (mode == 'Quit'): sys.exit(1)
if (mode == 'Close'): sys.exit(1)
if (mode == 'Kill'): sys.exit(1)
if __name__ == '__main__':
main()
# class singleComment:
# def __init__(self, raw):
# self.text = raw['body']
# self.name = raw['user']['login']
# class commentList:
# def __init__(self, raws):
# self = json2obj(raws)
# tmp = {raw: singleComment(raw) for raw in tmp}
# tmp = {d.pop(singleComment(raw)) for raw in tmp}