def get_status(self): try: conn = TorCtl.connect(controlAddr=self.host, controlPort=self.ctrl_port, passphrase=self.password) status = conn.is_live() conn.close() except: pass return status
def __init__(self, host, port, pwd): '''Create connection to Tor controller''' soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) soc.connect((host, port)) self.socket = soc ctl = TorCtl.Connection(self.socket) print ctl ctl.authenticate(pwd) self.ctl = ctl
def get_fast(self): try: ##this doesn't work anymore. It's been compiled into the Tor that you can't do this conn = TorCtl.connect(controlAddr=self.host, controlPort=self.ctrl_port, passphrase=self.password) conn.set_options([("FastFirstHop","0"),("EnforceDistinctSubnet","0"),("UseEntryGuards","0")]) conn.send_signal("RELOAD") print(conn.get_info("circuit-status")) #, "EnforceDistinctSubnet":"0","UseEntryGuards":"0"}) conn.close() except: pass
def process_request(self, request, spider): settings = spider.settings if TimedProxyChange.conn is None: TimedProxyChange.conn = TorCtl.connect(controlAddr=settings.get('TOR_HOST'), controlPort=settings.get('TOR_PORT'), passphrase=settings.get('TOR_PASSW')) TimedProxyChange.last = 0 TimedProxyChange.timelimit = settings.get('TOR_CHANGE_LIMIT') t = time.time() diff = t - TimedProxyChange.last if TimedProxyChange.conn and diff > TimedProxyChange.timelimit: TorCtl.Connection.send_signal(TimedProxyChange.conn, "NEWNYM") TimedProxyChange.last = t log.msg('Proxy changed! New last: %s' % time.strftime("%H:%M:%S"), log.INFO) else: log.msg('Proxy not changed! Time difference is %s seconds' % ("{:.2f}".format(diff)), log.INFO)
def _retry(self, request, reason, spider): settings = spider.settings if RetryChangeProxyMiddleware.conn is None: RetryChangeProxyMiddleware.conn = TorCtl.connect(controlAddr=settings.get('TOR_HOST'), controlPort=settings.get('TOR_PORT'), passphrase=settings.get('TOR_PASSW')) RetryChangeProxyMiddleware.last = 0 RetryChangeProxyMiddleware.timelimit = settings.get('TOR_CHANGE_LIMIT') if isinstance(reason, basestring): #log.msg('Valid retry, reason: ' + reason + ' for URL ' + request.url, log.INFO) t = time.time() diff = t - RetryChangeProxyMiddleware.last if RetryChangeProxyMiddleware.conn and diff > RetryChangeProxyMiddleware.timelimit: TorCtl.Connection.send_signal(RetryChangeProxyMiddleware.conn, "NEWNYM") RetryChangeProxyMiddleware.last = t log.msg('Proxy changed for reason %s. New last: %s' % (reason, time.strftime("%H:%M:%S")), log.INFO) #else: # log.msg('Proxy not changed! Time difference is %s seconds' % ("{:.2f}".format(diff)), log.INFO) return RetryMiddleware._retry(self, request, reason, spider)
def run_example(host, port): """ Example of basic TorCtl usage. See PathSupport for more advanced usage. """ print "host is %s:%d"%(host,port) setup_db("sqlite:///torflow.sqlite", echo=False) #print tc_session.query(((func.count(Extension.id)))).filter(and_(FailedExtension.table.c.row_type=='extension', FailedExtension.table.c.from_node_idhex == "7CAA2F5F998053EF5D2E622563DEB4A6175E49AC")).one() #return #for e in Extension.query.filter(FailedExtension.table.c.row_type=='extension').all(): # if e.from_node: print "From: "+e.from_node.idhex+" "+e.from_node.nickname # if e.to_node: print "To: "+e.to_node.idhex+" "+e.to_node.nickname #return s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((host,port)) c = Connection(s) th = c.launch_thread() c.authenticate(control_pass) c.set_event_handler(TorCtl.ConsensusTracker(c)) c.add_event_listener(ConsensusTrackerListener()) c.add_event_listener(CircuitListener()) print `c.extend_circuit(0,["moria1"])` try: print `c.extend_circuit(0,[""])` except TorCtl.ErrorReply: # wtf? print "got error. good." except: print "Strange error", sys.exc_info()[0] c.set_events([EVENT_TYPE.STREAM, EVENT_TYPE.CIRC, EVENT_TYPE.NEWCONSENSUS, EVENT_TYPE.NEWDESC, EVENT_TYPE.ORCONN, EVENT_TYPE.BW], True) th.join() return
class BandwidthListener(TorCtl.PostEventListener): def __init__(self): TorCtl.PostEventListener.__init__(self) def bandwidth_event(self, event): print "tor read %i bytes and wrote %i bytes" % (event.read, event.written) # constructs a listener that prints BW events myListener = BandwidthListener() # initiates a TorCtl connection, returning None if it was unsuccessful conn = TorCtl.connect() if conn: # tells tor to send us BW events conn.set_events(["BW"]) # attaches the listener so it'll receive BW events conn.add_event_listener(myListener) # run until we get a keyboard interrupt try: while True: time.sleep(10) except KeyboardInterrupt: pass
import time import TorCtl class BandwidthListener(TorCtl.PostEventListener): def __init__(self): TorCtl.PostEventListener.__init__(self) def bandwidth_event(self, event): print "tor read %i bytes and wrote %i bytes" % (event.read, event.written) # constructs a listener that prints BW events myListener = BandwidthListener() # initiates a TorCtl connection, returning None if it was unsuccessful conn = TorCtl.connect() if conn: # tells tor to send us BW events conn.set_events(["BW"]) # attaches the listener so it'll receive BW events conn.add_event_listener(myListener) # run until we get a keyboard interrupt try: while True: time.sleep(10) except KeyboardInterrupt: pass
def open_controller(filename): """ starts stat gathering thread """ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((TorUtil.control_host, TorUtil.control_port)) c = TorCtl.Connection(s) c.authenticate_cookie(file("./tor-data/control_auth_cookie", "r")) c.debug(file(filename + ".log", "w", buffering=0)) guards = get_guards(c, 3) guard_str = ",".join(map(lambda r: "$" + r.idhex, guards)) plog("NOTICE", str(pct_start) + "%: Choosing guards: " + guard_str) # Setconf guards for percentile range c.set_option("EntryNodes", guard_str) c.set_option("StrictNodes", "1") cond = threading.Condition() cond.min_circs = 0 # Python haxx cond.num_circs = 0 # Python haxx cond.acquire() h = CircHandler(c, guards) c.set_event_handler(h) c.add_event_listener(BuildTimeoutTracker(cond)) global FUDValue if not FUDValue: FUDValue = c.get_option("FetchUselessDescriptors")[0][1] c.set_option("FetchUselessDescriptors", "1") c.set_events([ TorCtl.EVENT_TYPE.BUILDTIMEOUT_SET, TorCtl.EVENT_TYPE.BW, TorCtl.EVENT_TYPE.GUARD, TorCtl.EVENT_TYPE.CIRC ], True) # Close all the already open circuits to start fresh h.close_all_circs() cond.wait() cond.release() # Write to output_file: # 1. Num circs # 2. Guards used # 3. Failure quantile (in rerun only) out = file(output_dir + "/result", "w") if not redo_run: out.write("MIN_CIRCS: " + str(cond.min_circs) + "\n") out.write("MIN_TIMEOUT: " + str(cond.min_timeout) + "\n") out.write("MIN_RESET_CNT: " + str(cond.min_reset_cnt) + "\n") out.write("MIN_RESET_TOTAL: " + str(cond.min_reset_total) + "\n") out.write("NUM_CIRCS: " + str(cond.num_circs) + "\n") out.write("NUM_TIMEOUT: " + str(cond.num_timeout) + "\n") out.write("NUM_RESET_CNT: " + str(cond.num_reset_cnt) + "\n") out.write("NUM_RESET_TOTAL: " + str(cond.num_reset_total) + "\n") timeout_cnt = len(h.timeout_circs) built_cnt = len(h.built_circs) build_rate = float(built_cnt) / (built_cnt + timeout_cnt) out.write("BUILD_RATE: " + str(built_cnt) + "/" + str(built_cnt + timeout_cnt) + " " + str(round(build_rate, 3)) + "\n") out.close() return 0
import socks import socket import os import cookielib import mechanize from random import * from time import * import TorCtl conn = TorCtl.connect(controlAddr="127.0.0.1", controlPort=9051, passphrase="123") def create_mac(): f = open("mac",'r') li = f.readlines() for i in range(0,len(li)): li[i] = li[i].strip("\n") shuffle(li) mac_list = li def visit(url): #NewMAC() NewTorIP() #import socks #import socket socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050) socket.socket = socks.socksocket #import urllib2 #import cookielib #import mechanize #def visit(url):
def get_new_circuit(self): conn = TorCtl.connect(controlAddr=self.host, controlPort=self.ctrl_port, passphrase=self.password) conn.send_signal("NEWNYM") conn.close()