Пример #1
0
    def render_GET(self, request):

        HashTableNew.HashTable.do_clean_up()

        start = time.clock()

        info_hash_lst = []
        if 'info_hash' in request.args:
            info_hash_lst = request.args['info_hash']
        else:
            return bencode_faild_str('Mising info_hash')

        ret = {}
        for info_hash in info_hash_lst:
            peers_cnt = HashTable.get_torrent_peers_count(info_hash)
            seeders_cnt = HashTable.get_torrent_seeders_count(info_hash)
            leechers_cnt = peers_cnt - seeders_cnt
            completed_cnt = HashTable.get_completed_by_info_hash(info_hash)
            info_table = {"complete": seeders_cnt, "downloaded": completed_cnt, "incomplete": leechers_cnt}
            ret.update({info_hash:info_table})

        ret = {'files': ret}
        ret = bencode.bencode(ret)
        end = time.clock()
        logging.debug('process scrape query at:%lf' % (end-start))
        Config.process_scrape_query_at = end-start
        return ret
Пример #2
0
    def render_GET(self, request):

        HashTableNew.HashTable.do_clean_up()

        start = time.clock()

        info_hash_lst = []
        if 'info_hash' in request.args:
            info_hash_lst = request.args['info_hash']
        else:
            return bencode_faild_str('Mising info_hash')

        ret = {}
        for info_hash in info_hash_lst:
            peers_cnt = HashTable.get_torrent_peers_count(info_hash)
            seeders_cnt = HashTable.get_torrent_seeders_count(info_hash)
            leechers_cnt = peers_cnt - seeders_cnt
            completed_cnt = HashTable.get_completed_by_info_hash(info_hash)
            info_table = {
                "complete": seeders_cnt,
                "downloaded": completed_cnt,
                "incomplete": leechers_cnt
            }
            ret.update({info_hash: info_table})

        ret = {'files': ret}
        ret = bencode.bencode(ret)
        end = time.clock()
        logging.debug('process scrape query at:%lf' % (end - start))
        Config.process_scrape_query_at = end - start
        return ret
Пример #3
0
 def render_GET(self, request):
     lst = {}
     lst.update({'torrent_count': HashTable.get_torrent_count()})
     lst.update({'peers_count': HashTable.get_peer_count()})
     lst.update({'process_announce_query_at': float(Config.process_announce_query_at)})
     lst.update({'process_scrape_query_at': float(Config.process_scrape_query_at)})
     lst.update({'cleanup_time_query_at': float(Config.cleanup_time_query_at)})
     return json.dumps(lst)
Пример #4
0
 def render_GET(self, request):
     lst = {}
     lst.update({'torrent_count': HashTable.get_torrent_count()})
     lst.update({'peers_count': HashTable.get_peer_count()})
     lst.update({
         'process_announce_query_at':
         float(Config.process_announce_query_at)
     })
     lst.update(
         {'process_scrape_query_at': float(Config.process_scrape_query_at)})
     lst.update(
         {'cleanup_time_query_at': float(Config.cleanup_time_query_at)})
     return json.dumps(lst)
Пример #5
0
    def render_GET(self, request):
        HashTableNew.HashTable.do_clean_up()
        # print request.uri
        start = time.clock()
        # ipv6ip = None
        client = request.client
        if isinstance(client, address.IPv4Address):
            socketIp = client.host

        # notice if use ipv6 couldn't get real ipv4 ip
        # elif isinstance(client, address.IPv6Address):
        #    ipv6ip = client.host

        # if ipv6ip is None:
        ipv6ip = self.get_argument(request, "ipv6")

        ip = self.get_argument(request, "ip")
        info_hash = self.get_argument(request, "info_hash")
        peer_id = self.get_argument(request, "peer_id")
        key = self.get_argument(request, "key")
        port = self.get_argument(request, "port")
        ipv6port = None
        downloaded = self.get_argument(request, "downloaded", 0)
        uploaded = self.get_argument(request, "uploaded", 0)
        left = self.get_argument(request, "left", 0)
        passkey = self.get_argument(request, "passkey")
        event = self.get_argument(request, "event", "")
        numwant = self.get_argument(request, "numwant", 50)

        if Config.PRIVETE_TRACKER:
            if passkey is None:
                return bencode_faild_str("Missing passkey")

        # check argments
        if info_hash is None:
            return bencode_faild_str("Missing info_hash")
        if len(info_hash) != 20:
            return bencode_faild_str("Valid info_hash")
        if peer_id is None:
            return bencode_faild_str("Missing peer_id")
        if len(peer_id) != 20:
            return bencode_faild_str("Valid peer_id")
        if port is None:
            return bencode_faild_str("Missing port")
        if len(port) > 5:
            return bencode_faild_str("Valid port")
        if ip is not None and len(ip) > 15:
            # return bencode_faild_str('Valid ip')
            ip = None
        if ipv6ip is not None and len(ipv6ip) > 37:
            # [0000:0000:0000:0000:0000:0000]:00000
            ipv6ip = None

        downloaded = self.str2long(downloaded)
        uploaded = self.str2long(uploaded)
        left = self.str2long(left)
        numwant = self.str2int(numwant)

        if downloaded is None:
            return bencode_faild_str("Missing downloaded")
        if uploaded is None:
            return bencode_faild_str("Missing uploaded")
        if left is None:
            return bencode_faild_str("Missing left")
        if numwant is None:
            return bencode_faild_str("Valid numwant")

        if numwant > 100:
            numwant = 100

        # bep_0007
        if ipv6ip is not None:
            if ipv6ip[0] == "[":
                pos = ipv6ip.rfind("]:")
                if pos != -1:
                    # [::]:0
                    ipv6port = ipv6ip[pos + 2 :]
                    ipv6ip = ipv6ip[0 : pos + 1]
                else:
                    # [::]
                    ipv6port = port
            else:
                #::
                ipv6ip = "[" + ipv6ip + "]"
                ipv6port = port

        ret_ipv6 = False
        if ipv6ip is not None:
            if ipv6ip[1:6].lower() != "fe80:":
                ret_ipv6 = True
        if socketIp is None:
            # ipv6 socket
            ret_ipv6 = True

        if event is None or event == "":
            # some client event=&
            p = HashTable.update_peer_by_peer_id_and_info_hash(peer_id, info_hash)
            p.update(
                socketIp, ip, ipv6ip, port, ipv6port, peer_id, info_hash, downloaded, uploaded, left, event, passkey
            )
        if event == "completed":
            p = HashTable.find_peer_by_peer_id_and_info_hash(peer_id, info_hash)
            HashTable.add_completed_by_info_hash(info_hash)
            p = HashTable.update_peer_by_peer_id_and_info_hash(peer_id, info_hash)
            p.update(
                socketIp, ip, ipv6ip, port, ipv6port, peer_id, info_hash, downloaded, uploaded, left, event, passkey
            )
        if event == "started":
            # pt need process something else
            p = HashTable.update_peer_by_peer_id_and_info_hash(peer_id, info_hash)
            p.update(
                socketIp, ip, ipv6ip, port, ipv6port, peer_id, info_hash, downloaded, uploaded, left, event, passkey
            )
            p.uploaded_first = uploaded
            p.downloaded_first = downloaded
            p.left_first = left
        elif event == "stopped":
            HashTable.remove_peer_by_peer_id_and_info_hash(peer_id, info_hash)

        if event == "stopped":
            peers = ""
        else:
            peers = HashTable.find_peers_by_info_hash(info_hash)
            peers = self.generate_peer_list(peers, numwant, ret_ipv6)

        peers_cnt = HashTable.get_torrent_peers_count(info_hash)
        seeders_cnt = HashTable.get_torrent_seeders_count(info_hash)
        leechers_cnt = peers_cnt - seeders_cnt

        ret = {
            "interval": Config.INTERVAL,
            "min interval": 15,
            "complete": seeders_cnt,
            "incomplete": leechers_cnt,
            "peers": peers,
        }

        ret = bencode.bencode(ret)
        end = time.clock()
        logging.debug("process announce query at:%lf" % (end - start))
        Config.process_announce_query_at = end - start
        return ret
Пример #6
0
    def render_GET(self, request):
        HashTableNew.HashTable.do_clean_up()
        #print request.uri
        start = time.clock()
        #ipv6ip = None
        client = request.client
        if isinstance(client, address.IPv4Address):
            socketIp = client.host

        #notice if use ipv6 couldn't get real ipv4 ip
        #elif isinstance(client, address.IPv6Address):
        #    ipv6ip = client.host

        #if ipv6ip is None:
        ipv6ip = self.get_argument(request, 'ipv6')

        ip = self.get_argument(request, 'ip')
        info_hash = self.get_argument(request, 'info_hash')
        peer_id = self.get_argument(request, 'peer_id')
        key = self.get_argument(request, 'key')
        port = self.get_argument(request, 'port')
        ipv6port = None
        downloaded = self.get_argument(request, 'downloaded', 0)
        uploaded = self.get_argument(request, 'uploaded', 0)
        left = self.get_argument(request, 'left', 0)
        passkey = self.get_argument(request, 'passkey')
        event = self.get_argument(request, 'event', '')
        numwant = self.get_argument(request, 'numwant', 50)

        if Config.PRIVETE_TRACKER:
            if passkey is None:
                return bencode_faild_str('Missing passkey')

        #check argments
        if info_hash is None:
            return bencode_faild_str('Missing info_hash')
        if len(info_hash) != 20:
            return bencode_faild_str('Valid info_hash')
        if peer_id is None:
            return bencode_faild_str('Missing peer_id')
        if len(peer_id) != 20:
            return bencode_faild_str('Valid peer_id')
        if port is None:
            return bencode_faild_str('Missing port')
        if len(port) > 5:
            return bencode_faild_str('Valid port')
        if ip is not None and len(ip) > 15:
            #return bencode_faild_str('Valid ip')
            ip = None
        if ipv6ip is not None and len(ipv6ip) > 37:
            #[0000:0000:0000:0000:0000:0000]:00000
            ipv6ip = None

        downloaded = self.str2long(downloaded)
        uploaded = self.str2long(uploaded)
        left = self.str2long(left)
        numwant = self.str2int(numwant)

        if downloaded is None:
            return bencode_faild_str('Missing downloaded')
        if uploaded is None:
            return bencode_faild_str('Missing uploaded')
        if left is None:
            return bencode_faild_str('Missing left')
        if numwant is None:
            return bencode_faild_str('Valid numwant')

        if numwant > 100:
            numwant = 100

        #bep_0007
        if ipv6ip is not None:
            if ipv6ip[0] == '[':
                pos = ipv6ip.rfind(']:')
                if pos != -1:
                    #[::]:0
                    ipv6port = ipv6ip[pos + 2:]
                    ipv6ip = ipv6ip[0:pos + 1]
                else:
                    #[::]
                    ipv6port = port
            else:
                #::
                ipv6ip = '[' + ipv6ip + ']'
                ipv6port = port

        ret_ipv6 = False
        if ipv6ip is not None:
            if ipv6ip[1:6].lower() != 'fe80:':
                ret_ipv6 = True
        if socketIp is None:
            #ipv6 socket
            ret_ipv6 = True

        if event is None or event == '':
            #some client event=&
            p = HashTable.update_peer_by_peer_id_and_info_hash(
                peer_id, info_hash)
            p.update(socketIp, ip, ipv6ip, port, ipv6port, peer_id, info_hash,
                     downloaded, uploaded, left, event, passkey)
        if event == 'completed':
            p = HashTable.find_peer_by_peer_id_and_info_hash(
                peer_id, info_hash)
            HashTable.add_completed_by_info_hash(info_hash)
            p = HashTable.update_peer_by_peer_id_and_info_hash(
                peer_id, info_hash)
            p.update(socketIp, ip, ipv6ip, port, ipv6port, peer_id, info_hash,
                     downloaded, uploaded, left, event, passkey)
        if event == 'started':
            #pt need process something else
            p = HashTable.update_peer_by_peer_id_and_info_hash(
                peer_id, info_hash)
            p.update(socketIp, ip, ipv6ip, port, ipv6port, peer_id, info_hash,
                     downloaded, uploaded, left, event, passkey)
            p.uploaded_first = uploaded
            p.downloaded_first = downloaded
            p.left_first = left
        elif event == 'stopped':
            HashTable.remove_peer_by_peer_id_and_info_hash(peer_id, info_hash)

        if event == 'stopped':
            peers = ''
        else:
            peers = HashTable.find_peers_by_info_hash(info_hash)
            peers = self.generate_peer_list(peers, numwant, ret_ipv6)

        peers_cnt = HashTable.get_torrent_peers_count(info_hash)
        seeders_cnt = HashTable.get_torrent_seeders_count(info_hash)
        leechers_cnt = peers_cnt - seeders_cnt

        ret = {
            "interval": Config.INTERVAL,
            "min interval": 15,
            "complete": seeders_cnt,
            "incomplete": leechers_cnt,
            "peers": peers,
        }

        ret = bencode.bencode(ret)
        end = time.clock()
        logging.debug('process announce query at:%lf' % (end - start))
        Config.process_announce_query_at = end - start
        return ret