从bep_0009 [golang]失败的对等节点下载元数据

时间:2018-10-24 14:00:17

标签: go bittorrent dht

当我向对等方发送扩展请求消息时,对等方没有响应。

我得到了握手和扩展位字段,并且可以正常运行,但是没有扩展数据消息。

这是我的Golang代码。

package main

import (
    "bytes"
    "crypto/rand"
    "encoding/binary"
    "fmt"
    "github.com/IncSW/go-bencode"
    "io"
    "log"
    "net"
    "time"
)

func readBuffer(conn *net.TCPConn, size uint32) ([]byte, error) {
    //temp := make([]byte, size)
    //_, err := conn.Read(temp)
    //if err != nil {
    //  return nil, fmt.Errorf("read %d bytes message failed: %v", size, err)
    //}
    //return temp, nil
    buffer := bytes.NewBuffer(nil)
    conn.SetReadDeadline(time.Now().Add(time.Second * 120))
    _, err := io.CopyN(buffer, conn, int64(size))
    if err != nil {
        return nil, fmt.Errorf("read %d bytes message failed: %v", size, err)
    }
    return buffer.Bytes(), nil
}

func next(conn *net.TCPConn) ([]byte, error) {
    buffer, err := readBuffer(conn, 4)
    if nil != err {
        return nil, err
    }
    buffer, err = readBuffer(conn, binary.BigEndian.Uint32(buffer))
    if nil != err {
        return nil, err
    }
    return buffer, nil
}

func getHandshake(infoHash []byte, nodeId []byte) []byte {
    packet := []byte{19, 66, 105, 116, 84, 111, 114, 114, 101, 110, 116, 32, 112, 114, 111, 116, 111, 99, 111, 108, 0, 0, 0, 0, 0, 16, 0, 1}
    packet = append(packet, infoHash...)
    packet = append(packet, nodeId...)
    return packet
}

// http://www.bittorrent.org/beps/bep_0009.html
// http://www.bittorrent.org/beps/bep_0010.html
func peerWire(addr string, infoHash []byte, nodeId []byte) {
    dial, err := net.DialTimeout("tcp", addr, 10*time.Second)
    if nil != err {
        return
    }
    conn := dial.(*net.TCPConn)
    conn.SetLinger(0)
    defer conn.Close()
    // handshake
    handshake := getHandshake(infoHash, nodeId)
    conn.Write(handshake)
    handshakeRes := make([]byte, 68)
    _, err = conn.Read(handshakeRes)
    if !(bytes.Equal(handshake[:20], handshakeRes[:20]) && handshakeRes[25]&0x10 != 0) {
        log.Println("invalid handshake response")
        return
    }
    // extended handshake
    // [length prefix][BitTorrent message ID][extended message ID]
    extendedHandshake, _ := bencode.Marshal(map[string]interface{}{
        "m": map[string]interface{}{"ut_metadata": 1},
    })
    extendedHandshake = append([]byte{20, 0}, extendedHandshake...)
    size := make([]byte, 4)
    extendedHandshake = append(size, extendedHandshake...)
    conn.Write(extendedHandshake)
    buffer, err := next(conn)
    if nil != err {
        log.Println(err.Error())
        return
    }
    if 0 < len(buffer) {
        msgUT, err := bencode.Unmarshal(buffer[2:])
        if nil != err {
            log.Println("error")
            return
        }
        metadataSize, ok := msgUT.(map[string]interface{})["metadata_size"].(int64)
        if !ok {
            return
        }
        m, ok := msgUT.(map[string]interface{})["m"].(map[string]interface{})
        if !ok {
            return
        }
        utMetadata, ok := m["ut_metadata"].(int64)
        if !ok {
            return
        }
        numberOfPieces := metadataSize / 16384
        if metadataSize%16384 != 0 {
            numberOfPieces++
        }

        for i := 0; i < int(numberOfPieces); i++ {
            packet, err := bencode.Marshal(map[string]interface{}{"msg_type":0, "piece":int(i)})
            if nil != err {
                log.Println(err)
            }
            packet = append([]byte{20, byte(utMetadata)}, packet...)
            size := make([]byte, 4)
            binary.BigEndian.PutUint32(size, uint32(len(packet)))
            packet = append(size, packet...)
            conn.Write(packet)
            break
        }
        piece := make([]byte, 0)
        for {
            buffer, err := next(conn)
            if nil != err {
                log.Println(err.Error())
                break
            }
            log.Println(buffer)
            if 20 != buffer[0] {
                continue
            }
            log.Println("buffer: ", buffer)
            piece = append(piece, buffer...)
        }
        log.Println("index :", bytes.Index(piece, []byte("ee")))
    }
}

func main() {
    nodeId := make([]byte, 20)
    rand.Read(nodeId)
    infoHash := []byte{231, 130, 244, 163, 244, 122, 203, 232, 78, 218, 29, 116, 240, 232, 146, 236, 199, 72, 132, 254}
    addr := "203.81.67.114:55254"
    peerWire(addr, infoHash, nodeId)
}

我认为扩展请求消息“ [] byte {0、0、0、27、20、2、100、53、58、112、105、101、99、101、105、48、101、56, 58,109,115,103,95,116,121,112,101,105,48,101,101}“格式正确。

enter image description here

WireShark Dump Data

1 个答案:

答案 0 :(得分:1)

通过pcap:

00000044  00 00 00 00 14 00 64 31  3a 6d 64 31 31 3a 75 74   ......d1 :md11:ut
00000054  5f 6d 65 74 61 64 61 74  61 69 31 65 65 65         _metadat ai1eee

您正在发送00 00 00 00作为扩展握手的长度。那将被解释为一个保持活动消息,其余被解释为长度为14 00 64 31并键入3a的消息,即完全废话。


完整的pcap:
00000000 13 42 69 74 54 6f 72 72 65 6e 74 20 70 72 6f 74 .BitTorr ent prot 00000010 6f 63 6f 6c 00 00 00 00 00 10 00 01 e7 82 f4 a3 ocol.... ........ 00000020 f4 7a cb e8 4e da 1d 74 f0 e8 92 ec c7 48 84 fe .z..N..t .....H.. 00000030 0d 3a 6c 0f 88 e8 41 45 b9 96 b4 52 62 eb 33 cc .:l...AE ...Rb.3. 00000040 ca 73 8b bf .s.. 00000000 13 42 69 74 54 6f 72 72 65 6e 74 20 70 72 6f 74 .BitTorr ent prot 00000010 6f 63 6f 6c 00 00 00 00 00 10 00 05 e7 82 f4 a3 ocol.... ........ 00000020 f4 7a cb e8 4e da 1d 74 f0 e8 92 ec c7 48 84 fe .z..N..t .....H.. 00000030 2d 55 54 33 35 34 53 2d 58 ae 73 ad 79 d9 8e d6 -UT354S- X.s.y... 00000040 bd be b0 5f 00 00 00 e6 14 00 64 31 3a 65 69 30 ..._.... ..d1:ei0 00000050 65 34 3a 69 70 76 34 34 3a cb 51 43 72 31 32 3a e4:ipv44 :.QCr12: 00000060 63 6f 6d 70 6c 65 74 65 5f 61 67 6f 69 complete _agoi 00000044 00 00 00 00 14 00 64 31 3a 6d 64 31 31 3a 75 74 ......d1 :md11:ut 00000054 5f 6d 65 74 61 64 61 74 61 69 31 65 65 65 _metadat ai1eee 0000006D 33 65 31 3a 6d 64 31 31 3a 75 70 6c 6f 61 64 5f 3e1:md11 :upload_ 0000007D 6f 6e 6c 79 69 33 65 31 31 3a 6c 74 5f 64 6f 6e onlyi3e1 1:lt_don 0000008D 74 68 61 76 65 69 37 65 31 32 3a 75 74 5f 68 6f thavei7e 12:ut_ho 0000009D 6c 65 70 75 6e 63 68 69 34 65 31 31 3a 75 74 5f lepunchi 4e11:ut_ 000000AD 6d 65 74 61 64 61 74 61 69 32 65 36 3a 75 74 5f metadata i2e6:ut_ 000000BD 70 65 78 69 31 65 31 30 3a 75 74 5f 63 6f 6d 6d pexi1e10 :ut_comm 000000CD 65 6e 74 69 36 65 65 31 33 3a 6d 65 74 61 64 61 enti6ee1 3:metada 000000DD 74 61 5f 73 69 7a 65 69 38 38 38 32 65 31 3a 70 ta_sizei 8882e1:p 000000ED 69 35 35 32 35 34 65 34 3a 72 65 71 71 69 32 35 i55254e4 :reqqi25 000000FD 35 65 31 3a 76 31 35 3a ce bc 54 6f 72 72 65 6e 5e1:v15: ..Torren 0000010D 74 20 33 2e 35 2e 34 32 3a 79 70 69 33 31 34 30 t 3.5.42 :ypi3140 0000011D 38 65 36 3a 79 6f 75 72 69 70 34 3a 75 8b d0 19 8e6:your ip4:u... 0000012D 65 00 00 00 38 05 ff ef ff df f7 f7 ff ff ff ff e...8... ........ 0000013D ff df ff ff df ff ff ff ef ff ff ee fe ff ff ff ........ ........ 0000014D 6f ff ff 99 ff ff fd ff ff ff ff ff ff ff ef ff o....... ........ 0000015D ff fe f7 ff ff fc ff bf ff ff bf ff c0 00 00 00 ........ ........ 0000016D 05 04 00 00 00 5a 00 00 00 05 04 00 00 00 0b 00 .....Z.. ........ 0000017D 00 00 05 04 00 00 00 e9 00 00 00 05 04 00 00 00 ........ ........ 0000018D ea 00 00 00 05 04 00 00 01 64 00 00 00 05 04 00 ........ .d...... 0000019D 00 01 5f 00 00 00 05 04 00 00 00 d3 00 00 00 05 .._..... ........ 000001AD 04 00 00 01 7f 00 00 00 05 04 00 00 00 1a 00 00 ........ ........ 000001BD 00 05 04 00 00 00 24 00 00 00 05 04 00 00 01 89 ......$. ........ 000001CD 00 00 00 05 04 00 00 00 ee 00 00 00 05 04 00 00 ........ ........ 000001DD 01 06 00 00 00 05 04 00 00 00 ed 00 00 00 05 04 ........ ........ 000001ED 00 00 00 2c 00 00 00 05 04 00 00 00 93 00 00 00 ...,.... ........ 000001FD 05 04 00 00 00 ab 00 00 00 05 04 00 00 00 d0 00 ........ ........ 0000020D 00 00 05 04 00 00 00 b7 00 00 00 05 04 00 00 01 ........ ........ 0000021D a1 00 00 00 05 04 00 00 00 72 00 00 00 05 04 00 ........ .r...... 0000022D 00 01 43 00 00 00 05 04 00 00 01 7e 00 00 00 05 ..C..... ...~.... 0000023D 04 00 00 00 af ..... 00000062 00 00 00 1b 14 02 64 38 3a 6d 73 67 5f 74 79 70 ......d8 :msg_typ 00000072 65 69 30 65 35 3a 70 69 65 63 65 69 30 65 65 ei0e5:pi ecei0ee 00000242 13 42 69 74 54 6f 72 72 65 6e 74 20 70 72 6f 74 .BitTorr ent prot 00000252 6f 63 6f 6c 00 00 00 00 00 10 00 05 e7 82 f4 a3 ocol.... ........ 00000262 f4 7a cb e8 4e da 1d 74 f0 e8 92 ec c7 48 84 fe .z..N..t .....H.. 00000272 2d 55 54 33 35 34 53 2d 58 ae 73 ad 79 d9 8e d6 -UT354S- X.s.y... 00000282 bd be b0 5f 00 00 00 e6 14 00 64 31 3a 65 69 30 ..._.... ..d1:ei0 00000292 65 34 3a 69 70 76 34 34 3a cb 51 43 72 31 32 3a e4:ipv44 :.QCr12: 000002A2 63 6f 6d 70 6c 65 74 65 5f 61 67 6f 69 33 65 31 complete _agoi3e1 000002B2 3a 6d 64 31 31 3a 75 70 6c 6f 61 64 5f 6f 6e 6c :md11:up load_onl 000002C2 79 69 33 65 31 31 3a 6c 74 5f 64 6f 6e 74 68 61 yi3e11:l t_dontha 000002D2 76 65 69 37 65 31 32 3a 75 74 5f 68 6f 6c 65 70 vei7e12: ut_holep 000002E2 75 6e 63 68 69 34 65 31 31 3a 75 74 5f 6d 65 74 unchi4e1 1:ut_met 000002F2 61 64 61 74 61 69 32 65 36 3a 75 74 5f 70 65 78 adatai2e 6:ut_pex 00000302 69 31 65 31 30 3a 75 74 5f 63 6f 6d 6d 65 6e 74 i1e10:ut _comment 00000312 69 36 65 65 31 33 3a 6d 65 74 61 64 61 74 61 5f i6ee13:m etadata_ 00000322 73 69 7a 65 69 38 38 38 32 65 31 3a 70 69 35 35 sizei888 2e1:pi55 00000332 32 35 34 65 34 3a 72 65 71 71 69 32 35 35 65 31 254e4:re qqi255e1 00000342 3a 76 31 35 3a ce bc 54 6f 72 72 65 6e 74 20 33 :v15:..T orrent 3 00000352 2e 35 2e 34 32 3a 79 70 69 33 31 34 30 38 65 36 .5.42:yp i31408e6 00000362 3a 79 6f 75 72 69 70 34 3a 75 8b d0 19 65 00 00 :yourip4 :u...e.. 00000372 00 38 05 ff ef ff df f7 f7 ff ff ff ff ff df ff .8...... ........ 00000382 ff df ff ff ff ef ff ff ee fe ff ff ff 6f ff ff ........ .....o.. 00000392 99 ff ff fd ff ff ff ff ff ff ff ef ff ff fe f7 ........ ........ 000003A2 ff ff fc ff bf ff ff bf ff c0 00 00 00 05 04 00 ........ ........ 000003B2 00 00 5a 00 00 00 05 04 00 00 00 0b 00 00 00 05 ..Z..... ........ 000003C2 04 00 00 00 e9 00 00 00 05 04 00 00 00 ea 00 00 ........ ........ 000003D2 00 05 04 00 00 01 64 00 00 00 05 04 00 00 01 5f ......d. ......._ 000003E2 00 00 00 05 04 00 00 00 d3 00 00 00 05 04 00 00 ........ ........ 000003F2 01 7f 00 00 00 05 04 00 00 00 1a 00 00 00 05 04 ........ ........ 00000402 00 00 00 24 00 00 00 05 04 00 00 01 89 00 00 00 ...$.... ........ 00000412 05 04 00 00 00 ee 00 00 00 05 04 00 00 01 06 00 ........ ........ 00000422 00 00 05 04 00 00 00 ed 00 00 00 05 04 00 00 00 ........ ........ 00000432 2c 00 00 00 05 04 00 00 00 93 00 00 00 05 04 00 ,....... ........ 00000442 00 00 ab 00 00 00 05 04 00 00 00 d0 00 00 00 05 ........ ........ 00000452 04 00 00 00 b7 00 00 00 05 04 00 00 01 a1 00 00 ........ ........ 00000462 00 05 04 00 00 00 72 00 00 00 05 04 00 00 01 43 ......r. .......C 00000472 00 00 00 05 04 00 00 01 7e 00 00 00 05 04 00 00 ........ ~....... 00000482 00 af 00 00 00 03 09 d7 d6 ........ .