Opened 3 years ago

Closed 3 years ago

#14046 closed defect (fixed)

stem.util.connection.port_usage always returns 'None'

Reported by: toralf Owned by: atagar
Priority: Medium Milestone:
Component: Core Tor/Stem Version:
Severity: Keywords:
Cc: Actual Points:
Parent ID: Points:
Reviewer: Sponsor:

Description

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@t44 ~ $ ssh tfoerste@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

Child Tickets

Change History (2)

comment:1 Changed 3 years ago by atagar

Hi toralf, think I see the issue. The setup.py doesn't inlcude ports.cfg in its package_data. This works with a git checkout...

% tor-prompt
>>> from stem.util.connection import port_usage
>>> port_usage(80)
'HTTP'

... but not via packages. Thanks for the catch! Gotta head to work but I'll fix this over the weekend.

comment:2 Changed 3 years ago by atagar

Resolution: fixed
Status: newclosed
Note: See TracTickets for help on using tickets.