From b163c3ac187eec06acd10b88e79b46e8be3e9e95 Mon Sep 17 00:00:00 2001 From: quadrismegistus Date: Mon, 5 Oct 2020 19:51:30 +0100 Subject: [PATCH] to post requests --- comrad/app/main.py | 4 +-- comrad/backend/switchboard.py | 47 +++++++++++++++++++---------- comrad/backend/the_telephone.py | 53 ++++++++++++++++++++++++++------- 3 files changed, 77 insertions(+), 27 deletions(-) diff --git a/comrad/app/main.py b/comrad/app/main.py index a86832e..79bfca0 100644 --- a/comrad/app/main.py +++ b/comrad/app/main.py @@ -963,8 +963,8 @@ class MainApp(MDApp, Logger): with open(path_avatar,'rb') as f: other_data['img_avatar']=f.read() - extra_msg = await self.get_input(f'Include a short message to {meet_name}? (optional)') - other_data['extra_msg']=extra_msg.strip() + # extra_msg = await self.get_input(f'Include a short message to {meet_name}? (optional)') + # other_data['extra_msg']=extra_msg.strip() await self.stat('Sending the invitation...') self.open_map() res = await self.comrad.meet_async( diff --git a/comrad/backend/switchboard.py b/comrad/backend/switchboard.py index cb9a4ba..3016628 100644 --- a/comrad/backend/switchboard.py +++ b/comrad/backend/switchboard.py @@ -24,31 +24,48 @@ class TheSwitchboard(FlaskView, Logger): self._op=OPERATOR=TheOperator() return OPERATOR - - def get(self,data_b64_str_esc): + def post(self): clear_screen() from comrad.cli.artcode import ART_OLDPHONE4 - # self.log(f'Incoming call!: {data_b64_str_esc}') - if not data_b64_str_esc: + data_b=requests.data + + self.log(f'Incoming call! {ART_OLDPHONE4}: {data_b}') + + if not data_b: self.log('empty request!') return OPERATOR_INTERCEPT_MESSAGE - # unenescape - data_b64_str = data_b64_str_esc.replace('_','/') - - # encode to binary - data_b64 = data_b64_str.encode() - data_b = b64decode(data_b64) - # ask operator to answer phone and request resp_data_b = self.op.answer_phone(data_b) # decode to str - resp_data_b64 = b64encode(resp_data_b) - resp_data_b64_str = resp_data_b64.decode() + self.log('about to send back -->',resp_data_b) + return resp_data_b + + # def get(self,data_b64_str_esc): + # clear_screen() + # from comrad.cli.artcode import ART_OLDPHONE4 + # # self.log(f'Incoming call!: {data_b64_str_esc}') + # if not data_b64_str_esc: + # self.log('empty request!') + # return OPERATOR_INTERCEPT_MESSAGE + + # # unenescape + # data_b64_str = data_b64_str_esc.replace('_','/') + + # # encode to binary + # data_b64 = data_b64_str.encode() + # data_b = b64decode(data_b64) + + # # ask operator to answer phone and request + # resp_data_b = self.op.answer_phone(data_b) + + # # decode to str + # resp_data_b64 = b64encode(resp_data_b) + # resp_data_b64_str = resp_data_b64.decode() - # return as str - return resp_data_b64_str + # # return as str + # return resp_data_b64_str def run_forever(port='8080'): # global OP_PASS diff --git a/comrad/backend/the_telephone.py b/comrad/backend/the_telephone.py index 2b53a39..230f12f 100644 --- a/comrad/backend/the_telephone.py +++ b/comrad/backend/the_telephone.py @@ -54,12 +54,13 @@ class TheTelephone(Operator): # self.log('but going to send just msg?',msg_b) msg_b=msg_d["msg"] - msg_b64 = b64encode(msg_b) - msg_b64_str = msg_b64.decode() - self.log(f'''Sending the encrypted content package:\n\n{msg_b64_str}''') + # msg_b64 = b64encode(msg_b) + # msg_b64_str = msg_b64.decode() + # self.log(f'''Sending the encrypted content package:\n\n{msg_b64_str}''') + self.log(f'''Sending the encrypted content package:\n\n{msg_b}''') # seal for transport - msg_b64_str_esc = msg_b64_str.replace('/','_') + # msg_b64_str_esc = msg_b64_str.replace('/','_') # dial the operator @@ -78,23 +79,34 @@ class TheTelephone(Operator): self.log('making first attempt to connect via Tor') try: # phonecall=self.comrad_request(URL) - phonecall = await texec(self.comrad_request, URL) + # phonecall = await texec(self.comrad_request, URL) + phonecall = await texec( + self.comrad_request_post, + self.api_url, + msg_b64_str_esc + ) + break except (TorSocketConnectError,ConnectionError) as e: self.log(f'!! {e} trying again?') pass if phonecall.status_code!=200: - self.log('!! error in request',phonecall.status_code,phonecall.text) + self.log('!! error in request',phonecall.status_code,phonecall) return # response back from Operator! - resp_msg_b64_str = phonecall.text - self.log(f'{self}: Received response from Operator! We got back:\n\n',resp_msg_b64_str) + # resp_msg_b64_str = phonecall.text + # self.log(f'{self}: Received response from Operator! We got back:\n\n',resp_msg_b64_str) - resp_msg_b64 = resp_msg_b64_str.encode() - resp_msg_b = b64decode(resp_msg_b64) + # resp_msg_b64 = resp_msg_b64_str.encode() + # resp_msg_b = b64decode(resp_msg_b64) # self.log('resp_msg_b:',resp_msg_b) + + + resp_msg_b = phonecall.data + self.log(f'{self}: Received response from Operator! We got back:\n\n',resp_msg_b) + resp_msg_d = pickle.loads(resp_msg_b) # self.log('unpickled:',resp_msg_d) @@ -137,6 +149,10 @@ class TheTelephone(Operator): return self.tor_request_in_python(url) # return tor_request_in_proxy(url) + def tor_request_post(self,url,data=''): + return self.tor_request_in_python_post(url,data=data) + # return tor_request_in_proxy_post(url,data=data) + async def tor_request_async(self,url): return await self.tor_request_in_python_async(url) @@ -159,6 +175,11 @@ class TheTelephone(Operator): self.log('<-- r',r) return r + def comrad_request_post(self,url,data='',allow_clearnet = ALLOW_CLEARNET): + if '.onion' in url or not allow_clearnet: + return self.tor_request_post(url) + return requests.post(url,data=data,timeout=60) + def tor_request_in_python(self,url): tor = TorClient() @@ -171,6 +192,18 @@ class TheTelephone(Operator): s.mount('https://', adapter) r = s.get(url, timeout=600) return r + + def tor_request_in_python_post(self,url,data=''): + tor = TorClient() + with tor.get_guard() as guard: + adapter = TorHttpAdapter(guard, 3, retries=RETRIES) + + with requests.Session() as s: + s.headers.update({'User-Agent': 'Mozilla/5.0'}) + s.mount('http://', adapter) + s.mount('https://', adapter) + r = s.post(url, data=data, timeout=600) + return r def get_tor_proxy_session(self): session = requests.session()