[tor-bugs] #14046 [Stem]: stem.util.connection.port_usage always returns 'None'

Tor Bug Tracker & Wiki blackhole at torproject.org
Tue Dec 30 13:36:30 UTC 2014


#14046: stem.util.connection.port_usage always returns 'None'
--------------------+------------------------
 Reporter:  toralf  |          Owner:  atagar
     Type:  defect  |         Status:  new
 Priority:  normal  |      Milestone:
Component:  Stem    |        Version:
 Keywords:          |  Actual Points:
Parent ID:          |         Points:
--------------------+------------------------
 At least in this example
 {{{
 #!/usr/bin/python2

 import sys
 from collections import OrderedDict

 #       https://stem.torproject.org/index.html
 #
 from stem.control import Controller
 from stem.util.system import pid_by_name
 from stem.util.connection import get_connections, system_resolvers,
 port_usage

 def main():
   with Controller.from_port() as controller:
     controller.authenticate()

     bytes_read = int (controller.get_info("traffic/read")) / 10e6
     bytes_written = int (controller.get_info("traffic/written")) / 10e6

     print(" Tor version is %s" % controller.get_version())
     print " read %i MiB, wrote %i. MiB" % (bytes_read, bytes_written)

     print " entry-guards:"
     print controller.get_info("entry-guards", None)

     policy = controller.get_exit_policy()

     resolvers = system_resolvers()
     resolver = resolvers[0]
     print " platform supports connection resolution via: %s (picked: %s)"
 % (', '.join(resolvers), resolver)

     pids = pid_by_name('tor', multiple = True)
     pid = pids[0]
     print " %i instance(s) of tor: %s (picked: %i)" % (len(pids), ',
 '.join(map (str, pids)), pid)

     try:
       connections = get_connections(resolver, process_pid = pid,
 process_name = 'tor')
       print " connections: %4i" % len(connections)

       ports = dict()
       countries = dict()
       n = 0
       for conn in connections:
         addr = conn.remote_address
         port = conn.remote_port
         if (port > 0) & (addr != '0.0.0.0'):
           if policy.can_exit_to(addr, port):
             n = n + 1
             country = controller.get_info("ip-to-country/%s" % addr,
 'unknown')
             if country in countries:
               countries[country] += 1
             else:
               countries[country] = 1
             if port in ports:
               ports[port] += 1
             else:
               ports[port] = 1

       print "\n  exit nodes : %3i" % n
       for port in sorted(ports.keys()):
         print "   port %5s: %3i (%s)" % (port,  ports[port],
 port_usage(port))

       print "\n  countries"
       s = OrderedDict(sorted(countries.items(), key=lambda x: x[1],
 reverse=True)[0:6])
       for country in s:
         print "    %s: %3i" % (country, countries[country])

     except Exception as exc:
       print exc

     print

 if __name__ == '__main__':
   main()
 }}}
 the output is :
 {{{
 tfoerste at t44 ~ $ ssh tfoerste at tor-relay "sudo grep -e Band -e
 '^#ExitPolicy' /etc/tor/torrc; echo; sudo netstat --tcp --udp --program -n
 | grep -c WAIT; echo; sudo /home/tfoerste/info.py"
 BandwidthRate  4 MB
 BandwidthBurst 6 MB
 #ExitPolicy reject *:* # comment this line to allow exit
 #ExitPolicy accept *:80         # http

 100

  Tor version is 0.2.5.10 (git-42b42605f8d8eac2)
  read 40967 MiB, wrote 42151. MiB
  entry-guards:

  platform supports connection resolution via: proc, netstat, lsof, ss
 (picked: proc)
  1 instance(s) of tor: 2223 (picked: 2223)
  connections: 1852

   exit nodes : 331
    port    20:   2 (None)
    port    21:   2 (None)
    port    22:   4 (None)
    port   110:   6 (None)
    port   143:   3 (None)
    port   443: 276 (None)
    port   636:  23 (None)
    port   993:   1 (None)
    port  5222:   4 (None)
    port  5223:   2 (None)
    port  6667:   1 (None)
    port  8080:   7 (None)

   countries
     us: 106
     de:  54
     ru:  51
     fr:  31
     ie:  19
     nl:  12
 }}}

--
Ticket URL: <https://trac.torproject.org/projects/tor/ticket/14046>
Tor Bug Tracker & Wiki <https://trac.torproject.org/>
The Tor Project: anonymity online


More information about the tor-bugs mailing list