]> git.mxchange.org Git - quix0rs-apt-p2p.git/blob - apt_dht/util.py
df8d014558c31d4cc42a08daea8d5f5a087e1cf6
[quix0rs-apt-p2p.git] / apt_dht / util.py
1 ## Copyright 2002-2003 Andrew Loewenstern, All Rights Reserved
2 # see LICENSE.txt for license information
3
4 import os, re
5
6 from twisted.python import log
7
8 isLocal = re.compile('^(192\.168\.[0-9]{1,3}\.[0-9]{1,3})|'+
9                      '(10\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})|'+
10                      '(172\.0?([1][6-9])|([2][0-9])|([3][0-1])\.[0-9]{1,3}\.[0-9]{1,3})|'+
11                      '(127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})$')
12
13 def findMyIPAddr(addrs, intended_port, local_ok = False):
14     """Find the best IP address to use from a list of possibilities.
15     
16     @param addrs: the list of possible IP addresses
17     @param intended_port: the port that was supposed to be used
18     @param local_ok: whether known local/private IP ranges are allowed
19         (defaults to False)
20     @return: the preferred IP address, or None if one couldn't be found
21     """
22     log.msg("got addrs: %r" % (addrs,))
23     my_addr = None
24     
25     try:
26         ifconfig = os.popen("/sbin/ifconfig |/bin/grep inet|"+
27                             "/usr/bin/awk '{print $2}' | "+
28                             "sed -e s/.*://", "r").read().strip().split('\n')
29     except:
30         ifconfig = []
31
32     # Get counts for all the non-local addresses returned
33     addr_count = {}
34     for addr in ifconfig:
35         if local_ok or not isLocal.match(addr):
36             addr_count.setdefault(addr, 0)
37             addr_count[addr] += 1
38     
39     local_addrs = addr_count.keys()    
40     if len(local_addrs) == 1:
41         my_addr = local_addrs[0]
42         log.msg('Found remote address from ifconfig: %r' % (my_addr,))
43     
44     # Get counts for all the non-local addresses returned
45     addr_count = {}
46     port_count = {}
47     for addr in addrs:
48         if local_ok or not isLocal.match(addr[0]):
49             addr_count.setdefault(addr[0], 0)
50             addr_count[addr[0]] += 1
51             port_count.setdefault(addr[1], 0)
52             port_count[addr[1]] += 1
53     
54     # Find the most popular address
55     popular_addr = []
56     popular_count = 0
57     for addr in addr_count:
58         if addr_count[addr] > popular_count:
59             popular_addr = [addr]
60             popular_count = addr_count[addr]
61         elif addr_count[addr] == popular_count:
62             popular_addr.append(addr)
63     
64     # Find the most popular port
65     popular_port = []
66     popular_count = 0
67     for port in port_count:
68         if port_count[port] > popular_count:
69             popular_port = [port]
70             popular_count = port_count[port]
71         elif port_count[port] == popular_count:
72             popular_port.append(port)
73             
74     port = intended_port
75     if len(port_count.keys()) > 1:
76         log.msg('Problem, multiple ports have been found: %r' % (port_count,))
77         if port not in port_count.keys():
78             log.msg('And none of the ports found match the intended one')
79     elif len(port_count.keys()) == 1:
80         port = port_count.keys()[0]
81     else:
82         log.msg('Port was not found')
83
84     if len(popular_addr) == 1:
85         log.msg('Found popular address: %r' % (popular_addr[0],))
86         if my_addr and my_addr != popular_addr[0]:
87             log.msg('But the popular address does not match: %s != %s' % (popular_addr[0], my_addr))
88         my_addr = popular_addr[0]
89     elif len(popular_addr) > 1:
90         log.msg('Found multiple popular addresses: %r' % (popular_addr,))
91         if my_addr and my_addr not in popular_addr:
92             log.msg('And none of the addresses found match the ifconfig one')
93     else:
94         log.msg('No non-local addresses found: %r' % (popular_addr,))
95         
96     if not my_addr:
97         log.msg("Remote IP Address could not be found for this machine")
98         
99     return my_addr
100
101 def ipAddrFromChicken():
102     import urllib
103     ip_search = re.compile('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')
104     try:
105          f = urllib.urlopen("http://www.ipchicken.com")
106          data = f.read()
107          f.close()
108          current_ip = ip_search.findall(data)
109          return current_ip
110     except Exception:
111          return []