X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=apt_dht%2Fapt_dht.py;h=9fc72ffedafe871cef7faed2967395adb2ce6de2;hb=842adfabdd1ea04a07998c0bfbbb21ffa6ecbfad;hp=c3f1a768c5b2b85db6d90cab6c6927e2e78b5244;hpb=3348b1340e4c881d930071e85d0ef986c71c93cf;p=quix0rs-apt-p2p.git diff --git a/apt_dht/apt_dht.py b/apt_dht/apt_dht.py index c3f1a76..9fc72ff 100644 --- a/apt_dht/apt_dht.py +++ b/apt_dht/apt_dht.py @@ -1,6 +1,6 @@ from binascii import b2a_hex -import os.path +import os, re from twisted.internet import defer from twisted.web2 import server, http, http_headers @@ -16,13 +16,113 @@ class AptDHT: def __init__(self, dht): log.msg('Initializing the main apt_dht application') self.dht = dht + self.dht.loadConfig(config, config.get('DEFAULT', 'DHT')) + self.dht.join().addCallbacks(self.joinComplete, self.joinError) self.http_server = TopLevel(config.get('DEFAULT', 'cache_dir'), self) self.http_site = server.Site(self.http_server) self.peers = PeerManager() - self.mirrors = MirrorManager(self, config.get('DEFAULT', 'cache_dir')) + self.mirrors = MirrorManager(config.get('DEFAULT', 'cache_dir'), self) + self.my_addr = None + self.isLocal = re.compile('^(192\.168\.[0-9]{1,3}\.[0-9]{1,3})|'+ + '(10\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})|'+ + '(172\.0?([1][6-9])|([2][0-9])|([3][0-1])\.[0-9]{1,3}\.[0-9]{1,3})|'+ + '(127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})$') def getSite(self): return self.http_site + + def joinComplete(self, addrs): + log.msg("got addrs: %r" % (addrs,)) + + try: + ifconfig = os.popen("/sbin/ifconfig |/bin/grep inet|"+ + "/usr/bin/awk '{print $2}' | "+ + "sed -e s/.*://", "r").read().strip().split('\n') + except: + ifconfig = [] + + # Get counts for all the non-local addresses returned + addr_count = {} + for addr in ifconfig: + if not self.isLocal.match(addr): + addr_count.setdefault(addr, 0) + addr_count[addr] += 1 + + local_addrs = addr_count.keys() + if len(local_addrs) == 1: + self.my_addr = local_addrs[0] + log.msg('Found remote address from ifconfig: %r' % (self.my_addr,)) + + # Get counts for all the non-local addresses returned + addr_count = {} + port_count = {} + for addr in addrs: + if not self.isLocal.match(addr[0]): + addr_count.setdefault(addr[0], 0) + addr_count[addr[0]] += 1 + port_count.setdefault(addr[1], 0) + port_count[addr[1]] += 1 + + # Find the most popular address + popular_addr = [] + popular_count = 0 + for addr in addr_count: + if addr_count[addr] > popular_count: + popular_addr = [addr] + popular_count = addr_count[addr] + elif addr_count[addr] == popular_count: + popular_addr.append(addr) + + # Find the most popular port + popular_port = [] + popular_count = 0 + for port in port_count: + if port_count[port] > popular_count: + popular_port = [port] + popular_count = port_count[port] + elif port_count[port] == popular_count: + popular_port.append(port) + + port = config.getint(config.get('DEFAULT', 'DHT'), 'PORT') + if len(port_count.keys()) > 1: + log.msg('Problem, multiple ports have been found: %r' % (port_count,)) + if port not in port_count.keys(): + log.msg('And none of the ports found match the intended one') + elif len(port_count.keys()) == 1: + port = port_count.keys()[0] + else: + log.msg('Port was not found') + + if len(popular_addr) == 1: + log.msg('Found popular address: %r' % (popular_addr[0],)) + if self.my_addr and self.my_addr != popular_addr[0]: + log.msg('But the popular address does not match: %s != %s' % (popular_addr[0], self.my_addr)) + self.my_addr = popular_addr[0] + elif len(popular_addr) > 1: + log.msg('Found multiple popular addresses: %r' % (popular_addr,)) + if self.my_addr and self.my_addr not in popular_addr: + log.msg('And none of the addresses found match the ifconfig one') + else: + log.msg('No non-local addresses found: %r' % (popular_addr,)) + + if not self.my_addr: + log.err(RuntimeError("Remote IP Address could not be found for this machine")) + + def ipAddrFromChicken(self): + import urllib + ip_search = re.compile('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}') + try: + f = urllib.urlopen("http://www.ipchicken.com") + data = f.read() + f.close() + current_ip = ip_search.findall(data) + return current_ip + except Exception: + return [] + + def joinError(self, failure): + log.msg("joining DHT failed miserably") + log.err(failure) def check_freshness(self, path, modtime, resp): log.msg('Checking if %s is still fresh' % path)