简体   繁体   English

BitTorrent对等握手

[英]BitTorrent Peer Handshake

I'm trying to send a BitTorrent handshake to a peer but it's not working. 我正在尝试将BitTorrent握手发送给同位体,但是它不起作用。 Can someone figure out what I'm doing wrong? 有人可以弄清楚我在做什么错吗? len is supposed to come back with something, but it's currently nil. 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

Here's what send_string looks like in the end: 这是send_string样子: 在此处输入图片说明

While having handshake from your side, PeerID used should be yours , not of the other side. 从您一侧握手时,使用的PeerID应该是您的,而不是另一端的。

So your message should look like this : 因此,您的消息应如下所示:

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

If you are developing your own bit-torrent client then you can have your own format for peer id following standards mentioned by bit-torrent protocol. 如果您正在开发自己的bit-torrent客户端,则可以遵循bit-torrent协议提到的标准,使用自己的对等ID格式。 You can read more about peer id here: https://wiki.theory.org/BitTorrentSpecification#peer_id 您可以在此处阅读有关同级ID的更多信息: https : //wiki.theory.org/BitTorrentSpecification#peer_id

If you have any confusion in future , start any bittorrent client and follow wire-shark packets. 如果您将来有任何困惑,请启动任何Bittorrent客户端并遵循Wire-shark数据包。 You will understand, where you are making mistake. 您会明白,您在哪里犯错。

I am attaching a sample handshake message here : 我将在此处附加示例握手消息: 在此处输入图片说明

Here "-KS0001-123456654321" is peerid for my bittorrent client. 在这里,“-KS0001-123456654321”是我的bittorrent客户端的对等体。

I cant write ruby. 我不能写红宝石。 This is python code: 这是python代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM