This repository has been archived by the owner on Jan 21, 2022. It is now read-only.
/
leech.go
482 lines (401 loc) · 11.4 KB
/
leech.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
package metadata
import (
"bytes"
"crypto/sha1"
"encoding/binary"
"fmt"
"io"
"math"
"net"
"time"
"github.com/anacrolix/torrent/bencode"
"github.com/anacrolix/torrent/metainfo"
"github.com/pkg/errors"
"go.uber.org/zap"
"github.com/boramalper/magnetico/pkg/persistence"
)
const MAX_METADATA_SIZE = 10 * 1024 * 1024
type rootDict struct {
M mDict `bencode:"m"`
MetadataSize int `bencode:"metadata_size"`
}
type mDict struct {
UTMetadata int `bencode:"ut_metadata"`
}
type extDict struct {
MsgType int `bencode:"msg_type"`
Piece int `bencode:"piece"`
}
type Leech struct {
infoHash [20]byte
peerAddr *net.TCPAddr
ev LeechEventHandlers
conn *net.TCPConn
clientID [20]byte
ut_metadata uint8
metadataReceived, metadataSize uint
metadata []byte
connClosed bool
}
type LeechEventHandlers struct {
OnSuccess func(Metadata) // must be supplied. args: metadata
OnError func([20]byte, error) // must be supplied. args: infohash, error
}
func NewLeech(infoHash [20]byte, peerAddr *net.TCPAddr, clientID []byte, ev LeechEventHandlers) *Leech {
l := new(Leech)
l.infoHash = infoHash
l.peerAddr = peerAddr
copy(l.clientID[:], clientID)
l.ev = ev
return l
}
func (l *Leech) writeAll(b []byte) error {
for len(b) != 0 {
n, err := l.conn.Write(b)
if err != nil {
return err
}
b = b[n:]
}
return nil
}
func (l *Leech) doBtHandshake() error {
lHandshake := []byte(fmt.Sprintf(
"\x13BitTorrent protocol\x00\x00\x00\x00\x00\x10\x00\x01%s%s",
l.infoHash,
l.clientID,
))
// ASSERTION
if len(lHandshake) != 68 {
panic(fmt.Sprintf("len(lHandshake) == %d", len(lHandshake)))
}
err := l.writeAll(lHandshake)
if err != nil {
return errors.Wrap(err, "writeAll lHandshake")
}
rHandshake, err := l.readExactly(68)
if err != nil {
return errors.Wrap(err, "readExactly rHandshake")
}
if !bytes.HasPrefix(rHandshake, []byte("\x13BitTorrent protocol")) {
return fmt.Errorf("corrupt BitTorrent handshake received")
}
// TODO: maybe check for the infohash sent by the remote peer to double check?
if (rHandshake[25] & 0x10) == 0 {
return fmt.Errorf("peer does not support the extension protocol")
}
return nil
}
func (l *Leech) doExHandshake() error {
err := l.writeAll([]byte("\x00\x00\x00\x1a\x14\x00d1:md11:ut_metadatai1eee"))
if err != nil {
return errors.Wrap(err, "writeAll lHandshake")
}
rExMessage, err := l.readExMessage()
if err != nil {
return errors.Wrap(err, "readExMessage")
}
// Extension Handshake has the Extension Message ID = 0x00
if rExMessage[1] != 0 {
return errors.Wrap(err, "first extension message is not an extension handshake")
}
rRootDict := new(rootDict)
err = bencode.Unmarshal(rExMessage[2:], rRootDict)
if err != nil {
return errors.Wrap(err, "unmarshal rExMessage")
}
if !(0 < rRootDict.MetadataSize && rRootDict.MetadataSize < MAX_METADATA_SIZE) {
return fmt.Errorf("metadata too big or its size is less than or equal zero")
}
if !(0 < rRootDict.M.UTMetadata && rRootDict.M.UTMetadata < 255) {
return fmt.Errorf("ut_metadata is not an uint8")
}
l.ut_metadata = uint8(rRootDict.M.UTMetadata) // Save the ut_metadata code the remote peer uses
l.metadataSize = uint(rRootDict.MetadataSize)
l.metadata = make([]byte, l.metadataSize)
return nil
}
func (l *Leech) requestAllPieces() error {
// Request all the pieces of metadata
nPieces := int(math.Ceil(float64(l.metadataSize) / math.Pow(2, 14)))
for piece := 0; piece < nPieces; piece++ {
// __request_metadata_piece(piece)
// ...............................
extDictDump, err := bencode.Marshal(extDict{
MsgType: 0,
Piece: piece,
})
if err != nil { // ASSERT
panic(errors.Wrap(err, "marshal extDict"))
}
err = l.writeAll([]byte(fmt.Sprintf(
"%s\x14%s%s",
toBigEndian(uint(2+len(extDictDump)), 4),
toBigEndian(uint(l.ut_metadata), 1),
extDictDump,
)))
if err != nil {
return errors.Wrap(err, "writeAll piece request")
}
}
return nil
}
// readMessage returns a BitTorrent message, sans the first 4 bytes indicating its length.
func (l *Leech) readMessage() ([]byte, error) {
rLengthB, err := l.readExactly(4)
if err != nil {
return nil, errors.Wrap(err, "readExactly rLengthB")
}
rLength := uint(binary.BigEndian.Uint32(rLengthB))
// Some malicious/faulty peers say that they are sending a very long
// message, and hence causing us to run out of memory.
// This is a crude check that does not let it happen (i.e. boundary can probably be
// tightened a lot more.)
if rLength > MAX_METADATA_SIZE {
return nil, errors.New("message is longer than max allowed metadata size")
}
rMessage, err := l.readExactly(rLength)
if err != nil {
return nil, errors.Wrap(err, "readExactly rMessage")
}
return rMessage, nil
}
// readExMessage returns an *extension* message, sans the first 4 bytes indicating its length.
//
// It will IGNORE all non-extension messages!
func (l *Leech) readExMessage() ([]byte, error) {
for {
rMessage, err := l.readMessage()
if err != nil {
return nil, errors.Wrap(err, "readMessage")
}
// Every extension message has at least 2 bytes.
if len(rMessage) < 2 {
continue
}
// We are interested only in extension messages, whose first byte is always 20
if rMessage[0] == 20 {
return rMessage, nil
}
}
}
// readUmMessage returns an ut_metadata extension message, sans the first 4 bytes indicating its
// length.
//
// It will IGNORE all non-"ut_metadata extension" messages!
func (l *Leech) readUmMessage() ([]byte, error) {
for {
rExMessage, err := l.readExMessage()
if err != nil {
return nil, errors.Wrap(err, "readExMessage")
}
if rExMessage[1] == 0x01 {
return rExMessage, nil
}
}
}
func (l *Leech) connect(deadline time.Time) error {
var err error
x, err := net.DialTimeout("tcp4", l.peerAddr.String(), 1*time.Second)
if err != nil {
return errors.Wrap(err, "dial")
}
l.conn = x.(*net.TCPConn)
// > If sec == 0, operating system discards any unsent or unacknowledged data [after Close()
// > has been called].
err = l.conn.SetLinger(0)
if err != nil {
if err := l.conn.Close(); err != nil {
zap.L().Panic("couldn't close leech connection!", zap.Error(err))
}
return errors.Wrap(err, "SetLinger")
}
err = l.conn.SetNoDelay(true)
if err != nil {
if err := l.conn.Close(); err != nil {
zap.L().Panic("couldn't close leech connection!", zap.Error(err))
}
return errors.Wrap(err, "NODELAY")
}
err = l.conn.SetDeadline(deadline)
if err != nil {
if err := l.conn.Close(); err != nil {
zap.L().Panic("couldn't close leech connection!", zap.Error(err))
}
return errors.Wrap(err, "SetDeadline")
}
return nil
}
func (l *Leech) closeConn() {
if l.connClosed {
return
}
if err := l.conn.Close(); err != nil {
zap.L().Panic("couldn't close leech connection!", zap.Error(err))
return
}
l.connClosed = true
}
func (l *Leech) Do(deadline time.Time) {
err := l.connect(deadline)
if err != nil {
l.OnError(errors.Wrap(err, "connect"))
return
}
defer l.closeConn()
err = l.doBtHandshake()
if err != nil {
l.OnError(errors.Wrap(err, "doBtHandshake"))
return
}
err = l.doExHandshake()
if err != nil {
l.OnError(errors.Wrap(err, "doExHandshake"))
return
}
err = l.requestAllPieces()
if err != nil {
l.OnError(errors.Wrap(err, "requestAllPieces"))
return
}
for l.metadataReceived < l.metadataSize {
rUmMessage, err := l.readUmMessage()
if err != nil {
l.OnError(errors.Wrap(err, "readUmMessage"))
return
}
// Run TestDecoder() function in leech_test.go in case you have any doubts.
rMessageBuf := bytes.NewBuffer(rUmMessage[2:])
rExtDict := new(extDict)
err = bencode.NewDecoder(rMessageBuf).Decode(rExtDict)
if err != nil {
l.OnError(errors.Wrap(err, "could not decode ext msg in the loop"))
return
}
if rExtDict.MsgType == 2 { // reject
l.OnError(fmt.Errorf("remote peer rejected sending metadata"))
return
}
if rExtDict.MsgType == 1 { // data
// Get the unread bytes!
metadataPiece := rMessageBuf.Bytes()
// BEP 9 explicitly states:
// > If the piece is the last piece of the metadata, it may be less than 16kiB. If
// > it is not the last piece of the metadata, it MUST be 16kiB.
//
// Hence...
// ... if the length of @metadataPiece is more than 16kiB, we err.
if len(metadataPiece) > 16*1024 {
l.OnError(fmt.Errorf("metadataPiece > 16kiB"))
return
}
piece := rExtDict.Piece
// metadata[piece * 2**14: piece * 2**14 + len(metadataPiece)] = metadataPiece is how it'd be done in Python
copy(l.metadata[piece*int(math.Pow(2, 14)):piece*int(math.Pow(2, 14))+len(metadataPiece)], metadataPiece)
l.metadataReceived += uint(len(metadataPiece))
// ... if the length of @metadataPiece is less than 16kiB AND metadata is NOT
// complete then we err.
if len(metadataPiece) < 16*1024 && l.metadataReceived != l.metadataSize {
l.OnError(fmt.Errorf("metadataPiece < 16 kiB but incomplete"))
return
}
if l.metadataReceived > l.metadataSize {
l.OnError(fmt.Errorf("metadataReceived > metadataSize"))
return
}
}
}
// We are done with the transfer, close socket as soon as possible (i.e. NOW) to avoid hitting "too many open files"
// error.
l.closeConn()
// Verify the checksum
sha1Sum := sha1.Sum(l.metadata)
if !bytes.Equal(sha1Sum[:], l.infoHash[:]) {
l.OnError(fmt.Errorf("infohash mismatch"))
return
}
// Check the info dictionary
info := new(metainfo.Info)
err = bencode.Unmarshal(l.metadata, info)
if err != nil {
l.OnError(errors.Wrap(err, "unmarshal info"))
return
}
err = validateInfo(info)
if err != nil {
l.OnError(errors.Wrap(err, "validateInfo"))
return
}
var files []persistence.File
// If there is only one file, there won't be a Files slice. That's why we need to add it here
if len(info.Files) == 0 {
files = append(files, persistence.File{
Size: info.Length,
Path: info.Name,
})
} else {
for _, file := range info.Files {
files = append(files, persistence.File{
Size: file.Length,
Path: file.DisplayPath(info),
})
}
}
var totalSize uint64
for _, file := range files {
if file.Size < 0 {
l.OnError(fmt.Errorf("file size less than zero"))
return
}
totalSize += uint64(file.Size)
}
l.ev.OnSuccess(Metadata{
InfoHash: l.infoHash[:],
Name: info.Name,
TotalSize: totalSize,
DiscoveredOn: time.Now().Unix(),
Files: files,
})
}
// COPIED FROM anacrolix/torrent
func validateInfo(info *metainfo.Info) error {
if len(info.Pieces)%20 != 0 {
return errors.New("pieces has invalid length")
}
if info.PieceLength == 0 {
if info.TotalLength() != 0 {
return errors.New("zero piece length")
}
} else {
if int((info.TotalLength()+info.PieceLength-1)/info.PieceLength) != info.NumPieces() {
return errors.New("piece count and file lengths are at odds")
}
}
return nil
}
func (l *Leech) readExactly(n uint) ([]byte, error) {
b := make([]byte, n)
_, err := io.ReadFull(l.conn, b)
return b, err
}
func (l *Leech) OnError(err error) {
l.ev.OnError(l.infoHash, err)
}
// TODO: add bounds checking!
func toBigEndian(i uint, n int) []byte {
b := make([]byte, n)
switch n {
case 1:
b = []byte{byte(i)}
case 2:
binary.BigEndian.PutUint16(b, uint16(i))
case 4:
binary.BigEndian.PutUint32(b, uint32(i))
default:
panic("n must be 1, 2, or 4!")
}
if len(b) != n {
panic(fmt.Sprintf("postcondition failed: len(b) != n in intToBigEndian (i %d, n %d, len b %d, b %s)", i, n, len(b), b))
}
return b
}