def connectback_result(self, result, downloadid, peerid, ip, port): record = self.downloads.get(downloadid, {}).get(peerid) if record is None or record['ip'] != ip or record['port'] != port: return if not record.has_key('nat'): record['nat'] = int(not result) else: if result: record['nat'] = 0 else: record['nat'] += 1 if result: self.becache1.setdefault(downloadid,{})[peerid] = Bencached(bencode({'ip': ip, 'port': port, 'peer id': peerid})) self.becache2.setdefault(downloadid,{})[peerid] = compact_peer_info(ip, port)
def test_encode_bencached_py2(): """Ensure Bencached objects can be encoded.""" assert bencode([Bencached(bencode('test'))]) == 'l4:teste'
def __init__(self, config, rawserver): self.response_size = config['response_size'] self.dfile = config['dfile'] self.natcheck = config['nat_check'] self.max_give = config['max_give'] self.reannounce_interval = config['reannounce_interval'] self.save_dfile_interval = config['save_dfile_interval'] self.show_names = config['show_names'] self.only_local_override_ip = config['only_local_override_ip'] favicon = config['favicon'] self.favicon = None if favicon: if isfile(favicon): h = open(favicon, 'rb') self.favicon = h.read() h.close() else: print "**warning** specified favicon file -- %s -- does not exist." % favicon self.rawserver = rawserver self.becache1 = {} self.becache2 = {} self.cache1 = {} self.cache2 = {} self.times = {} if exists(self.dfile): h = open(self.dfile, 'rb') ds = h.read() h.close() tempstate = bdecode(ds) else: tempstate = {} if tempstate.has_key('peers'): self.state = tempstate else: self.state = {} self.state['peers'] = tempstate self.downloads = self.state.setdefault('peers', {}) self.completed = self.state.setdefault('completed', {}) statefiletemplate(self.state) for x, dl in self.downloads.items(): self.times[x] = {} for y, dat in dl.items(): self.times[x][y] = 0 if not dat.get('nat', 1): ip = dat['ip'] gip = dat.get('given ip') if gip and is_valid_ipv4(gip) and ( not self.only_local_override_ip or is_local_ip(ip)): ip = gip self.becache1.setdefault(x, {})[y] = Bencached( bencode({ 'ip': ip, 'port': dat['port'], 'peer id': y })) self.becache2.setdefault(x, {})[y] = compact_peer_info( ip, dat['port']) rawserver.add_task(self.save_dfile, self.save_dfile_interval) self.prevtime = time() self.timeout_downloaders_interval = config[ 'timeout_downloaders_interval'] rawserver.add_task(self.expire_downloaders, self.timeout_downloaders_interval) self.logfile = None self.log = None if (config['logfile'] != '') and (config['logfile'] != '-'): try: self.logfile = config['logfile'] self.log = open(self.logfile, 'a') sys.stdout = self.log print "# Log Started: ", isotime() except: print "Error trying to redirect stdout to log file:", sys.exc_info( )[0] self.allow_get = config['allow_get'] if config['allowed_dir'] != '': self.allowed_dir = config['allowed_dir'] self.parse_allowed_interval = config['parse_allowed_interval'] self.parse_allowed() else: self.allowed = None if unquote('+') != ' ': self.uq_broken = 1 else: self.uq_broken = 0 self.keep_dead = config['keep_dead']
confirm = 1 if params.get('event', '') != 'stopped' and confirm: ts[myid] = time() if not peers.has_key(myid): peers[myid] = {'ip': ip, 'port': port, 'left': left} if params.has_key('key'): peers[myid]['key'] = params['key'] if params.has_key('ip') and is_valid_ipv4(params['ip']): peers[myid]['given ip'] = params['ip'] mip = ip if ip_override: mip = params['ip'] if not self.natcheck or ip_override: self.becache1.setdefault(infohash, {})[myid] = Bencached( bencode({ 'ip': mip, 'port': port, 'peer id': myid })) self.becache2.setdefault(infohash, {})[myid] = compact_peer_info( mip, port) else: peers[myid]['left'] = left peers[myid]['ip'] = ip if params.get('event', '') == 'completed': self.completed[infohash] = 1 + self.completed[infohash] if port == 0: peers[myid]['nat'] = 2**30 elif self.natcheck and not ip_override: to_nat = peers[myid].get('nat', -1) if to_nat and to_nat < self.natcheck: