komrade update!

back-to-app
quadrismegistus 4 years ago
parent 2af4df2537
commit 0aced94204

@ -4,6 +4,49 @@ from komrade import *
log=print
from logging import Handler
class MazeWalker(Handler):
def __init__(self,callbacks={},*x,**y):
super().__init__(*x,**y)
self._callbacks=callbacks
self.walk = []
def emit(self, record):
from torpy.documents.network_status import Router
walk=self.walk
for arg in record.args:
if type(arg)==Router:
router = arg
if router.ip in walk: continue
walk.append(router.ip)
# print('! Found router:',record.msg % record.args)
# print(router.ip,walk)
# print()
if record.msg.startswith('Connecting to guard node'):
f = self._callbacks.get('torpy_guard_node_connect')
if f: f(router)
elif record.msg.startswith('Extending the circuit'):
f = self._callbacks.get('torpy_extend_circuit')
if f: f(router)
pass
# def komrade_request(url,allow_clearnet = ALLOW_CLEARNET):
# if '.onion' in url or not allow_clearnet:
# return tor_request(url)

@ -6,6 +6,7 @@ from komrade.backend.phonelines import *
from komrade.backend.operators import CALLBACKS
import requests
# def TheTelephone(*x,**y):
# return Komrade(TELEPHONE_NAME,*x,**y)
@ -120,11 +121,9 @@ class TheTelephone(Operator):
def tor_request_in_python(self,url):
tor = TorClient(
callbacks=self._callbacks
)
tor = TorClient()
with tor.get_guard() as guard:
adapter = TorHttpAdapter(guard, 3, retries=RETRIES, callbacks=self._callbacks)
adapter = TorHttpAdapter(guard, 3, retries=RETRIES)
with requests.Session() as s:
s.headers.update({'User-Agent': 'Mozilla/5.0'})

@ -3,7 +3,7 @@ from komrade import *
from komrade.backend import *
import art
import textwrap as tw
import readline
import readline,logging
readline.set_completer_delims('\t')
# from tab_completer import tabCompleter
tabber=tabCompleter()
@ -11,6 +11,14 @@ if 'libedit' in readline.__doc__:
readline.parse_and_bind("bind ^I rl_complete")
else:
readline.parse_and_bind("tab: complete")
torpy_logger = logging.getLogger('torpy')
logging.getLogger('urllib3').propagate=False
logging.getLogger('shapely').propagate=False
logging.getLogger('pyproj').propagate=False
logging.getLogger('rtree').propagate=False
torpy_logger.propagate=False
class CLI(Logger):
@ -37,12 +45,24 @@ class CLI(Logger):
self.loggedin=False
self.tabber=tabber
self.log('ROUTES:',self.ROUTES)
self.hops=[]
# Routes
rts=['/'+k for k in self.ROUTES]
tabber.createListCompleter(rts)
readline.set_completer(tabber.listCompleter)
# logging
from komrade.backend.mazes import MazeWalker
self.walker=MazeWalker(callbacks=self.callbacks)
self.torpy_logger = logging.getLogger('torpy')
self.torpy_logger.propagate=False
self.torpy_logger.addHandler(self.walker)
import ipinfo
ipinfo_access_token = '90df1baf7c373a'
self.ipinfo_handler = ipinfo.getHandler(ipinfo_access_token)
def verbose(self,*x):
self.toggle_log()
@ -68,6 +88,7 @@ class CLI(Logger):
except KomradeException as e:
self.stat(f'I could not handle your request. {e}\n')
#await asyncio.sleep(0.5)
self.walker.walk=[] # reset logger's walk?
def route(self,inp):
inp=inp.strip()
@ -198,17 +219,34 @@ class CLI(Logger):
@property
def callbacks(self):
return {
'torpy_extend_circuit':self.callback_on_hop
'torpy_guard_node_connect':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')}) '''
def callback_on_hop(self,rtr):
from worldmap import print_map,print_map_simple
# clear_screen()
deets = self.ipinfo_handler.getDetails(rtr.ip)
self.hops.insert(0,(rtr,deets))
country = deets.country_name
places = dict([
(_deets.city,tuple(float(_) for _ in _deets.loc.split(',')))
for (_rtr,_deets) in self.hops
])
clear_screen()
# print_map_simple(places)
print_map(places)
# print_map([_deets.country_name for _rtr,_deets in self.hops])
msg=f'''Hopped to new router: {rtr.nickname} ({rtr.ip}) in {deets.city}, {deets.country_name} ({deets.loc})'''
self.stat(
msg,
komrade_name='Tor'
)
input('pausing on callback: '+msg)
print()
# input('pausing on callback: '+msg)
def register(self,name=None):

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -0,0 +1,205 @@
# Code from
# https://github.com/snorfalorpagus/ascii-world-map
import json
from functools import partial
from shutil import get_terminal_size
from shapely.geometry import shape, Point
from shapely import ops
import pyproj,math,os
import rtree
import warnings
warnings.filterwarnings(action='ignore')
# read the data into a list of shapely geometries
with open("data/world-countries2.json") as f:
data = json.load(f)
def print_map(countries=[]):
geoms_all = [
shape(feature["geometry"])
for feature in data["features"]
]
geoms = [
shape(feature["geometry"])
for feature in data["features"]
if not countries or feature.get('properties',{}).get('name',None) in countries
]
# transform the geometries into web mercator
wgs84 = pyproj.Proj(init="EPSG:4326")
webmerc = pyproj.Proj(proj="webmerc")
t = partial(pyproj.transform, wgs84, webmerc)
geoms = [ops.transform(t, geom) for geom in geoms]
geoms_all = [ops.transform(t, geom) for geom in geoms_all]
# create a spatial index of the geometries
def gen(geoms):
for n, geom in enumerate(geoms):
yield n, geom.bounds, geom
index = rtree.index.Index(gen(geoms))
index_all = rtree.index.Index(gen(geoms_all))
# get the window size
size = get_terminal_size(fallback=(80, 24))
columns = size.columns
lines = size.lines - 1 - 3 # allow for prompt at bottom
# calculate the projected extent and pixel size
# xmin, ymin = t(-180, -85)
# xmax, ymax = t(180, 85)
xmin, ymin = t(-180, -62)
xmax, ymax = t(180, 79)
pixel_width = (xmax - xmin) / columns
pixel_height = (ymax - ymin) / lines
land = "*"
water = " "
highlight=''
# stringl=[]
# os.system('cls' if os.name == 'nt' else 'clear')
for line in range(lines):
for col in range(columns):
# get the projected x, y of the pixel centroid
x = xmin + (col + 0.5) * pixel_width
y = ymax - (line + 0.5) * pixel_height
# check for a collision
objects = [n.object for n in index.intersection((x, y, x, y), objects=True)]
value = False
done=False
for geom in objects:
value = geom.intersects(Point(x, y))
if value:
print(highlight,end="")
# stringl+=[highlight]
done=True
break
if not done:
objects = [n.object for n in index_all.intersection((x, y, x, y), objects=True)]
for geom in objects:
value = geom.intersects(Point(x, y))
if value:
break
print(land if value else water, end="")
# stringl+=[land if value else water]
print("")
# stringl+=['\n']
# string = ''.join(stringl)
# print(string)
places = {
'Cambridge':(52.205338,0.121817),
'Sydney':(-33.868820,151.209290),
'New York':(40.712776,-74.005974),
'Hong Kong':(22.278300,114.174700),
'Cape Town':(33.9249, 18.4241),
'San Francisco':(-12.125640,-77.018967),
}
places_utm = {
'Honolulu':(618431.58,2357505.97),
'Tokyo':(394946.08,3946063.75),
'Ushuaia':(544808.23,3927028.51),
'Reykjavik':(459698.38,7111571.73)
}
def print_map_simple(places):
size = get_terminal_size(fallback=(80, 24))
columns = size.columns
lines = size.lines - 1 - 3 # allow for prompt at bottom
# calculate the projected extent and pixel size
# xmin, ymin = (-180, -85)
# xmax, ymax = (180, 85)
# pixel_width = (xmax - xmin) / columns
# pixel_height = (ymax - ymin) / lines
long_min,long_max = -180,180
# lat_min,lat_max = -85,85
lat_min,lat_max = -75,80
# utm_easting_min = 166640
# utm_easting_max = 833360
# utm_northing_min = 1110400
# utm_northing_max = 9334080
utm_easting_min = places_utm['Honolulu'][0]
utm_easting_max = places_utm['Tokyo'][0]
utm_northing_min = places_utm['Ushuaia'][1]
utm_northing_max = places_utm['Reykjavik'][1]
# import pyproj as proj
# setup your projections
# crs_wgs = proj.Proj(init='epsg:4326') # assuming you're using WGS84 geographic
# crs_bng = proj.Proj(init='epsg:27700') # use a locally appropriate projected CRS
import utm
normed = {}
for place,(lat,long) in places.items():
# wgs84 = pyproj.Proj(init="EPSG:4326")
# webmerc = pyproj.Proj(proj="webmerc")
# x, y = proj.transform(wgs84, webmerc, long, lat)
longx = (long - long_min) / (long_max - long_min)
laty = (lat - lat_min) / (lat_max - lat_min)
utm_easting,utm_northing,utm_zone_num,utm_zone_letter = utm.from_latlon(lat,long)
utmx = (utm_easting - utm_easting_min) / (utm_easting_max - utm_easting_min)
utmy = (utm_northing - utm_northing_min) / (utm_northing_max - utm_northing_min)
# norm = ( int(longx*columns), int(laty*lines) )
norm = ( int(utmx*columns), int(utmy*lines) )
# print(place,(utm_easting,utm_northing),(utmx,utmy),norm)
norm = (norm[0], lines - norm[1])
normed[norm] = place
p_i=None
place_now=None
for line in range(lines):
for col in range(columns):
if (col,line) in normed:
print('*',end="")
place=normed[(col,line)]
place_now=place
p_i=0
elif p_i is not None:
try:
print(place_now[p_i],end="")
p_i+=1
except IndexError:
place_now=None
p_i=None
else:
print(" ",end="")
print()
# print_map(['Brazil','Netherlands','Thailand'])
# print_map_simple(places)

@ -0,0 +1,258 @@
# Code from
# https://github.com/snorfalorpagus/ascii-world-map
import json
from functools import partial
from shutil import get_terminal_size
from shapely.geometry import shape, Point
from shapely import ops
import pyproj,math,os
import rtree
import curses,random,time
import warnings
warnings.filterwarnings(action='ignore')
# read the data into a list of shapely geometries
with open(os.path.join(os.path.dirname(__file__),"data/world-countries2.json")) as f:
data = json.load(f)
def print_map(countries=[]):
geoms_all = [
shape(feature["geometry"])
for feature in data["features"]
]
geoms = [
shape(feature["geometry"])
for feature in data["features"]
if not countries or feature.get('properties',{}).get('name',None) in countries
]
# transform the geometries into web mercator
wgs84 = pyproj.Proj(init="EPSG:4326")
webmerc = pyproj.Proj(proj="webmerc")
t = partial(pyproj.transform, wgs84, webmerc)
geoms = [ops.transform(t, geom) for geom in geoms]
geoms_all = [ops.transform(t, geom) for geom in geoms_all]
# create a spatial index of the geometries
def gen(geoms):
for n, geom in enumerate(geoms):
yield n, geom.bounds, geom
index = rtree.index.Index(gen(geoms))
index_all = rtree.index.Index(gen(geoms_all))
# get the window size
size = get_terminal_size(fallback=(80, 24))
columns = size.columns
lines = size.lines - 1 - 3 # allow for prompt at bottom
# calculate the projected extent and pixel size
# xmin, ymin = t(-180, -85)
# xmax, ymax = t(180, 85)
xmin, ymin = t(-180, -62)
xmax, ymax = t(180, 79)
pixel_width = (xmax - xmin) / columns
pixel_height = (ymax - ymin) / lines
land = "*"
water = " "
highlight=''
# stringl=[]
# os.system('cls' if os.name == 'nt' else 'clear')
for line in range(lines):
for col in range(columns):
# get the projected x, y of the pixel centroid
x = xmin + (col + 0.5) * pixel_width
y = ymax - (line + 0.5) * pixel_height
# check for a collision
objects = [n.object for n in index.intersection((x, y, x, y), objects=True)]
value = False
done=False
for geom in objects:
value = geom.intersects(Point(x, y))
if value:
print(highlight,end="")
# stringl+=[highlight]
done=True
break
if not done:
objects = [n.object for n in index_all.intersection((x, y, x, y), objects=True)]
for geom in objects:
value = geom.intersects(Point(x, y))
if value:
break
print(land if value else water, end="")
# stringl+=[land if value else water]
print("")
# stringl+=['\n']
# string = ''.join(stringl)
# print(string)
places = {
'Cambridge':(52.205338,0.121817),
'Sydney':(-33.868820,151.209290),
'New York':(40.712776,-74.005974),
'Hong Kong':(22.278300,114.174700),
'Cape Town':(-33.9249, 18.4241),
'San Francisco':(37.774929,-122.419418),
'Honolulu':(21.306944,-157.858337),
'Tokyo':(35.689487,139.691711),
'Ushuaia':(-54.801910,-68.302948),
'Reykjavik':(64.126518,-21.817438)
}
places_utm = {
'Honolulu':(618431.58,2357505.97),
'Tokyo':(394946.08,3946063.75),
'Ushuaia':(544808.23,3927028.51),
'Reykjavik':(459698.38,7111571.73)
}
def print_map_simple(places):
size = get_terminal_size(fallback=(80, 24))
columns = size.columns
lines = size.lines - 1 - 3 # allow for prompt at bottom
# calculate the projected extent and pixel size
# xmin, ymin = (-180, -85)
# xmax, ymax = (180, 85)
# pixel_width = (xmax - xmin) / columns
# pixel_height = (ymax - ymin) / lines
long_min,long_max = -180,180
# lat_min,lat_max = -85,85
lat_min,lat_max = -75,80
# utm_easting_min = 166640
# utm_easting_max = 833360
# utm_northing_min = 1110400
# utm_northing_max = 9334080
utm_easting_min = places_utm['Honolulu'][0]
utm_easting_max = places_utm['Tokyo'][0]
utm_northing_min = places_utm['Ushuaia'][1]
utm_northing_max = places_utm['Reykjavik'][1]
# import pyproj as proj
# setup your projections
# crs_wgs = proj.Proj(init='epsg:4326') # assuming you're using WGS84 geographic
# crs_bng = proj.Proj(init='epsg:27700') # use a locally appropriate projected CRS
import utm
normed = {}
for place,(lat,long) in places.items():
# wgs84 = pyproj.Proj(init="EPSG:4326")
# webmerc = pyproj.Proj(proj="webmerc")
# x, y = proj.transform(wgs84, webmerc, long, lat)
longx = (long - long_min) / (long_max - long_min)
laty = (lat - lat_min) / (lat_max - lat_min)
utm_easting,utm_northing,utm_zone_num,utm_zone_letter = utm.from_latlon(lat,long)
utmx = (utm_easting - utm_easting_min) / (utm_easting_max - utm_easting_min)
utmy = (utm_northing - utm_northing_min) / (utm_northing_max - utm_northing_min)
# norm = ( int(longx*columns), int(laty*lines) )
norm = ( int(utmx*columns), int(utmy*lines) )
# print(place,(utm_easting,utm_northing),(utmx,utmy),norm)
norm = (norm[0], lines - norm[1])
normed[norm] = place
p_i=None
place_now=None
for line in range(lines):
for col in range(columns):
if (col,line) in normed:
print('*',end="")
place=normed[(col,line)]
place_now=place
p_i=0
elif p_i is not None:
try:
print(place_now[p_i],end="")
p_i+=1
except IndexError:
place_now=None
p_i=None
else:
print(" ",end="")
print()
# print_map(['Brazil','Netherlands','Thailand'])
# print_map_simple(places)
def print_map(places):
curses.wrapper(run_print_map)
def run_print_map(stdscr):
curses.use_default_colors()
stdscr.addstr(0,0,'helloooooo')
stdscr.refresh()
rows, cols = stdscr.getmaxyx()
print(rows,cols)
rows = rows-10
cols = cols - 10
df = do_print_map(places,rows,cols)
for df_i,df_row in df.iterrows():
#try:
stdscr.addstr(df_row.y_win,df_row.x_win,'x '+df_row.place)
#except curses.error:
# pass
stdscr.getch()
def do_print_map(places,rows,cols):
normed = []
for place,(lat,long) in places.items():
wgs84 = pyproj.Proj(init="EPSG:4326")
webmerc = pyproj.Proj(proj="webmerc")
x, y = pyproj.transform(wgs84, webmerc, long, lat)
norm = {'place':place,'lat':lat,'long':long,'x':x,'y':y}
normed.append(norm)
import pandas as pd
df=pd.DataFrame(normed)
def do_norm(x,xcol): return (x - xcol.min()) / (xcol.max() - xcol.min())
df['x_norm'] = [do_norm(x,df['x']) for x in df['x']]
df['y_norm'] = [do_norm(y,df['y']) for y in df['y']]
df['x_win'] = [int(x*cols) for x in df['x_norm']]
df['y_win'] = [rows - int(y*rows) for y in df['y_norm']]
return df
if __name__ == '__main__':
# do_print_map(places,60,30)
print_map(places)

@ -4,8 +4,8 @@ KOMRADE_ONION = 'u7spnj3dmwumzoa4.onion'
KOMRADE_ONION2 = 'rwg4zcnpwshv4laq.onion' #'128.232.229.63' #'komrade.app'
OPERATOR_API_URL = f'http://{KOMRADE_ONION}/op/'
# OPERATOR_API_URL = f'http://{KOMRADE_URL}/op/'
# OPERATOR_API_URL = f'http://{KOMRADE_ONION}/op/'
OPERATOR_API_URL = f'http://{KOMRADE_URL}/op/'
# paths

@ -4,6 +4,29 @@ class KomradeException(Exception): pass
import sys,os
sys.path.append(os.path.dirname(__file__))
from logging import Handler
class CallbackHandler(Handler):
def __init__(self,callbacks={},*x,**y):
super().__init__(*x,**y)
self._callbacks=callbacks
def emit(self, record):
# print(record)
# stop
# print('!!!!',record.msg, record.args)
from torpy.documents.network_status import Router
for arg in record.args:
if type(arg)==Router:
print('! Found router:',record.msg % record.args)
# if len(record.args)==1 and type(record.args[0])==Router:
# print('! Router:',record.msg,record.args[0].nickname)
pass
#log_entry = self.format(record)
#return requests.post('http://example.com:8080/',
# log_entry, headers={"Content-type": "application/json"}).content
def logger():
import logging

Loading…
Cancel
Save