ruby - BitTorrent 对等握手

标签 ruby networking tcp p2p bittorrent

我正在尝试向对等点发送 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 最后的样子: enter image description here

最佳答案

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

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

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

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

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

我在此处附上示例握手消息: enter image description here

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

关于ruby - BitTorrent 对等握手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18394049/

相关文章:

ruby - 切片数组数组

javascript - 从外部访问由 node.js 运行的页面

ruby-on-rails - 如何定义一个代码块一次使用多次?

c# - 如何使用.NET监控应用程序状态?

linux - Traceroute : target doesn't acknowledge packet just from a specific host. 来自其他主机的 Traceroute 工作正常。 Ping 在所有主机上都能正常工作

tcp - 防止 netcat 中的 TCP SYN 重试(用于端口敲门)

c - 如何在混杂模式下创建一个 tcp 套接字?

asp.net - IIS 只监听 127.0.0.1 而不是 0.0.0.0

ruby-on-rails - 如何在 Ruby/Rails 中进行只发生一次的函数调用

ruby-on-rails - 从助手规范中获取 'action_name' 或 'controller'