def setup_handler(out_dir, cookie_file): plog( 'INFO', 'Connecting to Tor at ' + TorUtil.control_host + ":" + str(TorUtil.control_port)) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((TorUtil.control_host, TorUtil.control_port)) c = PathSupport.Connection(s) #c.debug(file(out_dir+"/control.log", "w", buffering=0)) c.authenticate_cookie(file(cookie_file, "r")) h = BwScanHandler(c, __selmgr, strm_selector=PathSupport.SmartSocket.StreamSelector) # ignore existing streams ignore_streams(c, h) c.set_event_handler(h) #c.set_periodic_timer(2.0, "PULSE") c.set_events([ TorCtl.EVENT_TYPE.STREAM, TorCtl.EVENT_TYPE.BW, TorCtl.EVENT_TYPE.NEWCONSENSUS, TorCtl.EVENT_TYPE.NEWDESC, TorCtl.EVENT_TYPE.CIRC, TorCtl.EVENT_TYPE.STREAM_BW ], True) atexit.register(cleanup) return (c, h)
def choose_url(percentile): # TODO: Maybe we don't want to read the file *every* time? # Maybe once per slice? # Read in the bw auths file # here is a fine place to make sure we have bwfiles try: f = file("./data/bwfiles", "r") except IOError: write_file_list('./data') lines = [] valid = False for l in f.readlines(): if l == ".\n": valid = True break pair = l.split() lines.append((int(pair[0]), pair[1])) if not valid: plog("ERROR", "File size list is invalid!") for (pct, fname) in lines: if percentile < pct: return random.choice(urls) + fname raise PathSupport.NoNodesRemain("No nodes left for url choice!")
def open_controller(): """ starts stat gathering thread """ s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.connect((control_host,control_port)) c = PathSupport.Connection(s) c.authenticate(control_pass) # also launches thread... return c
def open_controller(filename, ncircuits, use_sql): """ starts stat gathering thread """ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((control_host, control_port)) c = PathSupport.Connection(s) c.authenticate(control_pass) # also launches thread... c.debug(file(filename + ".log", "w", buffering=0)) h = CircStatsGatherer(c, __selmgr, filename, ncircuits) c.set_event_handler(h) if use_sql: from TorCtl import SQLSupport SQLSupport.setup_db("sqlite:///" + filename + ".sqlite", drop=True) c.add_event_listener(SQLSupport.ConsensusTrackerListener()) c.add_event_listener(SQLSupport.CircuitListener()) global FUDValue if not FUDValue: FUDValue = c.get_option("FetchUselessDescriptors")[0][1] c.set_option("FetchUselessDescriptors", "1") c.set_events([ TorCtl.EVENT_TYPE.STREAM, TorCtl.EVENT_TYPE.BW, TorCtl.EVENT_TYPE.NEWCONSENSUS, TorCtl.EVENT_TYPE.NEWDESC, TorCtl.EVENT_TYPE.CIRC, TorCtl.EVENT_TYPE.STREAM_BW ], True) return c
def cleanup(): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((TorUtil.control_host, TorUtil.control_port)) c = PathSupport.Connection(s) c.authenticate_cookie(file("./tor-data/control_auth_cookie", "r")) global FUDValue from TorCtl.TorUtil import plog plog("INFO", "Resetting FetchUselessDescriptors=" + FUDValue) c.set_option("FetchUselessDescriptors", FUDValue)
def cleanup(): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((control_host, control_port)) c = PathSupport.Connection(s) c.authenticate(control_pass) # also launches thread... global FUDValue from TorCtl.TorUtil import plog plog("INFO", "Resetting FetchUselessDescriptors=" + FUDValue) c.set_option("FetchUselessDescriptors", FUDValue)
def resetTor(self): try: s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.connect(("localhost",9051)) c = PathSupport.Connection(s) c.authenticate('Agora') c.send_signal(3) c.close() except socket.error, e: print "[!] Couldn't connect to TOR." sys.exit(-1)
def get_guards(c, n): # Get list of live routers sorted_rlist = filter(lambda r: not r.down, c.read_routers(c.get_network_status())) sorted_rlist.sort(lambda x, y: cmp(y.bw, x.bw)) list_len = len(sorted_rlist) for i in xrange(list_len): sorted_rlist[i].list_rank = i guard_rst = PathSupport.FlagsRestriction(["Guard"], []) if pct_start == 100: pct_rst = PathSupport.PercentileRestriction(0, pct_start, sorted_rlist) else: pct_rst = PathSupport.PercentileRestriction(pct_start, pct_start + PCT_SKIP, sorted_rlist) # XXX: Hrmm. UniformGenerator was broken? guard_gen = PathSupport.ExactUniformGenerator( sorted_rlist, PathSupport.NodeRestrictionList([guard_rst, pct_rst])) guard_gen.rewind() ggen = guard_gen.generate() # Generate 3 guards guards = [] for i in xrange(n): g = ggen.next() plog( "NOTICE", str(pct_start) + "%: Generated guard $" + g.idhex + " with rank " + str(g.list_rank) + "/" + str(list_len) + " " + str(round((100.0 * g.list_rank) / list_len, 1)) + "% with flags " + str(g.flags)) guards.append(g) return guards
def ignore_streams(c, hdlr): for stream in c.get_info("stream-status")['stream-status'].rstrip( "\n").split("\n"): m = re.match( "(?P<sid>\d*)\s(?P<status>\S*)\s(?P<cid>\d*)\s(?P<host>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(?P<port>\d{1,5})", stream) if m: f = m.groupdict() else: return # no streams s = PathSupport.Stream(int(f['sid']), f['host'], int(f['port']), 0) plog("DEBUG", "Ignoring foreign stream: %s" % f['sid']) s.ignored = True hdlr.streams[s.strm_id] = s
def startup(): c = TorCtl.connect(control_host, control_port, ConnClass=PathSupport.Connection) c.debug(file("control.log", "w", buffering=0)) h = PathSupport.PathBuilder(c, __selmgr) # StatsHandler(c, __selmgr) c.set_event_handler(h) c.set_events([TorCtl.EVENT_TYPE.STREAM, TorCtl.EVENT_TYPE.BW, TorCtl.EVENT_TYPE.NEWCONSENSUS, TorCtl.EVENT_TYPE.NEWDESC, TorCtl.EVENT_TYPE.CIRC, TorCtl.EVENT_TYPE.STREAM_BW], True) c.set_option("__LeaveStreamsUnattached", "1") f = c.get_option("FetchUselessDescriptors")[0][1] c.set_option("FetchUselessDescriptors", "1") return (c,h,f)
def __init__(self, c, selmgr, basefile_name, nstats): StatsHandler.__init__(self, c, selmgr, BTRouter, track_ranks=True) self.nodesfile = open(basefile_name + '.nodes', 'w') self.failfile = open(basefile_name + '.failed', 'w') self.extendtimesfile = open(basefile_name + '.extendtimes', 'w') self.buildtimesfile = open(basefile_name + '.buildtimes', 'w') self.circ_built = 0 self.nstats = nstats self.done = False if self.selmgr.bad_restrictions: raise PathSupport.NoNodesRemain("No nodes remain after init") # Set up the exit restriction to include either 443 or 80 exits. # Since Tor dynamically pre-builds circuits depending on port usage, and # these are the two most commonly used user ports, this seems as good # first approximation to model the dynamic behavior of a real client's # circuit choice. self.selmgr.exit_rstr.del_restriction(ExitPolicyRestriction) self.selmgr.exit_rstr.del_restriction(OrNodeRestriction) self.selmgr.exit_rstr.add_restriction( OrNodeRestriction([ ExitPolicyRestriction("255.255.255.255", 80), ExitPolicyRestriction("255.255.255.255", 443) ])) self.selmgr.path_selector.exit_gen.rebuild()
# Note these urls should be https due to caching considerations. # If you really must make them http, be sure to change exit_ports to [80] # below, or else the scan will not finish. # Doesn't work: "https://38.229.70.2/" urls = ["https://38.229.72.16/bwauth.torproject.org/"] # Do NOT modify this object directly after it is handed to PathBuilder # Use PathBuilder.schedule_selmgr instead. # (Modifying the arguments here is OK) __selmgr = PathSupport.SelectionManager( pathlen=2, order_exits=False, percent_fast=100, percent_skip=0, min_bw=1024, use_all_exits=False, uniform=True, use_exit=None, use_guards=False, exit_ports=[443], order_by_ratio=True, # XXX: may be a poor idea for PID control? min_exits=10) # exit code to indicate scan completion # make sure to update this in bwauthority.py as well STOP_PCT_REACHED = 9 RESTART_SLICE = 1 def read_config(filename): config = ConfigParser.SafeConfigParser()
from TorCtl.TorUtil import meta_port, meta_host, control_port, control_host, control_pass #from TorCtl.StatsSupport import StatsHandler,StatsRouter mt_version = "0.1.0-dev" max_detach = 3 # Do NOT modify this object directly after it is handed to PathBuilder # Use PathBuilder.schedule_selmgr instead. # (Modifying the arguments here is OK) # NOTE: Custom implementations may wish to replace this with their # own PathSupport.BaseSelectionManager implementation __selmgr = PathSupport.SelectionManager( pathlen=3, order_exits=True, percent_fast=80, percent_skip=0, min_bw=1024, use_all_exits=True, uniform=True, use_exit=None, use_guards=True) def clear_dns_cache(c): lines = c.sendAndRecv("SIGNAL CLEARDNSCACHE\r\n") for _,msg,more in lines: plog("DEBUG", msg) def commandloop(s, c, h): "The main metatroller listener loop" s.write("220 Welcome to the Tor Metatroller "+mt_version+"! Try HELP for Info\r\n\r\n")
c.debug(file("control.log", "w")) #c.authenticate_cookie(file("/home/torperf/tor-data1/control_auth_cookie", "r")) FUDValue = c.get_option("FetchUselessDescriptors")[0][1] ExtraInfoValue = c.get_option("DownloadExtraInfo")[0][1] c.set_option("FetchUselessDescriptors", "1") atexit.register(cleanup, *(c, FUDValue)) nslist = c.get_network_status() sorted_rlist = c.read_routers(c.get_network_status()) sorted_rlist.sort(lambda x, y: cmp(y.bw, x.bw)) for i in xrange(len(sorted_rlist)): sorted_rlist[i].list_rank = i mid_rst = FlagsRestriction([], ["Exit", "Guard"]) nmid_rst = PathSupport.OrNodeRestriction([ PathSupport.FlagsRestriction(mandatory=["Guard"], forbidden=[]), PathSupport.FlagsRestriction(mandatory=["Exit"], forbidden=[]) ]) bw_limit_rst = RateLimitedRestriction(True) nbw_limit_rst = RateLimitedRestriction(False) win_rst = PathSupport.OSRestriction(ok=["Win"]) nwin_rst = PathSupport.OSRestriction(ok=[], bad=["Win"]) v2dir_rst = PathSupport.FlagsRestriction(["V2Dir"]) nv2dir_rst = PathSupport.FlagsRestriction([], ["V2Dir"]) win_mid = NodeRestrictionList([mid_rst, win_rst]) win_nmid = NodeRestrictionList([nmid_rst, win_rst]) v2dir_mid = NodeRestrictionList([mid_rst, v2dir_rst])