-
Notifications
You must be signed in to change notification settings - Fork 906
/
meta.go
369 lines (309 loc) · 6.87 KB
/
meta.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package main
import (
"bytes"
"context"
"crypto/sha1"
"encoding/binary"
"errors"
"fmt"
"io"
"net"
"sync"
"time"
"github.com/marksamman/bencode"
)
const (
perBlock = 16384
maxMetadataSize = perBlock * 1024
extended = 20
extHandshake = 0
)
var (
errExtHeader = errors.New("invalid extension header response")
errInvalidPiece = errors.New("invalid piece response")
errTimeout = errors.New("time out")
)
var metaWirePool = sync.Pool{
New: func() interface{} {
return &metaWire{}
},
}
type metaWire struct {
infohash string
from string
peerID string
conn *net.TCPConn
timeout time.Duration
metadataSize int
utMetadata int
numOfPieces int
pieces [][]byte
err error
}
func newMetaWire(infohash string, from string, timeout time.Duration) *metaWire {
w := metaWirePool.Get().(*metaWire)
w.infohash = infohash
w.from = from
w.peerID = string(randBytes(20))
w.timeout = timeout
w.conn = nil
w.err = nil
return w
}
func (mw *metaWire) fetch() ([]byte, error) {
ctx, cancel := context.WithTimeout(context.Background(), mw.timeout)
defer cancel()
return mw.fetchCtx(ctx)
}
func (mw *metaWire) fetchCtx(ctx context.Context) ([]byte, error) {
mw.connect(ctx)
mw.handshake(ctx)
mw.onHandshake(ctx)
mw.extHandshake(ctx)
if mw.err != nil {
if mw.conn != nil {
mw.conn.Close()
}
return nil, mw.err
}
for {
data, err := mw.next(ctx)
if err != nil {
return nil, err
}
if data[0] != extended {
continue
}
if err := mw.onExtended(ctx, data[1], data[2:]); err != nil {
return nil, err
}
if !mw.checkDone() {
continue
}
m := bytes.Join(mw.pieces, []byte(""))
sum := sha1.Sum(m)
if bytes.Equal(sum[:], []byte(mw.infohash)) {
return m, nil
}
return nil, errors.New("metadata checksum mismatch")
}
}
func (mw *metaWire) connect(ctx context.Context) {
conn, err := net.DialTimeout("tcp", mw.from, mw.timeout)
if err != nil {
mw.err = fmt.Errorf("connect to remote peer failed: %v", err)
return
}
mw.conn = conn.(*net.TCPConn)
}
func (mw *metaWire) handshake(ctx context.Context) {
if mw.err != nil {
return
}
select {
case <-ctx.Done():
mw.err = errTimeout
return
default:
}
buf := bytes.NewBuffer(nil)
buf.Write(mw.preHeader())
buf.WriteString(mw.infohash)
buf.WriteString(mw.peerID)
_, mw.err = mw.conn.Write(buf.Bytes())
}
func (mw *metaWire) onHandshake(ctx context.Context) {
if mw.err != nil {
return
}
select {
case <-ctx.Done():
mw.err = errTimeout
return
default:
}
res, err := mw.read(ctx, 68)
if err != nil {
mw.err = err
return
}
if !bytes.Equal(res[:20], mw.preHeader()[:20]) {
mw.err = errors.New("remote peer not supporting bittorrent protocol")
return
}
if res[25]&0x10 != 0x10 {
mw.err = errors.New("remote peer not supporting extension protocol")
return
}
if !bytes.Equal(res[28:48], []byte(mw.infohash)) {
mw.err = errors.New("invalid bittorrent header response")
return
}
}
func (mw *metaWire) extHandshake(ctx context.Context) {
if mw.err != nil {
return
}
select {
case <-ctx.Done():
mw.err = errTimeout
return
default:
}
data := append([]byte{extended, extHandshake}, bencode.Encode(map[string]interface{}{
"m": map[string]interface{}{
"ut_metadata": 1,
},
})...)
if err := mw.write(ctx, data); err != nil {
mw.err = err
return
}
}
func (mw *metaWire) onExtHandshake(ctx context.Context, payload []byte) error {
select {
case <-ctx.Done():
return errTimeout
default:
}
dict, err := bencode.Decode(bytes.NewBuffer(payload))
if err != nil {
return errExtHeader
}
metadataSize, ok := dict["metadata_size"].(int64)
if !ok {
return errExtHeader
}
if metadataSize > maxMetadataSize {
return errors.New("metadata_size too long")
}
if metadataSize < 0 {
return errors.New("negative metadata_size")
}
m, ok := dict["m"].(map[string]interface{})
if !ok {
return errExtHeader
}
utMetadata, ok := m["ut_metadata"].(int64)
if !ok {
return errExtHeader
}
mw.metadataSize = int(metadataSize)
mw.utMetadata = int(utMetadata)
mw.numOfPieces = mw.metadataSize / perBlock
if mw.metadataSize%perBlock != 0 {
mw.numOfPieces++
}
mw.pieces = make([][]byte, mw.numOfPieces)
for i := 0; i < mw.numOfPieces; i++ {
mw.requestPiece(ctx, i)
}
return nil
}
func (mw *metaWire) requestPiece(ctx context.Context, i int) {
buf := bytes.NewBuffer(nil)
buf.WriteByte(byte(extended))
buf.WriteByte(byte(mw.utMetadata))
buf.Write(bencode.Encode(map[string]interface{}{
"msg_type": 0,
"piece": i,
}))
mw.write(ctx, buf.Bytes())
}
func (mw *metaWire) onExtended(ctx context.Context, ext byte, payload []byte) error {
if ext == 0 {
if err := mw.onExtHandshake(ctx, payload); err != nil {
return err
}
} else {
piece, index, err := mw.onPiece(ctx, payload)
if err != nil {
return err
}
mw.pieces[index] = piece
}
return nil
}
func (mw *metaWire) onPiece(ctx context.Context, payload []byte) ([]byte, int, error) {
select {
case <-ctx.Done():
return nil, -1, errTimeout
default:
}
trailerIndex := bytes.Index(payload, []byte("ee")) + 2
if trailerIndex == 1 {
return nil, 0, errInvalidPiece
}
dict, err := bencode.Decode(bytes.NewBuffer(payload[:trailerIndex]))
if err != nil {
return nil, 0, errInvalidPiece
}
pieceIndex, ok := dict["piece"].(int64)
if !ok || int(pieceIndex) >= mw.numOfPieces {
return nil, 0, errInvalidPiece
}
msgType, ok := dict["msg_type"].(int64)
if !ok || msgType != 1 {
return nil, 0, errInvalidPiece
}
return payload[trailerIndex:], int(pieceIndex), nil
}
func (mw *metaWire) checkDone() bool {
for _, b := range mw.pieces {
if b == nil {
return false
}
}
return true
}
func (mw *metaWire) preHeader() []byte {
buf := bytes.NewBuffer(nil)
buf.WriteByte(19)
buf.WriteString("BitTorrent protocol")
buf.Write([]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x01})
return buf.Bytes()
}
func (mw *metaWire) next(ctx context.Context) ([]byte, error) {
data, err := mw.read(ctx, 4)
if err != nil {
return nil, err
}
size := binary.BigEndian.Uint32(data)
data, err = mw.read(ctx, size)
if err != nil {
return nil, err
}
return data, nil
}
func (mw *metaWire) read(ctx context.Context, size uint32) ([]byte, error) {
select {
case <-ctx.Done():
return nil, errTimeout
default:
}
buf := bytes.NewBuffer(nil)
_, err := io.CopyN(buf, mw.conn, int64(size))
if err != nil {
return nil, fmt.Errorf("read %d bytes message failed: %v", size, err)
}
return buf.Bytes(), nil
}
func (mw *metaWire) write(ctx context.Context, data []byte) error {
select {
case <-ctx.Done():
return errTimeout
default:
}
buf := bytes.NewBuffer(nil)
length := int32(len(data))
binary.Write(buf, binary.BigEndian, length)
buf.Write(data)
_, err := mw.conn.Write(buf.Bytes())
if err != nil {
return fmt.Errorf("write message failed: %v", err)
}
return nil
}
func (mw *metaWire) free() {
metaWirePool.Put(mw)
}