<s> from BitTornado . parseargs import parseargs , formatDefinitions <EOL> from BitTornado . RawServer import RawServer , autodetect_socket_style <EOL> from BitTornado . HTTPHandler import HTTPHandler , months <EOL> from BitTornado . parsedir import parsedir <EOL> from NatCheck import NatCheck <EOL> from T2T import T2TList <EOL> from BitTornado . subnetparse import IP_List , ipv6_to_ipv4 , to_ipv4 , is_valid_ip , is_ipv4 <EOL> from BitTornado . iprangeparse import IP_List as IP_Range_List <EOL> from BitTornado . torrentlistparse import parsetorrentlist <EOL> from threading import Event , Thread <EOL> from BitTornado . bencode import bencode , bdecode , Bencached <EOL> from BitTornado . zurllib import urlopen <EOL> from urllib import quote , unquote <EOL> from Filter import Filter <EOL> from urlparse import urlparse <EOL> from os . path import exists <EOL> from cStringIO import StringIO <EOL> from traceback import print_exc <EOL> from time import time , gmtime , strftime , localtime <EOL> from BitTornado . clock import clock <EOL> from random import shuffle , seed <EOL> from types import StringType , IntType , LongType , DictType <EOL> from binascii import b2a_hex <EOL> import sys , os <EOL> import signal <EOL> import re <EOL> from BitTornado . __init__ import version , createPeerID <EOL> try : <EOL> True <EOL> except : <EOL> True = <NUM_LIT:1> <EOL> False = <NUM_LIT:0> <EOL> defaults = [ <EOL> ( '<STR_LIT:port>' , <NUM_LIT> , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , None , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:0> , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , autodetect_socket_style ( ) , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:15> , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:5> * <NUM_LIT> , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> * <NUM_LIT> , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:30> * <NUM_LIT> , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:50> , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:5> , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:3> , <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:0> , <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:0> , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:0> , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:2> * <NUM_LIT> , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:20> , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:0>' , '<STR_LIT>' + <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:0> , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> , '<STR_LIT>' + <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:1> , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:1> , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' + <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , "<STR_LIT>" + <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:2> , "<STR_LIT>" + <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:0> , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:0> , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> ] <EOL> def statefiletemplate ( x ) : <EOL> if type ( x ) != DictType : <EOL> raise ValueError <EOL> for cname , cinfo in x . items ( ) : <EOL> if cname == '<STR_LIT>' : <EOL> for y in cinfo . values ( ) : <EOL> if type ( y ) != DictType : <EOL> raise ValueError <EOL> for id , info in y . items ( ) : <EOL> if ( len ( id ) != <NUM_LIT:20> ) : <EOL> raise ValueError <EOL> if type ( info ) != DictType : <EOL> raise ValueError <EOL> if type ( info . get ( '<STR_LIT>' , '<STR_LIT>' ) ) != StringType : <EOL> raise ValueError <EOL> port = info . get ( '<STR_LIT:port>' ) <EOL> if type ( port ) not in ( IntType , LongType ) or port < <NUM_LIT:0> : <EOL> raise ValueError <EOL> left = info . get ( '<STR_LIT:left>' ) <EOL> if type ( left ) not in ( IntType , LongType ) or left < <NUM_LIT:0> : <EOL> raise ValueError <EOL> elif cname == '<STR_LIT>' : <EOL> if ( type ( cinfo ) != DictType ) : <EOL> raise ValueError <EOL> for y in cinfo . values ( ) : <EOL> if type ( y ) not in ( IntType , LongType ) : <EOL> raise ValueError <EOL> elif cname == '<STR_LIT>' : <EOL> if ( type ( cinfo ) != DictType ) : <EOL> raise ValueError <EOL> if x . has_key ( '<STR_LIT>' ) : <EOL> adlist = [ z [ <NUM_LIT:1> ] for z in x [ '<STR_LIT>' ] . values ( ) ] <EOL> for y in cinfo . keys ( ) : <EOL> if not y in adlist : <EOL> raise ValueError <EOL> elif cname == '<STR_LIT>' : <EOL> if ( type ( cinfo ) != DictType ) : <EOL> raise ValueError <EOL> dirkeys = { } <EOL> for y in cinfo . values ( ) : <EOL> if not y [ <NUM_LIT:1> ] : <EOL> continue <EOL> if not x [ '<STR_LIT>' ] . has_key ( y [ <NUM_LIT:1> ] ) : <EOL> raise ValueError <EOL> if dirkeys . has_key ( y [ <NUM_LIT:1> ] ) : <EOL> raise ValueError <EOL> dirkeys [ y [ <NUM_LIT:1> ] ] = <NUM_LIT:1> <EOL> alas = '<STR_LIT>' <EOL> local_IPs = IP_List ( ) <EOL> local_IPs . set_intranet_addresses ( ) <EOL> def isotime ( secs = None ) : <EOL> if secs == None : <EOL> secs = time ( ) <EOL> return strftime ( '<STR_LIT>' , gmtime ( secs ) ) <EOL> http_via_filter = re . compile ( '<STR_LIT>' ) <EOL> def _get_forwarded_ip ( headers ) : <EOL> if headers . has_key ( '<STR_LIT>' ) : <EOL> header = headers [ '<STR_LIT>' ] <EOL> try : <EOL> x , y = header . split ( '<STR_LIT:U+002C>' ) <EOL> except : <EOL> return header <EOL> if not local_IPs . includes ( x ) : <EOL> return x <EOL> return y <EOL> if headers . has_key ( '<STR_LIT>' ) : <EOL> return headers [ '<STR_LIT>' ] <EOL> if headers . has_key ( '<STR_LIT>' ) : <EOL> x = http_via_filter . search ( headers [ '<STR_LIT>' ] ) <EOL> try : <EOL> return x . group ( <NUM_LIT:1> ) <EOL> except : <EOL> pass <EOL> if headers . has_key ( '<STR_LIT>' ) : <EOL> return headers [ '<STR_LIT>' ] <EOL> return None <EOL> def get_forwarded_ip ( headers ) : <EOL> x = _get_forwarded_ip ( headers ) <EOL> if not is_valid_ip ( x ) or local_IPs . includes ( x ) : <EOL> return None <EOL> return x <EOL> def compact_peer_info ( ip , port ) : <EOL> try : <EOL> s = ( '<STR_LIT>' . join ( [ chr ( int ( i ) ) for i in ip . split ( '<STR_LIT:.>' ) ] ) <EOL> + chr ( ( port & <NUM_LIT> ) >> <NUM_LIT:8> ) + chr ( port & <NUM_LIT> ) ) <EOL> if len ( s ) != <NUM_LIT:6> : <EOL> raise ValueError <EOL> except : <EOL> s = '<STR_LIT>' <EOL> return s <EOL> class Tracker : <EOL> def __init__ ( self , config , rawserver ) : <EOL> self . config = config <EOL> self . response_size = config [ '<STR_LIT>' ] <EOL> self . dfile = config [ '<STR_LIT>' ] <EOL> self . natcheck = config [ '<STR_LIT>' ] <EOL> favicon = config [ '<STR_LIT>' ] <EOL> self . parse_dir_interval = config [ '<STR_LIT>' ] <EOL> self . favicon = None <EOL> if favicon : <EOL> try : <EOL> h = open ( favicon , '<STR_LIT:r>' ) <EOL> self . favicon = h . read ( ) <EOL> h . close ( ) <EOL> except : <EOL> print "<STR_LIT>" % favicon <EOL> self . rawserver = rawserver <EOL> self . cached = { } <EOL> self . cached_t = { } <EOL> self . times = { } <EOL> self . state = { } <EOL> self . seedcount = { } <EOL> self . allowed_IPs = None <EOL> self . banned_IPs = None <EOL> if config [ '<STR_LIT>' ] or config [ '<STR_LIT>' ] : <EOL> self . allowed_ip_mtime = <NUM_LIT:0> <EOL> self . banned_ip_mtime = <NUM_LIT:0> <EOL> self . read_ip_lists ( ) <EOL> self . only_local_override_ip = config [ '<STR_LIT>' ] <EOL> if self . only_local_override_ip == <NUM_LIT:2> : <EOL> self . only_local_override_ip = not config [ '<STR_LIT>' ] <EOL> if exists ( self . dfile ) : <EOL> try : <EOL> h = open ( self . dfile , '<STR_LIT:rb>' ) <EOL> ds = h . read ( ) <EOL> h . close ( ) <EOL> tempstate = bdecode ( ds ) <EOL> if not tempstate . has_key ( '<STR_LIT>' ) : <EOL> tempstate = { '<STR_LIT>' : tempstate } <EOL> statefiletemplate ( tempstate ) <EOL> self . state = tempstate <EOL> except : <EOL> print '<STR_LIT>' + self . dfile + '<STR_LIT>' <EOL> self . downloads = self . state . setdefault ( '<STR_LIT>' , { } ) <EOL> self . completed = self . state . setdefault ( '<STR_LIT>' , { } ) <EOL> self . becache = { } <EOL> for infohash , ds in self . downloads . items ( ) : <EOL> self . seedcount [ infohash ] = <NUM_LIT:0> <EOL> for x , y in ds . items ( ) : <EOL> ip = y [ '<STR_LIT>' ] <EOL> if ( ( self . allowed_IPs and not self . allowed_IPs . includes ( ip ) ) <EOL> or ( self . banned_IPs and self . banned_IPs . includes ( ip ) ) ) : <EOL> del ds [ x ] <EOL> continue <EOL> if not y [ '<STR_LIT:left>' ] : <EOL> self . seedcount [ infohash ] += <NUM_LIT:1> <EOL> if y . get ( '<STR_LIT>' , - <NUM_LIT:1> ) : <EOL> continue <EOL> gip = y . get ( '<STR_LIT>' ) <EOL> if is_valid_ip ( gip ) and ( <EOL> not self . only_local_override_ip or local_IPs . includes ( ip ) ) : <EOL> ip = gip <EOL> self . natcheckOK ( infohash , x , ip , y [ '<STR_LIT:port>' ] , y [ '<STR_LIT:left>' ] ) <EOL> for x in self . downloads . keys ( ) : <EOL> self . times [ x ] = { } <EOL> for y in self . downloads [ x ] . keys ( ) : <EOL> self . times [ x ] [ y ] = <NUM_LIT:0> <EOL> self . trackerid = createPeerID ( '<STR_LIT>' ) <EOL> seed ( self . trackerid ) <EOL> self . reannounce_interval = config [ '<STR_LIT>' ] <EOL> self . save_dfile_interval = config [ '<STR_LIT>' ] <EOL> self . show_names = config [ '<STR_LIT>' ] <EOL> rawserver . add_task ( self . save_state , self . save_dfile_interval ) <EOL> self . prevtime = clock ( ) <EOL> self . timeout_downloaders_interval = config [ '<STR_LIT>' ] <EOL> rawserver . add_task ( self . expire_downloaders , self . timeout_downloaders_interval ) <EOL> self . logfile = None <EOL> self . log = None <EOL> if ( config [ '<STR_LIT>' ] ) and ( config [ '<STR_LIT>' ] != '<STR_LIT:->' ) : <EOL> try : <EOL> self . logfile = config [ '<STR_LIT>' ] <EOL> self . log = open ( self . logfile , '<STR_LIT:a>' ) <EOL> sys . stdout = self . log <EOL> print "<STR_LIT>" , isotime ( ) <EOL> except : <EOL> print "<STR_LIT>" , sys . exc_info ( ) [ <NUM_LIT:0> ] <EOL> if config [ '<STR_LIT>' ] : <EOL> def huphandler ( signum , frame , self = self ) : <EOL> try : <EOL> self . log . close ( ) <EOL> self . log = open ( self . logfile , '<STR_LIT:a>' ) <EOL> sys . stdout = self . log <EOL> print "<STR_LIT>" , isotime ( ) <EOL> except : <EOL> print "<STR_LIT>" <EOL> signal . signal ( signal . SIGHUP , huphandler ) <EOL> self . allow_get = config [ '<STR_LIT>' ] <EOL> self . t2tlist = T2TList ( config [ '<STR_LIT>' ] , self . trackerid , <EOL> config [ '<STR_LIT>' ] , <EOL> config [ '<STR_LIT>' ] , config [ '<STR_LIT>' ] , <EOL> self . rawserver ) <EOL> if config [ '<STR_LIT>' ] : <EOL> if config [ '<STR_LIT>' ] : <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> config [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . allowed = self . state . setdefault ( '<STR_LIT>' , { } ) <EOL> self . allowed_list_mtime = <NUM_LIT:0> <EOL> self . parse_allowed ( ) <EOL> self . remove_from_state ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if config [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> config [ '<STR_LIT>' ] = '<STR_LIT:none>' <EOL> config [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> elif config [ '<STR_LIT>' ] : <EOL> self . allowed = self . state . setdefault ( '<STR_LIT>' , { } ) <EOL> self . allowed_dir_files = self . state . setdefault ( '<STR_LIT>' , { } ) <EOL> self . allowed_dir_blocked = { } <EOL> self . parse_allowed ( ) <EOL> self . remove_from_state ( '<STR_LIT>' ) <EOL> else : <EOL> self . allowed = None <EOL> self . remove_from_state ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if config [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> config [ '<STR_LIT>' ] = '<STR_LIT:none>' <EOL> config [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> self . uq_broken = unquote ( '<STR_LIT:+>' ) != '<STR_LIT:U+0020>' <EOL> self . keep_dead = config [ '<STR_LIT>' ] <EOL> self . Filter = Filter ( rawserver . add_task ) <EOL> aggregator = config [ '<STR_LIT>' ] <EOL> if aggregator == '<STR_LIT:0>' : <EOL> self . is_aggregator = False <EOL> self . aggregator_key = None <EOL> else : <EOL> self . is_aggregator = True <EOL> if aggregator == '<STR_LIT:1>' : <EOL> self . aggregator_key = None <EOL> else : <EOL> self . aggregator_key = aggregator <EOL> self . natcheck = False <EOL> send = config [ '<STR_LIT>' ] <EOL> if not send : <EOL> self . aggregate_forward = None <EOL> else : <EOL> try : <EOL> self . aggregate_forward , self . aggregate_password = send . split ( '<STR_LIT:U+002C>' ) <EOL> except : <EOL> self . aggregate_forward = send <EOL> self . aggregate_password = None <EOL> self . cachetime = <NUM_LIT:0> <EOL> self . cachetimeupdate ( ) <EOL> def cachetimeupdate ( self ) : <EOL> self . cachetime += <NUM_LIT:1> <EOL> self . rawserver . add_task ( self . cachetimeupdate , <NUM_LIT:1> ) <EOL> def aggregate_senddata ( self , query ) : <EOL> url = self . aggregate_forward + '<STR_LIT:?>' + query <EOL> if self . aggregate_password is not None : <EOL> url += '<STR_LIT>' + self . aggregate_password <EOL> rq = Thread ( target = self . _aggregate_senddata , args = [ url ] ) <EOL> rq . setDaemon ( False ) <EOL> rq . start ( ) <EOL> def _aggregate_senddata ( self , url ) : <EOL> try : <EOL> h = urlopen ( url ) <EOL> h . read ( ) <EOL> h . close ( ) <EOL> except : <EOL> return <EOL> def get_infopage ( self ) : <EOL> try : <EOL> if not self . config [ '<STR_LIT>' ] : <EOL> return ( <NUM_LIT> , '<STR_LIT>' , { '<STR_LIT:Content-Type>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , alas ) <EOL> red = self . config [ '<STR_LIT>' ] <EOL> if red : <EOL> return ( <NUM_LIT> , '<STR_LIT>' , { '<STR_LIT:Content-Type>' : '<STR_LIT>' , '<STR_LIT>' : red } , <EOL> '<STR_LIT>' + red + '<STR_LIT>' ) <EOL> s = StringIO ( ) <EOL> s . write ( '<STR_LIT>' '<STR_LIT>' ) <EOL> if self . favicon is not None : <EOL> s . write ( '<STR_LIT>' ) <EOL> s . write ( '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' <EOL> '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' % ( version , isotime ( ) ) ) <EOL> if self . config [ '<STR_LIT>' ] : <EOL> if self . show_names : <EOL> names = [ ( self . allowed [ hash ] [ '<STR_LIT:name>' ] , hash ) <EOL> for hash in self . allowed . keys ( ) ] <EOL> else : <EOL> names = [ ( None , hash ) <EOL> for hash in self . allowed . keys ( ) ] <EOL> else : <EOL> names = [ ( None , hash ) for hash in self . downloads . keys ( ) ] <EOL> if not names : <EOL> s . write ( '<STR_LIT>' ) <EOL> else : <EOL> names . sort ( ) <EOL> tn = <NUM_LIT:0> <EOL> tc = <NUM_LIT:0> <EOL> td = <NUM_LIT:0> <EOL> tt = <NUM_LIT:0> <EOL> ts = <NUM_LIT:0> <EOL> nf = <NUM_LIT:0> <EOL> if self . config [ '<STR_LIT>' ] and self . show_names : <EOL> s . write ( '<STR_LIT>' '<STR_LIT>' ) <EOL> else : <EOL> s . write ( '<STR_LIT>' '<STR_LIT>' ) <EOL> for name , hash in names : <EOL> l = self . downloads [ hash ] <EOL> n = self . completed . get ( hash , <NUM_LIT:0> ) <EOL> tn = tn + n <EOL> c = self . seedcount [ hash ] <EOL> tc = tc + c <EOL> d = len ( l ) - c <EOL> td = td + d <EOL> if self . config [ '<STR_LIT>' ] and self . show_names : <EOL> if self . allowed . has_key ( hash ) : <EOL> nf = nf + <NUM_LIT:1> <EOL> sz = self . allowed [ hash ] [ '<STR_LIT>' ] <EOL> ts = ts + sz <EOL> szt = sz * n <EOL> tt = tt + szt <EOL> if self . allow_get == <NUM_LIT:1> : <EOL> linkname = '<STR_LIT>' + quote ( hash ) + '<STR_LIT>' + name + '<STR_LIT>' <EOL> else : <EOL> linkname = name <EOL> s . write ( '<STR_LIT>' % ( b2a_hex ( hash ) , linkname , size_format ( sz ) , c , d , n , size_format ( szt ) ) ) <EOL> else : <EOL> s . write ( '<STR_LIT>' % ( b2a_hex ( hash ) , c , d , n ) ) <EOL> ttn = <NUM_LIT:0> <EOL> for i in self . completed . values ( ) : <EOL> ttn = ttn + i <EOL> if self . config [ '<STR_LIT>' ] and self . show_names : <EOL> s . write ( '<STR_LIT>' <EOL> % ( nf , size_format ( ts ) , tc , td , tn , ttn , size_format ( tt ) ) ) <EOL> else : <EOL> s . write ( '<STR_LIT>' <EOL> % ( nf , tc , td , tn , ttn ) ) <EOL> s . write ( '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' ) <EOL> s . write ( '<STR_LIT>' '<STR_LIT>' ) <EOL> return ( <NUM_LIT:200> , '<STR_LIT:OK>' , { '<STR_LIT:Content-Type>' : '<STR_LIT>' } , s . getvalue ( ) ) <EOL> except : <EOL> print_exc ( ) <EOL> return ( <NUM_LIT> , '<STR_LIT>' , { '<STR_LIT:Content-Type>' : '<STR_LIT>' } , '<STR_LIT>' ) <EOL> def scrapedata ( self , hash , return_name = True ) : <EOL> l = self . downloads [ hash ] <EOL> n = self . completed . get ( hash , <NUM_LIT:0> ) <EOL> c = self . seedcount [ hash ] <EOL> d = len ( l ) - c <EOL> f = { '<STR_LIT>' : c , '<STR_LIT>' : d , '<STR_LIT>' : n } <EOL> if return_name and self . show_names and self . config [ '<STR_LIT>' ] : <EOL> f [ '<STR_LIT:name>' ] = self . allowed [ hash ] [ '<STR_LIT:name>' ] <EOL> return ( f ) <EOL> def get_scrape ( self , paramslist ) : <EOL> fs = { } <EOL> if paramslist . has_key ( '<STR_LIT>' ) : <EOL> if self . config [ '<STR_LIT>' ] not in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> return ( <NUM_LIT> , '<STR_LIT>' , { '<STR_LIT:Content-Type>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> bencode ( { '<STR_LIT>' : <EOL> '<STR_LIT>' } ) ) <EOL> for hash in paramslist [ '<STR_LIT>' ] : <EOL> if self . allowed is not None : <EOL> if self . allowed . has_key ( hash ) : <EOL> fs [ hash ] = self . scrapedata ( hash ) <EOL> else : <EOL> if self . downloads . has_key ( hash ) : <EOL> fs [ hash ] = self . scrapedata ( hash ) <EOL> else : <EOL> if self . config [ '<STR_LIT>' ] != '<STR_LIT>' : <EOL> return ( <NUM_LIT> , '<STR_LIT>' , { '<STR_LIT:Content-Type>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> bencode ( { '<STR_LIT>' : <EOL> '<STR_LIT>' } ) ) <EOL> if self . allowed is not None : <EOL> keys = self . allowed . keys ( ) <EOL> else : <EOL> keys = self . downloads . keys ( ) <EOL> for hash in keys : <EOL> fs [ hash ] = self . scrapedata ( hash ) <EOL> return ( <NUM_LIT:200> , '<STR_LIT:OK>' , { '<STR_LIT:Content-Type>' : '<STR_LIT>' } , bencode ( { '<STR_LIT>' : fs } ) ) <EOL> def get_file ( self , hash ) : <EOL> if not self . allow_get : <EOL> return ( <NUM_LIT> , '<STR_LIT>' , { '<STR_LIT:Content-Type>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' ) <EOL> if not self . allowed . has_key ( hash ) : <EOL> return ( <NUM_LIT> , '<STR_LIT>' , { '<STR_LIT:Content-Type>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , alas ) <EOL> fname = self . allowed [ hash ] [ '<STR_LIT:file>' ] <EOL> fpath = self . allowed [ hash ] [ '<STR_LIT:path>' ] <EOL> return ( <NUM_LIT:200> , '<STR_LIT:OK>' , { '<STR_LIT:Content-Type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' + fname } , <EOL> open ( fpath , '<STR_LIT:rb>' ) . read ( ) ) <EOL> def check_allowed ( self , infohash , paramslist ) : <EOL> if ( self . aggregator_key is not None <EOL> and not ( paramslist . has_key ( '<STR_LIT:password>' ) <EOL> and paramslist [ '<STR_LIT:password>' ] [ <NUM_LIT:0> ] == self . aggregator_key ) ) : <EOL> return ( <NUM_LIT:200> , '<STR_LIT>' , { '<STR_LIT:Content-Type>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> bencode ( { '<STR_LIT>' : <EOL> '<STR_LIT>' } ) ) <EOL> if self . allowed is not None : <EOL> if not self . allowed . has_key ( infohash ) : <EOL> return ( <NUM_LIT:200> , '<STR_LIT>' , { '<STR_LIT:Content-Type>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> bencode ( { '<STR_LIT>' : <EOL> '<STR_LIT>' } ) ) <EOL> if self . config [ '<STR_LIT>' ] : <EOL> if self . allowed [ infohash ] . has_key ( '<STR_LIT>' ) : <EOL> return ( <NUM_LIT:200> , '<STR_LIT>' , { '<STR_LIT:Content-Type>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> bencode ( { '<STR_LIT>' : self . allowed [ infohash ] [ '<STR_LIT>' ] } ) ) <EOL> if paramslist . has_key ( '<STR_LIT>' ) : <EOL> if ( self . config [ '<STR_LIT>' ] == '<STR_LIT:none>' or <EOL> paramslist [ '<STR_LIT>' ] [ <NUM_LIT:0> ] == self . trackerid ) : <EOL> return ( <NUM_LIT:200> , '<STR_LIT>' , { '<STR_LIT:Content-Type>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> bencode ( { '<STR_LIT>' : '<STR_LIT>' } ) ) <EOL> if ( self . config [ '<STR_LIT>' ] == '<STR_LIT>' <EOL> and not self . allowed [ infohash ] . has_key ( '<STR_LIT>' ) ) : <EOL> return ( <NUM_LIT:200> , '<STR_LIT>' , { '<STR_LIT:Content-Type>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> bencode ( { '<STR_LIT>' : <EOL> '<STR_LIT>' } ) ) <EOL> return None <EOL> def add_data ( self , infohash , event , ip , paramslist ) : <EOL> peers = self . downloads . setdefault ( infohash , { } ) <EOL> ts = self . times . setdefault ( infohash , { } ) <EOL> self . completed . setdefault ( infohash , <NUM_LIT:0> ) <EOL> self . seedcount . setdefault ( infohash , <NUM_LIT:0> ) <EOL> def params ( key , default = None , l = paramslist ) : <EOL> if l . has_key ( key ) : <EOL> return l [ key ] [ <NUM_LIT:0> ] <EOL> return default <EOL> myid = params ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if len ( myid ) != <NUM_LIT:20> : <EOL> raise ValueError , '<STR_LIT>' <EOL> if event not in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , None ] : <EOL> raise ValueError , '<STR_LIT>' <EOL> port = long ( params ( '<STR_LIT:port>' , '<STR_LIT>' ) ) <EOL> if port < <NUM_LIT:0> or port > <NUM_LIT> : <EOL> raise ValueError , '<STR_LIT>' <EOL> left = long ( params ( '<STR_LIT:left>' , '<STR_LIT>' ) ) <EOL> if left < <NUM_LIT:0> : <EOL> raise ValueError , '<STR_LIT>' <EOL> uploaded = long ( params ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> downloaded = long ( params ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> peer = peers . get ( myid ) <EOL> islocal = local_IPs . includes ( ip ) <EOL> mykey = params ( '<STR_LIT:key>' ) <EOL> if peer : <EOL> auth = peer . get ( '<STR_LIT:key>' , - <NUM_LIT:1> ) == mykey or peer . get ( '<STR_LIT>' ) == ip <EOL> gip = params ( '<STR_LIT>' ) <EOL> if is_valid_ip ( gip ) and ( islocal or not self . only_local_override_ip ) : <EOL> ip1 = gip <EOL> else : <EOL> ip1 = ip <EOL> if params ( '<STR_LIT>' ) is not None : <EOL> rsize = min ( int ( params ( '<STR_LIT>' ) ) , self . response_size ) <EOL> else : <EOL> rsize = self . response_size <EOL> if event == '<STR_LIT>' : <EOL> if peer : <EOL> if auth : <EOL> self . delete_peer ( infohash , myid ) <EOL> elif not peer : <EOL> ts [ myid ] = clock ( ) <EOL> peer = { '<STR_LIT>' : ip , '<STR_LIT:port>' : port , '<STR_LIT:left>' : left } <EOL> if mykey : <EOL> peer [ '<STR_LIT:key>' ] = mykey <EOL> if gip : <EOL> peer [ '<STR_LIT>' ] = gip <EOL> if port : <EOL> if not self . natcheck or islocal : <EOL> peer [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> self . natcheckOK ( infohash , myid , ip1 , port , left ) <EOL> else : <EOL> NatCheck ( self . connectback_result , infohash , myid , ip1 , port , self . rawserver ) <EOL> else : <EOL> peer [ '<STR_LIT>' ] = <NUM_LIT:2> ** <NUM_LIT:30> <EOL> if event == '<STR_LIT>' : <EOL> self . completed [ infohash ] += <NUM_LIT:1> <EOL> if not left : <EOL> self . seedcount [ infohash ] += <NUM_LIT:1> <EOL> peers [ myid ] = peer <EOL> else : <EOL> if not auth : <EOL> return rsize <EOL> ts [ myid ] = clock ( ) <EOL> if not left and peer [ '<STR_LIT:left>' ] : <EOL> self . completed [ infohash ] += <NUM_LIT:1> <EOL> self . seedcount [ infohash ] += <NUM_LIT:1> <EOL> if not peer . get ( '<STR_LIT>' , - <NUM_LIT:1> ) : <EOL> for bc in self . becache [ infohash ] : <EOL> bc [ <NUM_LIT:1> ] [ myid ] = bc [ <NUM_LIT:0> ] [ myid ] <EOL> del bc [ <NUM_LIT:0> ] [ myid ] <EOL> if peer [ '<STR_LIT:left>' ] : <EOL> peer [ '<STR_LIT:left>' ] = left <EOL> if port : <EOL> recheck = False <EOL> if ip != peer [ '<STR_LIT>' ] : <EOL> peer [ '<STR_LIT>' ] = ip <EOL> recheck = True <EOL> if gip != peer . get ( '<STR_LIT>' ) : <EOL> if gip : <EOL> peer [ '<STR_LIT>' ] = gip <EOL> elif peer . has_key ( '<STR_LIT>' ) : <EOL> del peer [ '<STR_LIT>' ] <EOL> recheck = True <EOL> natted = peer . get ( '<STR_LIT>' , - <NUM_LIT:1> ) <EOL> if recheck : <EOL> if natted == <NUM_LIT:0> : <EOL> l = self . becache [ infohash ] <EOL> y = not peer [ '<STR_LIT:left>' ] <EOL> for x in l : <EOL> del x [ y ] [ myid ] <EOL> if not self . natcheck or islocal : <EOL> del peer [ '<STR_LIT>' ] <EOL> if natted and natted < self . natcheck : <EOL> recheck = True <EOL> if recheck : <EOL> if not self . natcheck or islocal : <EOL> peer [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> self . natcheckOK ( infohash , myid , ip1 , port , left ) <EOL> else : <EOL> NatCheck ( self . connectback_result , infohash , myid , ip1 , port , self . rawserver ) <EOL> return rsize <EOL> def peerlist ( self , infohash , stopped , tracker , is_seed , return_type , rsize ) : <EOL> data = { } <EOL> seeds = self . seedcount [ infohash ] <EOL> data [ '<STR_LIT>' ] = seeds <EOL> data [ '<STR_LIT>' ] = len ( self . downloads [ infohash ] ) - seeds <EOL> if ( self . config [ '<STR_LIT>' ] <EOL> and self . allowed [ infohash ] . has_key ( '<STR_LIT>' ) ) : <EOL> data [ '<STR_LIT>' ] = self . allowed [ infohash ] [ '<STR_LIT>' ] <EOL> if tracker : <EOL> data [ '<STR_LIT>' ] = self . config [ '<STR_LIT>' ] <EOL> if not rsize : <EOL> return data <EOL> cache = self . cached_t . setdefault ( infohash , None ) <EOL> if ( not cache or len ( cache [ <NUM_LIT:1> ] ) < rsize <EOL> or cache [ <NUM_LIT:0> ] + self . config [ '<STR_LIT>' ] < clock ( ) ) : <EOL> bc = self . becache . setdefault ( infohash , [ [ { } , { } ] , [ { } , { } ] , [ { } , { } ] ] ) <EOL> cache = [ clock ( ) , bc [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] . values ( ) + bc [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] . values ( ) ] <EOL> self . cached_t [ infohash ] = cache <EOL> shuffle ( cache [ <NUM_LIT:1> ] ) <EOL> cache = cache [ <NUM_LIT:1> ] <EOL> data [ '<STR_LIT>' ] = cache [ - rsize : ] <EOL> del cache [ - rsize : ] <EOL> return data <EOL> data [ '<STR_LIT>' ] = self . reannounce_interval <EOL> if stopped or not rsize : <EOL> data [ '<STR_LIT>' ] = [ ] <EOL> return data <EOL> bc = self . becache . setdefault ( infohash , [ [ { } , { } ] , [ { } , { } ] , [ { } , { } ] ] ) <EOL> len_l = len ( bc [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) <EOL> len_s = len ( bc [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ) <EOL> if not ( len_l + len_s ) : <EOL> data [ '<STR_LIT>' ] = [ ] <EOL> return data <EOL> l_get_size = int ( float ( rsize ) * ( len_l ) / ( len_l + len_s ) ) <EOL> cache = self . cached . setdefault ( infohash , [ None , None , None ] ) [ return_type ] <EOL> if cache and ( not cache [ <NUM_LIT:1> ] <EOL> or ( is_seed and len ( cache [ <NUM_LIT:1> ] ) < rsize ) <EOL> or len ( cache [ <NUM_LIT:1> ] ) < l_get_size <EOL> or cache [ <NUM_LIT:0> ] + self . config [ '<STR_LIT>' ] < self . cachetime ) : <EOL> cache = None <EOL> if not cache : <EOL> peers = self . downloads [ infohash ] <EOL> vv = [ [ ] , [ ] , [ ] ] <EOL> for key , ip , port in self . t2tlist . harvest ( infohash ) : <EOL> if not peers . has_key ( key ) : <EOL> vv [ <NUM_LIT:0> ] . append ( { '<STR_LIT>' : ip , '<STR_LIT:port>' : port , '<STR_LIT>' : key } ) <EOL> vv [ <NUM_LIT:1> ] . append ( { '<STR_LIT>' : ip , '<STR_LIT:port>' : port } ) <EOL> vv [ <NUM_LIT:2> ] . append ( compact_peer_info ( ip , port ) ) <EOL> cache = [ self . cachetime , <EOL> bc [ return_type ] [ <NUM_LIT:0> ] . values ( ) + vv [ return_type ] , <EOL> bc [ return_type ] [ <NUM_LIT:1> ] . values ( ) ] <EOL> shuffle ( cache [ <NUM_LIT:1> ] ) <EOL> shuffle ( cache [ <NUM_LIT:2> ] ) <EOL> self . cached [ infohash ] [ return_type ] = cache <EOL> for rr in xrange ( len ( self . cached [ infohash ] ) ) : <EOL> if rr != return_type : <EOL> try : <EOL> self . cached [ infohash ] [ rr ] [ <NUM_LIT:1> ] . extend ( vv [ rr ] ) <EOL> except : <EOL> pass <EOL> if len ( cache [ <NUM_LIT:1> ] ) < l_get_size : <EOL> peerdata = cache [ <NUM_LIT:1> ] <EOL> if not is_seed : <EOL> peerdata . extend ( cache [ <NUM_LIT:2> ] ) <EOL> cache [ <NUM_LIT:1> ] = [ ] <EOL> cache [ <NUM_LIT:2> ] = [ ] <EOL> else : <EOL> if not is_seed : <EOL> peerdata = cache [ <NUM_LIT:2> ] [ l_get_size - rsize : ] <EOL> del cache [ <NUM_LIT:2> ] [ l_get_size - rsize : ] <EOL> rsize -= len ( peerdata ) <EOL> else : <EOL> peerdata = [ ] <EOL> if rsize : <EOL> peerdata . extend ( cache [ <NUM_LIT:1> ] [ - rsize : ] ) <EOL> del cache [ <NUM_LIT:1> ] [ - rsize : ] <EOL> if return_type == <NUM_LIT:2> : <EOL> peerdata = '<STR_LIT>' . join ( peerdata ) <EOL> data [ '<STR_LIT>' ] = peerdata <EOL> return data <EOL> def get ( self , connection , path , headers ) : <EOL> real_ip = connection . get_ip ( ) <EOL> ip = real_ip <EOL> if is_ipv4 ( ip ) : <EOL> ipv4 = True <EOL> else : <EOL> try : <EOL> ip = ipv6_to_ipv4 ( ip ) <EOL> ipv4 = True <EOL> except ValueError : <EOL> ipv4 = False <EOL> if ( ( self . allowed_IPs and not self . allowed_IPs . includes ( ip ) ) <EOL> or ( self . banned_IPs and self . banned_IPs . includes ( ip ) ) ) : <EOL> return ( <NUM_LIT> , '<STR_LIT>' , { '<STR_LIT:Content-Type>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> bencode ( { '<STR_LIT>' : <EOL> '<STR_LIT>' } ) ) <EOL> nip = get_forwarded_ip ( headers ) <EOL> if nip and not self . only_local_override_ip : <EOL> ip = nip <EOL> try : <EOL> ip = to_ipv4 ( ip ) <EOL> ipv4 = True <EOL> except ValueError : <EOL> ipv4 = False <EOL> paramslist = { } <EOL> def params ( key , default = None , l = paramslist ) : <EOL> if l . has_key ( key ) : <EOL> return l [ key ] [ <NUM_LIT:0> ] <EOL> return default <EOL> try : <EOL> ( scheme , netloc , path , pars , query , fragment ) = urlparse ( path ) <EOL> if self . uq_broken == <NUM_LIT:1> : <EOL> path = path . replace ( '<STR_LIT:+>' , '<STR_LIT:U+0020>' ) <EOL> query = query . replace ( '<STR_LIT:+>' , '<STR_LIT:U+0020>' ) <EOL> path = unquote ( path ) [ <NUM_LIT:1> : ] <EOL> for s in query . split ( '<STR_LIT:&>' ) : <EOL> if s : <EOL> i = s . index ( '<STR_LIT:=>' ) <EOL> kw = unquote ( s [ : i ] ) <EOL> paramslist . setdefault ( kw , [ ] ) <EOL> paramslist [ kw ] += [ unquote ( s [ i + <NUM_LIT:1> : ] ) ] <EOL> if path == '<STR_LIT>' or path == '<STR_LIT>' : <EOL> return self . get_infopage ( ) <EOL> if ( path == '<STR_LIT:file>' ) : <EOL> return self . get_file ( params ( '<STR_LIT>' ) ) <EOL> if path == '<STR_LIT>' and self . favicon is not None : <EOL> return ( <NUM_LIT:200> , '<STR_LIT:OK>' , { '<STR_LIT:Content-Type>' : '<STR_LIT>' } , self . favicon ) <EOL> if path == '<STR_LIT>' : <EOL> return self . get_scrape ( paramslist ) <EOL> if path != '<STR_LIT>' : <EOL> return ( <NUM_LIT> , '<STR_LIT>' , { '<STR_LIT:Content-Type>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , alas ) <EOL> filtered = self . Filter . check ( real_ip , paramslist , headers ) <EOL> if filtered : <EOL> return ( <NUM_LIT> , '<STR_LIT>' , { '<STR_LIT:Content-Type>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> bencode ( { '<STR_LIT>' : filtered } ) ) <EOL> infohash = params ( '<STR_LIT>' ) <EOL> if not infohash : <EOL> raise ValueError , '<STR_LIT>' <EOL> notallowed = self . check_allowed ( infohash , paramslist ) <EOL> if notallowed : <EOL> return notallowed <EOL> event = params ( '<STR_LIT>' ) <EOL> rsize = self . add_data ( infohash , event , ip , paramslist ) <EOL> except ValueError , e : <EOL> return ( <NUM_LIT> , '<STR_LIT>' , { '<STR_LIT:Content-Type>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' + str ( e ) ) <EOL> if self . aggregate_forward and not paramslist . has_key ( '<STR_LIT>' ) : <EOL> self . aggregate_senddata ( query ) <EOL> if self . is_aggregator : <EOL> return ( <NUM_LIT:200> , '<STR_LIT:OK>' , { '<STR_LIT:Content-Type>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> bencode ( { '<STR_LIT>' : '<STR_LIT:OK>' } ) ) <EOL> if params ( '<STR_LIT>' ) and ipv4 : <EOL> return_type = <NUM_LIT:2> <EOL> elif params ( '<STR_LIT>' ) : <EOL> return_type = <NUM_LIT:1> <EOL> else : <EOL> return_type = <NUM_LIT:0> <EOL> data = self . peerlist ( infohash , event == '<STR_LIT>' , <EOL> params ( '<STR_LIT>' ) , not params ( '<STR_LIT:left>' ) , <EOL> return_type , rsize ) <EOL> if paramslist . has_key ( '<STR_LIT>' ) : <EOL> data [ '<STR_LIT>' ] = self . scrapedata ( infohash , False ) <EOL> return ( <NUM_LIT:200> , '<STR_LIT:OK>' , { '<STR_LIT:Content-Type>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , bencode ( data ) ) <EOL> def natcheckOK ( self , infohash , peerid , ip , port , not_seed ) : <EOL> bc = self . becache . setdefault ( infohash , [ [ { } , { } ] , [ { } , { } ] , [ { } , { } ] ] ) <EOL> bc [ <NUM_LIT:0> ] [ not not_seed ] [ peerid ] = Bencached ( bencode ( { '<STR_LIT>' : ip , '<STR_LIT:port>' : port , <EOL> '<STR_LIT>' : peerid } ) ) <EOL> bc [ <NUM_LIT:1> ] [ not not_seed ] [ peerid ] = Bencached ( bencode ( { '<STR_LIT>' : ip , '<STR_LIT:port>' : port } ) ) <EOL> bc [ <NUM_LIT:2> ] [ not not_seed ] [ peerid ] = compact_peer_info ( ip , port ) <EOL> def natchecklog ( self , peerid , ip , port , result ) : <EOL> year , month , day , hour , minute , second , a , b , c = localtime ( time ( ) ) <EOL> print '<STR_LIT>' % ( <EOL> ip , quote ( peerid ) , day , months [ month ] , year , hour , minute , second , <EOL> ip , port , result ) <EOL> def connectback_result ( self , result , downloadid , peerid , ip , port ) : <EOL> record = self . downloads . get ( downloadid , { } ) . get ( peerid ) <EOL> if ( record is None <EOL> or ( record [ '<STR_LIT>' ] != ip and record . get ( '<STR_LIT>' ) != ip ) <EOL> or record [ '<STR_LIT:port>' ] != port ) : <EOL> if self . config [ '<STR_LIT>' ] : <EOL> self . natchecklog ( peerid , ip , port , <NUM_LIT> ) <EOL> return <EOL> if self . config [ '<STR_LIT>' ] : <EOL> if result : <EOL> x = <NUM_LIT:200> <EOL> else : <EOL> x = <NUM_LIT> <EOL> self . natchecklog ( peerid , ip , port , x ) <EOL> if not record . has_key ( '<STR_LIT>' ) : <EOL> record [ '<STR_LIT>' ] = int ( not result ) <EOL> if result : <EOL> self . natcheckOK ( downloadid , peerid , ip , port , record [ '<STR_LIT:left>' ] ) <EOL> elif result and record [ '<STR_LIT>' ] : <EOL> record [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> self . natcheckOK ( downloadid , peerid , ip , port , record [ '<STR_LIT:left>' ] ) <EOL> elif not result : <EOL> record [ '<STR_LIT>' ] += <NUM_LIT:1> <EOL> def remove_from_state ( self , * l ) : <EOL> for s in l : <EOL> try : <EOL> del self . state [ s ] <EOL> except : <EOL> pass <EOL> def save_state ( self ) : <EOL> self . rawserver . add_task ( self . save_state , self . save_dfile_interval ) <EOL> h = open ( self . dfile , '<STR_LIT:wb>' ) <EOL> h . write ( bencode ( self . state ) ) <EOL> h . close ( ) <EOL> def parse_allowed ( self ) : <EOL> self . rawserver . add_task ( self . parse_allowed , self . parse_dir_interval ) <EOL> if self . config [ '<STR_LIT>' ] : <EOL> r = parsedir ( self . config [ '<STR_LIT>' ] , self . allowed , <EOL> self . allowed_dir_files , self . allowed_dir_blocked , <EOL> [ "<STR_LIT>" ] ) <EOL> ( self . allowed , self . allowed_dir_files , self . allowed_dir_blocked , <EOL> added , garbage2 ) = r <EOL> self . state [ '<STR_LIT>' ] = self . allowed <EOL> self . state [ '<STR_LIT>' ] = self . allowed_dir_files <EOL> self . t2tlist . parse ( self . allowed ) <EOL> else : <EOL> f = self . config [ '<STR_LIT>' ] <EOL> if self . allowed_list_mtime == os . path . getmtime ( f ) : <EOL> return <EOL> try : <EOL> r = parsetorrentlist ( f , self . allowed ) <EOL> ( self . allowed , added , garbage2 ) = r <EOL> self . state [ '<STR_LIT>' ] = self . allowed <EOL> except ( IOError , OSError ) : <EOL> print '<STR_LIT>' <EOL> return <EOL> self . allowed_list_mtime = os . path . getmtime ( f ) <EOL> for infohash in added . keys ( ) : <EOL> self . downloads . setdefault ( infohash , { } ) <EOL> self . completed . setdefault ( infohash , <NUM_LIT:0> ) <EOL> self . seedcount . setdefault ( infohash , <NUM_LIT:0> ) <EOL> def read_ip_lists ( self ) : <EOL> self . rawserver . add_task ( self . read_ip_lists , self . parse_dir_interval ) <EOL> f = self . config [ '<STR_LIT>' ] <EOL> if f and self . allowed_ip_mtime != os . path . getmtime ( f ) : <EOL> self . allowed_IPs = IP_List ( ) <EOL> try : <EOL> self . allowed_IPs . read_fieldlist ( f ) <EOL> self . allowed_ip_mtime = os . path . getmtime ( f ) <EOL> except ( IOError , OSError ) : <EOL> print '<STR_LIT>' <EOL> f = self . config [ '<STR_LIT>' ] <EOL> if f and self . banned_ip_mtime != os . path . getmtime ( f ) : <EOL> self . banned_IPs = IP_Range_List ( ) <EOL> try : <EOL> self . banned_IPs . read_rangelist ( f ) <EOL> self . banned_ip_mtime = os . path . getmtime ( f ) <EOL> except ( IOError , OSError ) : <EOL> print '<STR_LIT>' <EOL> def delete_peer ( self , infohash , peerid ) : <EOL> dls = self . downloads [ infohash ] <EOL> peer = dls [ peerid ] <EOL> if not peer [ '<STR_LIT:left>' ] : <EOL> self . seedcount [ infohash ] -= <NUM_LIT:1> <EOL> if not peer . get ( '<STR_LIT>' , - <NUM_LIT:1> ) : <EOL> l = self . becache [ infohash ] <EOL> y = not peer [ '<STR_LIT:left>' ] <EOL> for x in l : <EOL> del x [ y ] [ peerid ] <EOL> del self . times [ infohash ] [ peerid ] <EOL> del dls [ peerid ] <EOL> def expire_downloaders ( self ) : <EOL> for x in self . times . keys ( ) : <EOL> for myid , t in self . times [ x ] . items ( ) : <EOL> if t < self . prevtime : <EOL> self . delete_peer ( x , myid ) <EOL> self . prevtime = clock ( ) <EOL> if ( self . keep_dead != <NUM_LIT:1> ) : <EOL> for key , value in self . downloads . items ( ) : <EOL> if len ( value ) == <NUM_LIT:0> and ( <EOL> self . allowed is None or not self . allowed . has_key ( key ) ) : <EOL> del self . times [ key ] <EOL> del self . downloads [ key ] <EOL> del self . seedcount [ key ] <EOL> self . rawserver . add_task ( self . expire_downloaders , self . timeout_downloaders_interval ) <EOL> def track ( args ) : <EOL> if not args : <EOL> print formatDefinitions ( defaults , <NUM_LIT> ) <EOL> return <EOL> try : <EOL> config , files = parseargs ( args , defaults , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> except ValueError , e : <EOL> print '<STR_LIT>' + str ( e ) <EOL> print '<STR_LIT>' <EOL> return <EOL> r = RawServer ( Event ( ) , config [ '<STR_LIT>' ] , <EOL> config [ '<STR_LIT>' ] , ipv6_enable = config [ '<STR_LIT>' ] ) <EOL> t = Tracker ( config , r ) <EOL> r . bind ( config [ '<STR_LIT:port>' ] , config [ '<STR_LIT>' ] , <EOL> reuse = True , ipv6_socket_style = config [ '<STR_LIT>' ] ) <EOL> r . listen_forever ( HTTPHandler ( t . get , config [ '<STR_LIT>' ] ) ) <EOL> t . save_state ( ) <EOL> print '<STR_LIT>' + isotime ( ) <EOL> def size_format ( s ) : <EOL> if ( s < <NUM_LIT> ) : <EOL> r = str ( s ) + '<STR_LIT:B>' <EOL> elif ( s < <NUM_LIT> ) : <EOL> r = str ( int ( s / <NUM_LIT> ) ) + '<STR_LIT>' <EOL> elif ( s < <NUM_LIT> L ) : <EOL> r = str ( int ( s / <NUM_LIT> ) ) + '<STR_LIT>' <EOL> elif ( s < <NUM_LIT> L ) : <EOL> r = str ( int ( ( s / <NUM_LIT> ) * <NUM_LIT> ) / <NUM_LIT> ) + '<STR_LIT>' <EOL> else : <EOL> r = str ( int ( ( s / <NUM_LIT> ) * <NUM_LIT> ) / <NUM_LIT> ) + '<STR_LIT>' <EOL> return ( r ) </s>
