【问题标题】:BitTorrent Peer HandshakeBitTorrent 对等握手
【发布时间】:2016-06-12 15:10:35
【问题描述】:

我正在尝试向对等方发送 BitTorrent 握手,但它不起作用。有人能弄清楚我做错了什么吗? len 应该会带来一些东西,但它目前为零。

require 'bencode'
require 'digest/sha1'

file = File.open('./python.torrent').read
info_hash = Digest::SHA1.hexdigest(a['info'].bencode)

# Here's what parsed_response['peers'] returns:
# [{"ip"=>"8.19.35.234", "peer id"=>"-lt0C20-\x90\xE0\xE6\x0E\xD0\x8A\xE5\xA2\xF2b(!",          
# "port"=>9898}]
peer_id = parsed_response['peers'].first['peer id']

send_string = "\023BitTorrent protocol\0\0\0\0\0\0\0\0" << info_hash << peer_id

# ip and port are my current internet ip and 6881 respectively
client = TCPSocket.new ip, port

# What I'm sending over
client.send("\023BitTorrent protocol\0\0\0\0\0\0\0\0" << info_hash << peer_id, 0)
len = client.recv(1)
puts len

send_string 最终的样子如下:

【问题讨论】:

  • send_string 中的 info_hash 是一个 40 字节的十六进制字符串,应该是一个 20 字节的原始二进制字符串。

标签: ruby networking tcp p2p bittorrent


【解决方案1】:

在与你握手的同时, 使用的 PeerID 应该是你的,而不是对方的。

所以你的信息应该是这样的:

peer_id = "-MY0001-123456654321"
client.send("\023BitTorrent protocol\0\0\0\0\0\0\0\0" << info_hash << peer_id, 0)

如果您正在开发自己的 bit-torrent 客户端,那么您可以拥有自己的 peer id 格式,遵循 bit-torrent 协议提到的标准。 您可以在此处阅读有关对等 ID 的更多信息: https://wiki.theory.org/BitTorrentSpecification#peer_id

如果您以后有任何困惑,请启动任何 bittorrent 客户端并关注wire-shark 数据包。你会明白,你在哪里犯了错误。

我在此处附上示例握手消息:

这里“-KS0001-123456654321”是我的bittorrent客户端的peerid。

【讨论】:

    【解决方案2】:

    我不会写红宝石。 这是python代码:

    send_string = chr(19)+"BitTorrent Protocol"+8*chr(0)+info_hash+peer_id
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-23
      • 1970-01-01
      • 2023-01-30
      • 1970-01-01
      • 2021-11-11
      • 2021-04-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多