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

99 lines
2.9 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.
"""
4 years ago
def __init__(self, caller=None, keychain={}):
4 years ago
super().__init__(
name=TELEPHONE_NAME,
4 years ago
keychain=keychain,
4 years ago
path_crypt_keys=PATH_CRYPT_CA_KEYS,
path_crypt_data=PATH_CRYPT_CA_KEYS
)
self.caller=caller
4 years ago
def dial_operator(self,msg):
4 years ago
msg=msg.replace('/','_')
URL = OPERATOR_API_URL + msg + '/'
self.log("DIALING THE OPERATOR:",URL)
4 years ago
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):
4 years ago
if not caller: caller=self.caller
self.log(f"""
RING RING!
caller = {caller}
json_phone = {json_phone}
json_caller = {json_caller}""")
4 years ago
print(type(self),self._keychain,self.keychain())
self.log('op_keychain',op_keychain)
# 1) unencr header
# telephone_pubkey_decr | op_pubkey_decr | op_privkey_decr
unencr_header = phone_keychain['pubkey_decr'] + BSEP2 + op_keychain['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
)
4 years ago
# send
answer = self.dial_operator(encrypted_message_to_operator)
4 years ago
self.log('result from operator?',answer)
return answer
4 years ago
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()