-
Notifications
You must be signed in to change notification settings - Fork 123
/
torrent.go
1360 lines (1227 loc) · 36.8 KB
/
torrent.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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package torrent
import (
"bytes"
"crypto/sha1"
"encoding/hex"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"math"
"math/rand"
"net"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"time"
bencode "github.com/jackpal/bencode-go"
"github.com/nictuku/dht"
"github.com/nictuku/nettools"
)
const (
MAX_NUM_PEERS = 60
TARGET_NUM_PEERS = 15
)
// BitTorrent message types. Sources:
// http://bittorrent.org/beps/bep_0003.html
// http://wiki.theory.org/BitTorrentSpecification
const (
CHOKE = iota
UNCHOKE
INTERESTED
NOT_INTERESTED
HAVE
BITFIELD
REQUEST
PIECE
CANCEL
PORT // Not implemented. For DHT support.
EXTENSION = 20
)
const (
EXTENSION_HANDSHAKE = iota
)
const (
METADATA_REQUEST = iota
METADATA_DATA
METADATA_REJECT
)
func peerID() string {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
sid := "-tt" + strconv.Itoa(os.Getpid()) + "_" + strconv.FormatInt(r.Int63(), 10)
return sid[0:20]
}
var kBitTorrentHeader = []byte{'\x13', 'B', 'i', 't', 'T', 'o', 'r',
'r', 'e', 'n', 't', ' ', 'p', 'r', 'o', 't', 'o', 'c', 'o', 'l'}
type ActivePiece struct {
downloaderCount []int // -1 means piece is already downloaded
buffer []byte
}
func (a *ActivePiece) chooseBlockToDownload(endgame bool) (index int) {
if endgame {
return a.chooseBlockToDownloadEndgame()
}
return a.chooseBlockToDownloadNormal()
}
func (a *ActivePiece) chooseBlockToDownloadNormal() (index int) {
for i, v := range a.downloaderCount {
if v == 0 {
a.downloaderCount[i]++
return i
}
}
return -1
}
func (a *ActivePiece) chooseBlockToDownloadEndgame() (index int) {
index, minCount := -1, -1
for i, v := range a.downloaderCount {
if v >= 0 && (minCount == -1 || minCount > v) {
index, minCount = i, v
}
}
if index > -1 {
a.downloaderCount[index]++
}
return
}
func (a *ActivePiece) recordBlock(index int) (requestCount int) {
requestCount = a.downloaderCount[index]
a.downloaderCount[index] = -1
return
}
func (a *ActivePiece) isComplete() bool {
for _, v := range a.downloaderCount {
if v != -1 {
return false
}
}
return true
}
type TorrentSession struct {
flags *TorrentFlags
M *MetaInfo
Session SessionInfo
ti *TrackerResponse
torrentHeader []byte
fileStore FileStore
trackerReportChan chan ClientStatusReport
trackerInfoChan chan *TrackerResponse
hintNewPeerChan chan string
addPeerChan chan *BtConn
peers map[string]*peerState
peerMessageChan chan peerMessage
pieceSet *Bitset // The pieces we have
totalPieces int
totalSize int64
lastPieceLength int
goodPieces int
activePieces map[int]*ActivePiece
maxActivePieces int
heartbeat chan bool
dht *dht.DHT
quit chan bool
ended chan bool
trackerLessMode bool
torrentFile string
chokePolicy ChokePolicy
chokePolicyHeartbeat <-chan time.Time
execOnSeedingDone bool
}
func NewTorrentSession(flags *TorrentFlags, torrent string, listenPort uint16) (t *TorrentSession, err error) {
ts := &TorrentSession{
flags: flags,
peers: make(map[string]*peerState),
peerMessageChan: make(chan peerMessage),
activePieces: make(map[int]*ActivePiece),
quit: make(chan bool),
ended: make(chan bool),
torrentFile: torrent,
chokePolicy: &ClassicChokePolicy{},
chokePolicyHeartbeat: time.Tick(10 * time.Second),
execOnSeedingDone: len(flags.ExecOnSeeding) == 0,
}
fromMagnet := strings.HasPrefix(torrent, "magnet:")
ts.M, err = GetMetaInfo(flags.Dial, torrent)
if err != nil {
return
}
if ts.M.Announce == "" && len(ts.M.AnnounceList) == 0 {
ts.trackerLessMode = true
} else {
ts.trackerLessMode = ts.flags.TrackerlessMode
}
dhtAllowed := flags.UseDHT && ts.M.Info.Private == 0
if flags.UseDHT && !dhtAllowed {
log.Println("[", ts.M.Info.Name, "] Can't use DHT because torrent is marked Private")
}
ts.Session = SessionInfo{
PeerID: peerID(),
Port: listenPort,
UseDHT: dhtAllowed,
FromMagnet: fromMagnet,
HaveTorrent: false,
ME: &MetaDataExchange{},
OurExtensions: map[int]string{1: "ut_metadata"},
OurAddresses: map[string]bool{"127.0.0.1:" + strconv.Itoa(int(listenPort)): true},
}
ts.setHeader()
if !ts.Session.FromMagnet {
err = ts.load()
}
return ts, err
}
func (ts *TorrentSession) reload(metadata string) (err error) {
var info InfoDict
err = bencode.Unmarshal(bytes.NewReader([]byte(metadata)), &info)
if err != nil {
log.Println("[", ts.M.Info.Name, "] Error when reloading torrent: ", err)
return
}
ts.M.Info = info
err = ts.load()
if ts.flags.Cacher != nil && ts.fileStore != nil {
ts.fileStore = ts.flags.Cacher.NewCache(ts.M.InfoHash, ts.totalPieces, ts.M.Info.PieceLength, ts.totalSize, ts.fileStore)
}
return
}
func (ts *TorrentSession) load() (err error) {
log.Printf("[ %s ] Tracker: %v, Comment: %v, InfoHash: %x, Encoding: %v, Private: %v",
ts.M.Info.Name, ts.M.AnnounceList, ts.M.Comment, ts.M.InfoHash, ts.M.Encoding, ts.M.Info.Private)
if e := ts.M.Encoding; e != "" && e != "UTF-8" {
err = fmt.Errorf("Unknown encoding %v", e)
return
}
ext := ".torrent"
dir := ts.flags.FileDir
if len(ts.M.Info.Files) != 0 {
torrentName := ts.M.Info.Name
if torrentName == "" {
torrentName = filepath.Base(ts.torrentFile)
}
// canonicalize the torrent path and make sure it doesn't start with ".."
torrentName = path.Clean("/" + torrentName)
dir += torrentName
//Remove ".torrent" extension if present
if strings.HasSuffix(strings.ToLower(dir), ext) {
dir = dir[:len(dir)-len(ext)]
}
}
var fileSystem FileSystem
fileSystem, err = ts.flags.FileSystemProvider.NewFS(dir)
if err != nil {
return
}
ts.fileStore, ts.totalSize, err = NewFileStore(&ts.M.Info, fileSystem)
if err != nil {
return
}
if ts.M.Info.PieceLength == 0 {
err = fmt.Errorf("Bad PieceLength: %v", ts.M.Info.PieceLength)
return
}
ts.totalPieces = int(ts.totalSize / ts.M.Info.PieceLength)
ts.lastPieceLength = int(ts.totalSize % ts.M.Info.PieceLength)
if ts.lastPieceLength == 0 { // last piece is a full piece
ts.lastPieceLength = int(ts.M.Info.PieceLength)
} else {
ts.totalPieces++
}
if ts.flags.MemoryPerTorrent < 0 {
ts.maxActivePieces = 2147483640
log.Printf("[ %s ] Max Active Pieces set to Unlimited\n", ts.M.Info.Name)
} else {
ts.maxActivePieces = int(int64(ts.flags.MemoryPerTorrent*1024*1024) / ts.M.Info.PieceLength)
if ts.maxActivePieces == 0 {
ts.maxActivePieces++
}
log.Printf("[ %s ] Max Active Pieces set to %v\n", ts.M.Info.Name, ts.maxActivePieces)
}
ts.goodPieces = 0
if ts.flags.InitialCheck {
start := time.Now()
ts.goodPieces, _, ts.pieceSet, err = checkPieces(ts.fileStore, ts.totalSize, ts.M)
end := time.Now()
log.Printf("[ %s ] Computed missing pieces (%.2f seconds)\n", ts.M.Info.Name, end.Sub(start).Seconds())
if err != nil {
return
}
} else if ts.flags.QuickResume {
resumeFilePath := "./" + hex.EncodeToString([]byte(ts.M.InfoHash)) + "-haveBitset"
if resumeFile, err := os.Open(resumeFilePath); err == nil {
rfstat, _ := resumeFile.Stat()
tBA := make([]byte, 2*rfstat.Size())
count, _ := resumeFile.Read(tBA)
ts.pieceSet = NewBitsetFromBytes(ts.totalPieces, tBA[:count])
if ts.pieceSet == nil {
return fmt.Errorf("[ %s ] Malformed resume data: %v", ts.M.Info.Name, resumeFilePath)
}
for i := 0; i < ts.totalPieces; i++ {
if ts.pieceSet.IsSet(i) {
ts.goodPieces++
}
}
log.Printf("[ %s ] Got piece list from haveBitset file.\n", ts.M.Info.Name)
} else {
log.Printf("[ %s ] Couldn't open haveBitset file: %v", ts.M.Info.Name, err)
}
}
if ts.pieceSet == nil { //Blank slate it is then.
ts.pieceSet = NewBitset(ts.totalPieces)
log.Printf("[ %s ] Starting from scratch.\n", ts.M.Info.Name)
}
bad := ts.totalPieces - ts.goodPieces
left := uint64(bad) * uint64(ts.M.Info.PieceLength)
if !ts.pieceSet.IsSet(ts.totalPieces - 1) {
left = left - uint64(ts.M.Info.PieceLength) + uint64(ts.lastPieceLength)
}
ts.Session.Left = left
log.Println("[", ts.M.Info.Name, "] Good pieces:", ts.goodPieces, "Bad pieces:", bad, "Bytes left:", left)
// Enlarge any existing peers piece maps
for _, p := range ts.peers {
if p.have.n != ts.totalPieces {
if p.have.n != 0 {
panic("Expected p.have.n == 0")
}
p.have = NewBitset(ts.totalPieces)
}
}
ts.Session.HaveTorrent = true
return
}
func (ts *TorrentSession) pieceLength(piece int) int {
if piece < ts.totalPieces-1 {
return int(ts.M.Info.PieceLength)
}
return ts.lastPieceLength
}
func (ts *TorrentSession) fetchTrackerInfo(event string) {
m, si := ts.M, ts.Session
log.Println("[", ts.M.Info.Name, "] Stats: Uploaded", si.Uploaded, "Downloaded", si.Downloaded, "Left", si.Left)
ts.trackerReportChan <- ClientStatusReport{
event, m.InfoHash, si.PeerID, si.Port, si.Uploaded, si.Downloaded, si.Left}
}
func (ts *TorrentSession) setHeader() {
header := make([]byte, 68)
copy(header, kBitTorrentHeader[0:])
if ts.Session.UseDHT {
header[27] = header[27] | 0x01
}
// Support Extension Protocol (BEP-0010)
header[25] |= 0x10
copy(header[28:48], []byte(ts.M.InfoHash))
copy(header[48:68], []byte(ts.Session.PeerID))
ts.torrentHeader = header
}
func (ts *TorrentSession) Header() (header []byte) {
return ts.torrentHeader
}
// Try to connect if the peer is not already in our peers.
// Can be called from any goroutine.
func (ts *TorrentSession) HintNewPeer(peer string) {
if len(ts.hintNewPeerChan) < cap(ts.hintNewPeerChan) { //We don't want to block the main loop because a single torrent is having problems
select {
case ts.hintNewPeerChan <- peer:
case <-ts.ended:
}
} else {
// log.Println("[", ts.M.Info.Name, "] New peer hint failed, because DoTorrent() hasn't been clearing out the channel.")
}
}
func (ts *TorrentSession) tryNewPeer(peer string) bool {
if (ts.Session.HaveTorrent || ts.Session.FromMagnet) && len(ts.peers) < MAX_NUM_PEERS {
if _, ok := ts.Session.OurAddresses[peer]; !ok {
if _, ok := ts.peers[peer]; !ok {
go ts.connectToPeer(peer)
return true
}
} else {
// log.Println("[", ts.M.Info.Name, "] New peer hint rejected, because it's one of our addresses (", peer, ")")
}
}
return false
}
func (ts *TorrentSession) connectToPeer(peer string) {
conn, err := proxyNetDial(ts.flags.Dial, "tcp", peer)
if err != nil {
// log.Println("[", ts.M.Info.Name, "] Failed to connect to", peer, err)
return
}
_, err = conn.Write(ts.Header())
if err != nil {
log.Println("[", ts.M.Info.Name, "] Failed to send header to", peer, err)
return
}
theirheader, err := readHeader(conn)
if err != nil {
return
}
peersInfoHash := string(theirheader[8:28])
id := string(theirheader[28:48])
btconn := &BtConn{
header: theirheader,
Infohash: peersInfoHash,
id: id,
conn: conn,
}
// log.Println("[", ts.M.Info.Name, "] Connected to", peer)
ts.AddPeer(btconn)
}
func (ts *TorrentSession) AcceptNewPeer(btconn *BtConn) {
_, err := btconn.conn.Write(ts.Header())
if err != nil {
return
}
ts.AddPeer(btconn)
}
// Can be called from any goroutine
func (ts *TorrentSession) AddPeer(btconn *BtConn) {
if len(ts.addPeerChan) < cap(ts.addPeerChan) { //We don't want to block the main loop because a single torrent is having problems
select {
case ts.addPeerChan <- btconn:
case <-ts.ended:
}
} else {
// log.Println("[", ts.M.Info.Name, "] Add peer failed, because DoTorrent() hasn't been clearing out the channel.")
btconn.conn.Close()
}
}
func (ts *TorrentSession) addPeerImp(btconn *BtConn) {
if !ts.Session.HaveTorrent && !ts.Session.FromMagnet {
log.Println("[", ts.M.Info.Name, "] Rejecting peer because we don't have a torrent yet")
btconn.conn.Close()
return
}
peer := btconn.conn.RemoteAddr().String()
if btconn.id == ts.Session.PeerID {
log.Println("[", ts.M.Info.Name, "] Rejecting self-connection:", peer, "<->", btconn.conn.LocalAddr())
ts.Session.OurAddresses[btconn.conn.LocalAddr().String()] = true
ts.Session.OurAddresses[peer] = true
btconn.conn.Close()
return
}
for _, p := range ts.peers {
if p.id == btconn.id {
log.Println("[", ts.M.Info.Name, "] Rejecting peer because already have a peer with the same id")
btconn.conn.Close()
return
}
}
// log.Println("[", ts.M.Info.Name, "] Adding peer", peer)
if len(ts.peers) >= MAX_NUM_PEERS {
log.Println("[", ts.M.Info.Name, "] We have enough peers. Rejecting additional peer", peer)
btconn.conn.Close()
return
}
theirheader := btconn.header
if ts.Session.UseDHT {
// If 128, then it supports DHT.
if int(theirheader[7])&0x01 == 0x01 {
// It's OK if we know this node already. The DHT engine will
// ignore it accordingly.
go ts.dht.AddNode(peer)
}
}
ps := NewPeerState(btconn.conn)
ps.address = peer
ps.id = btconn.id
// By default, a peer has no pieces. If it has pieces, it should send
// a BITFIELD message as a first message
// If the torrent has not been received yet, ts.totalPieces will be 0, and
// the "have" map will have to be enlarged later when ts.totalPieces is
// learned.
ps.have = NewBitset(ts.totalPieces)
ts.peers[peer] = ps
go ps.peerWriter(ts.peerMessageChan)
go ps.peerReader(ts.peerMessageChan)
if int(theirheader[5])&0x10 == 0x10 {
ps.SendExtensions(ts.Session.Port)
} else if ts.pieceSet != nil {
ps.SendBitfield(ts.pieceSet)
}
}
func (ts *TorrentSession) ClosePeer(peer *peerState) {
if ts.Session.ME != nil && !ts.Session.ME.Transferring {
ts.Session.ME.Transferring = false
}
//log.Println("[", ts.M.Info.Name, "] Closing peer", peer.address)
_ = ts.removeRequests(peer)
peer.Close()
delete(ts.peers, peer.address)
}
func (ts *TorrentSession) deadlockDetector() {
// Wait for a heartbeat before we start deadlock detection.
// This handle the case where it takes a long time to find
// a tracker.
<-ts.heartbeat
lastHeartbeat := time.Now()
for {
select {
case <-ts.heartbeat:
lastHeartbeat = time.Now()
case <-time.After(15 * time.Second):
age := time.Now().Sub(lastHeartbeat)
log.Println("[", ts.M.Info.Name, "] Starvation or deadlock of main thread detected. Look in the stack dump for what DoTorrent() is currently doing")
log.Println("[", ts.M.Info.Name, "] Last heartbeat", age.Seconds(), "seconds ago")
panic("[" + ts.M.Info.Name + "] Killed by deadlock detector")
}
}
}
func (ts *TorrentSession) Quit() (err error) {
select {
case ts.quit <- true:
case <-ts.ended:
}
return
}
func (ts *TorrentSession) Shutdown() (err error) {
close(ts.ended)
if ts.fileStore != nil {
err = ts.fileStore.Close()
if err != nil {
log.Println("[", ts.M.Info.Name, "] Error closing filestore:", err)
}
}
for _, peer := range ts.peers {
peer.Close()
}
return
}
func (ts *TorrentSession) DoTorrent() {
ts.heartbeat = make(chan bool, 1)
if ts.flags.UseDeadlockDetector {
go ts.deadlockDetector()
}
if ts.flags.Cacher != nil && ts.fileStore != nil {
ts.fileStore = ts.flags.Cacher.NewCache(ts.M.InfoHash, ts.totalPieces, ts.M.Info.PieceLength, ts.totalSize, ts.fileStore)
}
heartbeatDuration := 1 * time.Second
heartbeatChan := time.Tick(heartbeatDuration)
keepAliveChan := time.Tick(60 * time.Second)
var retrackerChan <-chan time.Time
ts.hintNewPeerChan = make(chan string, MAX_NUM_PEERS)
ts.addPeerChan = make(chan *BtConn, MAX_NUM_PEERS)
if !ts.trackerLessMode {
// Start out polling tracker every 20 seconds until we get a response.
// Maybe be exponential backoff here?
retrackerChan = time.Tick(20 * time.Second)
ts.trackerInfoChan = make(chan *TrackerResponse)
ts.trackerReportChan = make(chan ClientStatusReport)
startTrackerClient(ts.flags.Dial, ts.M.Announce, ts.M.AnnounceList, ts.trackerInfoChan, ts.trackerReportChan)
}
if ts.Session.UseDHT {
ts.dht.PeersRequest(ts.M.InfoHash, true)
}
if !ts.trackerLessMode && ts.Session.HaveTorrent {
ts.fetchTrackerInfo("started")
}
defer ts.Shutdown()
lastDownloaded := ts.Session.Downloaded
for {
if !ts.execOnSeedingDone && ts.goodPieces == ts.totalPieces {
ts.execOnSeeding()
ts.execOnSeedingDone = true
}
select {
case <-ts.chokePolicyHeartbeat:
ts.chokePeers()
case hintNewPeer := <-ts.hintNewPeerChan:
ts.tryNewPeer(hintNewPeer)
case btconn := <-ts.addPeerChan:
ts.addPeerImp(btconn)
case <-retrackerChan:
if !ts.trackerLessMode {
ts.fetchTrackerInfo("")
}
case ti := <-ts.trackerInfoChan:
ts.ti = ti
log.Println("[", ts.M.Info.Name, "] Torrent has", ts.ti.Complete, "seeders and", ts.ti.Incomplete, "leachers")
if !ts.trackerLessMode {
newPeerCount := 0
{
peers := ts.ti.Peers
if len(peers) > 0 {
const peerLen = 6
log.Println("[", ts.M.Info.Name, "] Tracker gave us", len(peers)/peerLen, "peers")
for i := 0; i < len(peers); i += peerLen {
peer := nettools.BinaryToDottedPort(peers[i : i+peerLen])
if ts.tryNewPeer(peer) {
newPeerCount++
}
}
}
}
{
peers6 := ts.ti.Peers6
if len(peers6) > 0 {
const peerLen = 18
log.Println("[", ts.M.Info.Name, "] Tracker gave us", len(peers6)/peerLen, "IPv6 peers")
for i := 0; i < len(peers6); i += peerLen {
peerEntry := peers6[i : i+peerLen]
host := net.IP(peerEntry[0:16])
port := int((uint(peerEntry[16]) << 8) | uint(peerEntry[17]))
peer := net.JoinHostPort(host.String(), strconv.Itoa(port))
if ts.tryNewPeer(peer) {
newPeerCount++
}
}
}
}
log.Println("[", ts.M.Info.Name, "] Contacting", newPeerCount, "new peers")
}
interval := ts.ti.Interval
minInterval := uint(120)
maxInterval := uint(24 * 3600)
if interval < minInterval {
interval = minInterval
} else if interval > maxInterval {
interval = maxInterval
}
log.Println("[", ts.M.Info.Name, "] ..checking again in", interval, "seconds")
retrackerChan = time.Tick(time.Duration(interval) * time.Second)
case pm := <-ts.peerMessageChan:
peer, message := pm.peer, pm.message
peer.lastReadTime = time.Now()
err2 := ts.DoMessage(peer, message)
if err2 != nil {
if err2 != io.EOF {
log.Println("[", ts.M.Info.Name, "] Closing peer", peer.address, "because", err2)
}
ts.ClosePeer(peer)
}
case <-heartbeatChan:
if ts.flags.UseDeadlockDetector {
ts.heartbeat <- true
}
ratio := float64(0.0)
if ts.Session.Downloaded > 0 {
ratio = float64(ts.Session.Uploaded) / float64(ts.Session.Downloaded)
}
speed := humanSize(float64(ts.Session.Downloaded-lastDownloaded) / heartbeatDuration.Seconds())
lastDownloaded = ts.Session.Downloaded
log.Printf("[ %s ] Peers: %d downloaded: %d (%s/s) uploaded: %d ratio: %f pieces: %d/%d\n",
ts.M.Info.Name,
len(ts.peers),
ts.Session.Downloaded,
speed,
ts.Session.Uploaded,
ratio,
ts.goodPieces,
ts.totalPieces)
if ts.totalPieces != 0 && ts.goodPieces == ts.totalPieces && ratio >= ts.flags.SeedRatio {
log.Println("[", ts.M.Info.Name, "] Achieved target seed ratio", ts.flags.SeedRatio)
return
}
if len(ts.peers) < TARGET_NUM_PEERS && (ts.totalPieces == 0 || ts.goodPieces < ts.totalPieces) {
if ts.Session.UseDHT {
go ts.dht.PeersRequest(ts.M.InfoHash, true)
}
if !ts.trackerLessMode {
if ts.ti == nil || ts.ti.Complete > 100 {
ts.fetchTrackerInfo("")
}
}
}
case <-keepAliveChan:
now := time.Now()
for _, peer := range ts.peers {
if peer.lastReadTime.Second() != 0 && now.Sub(peer.lastReadTime) > 3*time.Minute {
// log.Println("[", ts.M.Info.Name, "] Closing peer", peer.address, "because timed out")
ts.ClosePeer(peer)
continue
}
err2 := ts.doCheckRequests(peer)
if err2 != nil {
if err2 != io.EOF {
log.Println("[", ts.M.Info.Name, "] Closing peer", peer.address, "because", err2)
}
ts.ClosePeer(peer)
continue
}
peer.keepAlive(now)
}
case <-ts.quit:
log.Println("[", ts.M.Info.Name, "] Quitting torrent session")
ts.fetchTrackerInfo("stopped")
time.Sleep(10 * time.Millisecond)
return
}
}
}
func (ts *TorrentSession) chokePeers() (err error) {
// log.Printf("[ %s ] Choking peers", ts.M.Info.Name)
peers := ts.peers
chokers := make([]Choker, 0, len(peers))
for _, peer := range peers {
if peer.peer_interested {
peer.computeDownloadRate()
// log.Printf("%s %g bps", peer.address, peer.DownloadBPS())
chokers = append(chokers, Choker(peer))
}
}
var unchokeCount int
unchokeCount, err = ts.chokePolicy.Choke(chokers)
if err != nil {
return
}
for i, c := range chokers {
shouldChoke := i >= unchokeCount
if peer, ok := c.(*peerState); ok {
if shouldChoke != peer.am_choking {
// log.Printf("[ %s ] Changing choke status %v -> %v", ts.M.Info.Name, peer.address, shouldChoke)
peer.SetChoke(shouldChoke)
}
}
}
return
}
func (ts *TorrentSession) RequestBlock(p *peerState) error {
if !ts.Session.HaveTorrent { // We can't request a block without a torrent
return nil
}
for k := range ts.activePieces {
if p.have.IsSet(k) {
err := ts.RequestBlock2(p, k, false)
if err != io.EOF {
return err
}
}
}
if len(ts.activePieces) >= ts.maxActivePieces {
return nil
}
// No active pieces. (Or no suitable active pieces.) Pick one
piece := ts.ChoosePiece(p)
if piece < 0 {
// No unclaimed pieces. See if we can double-up on an active piece
for k := range ts.activePieces {
if p.have.IsSet(k) {
err := ts.RequestBlock2(p, k, true)
if err != io.EOF {
return err
}
}
}
}
if piece < 0 {
p.SetInterested(false)
return nil
}
pieceLength := ts.pieceLength(piece)
pieceCount := (pieceLength + STANDARD_BLOCK_LENGTH - 1) / STANDARD_BLOCK_LENGTH
ts.activePieces[piece] = &ActivePiece{make([]int, pieceCount), make([]byte, pieceLength)}
return ts.RequestBlock2(p, piece, false)
}
func (ts *TorrentSession) ChoosePiece(p *peerState) (piece int) {
n := ts.totalPieces
start := rand.Intn(n)
piece = ts.checkRange(p, start, n)
if piece == -1 {
piece = ts.checkRange(p, 0, start)
}
return
}
// checkRange returns the first piece in range start..end that is not in the
// torrent's pieceSet but is in the peer's pieceSet.
func (ts *TorrentSession) checkRange(p *peerState, start, end int) (piece int) {
clampedEnd := min(end, min(p.have.n, ts.pieceSet.n))
for i := start; i < clampedEnd; i++ {
if (!ts.pieceSet.IsSet(i)) && p.have.IsSet(i) {
if _, ok := ts.activePieces[i]; !ok {
return i
}
}
}
return -1
}
func (ts *TorrentSession) RequestBlock2(p *peerState, piece int, endGame bool) (err error) {
v := ts.activePieces[piece]
block := v.chooseBlockToDownload(endGame)
if block >= 0 {
ts.requestBlockImp(p, piece, block, true)
} else {
return io.EOF
}
return
}
// Request or cancel a block
func (ts *TorrentSession) requestBlockImp(p *peerState, piece int, block int, request bool) {
begin := block * STANDARD_BLOCK_LENGTH
req := make([]byte, 13)
opcode := byte(REQUEST)
if !request {
opcode = byte(CANCEL)
}
length := STANDARD_BLOCK_LENGTH
if piece == ts.totalPieces-1 {
left := ts.lastPieceLength - begin
if left < length {
length = left
}
}
// log.Println("[", ts.M.Info.Name, "] Requesting block", piece, ".", block, length, request)
req[0] = opcode
uint32ToBytes(req[1:5], uint32(piece))
uint32ToBytes(req[5:9], uint32(begin))
uint32ToBytes(req[9:13], uint32(length))
requestIndex := (uint64(piece) << 32) | uint64(begin)
if !request {
delete(p.our_requests, requestIndex)
} else {
p.our_requests[requestIndex] = time.Now()
}
p.sendMessage(req)
return
}
func (ts *TorrentSession) RecordBlock(p *peerState, piece, begin, length uint32) (err error) {
block := begin / STANDARD_BLOCK_LENGTH
// log.Println("[", ts.M.Info.Name, "] Received block", piece, ".", block)
requestIndex := (uint64(piece) << 32) | uint64(begin)
delete(p.our_requests, requestIndex)
v, ok := ts.activePieces[int(piece)]
if ok {
requestCount := v.recordBlock(int(block))
if requestCount > 1 {
// Someone else has also requested this, so send cancel notices
for _, peer := range ts.peers {
if p != peer {
if _, ok := peer.our_requests[requestIndex]; ok {
ts.requestBlockImp(peer, int(piece), int(block), false)
requestCount--
}
}
}
}
ts.Session.Downloaded += uint64(length)
if v.isComplete() {
delete(ts.activePieces, int(piece))
ok, err = checkPiece(v.buffer, ts.M, int(piece))
if !ok || err != nil {
log.Println("[", ts.M.Info.Name, "] Closing peer that sent a bad piece", piece, p.id, err)
p.Close()
return
}
ts.fileStore.WritePiece(v.buffer, int(piece))
ts.Session.Left -= uint64(len(v.buffer))
ts.pieceSet.Set(int(piece))
ts.goodPieces++
if ts.flags.QuickResume {
ioutil.WriteFile("./"+hex.EncodeToString([]byte(ts.M.InfoHash))+"-haveBitset", ts.pieceSet.Bytes(), 0777)
}
var percentComplete float32
if ts.totalPieces > 0 {
percentComplete = float32(ts.goodPieces*100) / float32(ts.totalPieces)
}
log.Println("[", ts.M.Info.Name, "] Have", ts.goodPieces, "of", ts.totalPieces,
"pieces", percentComplete, "% complete")
if ts.goodPieces == ts.totalPieces {
if !ts.trackerLessMode {
ts.fetchTrackerInfo("completed")
}
// TODO: Drop connections to all seeders.
}
for _, p := range ts.peers {
if p.have != nil {
if int(piece) < p.have.n && p.have.IsSet(int(piece)) {
// We don't do anything special. We rely on the caller
// to decide if this peer is still interesting.
} else {
// log.Println("[", ts.M.Info.Name, "] ...telling ", p)
haveMsg := make([]byte, 5)
haveMsg[0] = HAVE
uint32ToBytes(haveMsg[1:5], piece)
p.sendMessage(haveMsg)
}
}
}
}
} else {
log.Println("[", ts.M.Info.Name, "] Received a block we already have.", piece, block, p.address)
}
return
}
func (ts *TorrentSession) doChoke(p *peerState) (err error) {
p.peer_choking = true
err = ts.removeRequests(p)
return
}
func (ts *TorrentSession) removeRequests(p *peerState) (err error) {
for k := range p.our_requests {
piece := int(k >> 32)
begin := int(k & 0xffffffff)
block := begin / STANDARD_BLOCK_LENGTH
// log.Println("[", ts.M.Info.Name, "] Forgetting we requested block ", piece, ".", block)
ts.removeRequest(piece, block)
}
p.our_requests = make(map[uint64]time.Time, MAX_OUR_REQUESTS)
return
}
func (ts *TorrentSession) removeRequest(piece, block int) {
v, ok := ts.activePieces[piece]
if ok && v.downloaderCount[block] > 0 {
v.downloaderCount[block]--
}
}
func (ts *TorrentSession) doCheckRequests(p *peerState) (err error) {
now := time.Now()
for k, v := range p.our_requests {
if now.Sub(v).Seconds() > 30 {
piece := int(k >> 32)
block := int(k&0xffffffff) / STANDARD_BLOCK_LENGTH
// log.Println("[", ts.M.Info.Name, "] timing out request of", piece, ".", block)
ts.removeRequest(piece, block)
}
}
return
}
func (ts *TorrentSession) DoMessage(p *peerState, message []byte) (err error) {
if message == nil {
return io.EOF // The reader or writer goroutine has exited
}
if len(message) == 0 { // keep alive
return
}
if ts.Session.HaveTorrent {
err = ts.generalMessage(message, p)
} else {
err = ts.extensionMessage(message, p)
}
return
}
func (ts *TorrentSession) extensionMessage(message []byte, p *peerState) (err error) {
if message[0] == EXTENSION {
err := ts.DoExtension(message[1:], p)
if err != nil {
log.Printf("[ %s ] Failed extensions for %s: %s\n", ts.M.Info.Name, p.address, err)
}