微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

UDP Tracker 只给我我的 ip 作为宣布请求的答案

如何解决UDP Tracker 只给我我的 ip 作为宣布请求的答案

我最近一直在尝试在 python 中创建一个 Torrent 客户端,并且刚刚让 UDP 宣布协议工作。 跟踪器很好地接受了我的连接请求,但仅在我向其宣布时返回我的 IP 和端口作为对等列表... 我试过在其他种子客户端中查看相同的种子,他们有多个工作对等点,而我的请求只显示我的计算机(我已经在许多种子上尝试过,都只返回我的 IP 和端口)

这是发送函数本身的代码

    async def announce_udp(self,try_num = 1):
    self.sock.settimeout(15)
    answer = {}
    inner_while = False
    while try_num < 4:
        while try_num < 4:
            try:
                print("trying to send")
                sended = self.send(1,self.announce_payload())
                print("sending the following packet: {0}".format(sended))
                print(self.url)
                inner_while = True
                break
            except Exception:
                print("problem in sending")
                try_num += 1
        if not inner_while:
            break
        try:
            answer = self.interpret(15)
            break
        except Exception:
            print("problem in receiving")
            try_num += 1
    print("announce answer is: {0}".format(answer))
    return answer

这是make payload函数代码

def announce_payload(self,downloaded = 0,left = 0,uploaded = 0,event = 0,key = get_transaction_id()):
    payload = [self.torrent.get_torrent_info_hash_decoded(),get_peer_id().encode(),downloaded,self.torrent.get_torrent_size(),uploaded,event,key,-1,6988]
    p_tosend = None
    try:
        p_tosend = struct.pack('!20s20sqqqiIIiH',*payload)
    except Exception as e:
        print("there was an error: {0}".format(e))
    return p_tosend

这是解释+处理函数代码

def interpret(self,timeout=10):
    self.sock.settimeout(timeout)
    print("got to interpret")
    try:
        response = self.sock.recv(10240)
        print("answer recieved")
    except socket.timeout:
        print("no answer,try again")
        raise TrackerResponseException("no answer",0)

    headers = response[:8]
    payload = response[8:]

    action,trans_id = struct.unpack('!ll',headers)

    try:
        trans = self.transactions[trans_id]
    except KeyError:
        raise TrackerResponseException("InvalidTransaction: id not found",trans_id)

    try:
        trans['response'] = self.process(action,payload,trans)
    except Exception as e:
        trans['response'] = None
        print("error occured: {0}".format(e))
    trans['completed'] = True
    del self.transactions[trans_id]
    #print(trans)
    return trans

  def process_announce(self,trans):
    response = {}
    info = payload[:struct.calcsize("!lll")]
    interval,leechers,seeders = struct.unpack("!lll",info)
    print(interval,seeders,"noamsssssss")
    peer_data = payload[struct.calcsize("!lll"):]
    peer_size = struct.calcsize("!lH")
    num_of_peers = int(len(peer_data) / peer_size)
    print("the number of peers is: {0} and the peer data is: {1}".format(num_of_peers,peer_data))
    print()
    peers = []
    for peer_offset in range(num_of_peers):
        off = peer_size * peer_offset
        peer = peer_data[off:off + peer_size]
        addr,port = struct.unpack("!lH",peer)
        peers.append({
            'addr': socket.inet_ntoa(struct.pack('!L',addr)),'port': port,})
    print(payload)
    return dict(interval=interval,leechers=leechers,seeders=seeders,peers=peers)

如果有任何不相关,我很抱歉,但我想给你所有的代码,以防它告诉你一些事情。

(get_peer_id() 根据跟踪器协议规范返回一个随机的 peer id,get_transaction_id() 返回 random.randint(0,1

编辑: 好吧,我找到了问题,现在我感觉很愚蠢...... 事实证明,即使在 udp 跟踪器中,每当您发送信息散列时,它也必须是 SHA1 编码的。 希望这可以帮助遇到同样问题的人:)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。