2
0
mirror of https://github.com/ComradCollective/Comrad synced 2024-11-03 23:15:33 +00:00
This commit is contained in:
quadrismegistus 2020-09-19 19:54:57 +01:00
parent 23f0d94f40
commit 2af4df2537
7 changed files with 90 additions and 72 deletions

View File

@ -255,7 +255,8 @@ class Keymaker(Logger):
uri_id=None,
keychain={},
path_crypt_keys=PATH_CRYPT_CA_KEYS,
path_crypt_data=PATH_CRYPT_CA_DATA):
path_crypt_data=PATH_CRYPT_CA_DATA,
callbacks={}):
# init logger with callbacks
super().__init__(callbacks=callbacks)

View File

@ -15,6 +15,7 @@ class KomradeX(Caller):
def __init__(self, name=None, pubkey=None, callbacks={}):
super().__init__(name=name, callbacks=callbacks)
self.log(f'Starting up with callbacks: {self._callbacks}')
self.boot(create=False)
# special?
if self.name==WORLD_NAME:

View File

@ -4,74 +4,74 @@ from komrade import *
log=print
def komrade_request(url,allow_clearnet = ALLOW_CLEARNET):
if '.onion' in url or not allow_clearnet:
return tor_request(url)
return requests.get(url,timeout=600)
# def komrade_request(url,allow_clearnet = ALLOW_CLEARNET):
# if '.onion' in url or not allow_clearnet:
# return tor_request(url)
# return requests.get(url,timeout=600)
def tor_request(url):
return tor_request_in_python(url)
# return tor_request_in_proxy(url)
# def tor_request(url):
# return tor_request_in_python(url)
# # return tor_request_in_proxy(url)
async def tor_request_async(url):
return await tor_request_in_python_async(url)
# async def tor_request_async(url):
# return await tor_request_in_python_async(url)
def tor_request_in_proxy(url):
with get_tor_proxy_session() as s:
return s.get(url,timeout=60)
# def tor_request_in_proxy(url):
# with get_tor_proxy_session() as s:
# return s.get(url,timeout=60)
async def tor_request_in_python_async(url):
import requests_async as requests
tor = TorClient()
with tor.get_guard() as guard:
adapter = TorHttpAdapter(guard, 3, retries=RETRIES)
# async def tor_request_in_python_async(url):
# import requests_async as requests
# tor = TorClient()
# with tor.get_guard() as guard:
# adapter = TorHttpAdapter(guard, 3, retries=RETRIES)
async with requests.Session() as s:
# await s
s.headers.update({'User-Agent': 'Mozilla/5.0'})
s.mount('http://', adapter)
s.mount('https://', adapter)
return await s.get(url, timeout=60)
# async with requests.Session() as s:
# # await 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(url):
tor = TorClient()
with tor.get_guard() as guard:
adapter = TorHttpAdapter(guard, 3, retries=RETRIES)
# def tor_request_in_python(url):
# 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)
# 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=30)
# r = s.post('http://u7spnj3dmwumzoa4.onion/op/',data=b'hello world', timeout=30)
#_dat = 'Z29iYmxlZHlnb29rZ29iYmxlZHlnb29rZ29iYmxlZHlnb29rZ29iYmxlZHlnb29rZ29iYmxlZHlnb29rZ29iYmxlZHlnb29rZ29iYmxlZHlnb29rZ29iYmxlZHlnb29rZ29iYmxlZHlnb29r'
r = s.get(url, timeout=60)
# # r = s.get(url, timeout=30)
# # r = s.post('http://u7spnj3dmwumzoa4.onion/op/',data=b'hello world', timeout=30)
# #_dat = 'Z29iYmxlZHlnb29rZ29iYmxlZHlnb29rZ29iYmxlZHlnb29rZ29iYmxlZHlnb29rZ29iYmxlZHlnb29rZ29iYmxlZHlnb29rZ29iYmxlZHlnb29rZ29iYmxlZHlnb29rZ29iYmxlZHlnb29r'
# r = s.get(url, timeout=60)
return r
#return r
# return r
# #return r
# #r = s.get('http://u7spnj3dmwumzoa4.onion',timeout=30)
# print (r, r.text[:1000])
# return s
# # #r = s.get('http://u7spnj3dmwumzoa4.onion',timeout=30)
# # print (r, r.text[:1000])
# # return s
def get_tor_proxy_session():
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_tor_proxy_session():
# 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():
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 get_async_tor_proxy_session():
# 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

View File

@ -6,6 +6,7 @@ from komrade.backend import *
# BEGIN PHONE BOOK (in memory singleton mapping)
PHONEBOOK = {}
CALLBACKS = {}
# Factory constructor
def Komrade(name=None,pubkey=None,*x,**y):
@ -13,7 +14,7 @@ def Komrade(name=None,pubkey=None,*x,**y):
if name and not pubkey and type(name)==bytes:
pubkey=b64enc(name)
name=None
from komrade.backend.the_operator import TheOperator
from komrade.backend.the_telephone import TheTelephone
@ -27,16 +28,25 @@ def Komrade(name=None,pubkey=None,*x,**y):
pk64 = None if not pubkey else b64enc(pubkey)
if pk64 in PHONEBOOK: return PHONEBOOK[pk64]
global CALLBACKS
callbacks = y.get('callbacks',{})
callbacks = callbacks if callbacks else CALLBACKS
CALLBACKS = callbacks
# print(f'finding Komrade {name} / {pubkey} for the first time!')
# operator?
if name==OPERATOR_NAME:
kommie = TheOperator() #(*x,**y)
kommie = TheOperator(callbacks=callbacks) #(*x,**y)
if name==TELEPHONE_NAME:
kommie = TheTelephone() #(*x,**y)
kommie = TheTelephone(callbacks=callbacks) #(*x,**y)
else:
# print('booting new kommie')
kommie = KomradeX(name,*x,**y)
# print('found!',name,PHONEBOOK[name],PHONEBOOK[name].keychain())
PHONEBOOK[name] = kommie
if kommie.pubkey:
@ -107,7 +117,7 @@ class Operator(Keymaker):
global TELEPHONE,TELEPHONE_KEYCHAIN
if TELEPHONE: return TELEPHONE
self._phone=TELEPHONE=TheTelephone()
self._phone=TELEPHONE=TheTelephone(callbacks=self._callbacks)
return TELEPHONE
@ -121,7 +131,7 @@ class Operator(Keymaker):
global OPERATOR,OPERATOR_KEYCHAIN
if OPERATOR: return OPERATOR
self._op=OPERATOR=TheOperator()
self._op=OPERATOR=TheOperator(callbacks=self._callbacks)
return OPERATOR

View File

@ -26,7 +26,7 @@ class TheOperator(Operator):
return TELEPHONE
def __init__(self, name = OPERATOR_NAME, passphrase=None):
def __init__(self, name = OPERATOR_NAME, passphrase=None, callbacks={}):
"""
Boot up the operator. Requires knowing or setting a password of memory.
"""
@ -34,7 +34,8 @@ class TheOperator(Operator):
super().__init__(
name,
path_crypt_keys=PATH_CRYPT_OP_KEYS,
path_crypt_data=PATH_CRYPT_OP_DATA
path_crypt_data=PATH_CRYPT_OP_DATA,
callbacks=callbacks
)
from komrade.backend.phonelines import check_phonelines
keychain = check_phonelines()[OPERATOR_NAME]

View File

@ -3,6 +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 *
from komrade.backend.operators import CALLBACKS
import requests
# def TheTelephone(*x,**y):
@ -14,12 +15,13 @@ class TheTelephone(Operator):
API client class for Caller to interact with The Operator.
"""
def __init__(self, caller=None, callbacks={}):
super().__init__(name=TELEPHONE_NAME)
super().__init__(name=TELEPHONE_NAME,callbacks=callbacks)
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
self.log(f'Starting up with callbacks: {self._callbacks}')
def send_and_receive(self,msg_d,**y):
@ -91,15 +93,15 @@ class TheTelephone(Operator):
return requests.get(url,timeout=600)
def tor_request(self,url):
return tor_request_in_python(url)
return self.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)
return await self.tor_request_in_python_async(url)
def tor_request_in_proxy(self,url):
with get_tor_proxy_session() as s:
with self.get_tor_proxy_session() as s:
return s.get(url,timeout=60)
async def tor_request_in_python_async(self,url):
@ -122,7 +124,7 @@ class TheTelephone(Operator):
callbacks=self._callbacks
)
with tor.get_guard() as guard:
adapter = TorHttpAdapter(guard, 3, retries=RETRIES)
adapter = TorHttpAdapter(guard, 3, retries=RETRIES, callbacks=self._callbacks)
with requests.Session() as s:
s.headers.update({'User-Agent': 'Mozilla/5.0'})

View File

@ -198,15 +198,18 @@ class CLI(Logger):
@property
def callbacks(self):
return {
'init_CircuitNode':self.callback_on_hop
'torpy_extend_circuit':self.callback_on_hop
}
def callback_on_hop(self,data):
rtr=data.get('router')
msg=f'''Hopped to new router: {rtr.get('nickname')} ({rtr.get('ip')}) '''
self.stat(
f'''Hopped to new router: {rtr.get('nickname')} ({rtr.get('ip')}) ''',
msg,
komrade_name='Tor'
)
input('pausing on callback: '+msg)
def register(self,name=None):
if not name: name=input('name: ')
@ -238,7 +241,7 @@ class CLI(Logger):
# self.print(self,name,self.name,self.komrade,self.loggedin)
if not name: name=input('name: ')
if not name: return
self.komrade=Komrade(name)
self.komrade=Komrade(name,callbacks=self.callbacks)
return self.refresh()
# res = self.komrade.login()
# return self.do_login(res)
@ -249,7 +252,7 @@ class CLI(Logger):
if res and type(res)==dict and 'success' in res and res['success']:
self.name=res['name']
self.komrade=Komrade(res['name'])
self.komrade=Komrade(res['name'],callbacks=self.callbacks)
self.loggedin=True
else:
self.name=None