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

92 lines
2.8 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):
global OPERATOR_KEYCHAIN,TELEPHONE_KEYCHAIN
4 years ago
print('OP???',OPERATOR_KEYCHAIN)
print('PH???',TELEPHONE_KEYCHAIN)
4 years ago
super().__init__(
name=TELEPHONE_NAME,
path_crypt_keys=PATH_CRYPT_CA_KEYS,
path_crypt_data=PATH_CRYPT_CA_KEYS
)
4 years ago
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
4 years ago
self._keychain = TELEPHONE_KEYCHAIN
print(type(self._keychain), self._keychain)
4 years ago
4 years ago
def send_and_receive(self,msg_b64_str):
4 years ago
msg=msg_b64_str.replace('/','_')
4 years ago
URL = OPERATOR_API_URL + msg + '/'
self.log("DIALING THE OPERATOR:",URL)
4 years ago
# stop
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)
4 years ago
def ring_operator(self,
from_caller=None,
to_caller=None,
json_phone2phone={},
json_caller2phone={}, # (person) -> operator or operator -> (person)
json_caller2caller={}):
4 years ago
4 years ago
encr_msg_to_send = self.ring_ring(
from_phone=self,
to_phone=self.op,
from_caller=from_caller,
to_caller=to_caller,
4 years ago
json_phone2phone=json_phone2phone,
4 years ago
json_caller2phone=json_caller2phone, # (person) -> operator
json_caller2caller=json_caller2caller)
self.send_and_receive(encr_msg_to_send)
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())
4 years ago
x=caller.get_new_keys(passphrase='1869')
4 years ago
print('YEAH COOL',x)
4 years ago
4 years ago
## main
if __name__=='__main__': test_call()