2
0
mirror of https://github.com/ComradCollective/Comrad synced 2024-11-01 21:40:32 +00:00
Comrad/komrade/backend/the_telephone.py

114 lines
3.4 KiB
Python
Raw Normal View History

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-07 17:27:46 +00:00
def __init__(self, caller=None):
global OPERATOR_KEYCHAIN,TELEPHONE_KEYCHAIN
2020-09-07 17:50:58 +00:00
print('OP???',OPERATOR_KEYCHAIN)
print('PH???',TELEPHONE_KEYCHAIN)
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-07 17:50:58 +00:00
if not TELEPHONE_KEYCHAIN or not OPERATOR_KEYCHAIN:
OPERATOR_KEYCHAIN,TELEPHONE_KEYCHAIN = connect_phonelines()
print('OP2???',OPERATOR_KEYCHAIN)
print('PH2???',TELEPHONE_KEYCHAIN)
self.caller=caller
2020-09-07 17:50:58 +00:00
self._keychain = TELEPHONE_KEYCHAIN
print(type(self._keychain), self._keychain)
2020-09-06 06:50:23 +00:00
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 17:13:39 +00:00
r=komrade_request(URL)
if r.status_code==200:
return r.text
else:
self.log('!! error in request',r.status_code,r.text)
return None
def recv(self,data):
# decrypt
data_in = self.decrypt_incoming(data)
# route
result = self.route(data_json)
# encrypt
data_out = self.encrypt_outgoing(result)
# send
return self.send(res)
# async def req(self,json_phone={},json_caller={},caller=None):
def ask_operator(self,json_phone={},json_caller={},caller=None):
2020-09-06 19:54:00 +00:00
if not caller: caller=self.caller
self.log(f"""
RING RING!
caller = {caller}
json_phone = {json_phone}
json_caller = {json_caller}""")
2020-09-07 17:50:58 +00:00
print('XXXXX',type(self),self._keychain)
print('YYYYY',self.keychain())
# stop
2020-09-07 17:50:58 +00:00
self.log('op_keychain',self.op.keychain())
# 1) unencr header
# telephone_pubkey_decr | op_pubkey_decr | op_privkey_decr
2020-09-07 17:50:58 +00:00
unencr_header = self.pubkey_decr_ + BSEP2 + self.op.pubkey_encr_
# 2) caller privkey?
from_caller_privkey=caller.privkey_ if caller and json_caller else None
# encrypt data
encrypted_message_to_operator = self.encrypt_outgoing(
json_phone=json_phone,
json_caller=json_caller,
from_phone_privkey=phone_keychain['privkey'],
from_caller_privkey=from_caller_privkey,
to_pubkey=op_keychain['pubkey'],
unencr_header=unencr_header
)
2020-09-06 06:50:23 +00:00
# send
answer = self.dial_operator(encrypted_message_to_operator)
2020-09-06 06:50:23 +00:00
self.log('result from operator?',answer)
return answer
2020-09-06 06:50:23 +00:00
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)
# asyncio.run(caller.get_new_keys())
x=caller.get_new_keys()
2020-09-06 06:50:23 +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()