2020-09-06 06:50:23 +00:00
|
|
|
# internal imports
|
|
|
|
import os,sys; sys.path.append(os.path.abspath(os.path.join(os.path.abspath(os.path.join(os.path.dirname(__file__),'..')),'..')))
|
|
|
|
from komrade import *
|
2020-09-06 19:41:17 +00:00
|
|
|
from komrade.backend import *
|
2020-09-06 06:50:23 +00:00
|
|
|
|
|
|
|
### ACTUAL PHONE CONNECTIONS
|
2020-09-06 19:24:36 +00:00
|
|
|
class TheTelephone(Operator):
|
2020-09-06 06:50:23 +00:00
|
|
|
"""
|
|
|
|
API client class for Caller to interact with The Operator.
|
|
|
|
"""
|
2020-09-06 22:40:53 +00:00
|
|
|
def __init__(self, caller=None, allow_builtin=True):
|
2020-09-06 19:24:36 +00:00
|
|
|
super().__init__(
|
|
|
|
name=TELEPHONE_NAME,
|
|
|
|
path_crypt_keys=PATH_CRYPT_CA_KEYS,
|
|
|
|
path_crypt_data=PATH_CRYPT_CA_KEYS
|
|
|
|
)
|
2020-09-06 22:40:53 +00:00
|
|
|
self.caller=caller
|
|
|
|
self.allow_builtin=allow_builtin
|
2020-09-06 06:50:23 +00:00
|
|
|
|
2020-09-07 06:03:41 +00:00
|
|
|
# @property
|
|
|
|
# def op(self):
|
|
|
|
# global OPERATOR
|
|
|
|
# from komrade.backend.the_operator import TheOperator
|
|
|
|
# if not OPERATOR: OPERATOR=TheOperator()
|
|
|
|
# return OPERATOR
|
2020-09-06 20:17:47 +00:00
|
|
|
|
2020-09-07 07:41:17 +00:00
|
|
|
# async def dial_operator(self,msg):
|
|
|
|
def dial_operator(self,msg):
|
2020-09-06 06:50:23 +00:00
|
|
|
msg=msg.replace('/','_')
|
|
|
|
URL = OPERATOR_API_URL + msg + '/'
|
|
|
|
self.log("DIALING THE OPERATOR:",URL)
|
2020-09-07 06:27:42 +00:00
|
|
|
# cmd='/home/ryan/etc/tor-browser_en-US/Browser/start-tor-browser --new-window "'+URL+'"'
|
|
|
|
# self.log(cmd)
|
|
|
|
# os.system(cmd)
|
|
|
|
# exit()
|
2020-09-07 07:41:17 +00:00
|
|
|
# r=await tor_request_async(URL)
|
|
|
|
r=tor_request(URL)
|
|
|
|
|
|
|
|
self.log('result!?!?!',r)
|
2020-09-07 07:46:58 +00:00
|
|
|
|
|
|
|
if r.status_code==200:
|
|
|
|
return r.text
|
|
|
|
else:
|
|
|
|
self.log('!! error in request',r.status_code,r.text)
|
|
|
|
return None
|
|
|
|
|
2020-09-06 06:50:23 +00:00
|
|
|
|
2020-09-06 22:09:36 +00:00
|
|
|
|
2020-09-06 22:01:36 +00:00
|
|
|
|
|
|
|
|
2020-09-07 07:41:17 +00:00
|
|
|
# async def req(self,json_coming_from_phone={},json_coming_from_caller={},caller=None):
|
|
|
|
def req(self,json_coming_from_phone={},json_coming_from_caller={},caller=None):
|
2020-09-06 19:54:00 +00:00
|
|
|
if not caller: caller=self.caller
|
2020-09-07 06:13:05 +00:00
|
|
|
self.log(f"""
|
|
|
|
RING RING!
|
|
|
|
caller = {caller}
|
|
|
|
json_coming_from_phone = {json_coming_from_phone}
|
2020-09-07 06:13:27 +00:00
|
|
|
json_coming_from_caller = {json_coming_from_caller}""")
|
2020-09-06 23:00:52 +00:00
|
|
|
|
2020-09-06 22:51:51 +00:00
|
|
|
|
2020-09-07 06:03:41 +00:00
|
|
|
# keychain = self.keychain(allow_builtin=self.allow_builtin, force=True)
|
2020-09-06 23:19:45 +00:00
|
|
|
# self.log('about to make a call. my keychain?',keychain)
|
|
|
|
# stop
|
2020-09-06 22:51:51 +00:00
|
|
|
# stop
|
2020-09-06 21:01:29 +00:00
|
|
|
# Three parts of every request:
|
|
|
|
|
|
|
|
# 0) Unencrypted. do not use except for very specific minimal reasons!
|
2020-09-06 22:51:51 +00:00
|
|
|
# exchange half-complete pieces of info, both of which necessary for other
|
2020-09-07 06:18:05 +00:00
|
|
|
|
2020-09-07 06:03:41 +00:00
|
|
|
|
2020-09-07 06:27:42 +00:00
|
|
|
unencr_header = OPERATOR_KEYCHAIN['privkey_decr'] + BSEP2 + TELEPHONE_KEYCHAIN['pubkey_decr']
|
2020-09-07 06:18:05 +00:00
|
|
|
self.log('unencr_header',unencr_header)
|
2020-09-06 06:50:23 +00:00
|
|
|
|
2020-09-06 23:00:52 +00:00
|
|
|
# ewrwerewrwerw
|
2020-09-06 21:01:29 +00:00
|
|
|
# 1) only overall encryption layer E2EE Telephone -> Operator:
|
2020-09-06 06:50:23 +00:00
|
|
|
if json_coming_from_phone:
|
|
|
|
json_coming_from_phone_s = json.dumps(json_coming_from_phone)
|
|
|
|
json_coming_from_phone_b = json_coming_from_phone_s.encode()
|
2020-09-07 06:03:41 +00:00
|
|
|
json_coming_from_phone_b_encr = SMessage(
|
|
|
|
TELEPHONE_KEYCHAIN['privkey'],
|
|
|
|
OPERATOR_KEYCHAIN['pubkey']
|
|
|
|
).wrap(json_coming_from_phone_b)
|
2020-09-06 06:50:23 +00:00
|
|
|
else:
|
|
|
|
json_coming_from_phone_b=b''
|
|
|
|
|
|
|
|
# 2) (optional) extra E2EE encrypted layer Caller -> Operator
|
2020-09-06 19:39:44 +00:00
|
|
|
if json_coming_from_caller and caller:
|
2020-09-06 06:50:23 +00:00
|
|
|
json_coming_from_caller_s = json.dumps(json_coming_from_caller)
|
|
|
|
json_coming_from_caller_b = json_coming_from_caller_s.encode()
|
2020-09-07 06:03:41 +00:00
|
|
|
json_coming_from_caller_b_encr = SMessage(
|
|
|
|
caller.privkey_,
|
|
|
|
OPERATOR_KEYCHAIN['pubkey']
|
|
|
|
).wrap(json_coming_from_caller_b)
|
2020-09-06 06:50:23 +00:00
|
|
|
else:
|
|
|
|
json_coming_from_caller_b_encr = b''
|
|
|
|
|
2020-09-06 22:51:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
req_data_encr = unencr_header + BSEP + json_coming_from_phone_b_encr + BSEP + json_coming_from_caller_b_encr
|
|
|
|
self.log('req_data_encr',req_data_encr)
|
2020-09-06 23:00:52 +00:00
|
|
|
# sewerwe
|
2020-09-06 19:54:00 +00:00
|
|
|
# req_data_encr = SMessage(self.privkey_,self.op.pubkey_).wrap(req_data)
|
2020-09-06 06:50:23 +00:00
|
|
|
req_data_encr_b64 = b64encode(req_data_encr)
|
|
|
|
self.log('req_data_encr_b64 <--',req_data_encr_b64)
|
|
|
|
|
|
|
|
# send!
|
|
|
|
req_data_encr_b64_str = req_data_encr_b64.decode('utf-8')
|
|
|
|
|
2020-09-07 07:35:54 +00:00
|
|
|
#try:
|
2020-09-07 07:41:17 +00:00
|
|
|
res = self.dial_operator(req_data_encr_b64_str)
|
|
|
|
# res = await self.dial_operator(req_data_encr_b64_str)
|
2020-09-07 07:35:54 +00:00
|
|
|
#except TypeError:
|
|
|
|
# res = None
|
2020-09-06 06:50:23 +00:00
|
|
|
self.log('result from operator?',res)
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
2020-09-06 19:54:00 +00:00
|
|
|
|
2020-09-06 06:50:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_call():
|
2020-09-06 19:59:14 +00:00
|
|
|
caller = Caller('marx33') #Caller('marx')
|
2020-09-06 06:50:23 +00:00
|
|
|
# caller.boot(create=True)
|
|
|
|
# print(caller.keychain())
|
2020-09-06 19:59:14 +00:00
|
|
|
# phone = TheTelephone()
|
2020-09-06 08:39:31 +00:00
|
|
|
# req_json = {'_route':'forge_new_keys','name':name, 'pubkey_is_public':pubkey_is_public}}
|
|
|
|
# req_json_s = jsonify(req_json)
|
|
|
|
# res = phone.req({'forge_new_keys':{'name':'marx', 'pubkey_is_public':True}})
|
|
|
|
# print(res)
|
2020-09-07 07:41:17 +00:00
|
|
|
# asyncio.run(caller.get_new_keys())
|
2020-09-07 07:46:58 +00:00
|
|
|
x=caller.get_new_keys()
|
2020-09-06 06:50:23 +00:00
|
|
|
|
2020-09-07 07:46:58 +00:00
|
|
|
print('YEAH COOL',x)
|
2020-09-06 11:48:35 +00:00
|
|
|
|
2020-09-06 06:50:23 +00:00
|
|
|
## main
|
|
|
|
if __name__=='__main__': test_call()
|