示例#1
0
    def get_highest_version(self, versionlist):
        """Return the highest Tor version from a list of versions.
        """
        if len(versionlist) is 0:
            return ""

        highest = TorCtl.RouterVersion("0.0.0.0")
        for v in versionlist:
            cur = TorCtl.RouterVersion(v)
            if cur > highest:
                highest = cur

        return str(highest)
示例#2
0
 def __init__(self):
     proxy_support = urllib2.ProxyHandler({"http": "127.0.0.1:8118"})
     opener = urllib2.build_opener(proxy_support)
     urllib2.install_opener(opener)
     self.conn = TorCtl.connect(controlAddr="127.0.0.1",
                                controlPort=9051,
                                passphrase="chijuzipi")
示例#3
0
def save_tor_settings(form):
    """Commit settings through torctl pipe, then send SAVECONF"""
    conn = TorCtl.connect()
    if not conn:
        raise Exception("couldn't connect to tor daemon; need to boot up tor daemon in disabled state")

    conn.set_option('RelayBandwidthRate',
                    "%dKB" % int(form['tor_relaybandwidthrateKBps']))
    conn.set_option('RelayBandwidthBurst',
                    "%dKB" % int(form['tor_relaybandwidthburstKBps']))

    conn.set_option('DNSListenAddress', form['tor_dnslistenaddress'])
    conn.set_option('SocksListenAddress', form['tor_sockslistenaddress'])
    conn.set_option('VirtualAddrNetwork', form['tor_virtualaddrnetwork'])
    conn.set_option('TransListenAddress', form['tor_translistenaddress'])

    if form.get('tor_enable') == 'true':
        conn.set_option('DisableNetwork', '0')
    else:
        conn.set_option('DisableNetwork', '1')

    if form.get('tor_exitnodeenable') == 'true':
        conn.set_option('ExitPolicy', '')
    else: 
        conn.set_option('ExitPolicy', 'reject *:*')

    if form['tor_relaytype'] == 'relay':
        conn.set_options([('ORPort', 'auto'), ('BridgeRelay', '0')])
    elif form['tor_relaytype'] == 'bridge':
        conn.set_options([('ORPort', 'auto'), ('BridgeRelay', '1')])
    else:   # type = 'none'
        conn.set_options([('ORPort', '0'), ('BridgeRelay', '0')])

    conn.save_conf() 
    conn.close()
示例#4
0
    def __init__(self, control_host = _CONTROL_HOST, 
                control_port = _CONTROL_PORT, sock = None, 
                authenticator = _AUTHENTICATOR):
        """Initialize the CtlUtil object, connect to TorCtl."""

        self.sock = sock

        if not sock:
            self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        self.control_host = control_host
        self.control_port = control_port
        self.authenticator = authenticator

        # Try to connect 
        try:
            self.sock.connect((self.control_host, self.control_port))
        except:
            errormsg = "Could not connect to Tor control port.\n" + \
            "Is Tor running on %s with its control port opened on %s?" %\
            (control_host, control_port)

            logging.error(errormsg)
            raise

        
        self.control = TorCtl.Connection(self.sock)

        # Authenticate connection
        self.control.authenticate(config.authenticator)

        # Set up log file
        self.control.debug(debugfile)
示例#5
0
def renew_connection():
    """Connects to local TOR and send signal to renew IP address"""
    conn = TorCtl.connect(controlAddr="127.0.0.1", controlPort=9051, passphrase="12345")
    if not conn:
        raise Exception('Seems you are not running Tor, cant let you do this Dave')
    conn.send_signal("NEWNYM")
    conn.close()
示例#6
0
文件: torTools.py 项目: zhou-kz/arm
 def init(self, conn=None):
   """
   Uses the given TorCtl instance for future operations, notifying listeners
   about the change.
   
   Arguments:
     conn - TorCtl instance to be used, if None then a new instance is fetched
            via the connect function
   """
   
   if conn == None:
     conn = TorCtl.connect()
     
     if conn == None: raise ValueError("Unable to initialize TorCtl instance.")
   
   if conn.is_live() and conn != self.conn:
     self.connLock.acquire()
     
     if self.conn: self.close() # shut down current connection
     self.conn = conn
     self.conn.add_event_listener(self)
     for listener in self.eventListeners: self.conn.add_event_listener(listener)
     
     # sets the events listened for by the new controller (incompatible events
     # are dropped with a logged warning)
     self.setControllerEvents(self.controllerEvents)
     
     self.connLock.release()
     
     self._status = TOR_INIT
     self._statusTime = time.time()
     
     # notifies listeners that a new controller is available
     thread.start_new_thread(self._notifyStatusListeners, (TOR_INIT,))
示例#7
0
 def signal(self, cmd="NEWNYM", country=None):
     self.conn = TorCtl.connect(controlAddr="127.0.0.1", controlPort=9051)
     self.conn.send_signal('HUP')
     if country:
         country = '{%s}' % country.upper()
     self.conn.set_option("ExitNodes", country)
     self.conn.send_signal(cmd)
     self.conn.close()
示例#8
0
 def _retry(self, request, reason, spider):
     log.message('Changing proxy')
     conn = TorCtl.connect(passphrase="1234")
     conn.sendAndRecv('signal newnym\r\n')
     conn.close()      
     time.sleep(3)
     log.message("renewed")
     
     return RetryMiddleware._retry(self, request, reason, spider)
示例#9
0
 def run(self):
     # force to TOR to allocate new IP
     conn = TorCtl.connect(passphrase=settings.TOR_PASSPHRASE)
     
     if conn.is_live():
         conn.sendAndRecv('signal newnym\r\n')
         
     conn.close()
     return True
示例#10
0
def renew_connection():
    """
    Renews the connection to the Tor network, in order to get a new exit node with a different IP address
    See for setup details:
    http://lyle.smu.edu/~jwadleigh/seltest/
    http://sacharya.com/crawling-anonymously-with-tor-in-python/
    For setting up privoxy, need to add { -block } to /etc/privoxy/user.action to prevent filtering of urls
    For TorCTL setup in source 2, also need to change /etc/tor/torrc variable about cookies on line after HashedControlPassword to 0
    """
    conn = TorCtl.connect(controlAddr="127.0.0.1", controlPort=9051,passphrase="mypassword")
    time.sleep(3)
    conn = TorCtl.connect(controlAddr="127.0.0.1", controlPort=9051,passphrase="mypassword")
    time.sleep(3)
    conn = TorCtl.connect(controlAddr="127.0.0.1", controlPort=9051,passphrase="mypassword")
    conn.send_signal("NEWNYM")
    TimeoutException = False
    if TimeoutException == True:
        renew_connection()
示例#11
0
    def connect(self, auth_password = None, torctl_debug_file = None):
        """ Attempt to connect to the Tor control port with the given passphrase. """
        conn = TorCtl.connect(self.host, self.port, auth_password)
        # atagar's patch returns None on error.  Should raise an exception,
        # so for now we do that ourself (torbel.controller.Controller.ConnectError,
        # say that five times fast).
        if not conn:
            raise self.ConnectError()
        
        conn.set_event_handler(self)

        log.notice("Connected to running Tor instance (version %s) on %s:%d",
                   conn.get_info("version")['version'], self.host, self.port)
        # We're interested in:
        #  - New descriptor/consensus events, to keep track of new exit routers.
        events = [TorCtl.EVENT_TYPE.NEWDESC,
                  TorCtl.EVENT_TYPE.NEWCONSENSUS]
        # If we're testing routers, we are also in need of:
        #  - Circuit events
        #  - Stream events.
        #  - Tor connection events.
        if self.tests_enabled:
            events += [TorCtl.EVENT_TYPE.CIRC,
                       TorCtl.EVENT_TYPE.STREAM,
                       TorCtl.EVENT_TYPE.ORCONN]
        # - We NEED extended events to function.
        conn.set_events(events, extended = True)

        self.conn = conn
        # Set up TorCtl debugging file if requested.
        # Logs all controller communications.
        if torctl_debug_file:
            if type(torctl_debug_file) is file:
                self.conn.debug(torctl_debug_file)
            else:
                raise ValueError("torctl_debug_file must be an open file object.")

        self.init_tor()

        ## If the user has not configured test_host, use Tor's
        ## best guess at our external IP address.
        if not config.test_host:
            config.test_host = conn.get_info("address")["address"]

        # Resolve config.test_host for later use.
        self.test_ip = socket.gethostbyname(config.test_host)

        log.notice("Our external test IP address should be %s.", config.test_host)
            
        # Build a list of Guard routers, so we have a list of reliable
        # first hops for our test circuits.
        log.info("Building router and guard caches from NetworkStatus documents.")
        self._update_consensus(self.conn.get_network_status())

        with self.consensus_cache_lock:
            log.notice("Tracking %d routers, %d of which are guards.",
                       len(self.router_cache), len(self.guard_cache))
示例#12
0
def renew_connection():
	try:
		threading.Lock().acquire()
	 	conn = TorCtl.connect(controlAddr="127.0.0.1", controlPort=9051, passphrase="Camicyoab")
		conn.send_signal("NEWNYM")
		conn.close()
	except:
		sys.exit()
	else:
		threading.Lock().release()
示例#13
0
文件: base.py 项目: raisulru/crawler
 def renew_identity(self):
     try:
         conn = TorCtl.connect(controlAddr="127.0.0.1",
                               controlPort=9051,
                               passphrase="@hirok32")
         conn.send_signal("NEWNYM")
         conn.close()
     except:
         logging.critical(
             "SOMETHINGS WENT WRONG!!, YOUR IP MAY BE BLOCKED IF CONTINUE. PLEASE EXIT AND BE SAFE"
         )
示例#14
0
def download_network_view():
    global VIDALIA_PATH #I belive this is unneeded in this fork
    global CONTROL_PORT
    global AUTH_PASS    #I belive this is unneeded in this fork

    print "starting.."
    # open the TOR connection
    conn = TorCtl.connect(controlAddr="127.0.0.1", controlPort=CONTROL_PORT)
    all_nodes = conn.get_network_status()
    print "wrapping it up."
    conn.close()
    return all_nodes
示例#15
0
def download_network_view():
    global VIDALIA_PATH  #I belive this is unneeded in this fork
    global CONTROL_PORT
    global AUTH_PASS  #I belive this is unneeded in this fork

    print "starting.."
    # open the TOR connection
    conn = TorCtl.connect(controlAddr="127.0.0.1", controlPort=CONTROL_PORT)
    all_nodes = conn.get_network_status()
    print "wrapping it up."
    conn.close()
    return all_nodes
示例#16
0
def renew_connection():
    try:
        threading.Lock().acquire()
        conn = TorCtl.connect(controlAddr="127.0.0.1",
                              controlPort=9051,
                              passphrase="Camicyoab")
        conn.send_signal("NEWNYM")
        conn.close()
    except:
        sys.exit()
    else:
        threading.Lock().release()
示例#17
0
文件: scraper.py 项目: cmry/gomi
 def renew_connection(self):
     saved_stdout, saved_stderr = sys.stdout, sys.stderr
     sys.stdout = sys.stderr = open(os.devnull, "w")
     try:
         conn = TorCtl.connect(controlAddr="127.0.0.1", controlPort=9051, passphrase="ThisIsTor24")
         conn.send_signal("NEWNYM")
         conn.close()
     except AttributeError:
         self.log.nlog.warning("No onion for you.")
         time.sleep(60)
         self.renew_connection()
     sys.stdout, sys.stderr = saved_stdout, saved_stderr
示例#18
0
def listen():
    """Sets up a connection to TorCtl and launches a thread to listen for
    new consensus events.
    """
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    ctrl_host = '127.0.0.1'
    ctrl_port = config.control_port
    sock.connect((ctrl_host, ctrl_port))
    ctrl = TorCtl.Connection(sock)
    ctrl.launch_thread(daemon=0)
    ctrl.authenticate(config.authenticator)
    ctrl.set_event_handler(MyEventHandler())
    ctrl.set_events([TorCtl.EVENT_TYPE.NEWCONSENSUS])
    print 'Listening for new consensus events.'
    logging.info('Listening for new consensus events.')
示例#19
0
def restart_tor():
    grepcode = os.system("grep '^DisableNetwork 1' /etc/tor/torrc")
    if grepcode == 512:
        # permission denied
        raise IOError("Don't have permission to read /etc/tor/torrc")
    elif grepcode == 0:
        pass # found
    else:
        if os.system('echo "DisableNetwork 1" >> /etc/tor/torrc') != 0:
            raise IOError("Don't have permission to write /etc/tor/torrc")
    util.enable_service('tor')
    time.sleep(3)
    conn = TorCtl.connect()
    if not conn:
        raise Exception("Could not start tor daemon!")
    conn.close()
示例#20
0
def renew_connection():
    '''
    When bandwidth limit is exceeded (http error 509), this function is
    called to change ip address by establishing new tor connection
    '''
    old_ip = get_ip_address()
    print "Old ip address is " + old_ip
    conn = TorCtl.connect(controlAddr="127.0.0.1",
                          controlPort=tor_control_port,
                          passphrase=password)
    conn.send_signal("NEWNYM")
    conn.close()
    new_ip = get_ip_address()
    while new_ip == old_ip:
        new_ip = get_ip_address()
    print "New ip address is " + new_ip
示例#21
0
def changeip(host="localhost", port=9051, passphrase=""):
    #from TorCtl import TorCtl
    #conn = TorCtl.connect()

    socket.socket = __originalSocket
    socket.create_connection = __originalCreateConnection

    conn = TorCtl.connect(controlAddr=host,
                          controlPort=port,
                          passphrase=passphrase)
    conn.send_signal(0x03)  #NEWNYMRN
    #conn.send_signal("NEWNYM")
    #conn.sendAndRecv('signal newnymrn')
    #conn.close()
    socket.socket = socks.socksocket
    socket.create_connection = create_connection
    time.sleep(5)
def renew_connection():
    '''
    When bandwidth limit is exceeded (http error 509), this function is
    called to change ip address by establishing new tor connection
    '''
    old_ip = get_ip_address()
    print "Old ip address is " + old_ip
    conn = TorCtl.connect(
        controlAddr="127.0.0.1",
        controlPort=tor_control_port,
        passphrase=password)
    conn.send_signal("NEWNYM")
    conn.close()
    new_ip = get_ip_address()
    while new_ip == old_ip:
        new_ip = get_ip_address()
    print "New ip address is " + new_ip
示例#23
0
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)
示例#24
0
 def connectManagedInstance(self):
   """
   Attempts to connect to a managed tor instance, raising an IOError if
   unsuccessful.
   """
   
   torctlConn, authType, authValue = TorCtl.preauth_connect(controlPort = int(CONFIG["wizard.default"]["Control"]))
   
   if not torctlConn:
     msg = "Unable to start tor, try running \"tor -f %s\" to see the error output" % self.getTorrcPath()
     raise IOError(msg)
   
   if authType == TorCtl.AUTH_TYPE.COOKIE:
     try:
       torctlConn.authenticate(authValue)
       torTools.getConn().init(torctlConn)
     except Exception, exc:
       raise IOError("Unable to connect to Tor: %s" % exc)
示例#25
0
文件: torTools.py 项目: zhou-kz/arm
 def setOption(self, param, value):
   """
   Issues a SETCONF to set the given option/value pair. An exeptions raised
   if it fails to be set.
   
   Arguments:
     param - configuration option to be set
     value - value to set the parameter to (this can be either a string or a
             list of strings)
   """
   
   isMultiple = isinstance(value, list) or isinstance(value, tuple)
   self.connLock.acquire()
   
   startTime, raisedExc = time.time(), None
   if self.isAlive():
     try:
       if isMultiple: self.conn.set_options([(param, val) for val in value])
       else: self.conn.set_option(param, value)
       
       # flushing cached values (needed until we can detect SETCONF calls)
       for fetchType in ("str", "list", "map"):
         entry = (param, fetchType)
         
         if entry in self._cachedConf:
           del self._cachedConf[entry]
     except (socket.error, TorCtl.ErrorReply, TorCtl.TorCtlClosed), exc:
       if type(exc) == TorCtl.TorCtlClosed: self.close()
       elif type(exc) == TorCtl.ErrorReply:
         excStr = str(exc)
         if excStr.startswith("513 Unacceptable option value: "):
           # crops off the common error prefix
           excStr = excStr[31:]
           
           # Truncates messages like:
           # Value 'BandwidthRate la de da' is malformed or out of bounds.
           # to: Value 'la de da' is malformed or out of bounds.
           if excStr.startswith("Value '"):
             excStr = excStr.replace("%s " % param, "", 1)
           
           exc = TorCtl.ErrorReply(excStr)
       
       raisedExc = exc
示例#26
0
    def open(self, url):
        """
        Apertura una pagina web con Proxy o no, y retorna lo leido.
        * url -- URL de la pagina web
        """
        if self.proxy_status:
            tor = TorCtl.connect(
                controlAddr=self.addr,
                controlPort=self.port,
                passphrase=self.passwd)
            tor.send_signal('NEWNYM')
            tor.close()

            proxy = ProxyHandler({'http': '127.0.0.1:8118'})
            proxy = build_opener(proxy)
            install_opener(proxy)
            request = Request(url, None, self.header)
            return urlopen(request).read()
        else:
            return urlopen(url).read()
示例#27
0
def change_ip(verbose=False):

    import time
    import os
    import sys
    from TorCtl import TorCtl

    if verbose: print "Renewing TOR route:", 
    sys.stdout.flush()
    torcontrol = TorCtl.connect()
    if torcontrol is None: raise TorNotReachableException()
    torcontrol.sendAndRecv("signal newnym\r\n")
    time.sleep(5)
    if verbose: print "done"
    sys.stdout.flush()
    # stupid function is sending output
    stdout = sys.stdout
    sys.stdout = open(os.devnull,"w")
    torcontrol.close()
    sys.stdout = stdout
示例#28
0
    def connectManagedInstance(self):
        """
    Attempts to connect to a managed tor instance, raising an IOError if
    unsuccessful.
    """

        torctlConn, authType, authValue = TorCtl.preauth_connect(
            controlPort=int(CONFIG["wizard.default"]["Control"]))

        if not torctlConn:
            msg = "Unable to start tor, try running \"tor -f %s\" to see the error output" % self.getTorrcPath(
            )
            raise IOError(msg)

        if authType == TorCtl.AUTH_TYPE.COOKIE:
            try:
                torctlConn.authenticate(authValue)
                torTools.getConn().init(torctlConn)
            except Exception, exc:
                raise IOError("Unable to connect to Tor: %s" % exc)
def retrieve(idx):

    while True:
        try:
            cnt = random.randint(0, len(api) - 1)
            response = api[cnt].get_match_details(match_id=idx)
            #print '%d success'%idx
            break
        except Exception, e:
            #print "%s %d"%(e,idx)
            cnt = random.randint(0, num_process)
            if cnt == 0:
                conn = TorCtl.connect(controlAddr="127.0.0.1",
                                      controlPort=9051,
                                      passphrase="123456")
                conn.send_signal("NEWNYM")
                conn.close()
            else:
                time.sleep(random.randint(0, 10))

            continue
示例#30
0
def main():
  if len(sys.argv) < 3:
    usage()
    return

  port = int(sys.argv[1])
  speed = sys.argv[2]

  if not speed in ("fast", "slow", "fastratio", "slowratio"):
    TorUtil.plog("ERROR",
        "Second parameter must be 'fast', 'slow', 'fastratio', or 'slowratio'")
    return

  conn = TorCtl.connect(HOST, port)

  conn.set_option("StrictEntryNodes", "1")
  conn.set_option("UseEntryNodes", "1")

  EntryTracker(conn, speed)
  conn.set_events(["NEWCONSENSUS", "NEWDESC", "NS", "GUARD"])
  conn.block_until_close()
示例#31
0
def main():
	"""Main function for testing other tool functions.
	Not intended to be used as a script by itself.
	"""
	#__originalSocket = setWebDefaults()

	for i in range(1,51):

		#proxy_support = urllib2.ProxyHandler({"http" : "127.0.0.1:8118"} )
		#opener = urllib2.build_opener(proxy_support)
		#opener.addheaders = [('User-agent', 'Mozilla/5.0')]
		#urllib2.install_opener(opener)

		# every urlopen connection will then use the TOR proxy like this one :
		print "IP " + str(i) + ":"
		print urllib2.urlopen('http://ifconfig.me/ip').read()
		# and to renew my route when i need to change the IP :
		#print "Renewing tor route wait a bit for 5 seconds"
		#from TorCtl import TorCtl
		conn = TorCtl.connect(controlAddr="127.0.0.1", controlPort=9051, passphrase="torPass")
		conn.sendAndRecv('signal newnymrn')
		conn.close()
def download_network_view():
        global VIDALIA_PATH
        global CONTROL_PORT
        global AUTH_PASS
		
        start_tor=True
        ps_list = subprocess.Popen(TASKLIST, stdout=subprocess.PIPE).communicate()[0]
	for ps in ps_list:
                if ps.startswith(TOR_PS_NAME):
			start_tor = False
	if start_tor:
                print "Initializing TOR.."
                subprocess.Popen([TOR_PATH])
                time.sleep(20)
        
	print "starting.."
        # open the TOR connection
        conn = TorCtl.connect(controlAddr="127.0.0.1", controlPort=CONTROL_PORT)
        all_nodes = conn.get_network_status()

        print "wrapping it up."
        conn.close()
        return all_nodes
示例#33
0
def get_tor_settings():
    """Ask for settings over torctl pipe; don't use augeas"""
    d = dict()
    conn = TorCtl.connect()
    if not conn:
        # couldn't connect; will need to display error to user
        return None
    d['tor_relaybandwidthrateKBps'] = \
        int(int(tor_getoption(conn, 'RelayBandwidthRate'))/1024.)
    d['tor_relaybandwidthburstKBps'] = \
        int(int(tor_getoption(conn, 'RelayBandwidthBurst'))/1024.)

    d['tor_sockslistenaddress'] = tor_getoption(conn, 'SocksListenAddress')
    d['tor_virtualaddrnetwork'] = tor_getoption(conn, 'VirtualAddrNetwork')
    d['tor_translistenaddress'] = tor_getoption(conn, 'TransListenAddress')
    d['tor_dnslistenaddress'] = tor_getoption(conn, 'DNSListenAddress')

    d['tor_enable'] = (not bool(int(tor_getoption(conn, 'DisableNetwork')))) and 'true'
    if tor_getoption(conn, 'ExitPolicy') == 'reject *:*':
        d['tor_exitnodeenable'] = False
    else:
        d['tor_exitnodeenable'] = 'true'

    orport = tor_getoption(conn, 'ORPort')
    bridgerelay = tor_getoption(conn, 'BridgeRelay')
    if orport == 'auto' and bridgerelay == '0':
        d['tor_relaytype'] = 'relay'
    elif orport == 'auto' and bridgerelay == '1':
        d['tor_relaytype'] = 'bridge'
    elif orport == '0':
        d['tor_relaytype'] = 'none'
    else:
        print "WARNING: unknown tor_relaytype state"
        d['tor_relaytype'] = 'unknown'

    conn.close()
    return d
def download_network_view():
    global VIDALIA_PATH
    global CONTROL_PORT
    global AUTH_PASS

    start_tor = True
    ps_list = subprocess.Popen(TASKLIST,
                               stdout=subprocess.PIPE).communicate()[0]
    for ps in ps_list:
        if ps.startswith(TOR_PS_NAME):
            start_tor = False
    if start_tor:
        print "Initializing TOR.."
        subprocess.Popen([TOR_PATH])
        time.sleep(20)

    print "starting.."
    # open the TOR connection
    conn = TorCtl.connect(controlAddr="127.0.0.1", controlPort=CONTROL_PORT)
    all_nodes = conn.get_network_status()

    print "wrapping it up."
    conn.close()
    return all_nodes
示例#35
0
def newId():
    conn = TorCtl.connect(controlAddr="127.0.0.1", controlPort=9051)
    conn.send_signal("NEWNYM")
示例#36
0
def renew_connection():
    conn = TorCtl.connect(controlAddr="127.0.0.1",
                          controlPort=9051,
                          passphrase="password")
    conn.send_signal("NEWNYM")
    conn.close()
示例#37
0
 d = dict()
 try:
     f = open('/var/run/tor/tor.pid', 'r')
     f.close()
 except IOError, ioe:
     if ioe.errno == 13:
         # "Permission denied"
         d['state'] = 'PERMISSION_DENIED'
         return d
     elif ioe.errno == 2:
         # "No such file or directory"
         d['state'] = 'DISABLED'
         return d
     else:
         raise ioe
 conn = TorCtl.connect()
 if not conn:
     d['state'] = 'DISABLED'
     return d
 d['version'] = tor_getinfo(conn, 'version')
 d['traffic_read_bytes'] = int(tor_getinfo(conn, 'traffic/read'))
 d['traffic_written_bytes'] = int(tor_getinfo(conn, 'traffic/written'))
 d['version_current'] = tor_getinfo(conn, 'status/version/current')
 d['circuit_established'] = bool(
     tor_getinfo(conn, 'status/circuit-established'))
 d['is_alive'] = conn.is_live()
 if d['circuit_established']:
     d['state'] = 'ESTABLISHED'
 else:
     d['state'] = 'CONNECTING'
 conn.close()
示例#38
0
    def connect(self, bridge, timeout=None):
        bridgeinfo = None
        bandwidth = None
        public = None
        if not timeout:
            if self.config.tests.tor_bridges_timeout:
                self.timeout = self.config.tests.tor_bridges_timeout
            timeout = self.timeout
        torrc, tordir, controlport, socksport = self.writetorrc(bridge)
        cmd = ["tor", "-f", torrc]

        tupdate = time.time()
        debugupdate = time.time()

        try:
            p = Popen(cmd, stdout=PIPE)
        except:
            self.logger.error("Error in starting Tor (do you have tor installed?)")

        # XXX this only works on UNIX (do we care?)
        # Make file reading non blocking
        try:
            fcntl.fcntl(p.stdout, fcntl.F_SETFL, os.O_NONBLOCK)
        except:
            self.logger.error("Unable to set file descriptor to non blocking")

        self.logger.info("Testing bridge: %s" % bridge)
        while True:
            o = ""
            try:
                o = p.stdout.read(4096)
                if o:
                    self.logger.debug(str(o))
                if re.search("100%", o):
                    self.logger.info("Success in connecting to %s" % bridge)

                    print "%s bridge works" % bridge
                    # print "%s controlport" % controlport
                    try:
                        c = TorCtl.connect('127.0.0.1', controlport)
                        bridgeinfo = self.parsebridgeinfo(c.get_info('dir/server/all')['dir/server/all'])
                        c.close()
                    except:
                        self.logger.error("Error in connecting to Tor Control port")

                    # XXX disable the public checking
                    #public = self.is_public(bridgeinfo['fingerprint'], socksport)
                    #self.logger.info("Public: %s" % public)

                    bandwidth = self.download_file(socksport)
                    self.logger.info("Bandwidth: %s" % bandwidth)

                    try:
                        p.stdout.close()
                    except:
                        self.logger.error("Error in closing stdout FD.")

                    try:
                        os.unlink(os.path.join(os.getcwd(), torrc))
                        rmtree(tordir)
                    except:
                        self.logger.error("Error in unlinking files.")

                    p.terminate()
                    return {
                            'Time': datetime.now(),
                            'Bridge': bridge,
                            'Working': True,
                            'Descriptor': bridgeinfo,
                            'Calculated bandwidth': bandwidth,
                            'Public': public
                            }

                if re.search("%", o):
                    # Keep updating the timeout if there is progress
                    self.logger.debug("Updating time...")
                    tupdate = time.time()
                    #print o
                    continue

            except IOError:
                ex = sys.exc_info()[1]
                if ex[0] != errno.EAGAIN:
                    self.logger.error("Error IOError: EAGAIN")
                    raise
                sys.exc_clear()

            try:
                # Set the timeout for the socket wait
                ct = timeout-(time.time() - tupdate)
                socket.wait_read(p.stdout.fileno(), timeout=ct)

            except:
                lfh = open(os.path.join(tordir, 'tor.log'), 'r')
                log = lfh.readlines()
                lfh.close()
                self.logger.info("%s bridge does not work (%s s timeout)" % (bridge, timeout))
                print "%s bridge does not work (%s s timeout)" % (bridge, timeout)
                self.failures.append(bridge)
                p.stdout.close()
                os.unlink(os.path.join(os.getcwd(), torrc))
                rmtree(tordir)
                p.terminate()
                return {
                        'Time': datetime.now(),
                        'Bridge': bridge,
                        'Working': False,
                        'Descriptor': {},
                        'Log': log
                        }
示例#39
0
import urllib2
# using TOR !
proxy_support = urllib2.ProxyHandler({"http" : "127.0.0.1:8118"} )
opener = urllib2.build_opener(proxy_support)
urllib2.install_opener(opener)
# every urlopen connection will then use the TOR proxy like this one :
urllib2.urlopen('http://www.google.fr').read()
# and to renew my route when i need to change the IP :
print "Renewing tor route wait a bit for 5 seconds"
from TorCtl import TorCtl
conn = TorCtl.connect(passphrase="1234")
conn.sendAndRecv('signal newnym\r\n')
conn.close()
import time
time.sleep(5)
print "renewed"
示例#40
0
def check_tor():
    conn = TorCtl.connect(passphrase=TORCTL_PASS)
    if conn:
        return True
    else:
        return False
示例#41
0
文件: base.py 项目: dvomaks/inspirado
def newnym():
    print 'NEWNYM'
    conn = TorCtl.connect()
    conn.send_signal('NEWNYM')
示例#42
0
def get_new_address():
    conn = TorCtl.connect(passphrase=TORCTL_PASS)
    conn.sendAndRecv('signal newnym\r\n')
    conn.close()
    time.sleep(10)
示例#43
0
import urllib2
from TorCtl import TorCtl

proxy_support = urllib2.ProxyHandler({"http": "127.0.0.1:8118"})
opener = urllib2.build_opener(proxy_support)
urllib2.install_opener(opener)

conn = TorCtl.connect(controlAddr="127.0.0.1",
                      controlPort=9051,
                      passphrase="chijuzipi")


def newId():
    conn.send_signal("NEWNYM")


def readURL(url):
    req = urllib2.Request(url)
    try:
        resp = urllib2.urlopen(req)
    except urllib2.HTTPError as e:
        return e.code
    except urllib2.URLError as e:
        return "connection refused"
    else:
        # 200
        body = resp.read()
        print body
        return 200

示例#44
0
 def change_my_ip_connection(self):
     conn = TorCtl.connect(controlAddr="127.0.0.1",
                           controlPort=9051,
                           passphrase=self.hashed)
     conn.send_signal("NEWNYM")
     conn.close()
示例#45
0
 def connect(self):
   self._conn = TorCtl.connect(HOST, self._port)
   self._conn.debug(file("control.log", "w"))
   if not self._conn:
     sys.exit(2)
示例#46
0
def main(argv):
  TorUtil.read_config(argv[1]+"/scanner.1/bwauthority.cfg")
  TorUtil.logfile = "data/aggregate-debug.log"

  (branch, head) = TorUtil.get_git_version(PATH_TO_TORFLOW_REPO)
  plog('NOTICE', 'TorFlow Version: %s' % branch+' '+head)
  (branch, head) = TorUtil.get_git_version(PATH_TO_TORCTL_REPO)
  plog('NOTICE', 'TorCtl Version: %s' % branch+' '+head)

  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  s.connect((TorUtil.control_host,TorUtil.control_port))
  c = TorCtl.Connection(s)
  c.debug(file(argv[1]+"/aggregate-control.log", "w", buffering=0))
  c.authenticate_cookie(file(argv[1]+"/tor.1/control_auth_cookie",
                         "r"))

  ns_list = c.get_network_status()
  for n in ns_list:
    if n.bandwidth == None: n.bandwidth = -1
  ns_list.sort(lambda x, y: int(y.bandwidth/10000.0 - x.bandwidth/10000.0))
  for n in ns_list:
    if n.bandwidth == -1: n.bandwidth = None
  got_ns_bw = False
  max_rank = len(ns_list)

  cs_junk = ConsensusJunk(c)

  # TODO: This is poor form.. We should subclass the Networkstatus class
  # instead of just adding members
  for i in xrange(max_rank):
    n = ns_list[i]
    n.list_rank = i
    if n.bandwidth == None:
      plog("NOTICE", "Your Tor is not providing NS w bandwidths for "+n.idhex)
    else:
      got_ns_bw = True
    n.measured = False
    prev_consensus["$"+n.idhex] = n

  if not got_ns_bw:
    # Sometimes the consensus lacks a descriptor. In that case,
    # it will skip outputting 
    plog("ERROR", "Your Tor is not providing NS w bandwidths!")
    sys.exit(0)

  # Take the most recent timestamp from each scanner 
  # and use the oldest for the timestamp of the result.
  # That way we can ensure all the scanners continue running.
  scanner_timestamps = {}
  for da in argv[1:-1]:
    # First, create a list of the most recent files in the
    # scan dirs that are recent enough
    for root, dirs, f in os.walk(da):
      for ds in dirs:
        if re.match("^scanner.[\d+]$", ds):
          newest_timestamp = 0
          for sr, sd, files in os.walk(da+"/"+ds+"/scan-data"):
            for f in files:
              if re.search("^bws-[\S]+-done-", f):
                fp = file(sr+"/"+f, "r")
                slicenum = sr+"/"+fp.readline()
                timestamp = float(fp.readline())
                fp.close()
                # old measurements are probably
                # better than no measurements. We may not
                # measure hibernating routers for days.
                # This filter is just to remove REALLY old files
                if time.time() - timestamp > MAX_AGE:
                  sqlf = f.replace("bws-", "sql-")
                  plog("INFO", "Removing old file "+f+" and "+sqlf)
                  os.remove(sr+"/"+f)
                  try:
                    os.remove(sr+"/"+sqlf)
                  except:
                    pass # In some cases the sql file may not exist
                  continue
                if timestamp > newest_timestamp:
                  newest_timestamp = timestamp
                bw_files.append((slicenum, timestamp, sr+"/"+f))
          scanner_timestamps[ds] = newest_timestamp

  # Need to only use most recent slice-file for each node..
  for (s,t,f) in bw_files:
    fp = file(f, "r")
    fp.readline() # slicenum
    fp.readline() # timestamp
    for l in fp.readlines():
      try:
        line = Line(l,s,t)
        if line.idhex not in nodes:
          n = Node()
          nodes[line.idhex] = n
        else:
          n = nodes[line.idhex]
        n.add_line(line)
      except ValueError,e:
        plog("NOTICE", "Conversion error "+str(e)+" at "+l)
      except AttributeError, e:
        plog("NOTICE", "Slice file format error "+str(e)+" at "+l)
      except Exception, e:
        plog("WARN", "Unknown slice parse error "+str(e)+" at "+l)
        traceback.print_exc()
示例#47
0
 def __init__(self, waitTime):
   self.conn = TorCtl.connect(controlAddr="127.0.0.1", controlPort=9051, passphrase="test")
   self.lastChangeId = int(time.time())
   self.waitTime = waitTime
def newId():
    conn = TorCtl.connect(controlAddr="127.0.0.1", controlPort=9051, passphrase="123")
    conn.send_signal("NEWNYM")
示例#49
0
def change_identity():
    conn = TorCtl.connect(controlAddr="127.0.0.1",
                          controlPort=9051,
                          passphrase="pubbeti6")
    conn.send_signal("NEWNYM")
    conn.close()
示例#50
0
 def connect(self):
     self._conn = TorCtl.connect(HOST, self._port)
     if not self._conn:
         sys.exit(2)
示例#51
0
def renew_connection():
	conn = TorCtl.connect(controlAddr="127.0.0.1", controlPort=9051, passphrase="password") #use a better password than this
	conn.send_signal("NEWNYM")
	conn.close()
	print "Waiting for connection to renew"
	time.sleep(10) #added so it could finish drawing new circuit before next step
示例#52
0
def renew_connection():
    conn = TorCtl.connect(controlAddr="localhost",
                          controlPort=9151,
                          passphrase="password")
    conn.send_signal("NEWNYM")
    conn.close()
示例#53
0
 def connect(self):
   self._conn = TorCtl.connect(HOST, self._port)
   if not self._conn:
     sys.exit(2)
示例#54
0
 def renew_connection():
     conn = TorCtl.connect(controlAddr="127.0.0.1", controlPort=9051, passphrase="your_password")
     conn.send_signal("NEWNYM")
     conn.close()
示例#55
0
from TorCtl.PathSupport import *
import atexit

TorUtil.loglevel = "WARN"

# TODO:
#  Print ratios of All, Guard, Mid, Exit and Guard+Exit nodes
#    - Print Num < 1, Num >= 1, avg < 1, avg >= 1, and avg for each

def cleanup(c, f):
  print "Resetting FetchUselessDescriptors to "+f
  c.set_option("FetchUselessDescriptors", f) 
  

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
c = TorCtl.connect("127.0.0.1",9051)
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(
          [
def get_new_id():
    conn = TorCtl.connect(controlAddr="127.0.0.1", controlPort=9051, passphrase="innovaccer@123")
    print "Connection : " , conn
    conn.send_signal("NEWNYM")
示例#57
0
def Granularity(sentenceArray):
    bad = 0
    good = 0
    for sentence in sentenceArray:
        # print(sentence)
        try:
            buf = StringIO()
            c = pycurl.Curl()
            c.setopt(c.URL, 'http://text-processing.com/api/sentiment/')
            c.setopt(c.WRITEFUNCTION, buf.write)
            c.setopt(c.HTTPHEADER, ['Accept-Charset: UTF-8'])
            c.setopt(c.POSTFIELDS, "text=" + sentence)
            c.setopt(pycurl.PROXY, '127.0.0.1:9050')
            c.setopt(pycurl.PROXYTYPE, pycurl.PROXYTYPE_SOCKS5)
            c.perform()
            c.close()
            data = buf.getvalue()
            os.system("printf \"" + word + " , " + sentence + data + "\n\">> granworddebug.txt")
            print(word, sentence, data)
            if ("Throttled" in data):
                print("Throttled")
                from TorCtl import TorCtl

                conn = TorCtl.connect(controlAddr="127.0.0.1", controlPort=9151, passphrase="zazaking")
                print(conn.is_live())
                conn.send_signal("NEWNYM")
                # conn.sendAndRecv('signal newnymrn')
                conn.close()
                import time

                time.sleep(5)
                print
                "renewed"
            info = json.dumps(data)
            json_object = json.loads(json.loads(info))
            # print(json_object)
            #print(json_object['label'])
            if str(json_object['label']) == "neg":
                #print("neg")
                bad = bad + 1
            if str(json_object['label']) == "pos":
                #print("pos")
                good = good + 1

        except Exception as e:
            print(e)
        # print (good)
        # print (bad)
        shifted = 0
        #if 'not' in sentence:
        #    shifted == 1


    # 0 good
    # 1 bad
    #2 neutral
    if ((good > bad) & (shifted == 0)):
        osprint(0)
        #print ("a good word")
    elif ((good > bad) & (shifted == 1)):
        osprint(1)
        #print ("bad word")
    elif ((good < bad) & (shifted == 0)):
        osprint(1)
        # print ("a bad word")
    elif ((good < bad) & (shifted == 1)):
        osprint(0)
        # print ("good word")
    else:
        osprint(2)