--- BitTorrent/HTTPHandler.py 13 Apr 2004 02:22:07 -0000 1.1.1.7 +++ BitTorrent/HTTPHandler.py 21 Oct 2004 17:49:55 -0000 1.1.1.7.2.1 @@ -94,7 +94,7 @@ print data[:i].strip() + ": " + data[i+1:].strip() return self.read_header - def answer(self, (responsecode, responsestring, headers, data)): + def answer(self, (responsecode, responsestring, headers, data, username)): if self.closed: return if self.encoding == 'gzip': @@ -120,7 +120,6 @@ ident = '-' else: ident = self.encoding - username = '-' referer = self.headers.get('referer','-') useragent = self.headers.get('user-agent','-') year, month, day, hour, minute, second, a, b, c = time.localtime(time.time()) --- BitTorrent/track.py 13 Apr 2004 02:22:07 -0000 1.1.1.11 +++ BitTorrent/track.py 3 Nov 2004 15:59:53 -0000 1.1.1.11.2.13 @@ -150,6 +150,37 @@ self.state['peers'] = tempstate self.downloads = self.state.setdefault('peers', {}) self.completed = self.state.setdefault('completed', {}) + + class mydict(dict) : + def __init__(self,default=None) : + dict.__init__(self) + self.default=default + + def __getitem__(self,clave) : + try : + return dict.__getitem__(self,clave) + except KeyError: + return self.default + + self.ips_autentificadas=mydict(default=(0,'-')) + self.usuarios_autentificados=mydict(default=None) + self.ips_autentificadas_timeout=0 + + import sys + f=open("z-usuarios-ip-fija.txt") + for i in f : + i,j=i.split() + self.ips_autentificadas[i]=(sys.maxint,j) + self.usuarios_autentificados[j]=i + f.close() + + self.usuarios={} + f=open("z-usuarios-claves.txt") + for i in f : + i,j=i.split() + self.usuarios[i]=j + f.close() + statefiletemplate(self.state) for x, dl in self.downloads.items(): self.times[x] = {} @@ -190,6 +221,9 @@ self.uq_broken = 0 self.keep_dead = config['keep_dead'] + def get_usuario(self,connection) : + return self.ips_autentificadas[connection.get_ip()][1] + def get(self, connection, path, headers): try: (scheme, netloc, path, pars, query, fragment) = urlparse(path) @@ -204,7 +238,29 @@ params[unquote(s[:i])] = unquote(s[i+1:]) except ValueError, e: return (400, 'Bad Request', {'Content-Type': 'text/plain'}, - 'you sent me garbage - ' + str(e)) + 'you sent me garbage - ' + str(e),self.get_usuario(connection)) + + t=time() + + if self.ips_autentificadas_timeout\n' \ @@ -287,7 +343,7 @@ s.write('

not tracking any files yet...

\n') s.write('\n' \ '\n') - return (200, 'OK', {'Content-Type': 'text/html; charset=iso-8859-1'}, s.getvalue()) + return (200, 'OK', {'Content-Type': 'text/html; charset=iso-8859-1'}, s.getvalue(),self.get_usuario(connection)) elif path == 'scrape': fs = {} names = [] @@ -307,16 +363,59 @@ if (self.allowed is not None) and self.allowed.has_key(name) and self.show_names: fs[name]['name'] = self.allowed[name]['name'] r = {'files': fs} - return (200, 'OK', {'Content-Type': 'text/plain'}, bencode(r)) + return (200, 'OK', {'Content-Type': 'text/plain'}, bencode(r),self.get_usuario(connection)) elif (path == 'file') and (self.allow_get == 1) and params.has_key('info_hash') and self.allowed.has_key(params['info_hash']): hash = params['info_hash'] fname = self.allowed[hash]['file'] fpath = self.allowed[hash]['path'] - return (200, 'OK', {'Content-Type': 'application/x-bittorrent', 'Content-Disposition': 'attachment; filename=' + fname}, open(fpath, 'rb').read()) + return (200, 'OK', {'Content-Type': 'application/x-bittorrent', 'Content-Disposition': 'attachment; filename=' + fname}, open(fpath, 'rb').read(),self.get_usuario(connection)) elif path == 'favicon.ico' and self.favicon != None: - return (200, 'OK', {'Content-Type' : 'image/x-icon'}, self.favicon) + return (200, 'OK', {'Content-Type' : 'image/x-icon'}, self.favicon,self.get_usuario(connection)) + elif path == 'autentificacion' : + usuario=unquote(params.get('usuario')) + autentificador=params.get('autentificador','').lower() + if (not usuario) or (not autentificador) : + return (403, 'Forbidden', + {'Content-Type': 'text/plain', "x-jcea-autentificacion-tiempo": "1800", 'Pragma': 'no-cache'}, + 'Peticion Incorrecta\n',self.get_usuario(connection)) + autentificador2=None + import hmac + import sha + t=int(time()) + t2=t/300 + clave=self.usuarios.get(usuario) + if clave : + for i in [t2,t2-1,t2+1] : + autentificador2=hmac.new(clave,str(i),sha).hexdigest().lower() + if autentificador2==autentificador : + break + else : + autentificador2=None + + if autentificador2 : + ip_vieja_usuario=self.usuarios_autentificados[usuario] + ip_nueva_usuario=connection.get_ip() + if ip_vieja_usuario and (ip_vieja_usuario!=ip_nueva_usuario) : + return (403, 'Forbidden', + {'Content-Type': 'text/plain', "x-jcea-autentificacion-tiempo": "900", 'Pragma': 'no-cache'}, + 'Un mismo usuario aparece en varias IPs\n',self.get_usuario(connection)) + usuario_viejo_ip=self.get_usuario(connection) + if (usuario_viejo_ip!='-') and (usuario_viejo_ip!=usuario) : + return (403, 'Forbidden', + {'Content-Type': 'text/plain', "x-jcea-autentificacion-tiempo": "900", 'Pragma': 'no-cache'}, + 'Una IP solo puede tener un usuario\n',self.get_usuario(connection)) + + self.usuarios_autentificados[usuario]=ip_nueva_usuario + self.ips_autentificadas[ip_nueva_usuario]=(t+300,usuario) + self.ips_autentificadas_timeout=min(self.ips_autentificadas_timeout,60+t) + return (200, 'OK', + {'Content-Type': 'text/plain', "x-jcea-autentificacion-tiempo": "60", 'Pragma': 'no-cache'}, + 'OK\n',usuario) + return (403, 'Forbidden', + {'Content-Type': 'text/plain', "x-jcea-autentificacion-tiempo": "900", 'Pragma': 'no-cache'}, + 'Autentificacion Incorrecta, por usuario/clave invalida o por reloj desajustado en su maquina\n',self.get_usuario(connection)) if path != 'announce': - return (404, 'Not Found', {'Content-Type': 'text/plain', 'Pragma': 'no-cache'}, alas) + return (404, 'Not Found', {'Content-Type': 'text/plain', 'Pragma': 'no-cache'}, alas,self.get_usuario(connection)) try: if not params.has_key('info_hash'): raise ValueError, 'no info hash' @@ -326,7 +425,7 @@ if self.allowed != None: if not self.allowed.has_key(infohash): return (200, 'OK', {'Content-Type': 'text/plain', 'Pragma': 'no-cache'}, bencode({'failure reason': - 'Requested download is not authorized for use with this tracker.'})) + 'Requested download is not authorized for use with this tracker.'}),self.get_usuario(connection)) ip = connection.get_ip() ip_override = 0 if params.has_key('ip') and is_valid_ipv4(params['ip']) and ( @@ -346,7 +445,7 @@ rsize = min(long(params['numwant']), self.max_give) except ValueError, e: return (400, 'Bad Request', {'Content-Type': 'text/plain'}, - 'you sent me garbage - ' + str(e)) + 'you sent me garbage - ' + str(e),self.get_usuario(connection)) peers = self.downloads.setdefault(infohash, {}) self.completed.setdefault(infohash, 0) ts = self.times.setdefault(infohash, {}) @@ -356,7 +455,7 @@ if myinfo.has_key('key'): if params.get('key') != myinfo['key']: return (200, 'OK', {'Content-Type': 'text/plain', 'Pragma': 'no-cache'}, - bencode({'failure reason': 'key did not match key supplied earlier'})) + bencode({'failure reason': 'key did not match key supplied earlier'}),self.get_usuario(connection)) confirm = 1 elif myinfo['ip'] == ip: confirm = 1 @@ -385,8 +484,18 @@ 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: - NatCheck(self.connectback_result, infohash, myid, ip, port, self.rawserver) + if to_nat : + if True : + if to_nat < self.natcheck : + NatCheck(self.connectback_result, infohash, myid, ip, port, self.rawserver) + #return (200, 'OK', {'Content-Type': 'text/plain', 'Pragma': 'no-cache'}, bencode({'failure reason': + # 'Verificando puerto abierto...'}),self.get_usuario(connection)) + return (200, 'OK', {'Content-Type': 'text/plain', 'Pragma': 'no-cache'}, + bencode({'peers':[],"interval":60}), + self.get_usuario(connection)) + else : + return (200, 'OK', {'Content-Type': 'text/plain', 'Pragma': 'no-cache'}, bencode({'failure reason': + 'ACCESO DENEGADO POR PUERTO CERRADO. !!ABRA PUERTOS Y REINICIE SU CLIENTE BITTORRENT!!'}),self.get_usuario(connection)) else: peers[myid]['nat'] = 0 elif confirm: @@ -421,7 +530,7 @@ del self.cache2.get(infohash, [])[:] data['peers'] = cache[-rsize:] del cache[-rsize:] - connection.answer((200, 'OK', {'Content-Type': 'text/plain', 'Pragma': 'no-cache'}, bencode(data))) + connection.answer((200, 'OK', {'Content-Type': 'text/plain', 'Pragma': 'no-cache'}, bencode(data),self.get_usuario(connection))) def connectback_result(self, result, downloadid, peerid, ip, port): record = self.downloads.get(downloadid, {}).get(peerid)