1

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ようになります。 ここに画像の説明を入力

4

2 に答える 2

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 クライアントを開発している場合は、bit-torrent プロトコルで言及されている標準に従って、ピア ID の独自の形式を持つことができます。ピア ID の詳細については、 https ://wiki.theory.org/BitTorrentSpecification#peer_id をご覧ください。

今後混乱する場合は、任意の bittorrent クライアントを起動して、wire-sharkパケットに従ってください。どこで間違いを犯しているかがわかります。

サンプルのハンドシェイク メッセージをここに添付します。 ここに画像の説明を入力

ここで、「-KS0001-123456654321」は、bittorrent クライアントの peerid です。

于 2016-06-12T07:10:35.007 に答える
0

ルビーが書けません。これはpythonコードです:

send_string = chr(19)+"BitTorrent Protocol"+8*chr(0)+info_hash+peer_id
于 2014-01-31T19:08:20.070 に答える