From: Cameron Dale Date: Sat, 12 Jan 2008 18:16:32 +0000 (-0800) Subject: Moved the finding IP address function to the util module. X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=0447dd29ac037182294ca7fd80ea47bb028d38c2;p=quix0rs-apt-p2p.git Moved the finding IP address function to the util module. --- diff --git a/apt_dht/apt_dht.py b/apt_dht/apt_dht.py index 0a0059f..0682c8a 100644 --- a/apt_dht/apt_dht.py +++ b/apt_dht/apt_dht.py @@ -13,6 +13,7 @@ from HTTPServer import TopLevel from MirrorManager import MirrorManager from Hash import HashObject from db import DB +from util import findMyIPAddr class AptDHT: def __init__(self, dht): @@ -26,102 +27,12 @@ class AptDHT: self.peers = PeerManager() 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 joinComplete(self, result): + self.my_addr = findMyIPAddr(result, config.getint(config.get('DEFAULT', 'DHT'), 'PORT')) def joinError(self, failure): log.msg("joining DHT failed miserably") diff --git a/apt_dht_Khashmir/util.py b/apt_dht_Khashmir/util.py index 7808857..43c3e44 100644 --- a/apt_dht_Khashmir/util.py +++ b/apt_dht_Khashmir/util.py @@ -1,6 +1,10 @@ ## Copyright 2002-2003 Andrew Loewenstern, All Rights Reserved # see LICENSE.txt for license information +import os, re + +from twisted.python import log + def bucket_stats(l): """given a list of khashmir instances, finds min, max, and average number of nodes in tables""" max = avg = 0 @@ -21,3 +25,100 @@ def bucket_stats(l): avg = avg + c avg = avg / len(l) return {'min':min, 'max':max, 'avg':avg} + +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 findMyIPAddr(addrs, intended_port): + log.msg("got addrs: %r" % (addrs,)) + my_addr = None + + 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 isLocal.match(addr): + addr_count.setdefault(addr, 0) + addr_count[addr] += 1 + + local_addrs = addr_count.keys() + if len(local_addrs) == 1: + my_addr = local_addrs[0] + log.msg('Found remote address from ifconfig: %r' % (my_addr,)) + + # Get counts for all the non-local addresses returned + addr_count = {} + port_count = {} + for addr in addrs: + if not 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 = intended_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 my_addr and my_addr != popular_addr[0]: + log.msg('But the popular address does not match: %s != %s' % (popular_addr[0], my_addr)) + my_addr = popular_addr[0] + elif len(popular_addr) > 1: + log.msg('Found multiple popular addresses: %r' % (popular_addr,)) + if my_addr and 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 my_addr: + log.msg("Remote IP Address could not be found for this machine") + + return my_addr + +def ipAddrFromChicken(): + 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 []