-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprotocol.go
189 lines (154 loc) · 3.52 KB
/
protocol.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package metadata
import (
"bytes"
"encoding/binary"
"encoding/hex"
"errors"
"github.com/IncSW/go-bencode"
"io"
"net"
"time"
)
const (
// REQUEST type
REQUEST = 0
// DATA type
DATA = 1
// REJECT type
REJECT = 2
)
var (
readTimeout = 2 * time.Second
writeTimeout = 1 * time.Second
)
func writePacket(conn net.Conn, data []byte) error {
conn.SetWriteDeadline(time.Now().Add(writeTimeout))
n, err := conn.Write(data)
if err != nil {
return err
}
if n != len(data) {
return errors.New("write error")
}
// log.Println("send =>", data)
return nil
}
func readPacket(conn net.Conn, buf *bytes.Buffer, size int64) error {
conn.SetReadDeadline(time.Now().Add(readTimeout))
readLen, err := io.CopyN(buf, conn, size)
if err != nil {
return err
}
if readLen != size {
return errors.New("readPacket error")
}
// log.Println("recv <=", buf.Bytes())
return nil
}
func readMessage(conn net.Conn, buf *bytes.Buffer) (bid int, eid int, err error) {
err = readPacket(conn, buf, 4)
if err != nil {
return
}
msgLen := int64(binary.BigEndian.Uint32(buf.Next(4)))
if msgLen == 0 {
return 0, 0, errors.New("read message error")
}
err = readPacket(conn, buf, msgLen)
if err != nil {
return
}
b, err := buf.ReadByte()
if err != nil {
return
}
bid = int(b)
e, err := buf.ReadByte()
if err != nil {
return
}
eid = int(e)
return
}
func writePacketExt(conn net.Conn, data []byte, bid, eid int) error {
length := len(data) + 2
packet := make([]byte, 4+length)
binary.BigEndian.PutUint32(packet[:4], uint32(length))
packet[4] = byte(bid)
packet[5] = byte(eid)
copy(packet[6:], data)
return writePacket(conn, packet)
}
func sendHandshake(conn net.Conn, query *metadataQuery) error {
packet := make([]byte, 68)
packet[0] = 19
copy(packet[1:20], []byte("BitTorrent protocol"))
packet[25] = 0x10
infohash, _ := hex.DecodeString(query.HashInfo)
peerID, _ := hex.DecodeString(query.PeerID)
copy(packet[28:48], infohash)
copy(packet[48:68], peerID)
return writePacket(conn, packet)
}
func recvHandshake(conn net.Conn, buf *bytes.Buffer) error {
return readPacket(conn, buf, 68)
}
func sendHandshakeExt(conn net.Conn, query *metadataQuery) error {
d := map[string]interface{}{
"m": map[string]interface{}{
"ut_metadata": 0,
},
}
var (
bid = 20
eid int
)
out, _ := bencode.Marshal(d)
return writePacketExt(conn, out, bid, eid)
}
func sendPieceRequst(conn net.Conn, utMetadata int, pieceID int) error {
out, _ := bencode.Marshal(map[string]interface{}{
"msg_type": REQUEST,
"piece": pieceID,
})
var (
bid = 20
eid = utMetadata
)
return writePacketExt(conn, out, bid, eid)
}
func recvHandshakeExt(conn net.Conn, buf *bytes.Buffer) (utMetadata int64, metadataSize int64, err error) {
_, _, err = readMessage(conn, buf)
if err != nil {
return
}
if buf.Len() == 0 {
err = errors.New("read error")
return
}
i, err := bencode.Unmarshal(buf.Bytes())
if err != nil {
return
}
metadataSize, ok := i.(map[string]interface{})["metadata_size"].(int64)
if !ok {
err = errors.New("no metadata_size")
return
}
utMetadata = i.(map[string]interface{})["m"].(map[string]interface{})["ut_metadata"].(int64)
return
}
func parsePiece(data []byte) (msgType, pieceID, totalSize, offset int, err error) {
i, err := bencode.Unmarshal(data)
if err != nil {
return
}
m := i.(map[string]interface{})
msgType = int(m["msg_type"].(int64))
pieceID = int(m["piece"].(int64))
// totalSize = int(m["total_size"].(int64))
// TODO: FIX
b, _ := bencode.Marshal(i)
offset = len(b)
return
}