adding callbacks

back-to-app
quadrismegistus 4 years ago
parent 5a019e1cb1
commit 23f0d94f40

1
.gitignore vendored

@ -12,3 +12,4 @@ lib
#*.keys
.op.*
*.pyc
torpy

@ -257,6 +257,9 @@ class Keymaker(Logger):
path_crypt_keys=PATH_CRYPT_CA_KEYS,
path_crypt_data=PATH_CRYPT_CA_DATA):
# init logger with callbacks
super().__init__(callbacks=callbacks)
# set defaults
self.name=name
self._uri_id=uri_id

@ -13,8 +13,8 @@ from komrade.backend.messages import Message
class KomradeX(Caller):
def __init__(self, name=None, pubkey=None):
super().__init__(name=name)
def __init__(self, name=None, pubkey=None, callbacks={}):
super().__init__(name=name, callbacks=callbacks)
self.boot(create=False)
# special?
if self.name==WORLD_NAME:

@ -166,9 +166,9 @@ from torpy.hiddenservice import HiddenService
from torpy.http.requests import TorRequests, tor_requests_session, do_request as requests_request
from torpy.http.urlopener import do_request as urllib_request
# logging.getLogger('requests').setLevel(logging.CRITICAL)
# logging.basicConfig(format='[%(asctime)s] [%(threadName)-16s] %(message)s', level=logging.DEBUG)
# logger = logging.getLogger('komrade')
logging.getLogger('requests').setLevel(logging.CRITICAL)
logging.basicConfig(format='[%(asctime)s] [%(threadName)-16s] %(message)s', level=logging.DEBUG)
logger = logging.getLogger('komrade')
HS_BASIC_HOST = os.getenv('HS_BASIC_HOST')

@ -59,6 +59,13 @@ class Operator(Keymaker):
]
def __eq__(self,other):
if not self.pubkey or not other.pubkey: return False
return self.pubkey.data == other.pubkey.data
@ -68,7 +75,8 @@ class Operator(Keymaker):
pubkey=None,
keychain = {},
path_crypt_keys=PATH_CRYPT_CA_KEYS,
path_crypt_data=PATH_CRYPT_CA_DATA
path_crypt_data=PATH_CRYPT_CA_DATA,
callbacks={}
):
global PHONEBOOK
@ -78,7 +86,8 @@ class Operator(Keymaker):
name=name,
keychain=keychain,
path_crypt_keys=path_crypt_keys,
path_crypt_data=path_crypt_data
path_crypt_data=path_crypt_data,
callbacks=callbacks
)

@ -3,7 +3,7 @@ import os,sys; sys.path.append(os.path.abspath(os.path.join(os.path.abspath(os.p
from komrade import *
from komrade.backend import *
from komrade.backend.phonelines import *
import requests
# def TheTelephone(*x,**y):
# return Komrade(TELEPHONE_NAME,*x,**y)
@ -13,12 +13,13 @@ class TheTelephone(Operator):
"""
API client class for Caller to interact with The Operator.
"""
def __init__(self, caller=None):
def __init__(self, caller=None, callbacks={}):
super().__init__(name=TELEPHONE_NAME)
self.caller=caller
from komrade.backend.phonelines import check_phonelines
keychain = check_phonelines()[TELEPHONE_NAME]
self._keychain ={**self.load_keychain_from_bytes(keychain)}
self._callbacks=callbacks
def send_and_receive(self,msg_d,**y):
@ -46,7 +47,7 @@ class TheTelephone(Operator):
# dial the operator
URL = OPERATOR_API_URL + msg_b64_str_esc + '/'
self.log("DIALING THE OPERATOR:",URL)
phonecall=komrade_request(URL)
phonecall=self.komrade_request(URL)
if phonecall.status_code!=200:
self.log('!! error in request',phonecall.status_code,phonecall.text)
return
@ -82,6 +83,85 @@ class TheTelephone(Operator):
get_resp_from=self.send_and_receive,
**y
)
### Requests functionality
def komrade_request(self,url,allow_clearnet = ALLOW_CLEARNET):
if '.onion' in url or not allow_clearnet:
return self.tor_request(url)
return requests.get(url,timeout=600)
def tor_request(self,url):
return tor_request_in_python(url)
# return tor_request_in_proxy(url)
async def tor_request_async(self,url):
return await tor_request_in_python_async(url)
def tor_request_in_proxy(self,url):
with get_tor_proxy_session() as s:
return s.get(url,timeout=60)
async def tor_request_in_python_async(self,url):
import requests_async as requests
tor = TorClient(
callbacks=self._callbacks
)
with tor.get_guard() as guard:
adapter = TorHttpAdapter(guard, 3, retries=RETRIES)
async with requests.Session() as s:
s.headers.update({'User-Agent': 'Mozilla/5.0'})
s.mount('http://', adapter)
s.mount('https://', adapter)
return await s.get(url, timeout=60)
def tor_request_in_python(self,url):
tor = TorClient(
callbacks=self._callbacks
)
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.get(url, timeout=60)
return r
def get_tor_proxy_session(self):
session = requests.session()
# Tor uses the 9050 port as the default socks port
session.proxies = {'http': 'socks5://127.0.0.1:9050',
'https': 'socks5://127.0.0.1:9050'}
return session
def get_async_tor_proxy_session(self):
import requests_futures
from requests_futures.sessions import FuturesSession
session = FuturesSession()
# Tor uses the 9050 port as the default socks port
session.proxies = {'http': 'socks5://127.0.0.1:9050',
'https': 'socks5://127.0.0.1:9050'}
return session
def test_call():

@ -194,11 +194,24 @@ class CLI(Logger):
contacts = self.komrade.contacts()
self.print(' ' + '\n '.join(contacts))
@property
def callbacks(self):
return {
'init_CircuitNode':self.callback_on_hop
}
def callback_on_hop(self,data):
rtr=data.get('router')
self.stat(
f'''Hopped to new router: {rtr.get('nickname')} ({rtr.get('ip')}) ''',
komrade_name='Tor'
)
def register(self,name=None):
if not name: name=input('name: ')
if not name: return
self.komrade = Komrade(name)
self.komrade = Komrade(name,callbacks=self.callbacks)
was_off=self.off
# if was_off: self.show_log()
def logfunc(*x,komrade_name='Keymaker',**y):

@ -91,6 +91,9 @@ def dict_format(d, tab=0):
import inspect,time
from komrade.constants import *
class Logger(object):
def __init__(self,callbacks={}):
self._callbacks=callbacks
@property
def off(self):
x=os.environ.get('KOMRADE_SHOW_LOG')

Loading…
Cancel
Save