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

101 lines
3.4 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
from komrade.backend.phonelines 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)
4 years ago
# stop
4 years ago
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):
msg_b64_str = b64encode(msg).decode()
4 years ago
msg_b64_str_esc = msg_b64_str.replace('/','_')
self.log('msg_b64_str_esc',type(msg_b64_str_esc),msg_b64_str_esc)
URL = OPERATOR_API_URL + msg_b64_str_esc + '/'
4 years ago
self.log("DIALING THE OPERATOR:",URL)
4 years ago
ringring=komrade_request(URL)
if ringring.status_code==200:
4 years ago
# response back from Operator!
encr_str_response_from_op = ringring.text
self.log('encr_str_response_from_op',encr_str_response_from_op)
4 years ago
return encr_str_response_from_op #.encode()
else:
4 years ago
self.log('!! error in request',ringring.status_code,ringring.text)
return None
4 years ago
def ring_ring(self,with_msg,to_phone=None):
if not to_phone: to_phone=self.op
to_whom = to_phone
4 years ago
4 years ago
# msg is of type
msg_encr_caller2caller_caller2phone = with_msg
4 years ago
# ring 1: encrypt
msg_encr_caller2caller_caller2phone_phone2phone = self.package_msg_to(
4 years ago
msg_encr_caller2caller_caller2phone,
4 years ago
to_whom
4 years ago
)
self.log('final form of encr msg!',msg_encr_caller2caller_caller2phone_phone2phone)
# ring 2: dial and get response
4 years ago
resp_msg_encr_caller2caller_caller2phone_phone2phone = self.send_and_receive(
4 years ago
msg_encr_caller2caller_caller2phone_phone2phone
4 years ago
)
4 years ago
# msg_encr_caller2caller_caller2phone_phone2phone: return
4 years ago
# ring 3: decrypt
4 years ago
resp_msg_encr_caller2caller_caller2phone = self.unpackage_msg_from(
resp_msg_encr_caller2caller_caller2phone_phone2phone,
to_whom
4 years ago
)
4 years ago
return resp_msg_encr_caller2caller_caller2phone
4 years ago
4 years ago
def test_call():
4 years ago
phone = TheTelephone()
pprint(phone.keychain())
# 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 = {'_please':'forge_new_keys','name':name, 'pubkey_is_public':pubkey_is_public}}
4 years ago
# 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
4 years ago
# print('YEAH COOL',x)
4 years ago
4 years ago
## main
if __name__=='__main__': test_call()