You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Comrad/komrade/backend/the_telephone.py

142 lines
4.7 KiB
Python

4 years ago
# 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 *
4 years ago
from komrade.backend import *
4 years ago
### ACTUAL PHONE CONNECTIONS
4 years ago
class TheTelephone(Operator):
4 years ago
"""
API client class for Caller to interact with The Operator.
"""
def __init__(self, caller=None, allow_builtin=True):
4 years ago
super().__init__(
name=TELEPHONE_NAME,
path_crypt_keys=PATH_CRYPT_CA_KEYS,
path_crypt_data=PATH_CRYPT_CA_KEYS
)
self.caller=caller
self.allow_builtin=allow_builtin
4 years ago
4 years ago
# @property
# def op(self):
# global OPERATOR
# from komrade.backend.the_operator import TheOperator
# if not OPERATOR: OPERATOR=TheOperator()
# return OPERATOR
4 years ago
# async def dial_operator(self,msg):
def dial_operator(self,msg):
4 years ago
msg=msg.replace('/','_')
URL = OPERATOR_API_URL + msg + '/'
self.log("DIALING THE OPERATOR:",URL)
# cmd='/home/ryan/etc/tor-browser_en-US/Browser/start-tor-browser --new-window "'+URL+'"'
# self.log(cmd)
# os.system(cmd)
# exit()
# r=await tor_request_async(URL)
r=tor_request(URL)
self.log('result!?!?!',r)
if r.status_code==200:
return r.text
else:
self.log('!! error in request',r.status_code,r.text)
return None
4 years ago
4 years ago
4 years ago
# 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):
4 years ago
if not caller: caller=self.caller
self.log(f"""
RING RING!
caller = {caller}
json_coming_from_phone = {json_coming_from_phone}
json_coming_from_caller = {json_coming_from_caller}""")
4 years ago
# keychain = self.keychain(allow_builtin=self.allow_builtin, force=True)
# self.log('about to make a call. my keychain?',keychain)
# stop
# stop
4 years ago
# Three parts of every request:
# 0) Unencrypted. do not use except for very specific minimal reasons!
# exchange half-complete pieces of info, both of which necessary for other
4 years ago
unencr_header = OPERATOR_KEYCHAIN['privkey_decr'] + BSEP2 + TELEPHONE_KEYCHAIN['pubkey_decr']
self.log('unencr_header',unencr_header)
4 years ago
# ewrwerewrwerw
4 years ago
# 1) only overall encryption layer E2EE Telephone -> Operator:
4 years ago
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()
4 years ago
json_coming_from_phone_b_encr = SMessage(
TELEPHONE_KEYCHAIN['privkey'],
OPERATOR_KEYCHAIN['pubkey']
).wrap(json_coming_from_phone_b)
4 years ago
else:
json_coming_from_phone_b=b''
# 2) (optional) extra E2EE encrypted layer Caller -> Operator
4 years ago
if json_coming_from_caller and caller:
4 years ago
json_coming_from_caller_s = json.dumps(json_coming_from_caller)
json_coming_from_caller_b = json_coming_from_caller_s.encode()
4 years ago
json_coming_from_caller_b_encr = SMessage(
caller.privkey_,
OPERATOR_KEYCHAIN['pubkey']
).wrap(json_coming_from_caller_b)
4 years ago
else:
json_coming_from_caller_b_encr = b''
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)
# sewerwe
4 years ago
# req_data_encr = SMessage(self.privkey_,self.op.pubkey_).wrap(req_data)
4 years ago
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')
#try:
res_s = self.dial_operator(req_data_encr_b64_str)
# try decoding
# res = await self.dial_operator(req_data_encr_b64_str)
#except TypeError:
# res = None
4 years ago
self.log('result from operator?',res)
return res
4 years ago
4 years ago
def test_call():
4 years ago
caller = Caller('marx33') #Caller('marx')
4 years ago
# caller.boot(create=True)
# print(caller.keychain())
4 years ago
# phone = TheTelephone()
4 years ago
# 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)
# asyncio.run(caller.get_new_keys())
x=caller.get_new_keys()
4 years ago
print('YEAH COOL',x)
4 years ago
4 years ago
## main
if __name__=='__main__': test_call()