]> Sergey Matveev's repositories - btrtrc.git/blob - client.go
2dba6973c516cdbbcd5a131c5b895c2dd506fc9f
[btrtrc.git] / client.go
1 package torrent
2
3 import (
4         "bufio"
5         "bytes"
6         "context"
7         "crypto/rand"
8         "encoding/binary"
9         "errors"
10         "fmt"
11         "io"
12         "net"
13         "strconv"
14         "strings"
15         "time"
16
17         "github.com/anacrolix/dht"
18         "github.com/anacrolix/dht/krpc"
19         "github.com/anacrolix/log"
20         "github.com/anacrolix/missinggo"
21         "github.com/anacrolix/missinggo/bitmap"
22         "github.com/anacrolix/missinggo/perf"
23         "github.com/anacrolix/missinggo/pproffd"
24         "github.com/anacrolix/missinggo/pubsub"
25         "github.com/anacrolix/missinggo/slices"
26         "github.com/anacrolix/sync"
27         "github.com/davecgh/go-spew/spew"
28         "github.com/dustin/go-humanize"
29         "github.com/google/btree"
30
31         "github.com/anacrolix/torrent/bencode"
32         "github.com/anacrolix/torrent/iplist"
33         "github.com/anacrolix/torrent/metainfo"
34         "github.com/anacrolix/torrent/mse"
35         pp "github.com/anacrolix/torrent/peer_protocol"
36         "github.com/anacrolix/torrent/storage"
37 )
38
39 // Clients contain zero or more Torrents. A Client manages a blocklist, the
40 // TCP/UDP protocol ports, and DHT as desired.
41 type Client struct {
42         // An aggregate of stats over all connections. First in struct to ensure
43         // 64-bit alignment of fields. See #262.
44         stats ConnStats
45
46         _mu    sync.RWMutex
47         event  sync.Cond
48         closed missinggo.Event
49
50         config *ClientConfig
51         logger *log.Logger
52
53         peerID         PeerID
54         defaultStorage *storage.Client
55         onClose        []func()
56         conns          []socket
57         dhtServers     []*dht.Server
58         ipBlockList    iplist.Ranger
59         // Our BitTorrent protocol extension bytes, sent in our BT handshakes.
60         extensionBytes pp.PeerExtensionBits
61
62         // Set of addresses that have our client ID. This intentionally will
63         // include ourselves if we end up trying to connect to our own address
64         // through legitimate channels.
65         dopplegangerAddrs map[string]struct{}
66         badPeerIPs        map[string]struct{}
67         torrents          map[InfoHash]*Torrent
68
69         acceptLimiter map[ipStr]int
70 }
71
72 type ipStr string
73
74 func (cl *Client) BadPeerIPs() []string {
75         cl.rLock()
76         defer cl.rUnlock()
77         return cl.badPeerIPsLocked()
78 }
79
80 func (cl *Client) badPeerIPsLocked() []string {
81         return slices.FromMapKeys(cl.badPeerIPs).([]string)
82 }
83
84 func (cl *Client) PeerID() PeerID {
85         return cl.peerID
86 }
87
88 type torrentAddr string
89
90 func (torrentAddr) Network() string { return "" }
91
92 func (me torrentAddr) String() string { return string(me) }
93
94 func (cl *Client) LocalPort() (port int) {
95         cl.eachListener(func(l socket) bool {
96                 _port := missinggo.AddrPort(l.Addr())
97                 if _port == 0 {
98                         panic(l)
99                 }
100                 if port == 0 {
101                         port = _port
102                 } else if port != _port {
103                         panic("mismatched ports")
104                 }
105                 return true
106         })
107         return
108 }
109
110 func writeDhtServerStatus(w io.Writer, s *dht.Server) {
111         dhtStats := s.Stats()
112         fmt.Fprintf(w, "\t# Nodes: %d (%d good, %d banned)\n", dhtStats.Nodes, dhtStats.GoodNodes, dhtStats.BadNodes)
113         fmt.Fprintf(w, "\tServer ID: %x\n", s.ID())
114         fmt.Fprintf(w, "\tAnnounces: %d\n", dhtStats.SuccessfulOutboundAnnouncePeerQueries)
115         fmt.Fprintf(w, "\tOutstanding transactions: %d\n", dhtStats.OutstandingTransactions)
116 }
117
118 // Writes out a human readable status of the client, such as for writing to a
119 // HTTP status page.
120 func (cl *Client) WriteStatus(_w io.Writer) {
121         cl.rLock()
122         defer cl.rUnlock()
123         w := bufio.NewWriter(_w)
124         defer w.Flush()
125         fmt.Fprintf(w, "Listen port: %d\n", cl.LocalPort())
126         fmt.Fprintf(w, "Peer ID: %+q\n", cl.PeerID())
127         fmt.Fprintf(w, "Announce key: %x\n", cl.announceKey())
128         fmt.Fprintf(w, "Banned IPs: %d\n", len(cl.badPeerIPsLocked()))
129         cl.eachDhtServer(func(s *dht.Server) {
130                 fmt.Fprintf(w, "%s DHT server at %s:\n", s.Addr().Network(), s.Addr().String())
131                 writeDhtServerStatus(w, s)
132         })
133         spew.Fdump(w, cl.stats)
134         fmt.Fprintf(w, "# Torrents: %d\n", len(cl.torrentsAsSlice()))
135         fmt.Fprintln(w)
136         for _, t := range slices.Sort(cl.torrentsAsSlice(), func(l, r *Torrent) bool {
137                 return l.InfoHash().AsString() < r.InfoHash().AsString()
138         }).([]*Torrent) {
139                 if t.name() == "" {
140                         fmt.Fprint(w, "<unknown name>")
141                 } else {
142                         fmt.Fprint(w, t.name())
143                 }
144                 fmt.Fprint(w, "\n")
145                 if t.info != nil {
146                         fmt.Fprintf(w, "%f%% of %d bytes (%s)", 100*(1-float64(t.bytesMissingLocked())/float64(t.info.TotalLength())), t.length, humanize.Bytes(uint64(t.info.TotalLength())))
147                 } else {
148                         w.WriteString("<missing metainfo>")
149                 }
150                 fmt.Fprint(w, "\n")
151                 t.writeStatus(w)
152                 fmt.Fprintln(w)
153         }
154 }
155
156 const debugLogValue = "debug"
157
158 func (cl *Client) debugLogFilter(m *log.Msg) bool {
159         if !cl.config.Debug {
160                 _, ok := m.Values()[debugLogValue]
161                 return !ok
162         }
163         return true
164 }
165
166 func (cl *Client) initLogger() {
167         cl.logger = log.Default.Clone().AddValue(cl).AddFilter(log.NewFilter(cl.debugLogFilter))
168 }
169
170 func (cl *Client) announceKey() int32 {
171         return int32(binary.BigEndian.Uint32(cl.peerID[16:20]))
172 }
173
174 func NewClient(cfg *ClientConfig) (cl *Client, err error) {
175         if cfg == nil {
176                 cfg = NewDefaultClientConfig()
177         }
178         defer func() {
179                 if err != nil {
180                         cl = nil
181                 }
182         }()
183         cl = &Client{
184                 config:            cfg,
185                 dopplegangerAddrs: make(map[string]struct{}),
186                 torrents:          make(map[metainfo.Hash]*Torrent),
187         }
188         go cl.acceptLimitClearer()
189         cl.initLogger()
190         defer func() {
191                 if err == nil {
192                         return
193                 }
194                 cl.Close()
195         }()
196         cl.extensionBytes = defaultPeerExtensionBytes()
197         cl.event.L = cl.locker()
198         storageImpl := cfg.DefaultStorage
199         if storageImpl == nil {
200                 // We'd use mmap but HFS+ doesn't support sparse files.
201                 storageImpl = storage.NewFile(cfg.DataDir)
202                 cl.onClose = append(cl.onClose, func() {
203                         if err := storageImpl.Close(); err != nil {
204                                 log.Printf("error closing default storage: %s", err)
205                         }
206                 })
207         }
208         cl.defaultStorage = storage.NewClient(storageImpl)
209         if cfg.IPBlocklist != nil {
210                 cl.ipBlockList = cfg.IPBlocklist
211         }
212
213         if cfg.PeerID != "" {
214                 missinggo.CopyExact(&cl.peerID, cfg.PeerID)
215         } else {
216                 o := copy(cl.peerID[:], cfg.Bep20)
217                 _, err = rand.Read(cl.peerID[o:])
218                 if err != nil {
219                         panic("error generating peer id")
220                 }
221         }
222
223         cl.conns, err = listenAll(cl.enabledPeerNetworks(), cl.config.ListenHost, cl.config.ListenPort, cl.config.ProxyURL, cl.firewallCallback)
224         if err != nil {
225                 return
226         }
227         // Check for panics.
228         cl.LocalPort()
229
230         for _, s := range cl.conns {
231                 if peerNetworkEnabled(s.Addr().Network(), cl.config) {
232                         go cl.acceptConnections(s)
233                 }
234         }
235
236         go cl.forwardPort()
237         if !cfg.NoDHT {
238                 for _, s := range cl.conns {
239                         if pc, ok := s.(net.PacketConn); ok {
240                                 ds, err := cl.newDhtServer(pc)
241                                 if err != nil {
242                                         panic(err)
243                                 }
244                                 cl.dhtServers = append(cl.dhtServers, ds)
245                         }
246                 }
247         }
248
249         return
250 }
251
252 func (cl *Client) firewallCallback(net.Addr) bool {
253         cl.rLock()
254         block := !cl.wantConns()
255         cl.rUnlock()
256         if block {
257                 torrent.Add("connections firewalled", 1)
258         } else {
259                 torrent.Add("connections not firewalled", 1)
260         }
261         return block
262 }
263
264 func (cl *Client) enabledPeerNetworks() (ns []string) {
265         for _, n := range allPeerNetworks {
266                 if peerNetworkEnabled(n, cl.config) {
267                         ns = append(ns, n)
268                 }
269         }
270         return
271 }
272
273 func (cl *Client) newDhtServer(conn net.PacketConn) (s *dht.Server, err error) {
274         cfg := dht.ServerConfig{
275                 IPBlocklist:    cl.ipBlockList,
276                 Conn:           conn,
277                 OnAnnouncePeer: cl.onDHTAnnouncePeer,
278                 PublicIP: func() net.IP {
279                         if connIsIpv6(conn) && cl.config.PublicIp6 != nil {
280                                 return cl.config.PublicIp6
281                         }
282                         return cl.config.PublicIp4
283                 }(),
284                 StartingNodes: cl.config.DhtStartingNodes,
285         }
286         s, err = dht.NewServer(&cfg)
287         if err == nil {
288                 go func() {
289                         if _, err := s.Bootstrap(); err != nil {
290                                 log.Printf("error bootstrapping dht: %s", err)
291                         }
292                 }()
293         }
294         return
295 }
296
297 func firstNonEmptyString(ss ...string) string {
298         for _, s := range ss {
299                 if s != "" {
300                         return s
301                 }
302         }
303         return ""
304 }
305
306 func (cl *Client) Closed() <-chan struct{} {
307         cl.lock()
308         defer cl.unlock()
309         return cl.closed.C()
310 }
311
312 func (cl *Client) eachDhtServer(f func(*dht.Server)) {
313         for _, ds := range cl.dhtServers {
314                 f(ds)
315         }
316 }
317
318 func (cl *Client) closeSockets() {
319         cl.eachListener(func(l socket) bool {
320                 l.Close()
321                 return true
322         })
323         cl.conns = nil
324 }
325
326 // Stops the client. All connections to peers are closed and all activity will
327 // come to a halt.
328 func (cl *Client) Close() {
329         cl.lock()
330         defer cl.unlock()
331         cl.closed.Set()
332         cl.eachDhtServer(func(s *dht.Server) { s.Close() })
333         cl.closeSockets()
334         for _, t := range cl.torrents {
335                 t.close()
336         }
337         for _, f := range cl.onClose {
338                 f()
339         }
340         cl.event.Broadcast()
341 }
342
343 func (cl *Client) ipBlockRange(ip net.IP) (r iplist.Range, blocked bool) {
344         if cl.ipBlockList == nil {
345                 return
346         }
347         return cl.ipBlockList.Lookup(ip)
348 }
349
350 func (cl *Client) ipIsBlocked(ip net.IP) bool {
351         _, blocked := cl.ipBlockRange(ip)
352         return blocked
353 }
354
355 func (cl *Client) wantConns() bool {
356         for _, t := range cl.torrents {
357                 if t.wantConns() {
358                         return true
359                 }
360         }
361         return false
362 }
363
364 func (cl *Client) waitAccept() {
365         for {
366                 if cl.closed.IsSet() {
367                         return
368                 }
369                 if cl.wantConns() {
370                         return
371                 }
372                 cl.event.Wait()
373         }
374 }
375
376 func (cl *Client) rejectAccepted(conn net.Conn) bool {
377         ra := conn.RemoteAddr()
378         rip := missinggo.AddrIP(ra)
379         if cl.config.DisableIPv4Peers && rip.To4() != nil {
380                 return true
381         }
382         if cl.config.DisableIPv4 && len(rip) == net.IPv4len {
383                 return true
384         }
385         if cl.config.DisableIPv6 && len(rip) == net.IPv6len && rip.To4() == nil {
386                 return true
387         }
388         if cl.rateLimitAccept(rip) {
389                 return true
390         }
391         return cl.badPeerIPPort(rip, missinggo.AddrPort(ra))
392 }
393
394 func (cl *Client) acceptConnections(l net.Listener) {
395         for {
396                 conn, err := l.Accept()
397                 conn = pproffd.WrapNetConn(conn)
398                 cl.rLock()
399                 closed := cl.closed.IsSet()
400                 reject := false
401                 if conn != nil {
402                         reject = cl.rejectAccepted(conn)
403                 }
404                 cl.rUnlock()
405                 if closed {
406                         if conn != nil {
407                                 conn.Close()
408                         }
409                         return
410                 }
411                 if err != nil {
412                         log.Printf("error accepting connection: %s", err)
413                         continue
414                 }
415                 go func() {
416                         if reject {
417                                 torrent.Add("rejected accepted connections", 1)
418                                 conn.Close()
419                         } else {
420                                 go cl.incomingConnection(conn)
421                         }
422                         log.Fmsg("accepted %s connection from %s", conn.RemoteAddr().Network(), conn.RemoteAddr()).AddValue(debugLogValue).Log(cl.logger)
423                         torrent.Add(fmt.Sprintf("accepted conn remote IP len=%d", len(missinggo.AddrIP(conn.RemoteAddr()))), 1)
424                         torrent.Add(fmt.Sprintf("accepted conn network=%s", conn.RemoteAddr().Network()), 1)
425                         torrent.Add(fmt.Sprintf("accepted on %s listener", l.Addr().Network()), 1)
426                 }()
427         }
428 }
429
430 func (cl *Client) incomingConnection(nc net.Conn) {
431         defer nc.Close()
432         if tc, ok := nc.(*net.TCPConn); ok {
433                 tc.SetLinger(0)
434         }
435         c := cl.newConnection(nc, false)
436         c.Discovery = peerSourceIncoming
437         cl.runReceivedConn(c)
438 }
439
440 // Returns a handle to the given torrent, if it's present in the client.
441 func (cl *Client) Torrent(ih metainfo.Hash) (t *Torrent, ok bool) {
442         cl.lock()
443         defer cl.unlock()
444         t, ok = cl.torrents[ih]
445         return
446 }
447
448 func (cl *Client) torrent(ih metainfo.Hash) *Torrent {
449         return cl.torrents[ih]
450 }
451
452 type dialResult struct {
453         Conn net.Conn
454 }
455
456 func countDialResult(err error) {
457         if err == nil {
458                 torrent.Add("successful dials", 1)
459         } else {
460                 torrent.Add("unsuccessful dials", 1)
461         }
462 }
463
464 func reducedDialTimeout(minDialTimeout, max time.Duration, halfOpenLimit int, pendingPeers int) (ret time.Duration) {
465         ret = max / time.Duration((pendingPeers+halfOpenLimit)/halfOpenLimit)
466         if ret < minDialTimeout {
467                 ret = minDialTimeout
468         }
469         return
470 }
471
472 // Returns whether an address is known to connect to a client with our own ID.
473 func (cl *Client) dopplegangerAddr(addr string) bool {
474         _, ok := cl.dopplegangerAddrs[addr]
475         return ok
476 }
477
478 func ipNetworkSuffix(allowIpv4, allowIpv6 bool) string {
479         switch {
480         case allowIpv4 && allowIpv6:
481                 return ""
482         case allowIpv4 && !allowIpv6:
483                 return "4"
484         case !allowIpv4 && allowIpv6:
485                 return "6"
486         default:
487                 panic("unhandled ip network combination")
488         }
489 }
490
491 func dialUTP(ctx context.Context, addr string, sock utpSocket) (c net.Conn, err error) {
492         return sock.DialContext(ctx, "", addr)
493 }
494
495 var allPeerNetworks = []string{"tcp4", "tcp6", "udp4", "udp6"}
496
497 func peerNetworkEnabled(network string, cfg *ClientConfig) bool {
498         c := func(s string) bool {
499                 return strings.Contains(network, s)
500         }
501         if cfg.DisableUTP {
502                 if c("udp") || c("utp") {
503                         return false
504                 }
505         }
506         if cfg.DisableTCP && c("tcp") {
507                 return false
508         }
509         if cfg.DisableIPv6 && c("6") {
510                 return false
511         }
512         return true
513 }
514
515 // Returns a connection over UTP or TCP, whichever is first to connect.
516 func (cl *Client) dialFirst(ctx context.Context, addr string) net.Conn {
517         ctx, cancel := context.WithCancel(ctx)
518         // As soon as we return one connection, cancel the others.
519         defer cancel()
520         left := 0
521         resCh := make(chan dialResult, left)
522         dial := func(f func(_ context.Context, addr string) (net.Conn, error)) {
523                 left++
524                 go func() {
525                         c, err := f(ctx, addr)
526                         // This is a bit optimistic, but it looks non-trivial to thread
527                         // this through the proxy code. Set it now in case we close the
528                         // connection forthwith.
529                         if tc, ok := c.(*net.TCPConn); ok {
530                                 tc.SetLinger(0)
531                         }
532                         countDialResult(err)
533                         resCh <- dialResult{c}
534                 }()
535         }
536         func() {
537                 cl.lock()
538                 defer cl.unlock()
539                 cl.eachListener(func(s socket) bool {
540                         if peerNetworkEnabled(s.Addr().Network(), cl.config) {
541                                 dial(s.dial)
542                         }
543                         return true
544                 })
545         }()
546         var res dialResult
547         // Wait for a successful connection.
548         func() {
549                 defer perf.ScopeTimer()()
550                 for ; left > 0 && res.Conn == nil; left-- {
551                         res = <-resCh
552                 }
553         }()
554         // There are still incompleted dials.
555         go func() {
556                 for ; left > 0; left-- {
557                         conn := (<-resCh).Conn
558                         if conn != nil {
559                                 conn.Close()
560                         }
561                 }
562         }()
563         if res.Conn != nil {
564                 go torrent.Add(fmt.Sprintf("network dialed first: %s", res.Conn.RemoteAddr().Network()), 1)
565         }
566         return res.Conn
567 }
568
569 func (cl *Client) noLongerHalfOpen(t *Torrent, addr string) {
570         if _, ok := t.halfOpen[addr]; !ok {
571                 panic("invariant broken")
572         }
573         delete(t.halfOpen, addr)
574         t.openNewConns()
575 }
576
577 // Performs initiator handshakes and returns a connection. Returns nil
578 // *connection if no connection for valid reasons.
579 func (cl *Client) handshakesConnection(ctx context.Context, nc net.Conn, t *Torrent, encryptHeader bool) (c *connection, err error) {
580         c = cl.newConnection(nc, true)
581         c.headerEncrypted = encryptHeader
582         ctx, cancel := context.WithTimeout(ctx, cl.config.HandshakesTimeout)
583         defer cancel()
584         dl, ok := ctx.Deadline()
585         if !ok {
586                 panic(ctx)
587         }
588         err = nc.SetDeadline(dl)
589         if err != nil {
590                 panic(err)
591         }
592         ok, err = cl.initiateHandshakes(c, t)
593         if !ok {
594                 c = nil
595         }
596         return
597 }
598
599 // Returns nil connection and nil error if no connection could be established
600 // for valid reasons.
601 func (cl *Client) establishOutgoingConnEx(t *Torrent, addr string, ctx context.Context, obfuscatedHeader bool) (c *connection, err error) {
602         nc := cl.dialFirst(ctx, addr)
603         if nc == nil {
604                 return
605         }
606         defer func() {
607                 if c == nil || err != nil {
608                         nc.Close()
609                 }
610         }()
611         return cl.handshakesConnection(ctx, nc, t, obfuscatedHeader)
612 }
613
614 // Returns nil connection and nil error if no connection could be established
615 // for valid reasons.
616 func (cl *Client) establishOutgoingConn(t *Torrent, addr string) (c *connection, err error) {
617         ctx, cancel := context.WithTimeout(context.Background(), func() time.Duration {
618                 cl.rLock()
619                 defer cl.rUnlock()
620                 return t.dialTimeout()
621         }())
622         defer cancel()
623         obfuscatedHeaderFirst := !cl.config.DisableEncryption && !cl.config.PreferNoEncryption
624         c, err = cl.establishOutgoingConnEx(t, addr, ctx, obfuscatedHeaderFirst)
625         if err != nil {
626                 return
627         }
628         if c != nil {
629                 torrent.Add("initiated conn with preferred header obfuscation", 1)
630                 return
631         }
632         if cl.config.ForceEncryption {
633                 // We should have just tried with an obfuscated header. A plaintext
634                 // header can't result in an encrypted connection, so we're done.
635                 if !obfuscatedHeaderFirst {
636                         panic(cl.config.EncryptionPolicy)
637                 }
638                 return
639         }
640         // Try again with encryption if we didn't earlier, or without if we did.
641         c, err = cl.establishOutgoingConnEx(t, addr, ctx, !obfuscatedHeaderFirst)
642         if c != nil {
643                 torrent.Add("initiated conn with fallback header obfuscation", 1)
644         }
645         return
646 }
647
648 // Called to dial out and run a connection. The addr we're given is already
649 // considered half-open.
650 func (cl *Client) outgoingConnection(t *Torrent, addr string, ps peerSource) {
651         c, err := cl.establishOutgoingConn(t, addr)
652         cl.lock()
653         defer cl.unlock()
654         // Don't release lock between here and addConnection, unless it's for
655         // failure.
656         cl.noLongerHalfOpen(t, addr)
657         if err != nil {
658                 if cl.config.Debug {
659                         log.Printf("error establishing outgoing connection: %s", err)
660                 }
661                 return
662         }
663         if c == nil {
664                 return
665         }
666         defer c.Close()
667         c.Discovery = ps
668         cl.runHandshookConn(c, t)
669 }
670
671 // The port number for incoming peer connections. 0 if the client isn't
672 // listening.
673 func (cl *Client) incomingPeerPort() int {
674         return cl.LocalPort()
675 }
676
677 func (cl *Client) initiateHandshakes(c *connection, t *Torrent) (ok bool, err error) {
678         if c.headerEncrypted {
679                 var rw io.ReadWriter
680                 rw, c.cryptoMethod, err = mse.InitiateHandshake(
681                         struct {
682                                 io.Reader
683                                 io.Writer
684                         }{c.r, c.w},
685                         t.infoHash[:],
686                         nil,
687                         func() mse.CryptoMethod {
688                                 switch {
689                                 case cl.config.ForceEncryption:
690                                         return mse.CryptoMethodRC4
691                                 case cl.config.DisableEncryption:
692                                         return mse.CryptoMethodPlaintext
693                                 default:
694                                         return mse.AllSupportedCrypto
695                                 }
696                         }(),
697                 )
698                 c.setRW(rw)
699                 if err != nil {
700                         return
701                 }
702         }
703         ih, ok, err := cl.connBTHandshake(c, &t.infoHash)
704         if ih != t.infoHash {
705                 ok = false
706         }
707         return
708 }
709
710 // Calls f with any secret keys.
711 func (cl *Client) forSkeys(f func([]byte) bool) {
712         cl.lock()
713         defer cl.unlock()
714         for ih := range cl.torrents {
715                 if !f(ih[:]) {
716                         break
717                 }
718         }
719 }
720
721 // Do encryption and bittorrent handshakes as receiver.
722 func (cl *Client) receiveHandshakes(c *connection) (t *Torrent, err error) {
723         defer perf.ScopeTimerErr(&err)()
724         var rw io.ReadWriter
725         rw, c.headerEncrypted, c.cryptoMethod, err = handleEncryption(c.rw(), cl.forSkeys, cl.config.EncryptionPolicy)
726         c.setRW(rw)
727         if err == nil || err == mse.ErrNoSecretKeyMatch {
728                 if c.headerEncrypted {
729                         torrent.Add("handshakes received encrypted", 1)
730                 } else {
731                         torrent.Add("handshakes received unencrypted", 1)
732                 }
733         } else {
734                 torrent.Add("handshakes received with error while handling encryption", 1)
735         }
736         if err != nil {
737                 if err == mse.ErrNoSecretKeyMatch {
738                         err = nil
739                 }
740                 return
741         }
742         if cl.config.ForceEncryption && !c.headerEncrypted {
743                 err = errors.New("connection not encrypted")
744                 return
745         }
746         ih, ok, err := cl.connBTHandshake(c, nil)
747         if err != nil {
748                 err = fmt.Errorf("error during bt handshake: %s", err)
749                 return
750         }
751         if !ok {
752                 return
753         }
754         cl.lock()
755         t = cl.torrents[ih]
756         cl.unlock()
757         return
758 }
759
760 // Returns !ok if handshake failed for valid reasons.
761 func (cl *Client) connBTHandshake(c *connection, ih *metainfo.Hash) (ret metainfo.Hash, ok bool, err error) {
762         res, ok, err := pp.Handshake(c.rw(), ih, cl.peerID, cl.extensionBytes)
763         if err != nil || !ok {
764                 return
765         }
766         ret = res.Hash
767         c.PeerExtensionBytes = res.PeerExtensionBits
768         c.PeerID = res.PeerID
769         c.completedHandshake = time.Now()
770         return
771 }
772
773 func (cl *Client) runReceivedConn(c *connection) {
774         err := c.conn.SetDeadline(time.Now().Add(cl.config.HandshakesTimeout))
775         if err != nil {
776                 panic(err)
777         }
778         t, err := cl.receiveHandshakes(c)
779         if err != nil {
780                 log.Fmsg(
781                         "error receiving handshakes: %s", err,
782                 ).AddValue(
783                         debugLogValue,
784                 ).Add(
785                         "network", c.remoteAddr().Network(),
786                 ).Log(cl.logger)
787                 torrent.Add("error receiving handshake", 1)
788                 cl.lock()
789                 cl.onBadAccept(c.remoteAddr())
790                 cl.unlock()
791                 return
792         }
793         if t == nil {
794                 torrent.Add("received handshake for unloaded torrent", 1)
795                 cl.lock()
796                 cl.onBadAccept(c.remoteAddr())
797                 cl.unlock()
798                 return
799         }
800         torrent.Add("received handshake for loaded torrent", 1)
801         cl.lock()
802         defer cl.unlock()
803         cl.runHandshookConn(c, t)
804 }
805
806 func (cl *Client) runHandshookConn(c *connection, t *Torrent) {
807         c.setTorrent(t)
808         if c.PeerID == cl.peerID {
809                 if c.outgoing {
810                         connsToSelf.Add(1)
811                         addr := c.conn.RemoteAddr().String()
812                         cl.dopplegangerAddrs[addr] = struct{}{}
813                 } else {
814                         // Because the remote address is not necessarily the same as its
815                         // client's torrent listen address, we won't record the remote address
816                         // as a doppleganger. Instead, the initiator can record *us* as the
817                         // doppleganger.
818                 }
819                 return
820         }
821         c.conn.SetWriteDeadline(time.Time{})
822         c.r = deadlineReader{c.conn, c.r}
823         completedHandshakeConnectionFlags.Add(c.connectionFlags(), 1)
824         if connIsIpv6(c.conn) {
825                 torrent.Add("completed handshake over ipv6", 1)
826         }
827         if err := t.addConnection(c); err != nil {
828                 log.Fmsg("error adding connection: %s", err).AddValues(c, debugLogValue).Log(t.logger)
829                 return
830         }
831         defer t.dropConnection(c)
832         go c.writer(time.Minute)
833         cl.sendInitialMessages(c, t)
834         err := c.mainReadLoop()
835         if err != nil && cl.config.Debug {
836                 log.Printf("error during connection main read loop: %s", err)
837         }
838 }
839
840 // See the order given in Transmission's tr_peerMsgsNew.
841 func (cl *Client) sendInitialMessages(conn *connection, torrent *Torrent) {
842         if conn.PeerExtensionBytes.SupportsExtended() && cl.extensionBytes.SupportsExtended() {
843                 conn.Post(pp.Message{
844                         Type:       pp.Extended,
845                         ExtendedID: pp.HandshakeExtendedID,
846                         ExtendedPayload: func() []byte {
847                                 msg := pp.ExtendedHandshakeMessage{
848                                         M: map[pp.ExtensionName]pp.ExtensionNumber{
849                                                 pp.ExtensionNameMetadata: metadataExtendedId,
850                                         },
851                                         V:            cl.config.ExtendedHandshakeClientVersion,
852                                         Reqq:         64, // TODO: Really?
853                                         YourIp:       pp.CompactIp(missinggo.AddrIP(conn.remoteAddr())),
854                                         Encryption:   !cl.config.DisableEncryption,
855                                         Port:         cl.incomingPeerPort(),
856                                         MetadataSize: torrent.metadataSize(),
857                                         // TODO: We can figured these out specific to the socket
858                                         // used.
859                                         Ipv4: pp.CompactIp(cl.config.PublicIp4.To4()),
860                                         Ipv6: cl.config.PublicIp6.To16(),
861                                 }
862                                 if !cl.config.DisablePEX {
863                                         msg.M[pp.ExtensionNamePex] = pexExtendedId
864                                 }
865                                 return bencode.MustMarshal(msg)
866                         }(),
867                 })
868         }
869         func() {
870                 if conn.fastEnabled() {
871                         if torrent.haveAllPieces() {
872                                 conn.Post(pp.Message{Type: pp.HaveAll})
873                                 conn.sentHaves.AddRange(0, bitmap.BitIndex(conn.t.NumPieces()))
874                                 return
875                         } else if !torrent.haveAnyPieces() {
876                                 conn.Post(pp.Message{Type: pp.HaveNone})
877                                 conn.sentHaves.Clear()
878                                 return
879                         }
880                 }
881                 conn.PostBitfield()
882         }()
883         if conn.PeerExtensionBytes.SupportsDHT() && cl.extensionBytes.SupportsDHT() && cl.haveDhtServer() {
884                 conn.Post(pp.Message{
885                         Type: pp.Port,
886                         Port: cl.dhtPort(),
887                 })
888         }
889 }
890
891 func (cl *Client) dhtPort() (ret uint16) {
892         cl.eachDhtServer(func(s *dht.Server) {
893                 ret = uint16(missinggo.AddrPort(s.Addr()))
894         })
895         return
896 }
897
898 func (cl *Client) haveDhtServer() (ret bool) {
899         cl.eachDhtServer(func(_ *dht.Server) {
900                 ret = true
901         })
902         return
903 }
904
905 // Process incoming ut_metadata message.
906 func (cl *Client) gotMetadataExtensionMsg(payload []byte, t *Torrent, c *connection) error {
907         var d map[string]int
908         err := bencode.Unmarshal(payload, &d)
909         if _, ok := err.(bencode.ErrUnusedTrailingBytes); ok {
910         } else if err != nil {
911                 return fmt.Errorf("error unmarshalling bencode: %s", err)
912         }
913         msgType, ok := d["msg_type"]
914         if !ok {
915                 return errors.New("missing msg_type field")
916         }
917         piece := d["piece"]
918         switch msgType {
919         case pp.DataMetadataExtensionMsgType:
920                 c.allStats(add(1, func(cs *ConnStats) *Count { return &cs.MetadataChunksRead }))
921                 if !c.requestedMetadataPiece(piece) {
922                         return fmt.Errorf("got unexpected piece %d", piece)
923                 }
924                 c.metadataRequests[piece] = false
925                 begin := len(payload) - metadataPieceSize(d["total_size"], piece)
926                 if begin < 0 || begin >= len(payload) {
927                         return fmt.Errorf("data has bad offset in payload: %d", begin)
928                 }
929                 t.saveMetadataPiece(piece, payload[begin:])
930                 c.lastUsefulChunkReceived = time.Now()
931                 return t.maybeCompleteMetadata()
932         case pp.RequestMetadataExtensionMsgType:
933                 if !t.haveMetadataPiece(piece) {
934                         c.Post(t.newMetadataExtensionMessage(c, pp.RejectMetadataExtensionMsgType, d["piece"], nil))
935                         return nil
936                 }
937                 start := (1 << 14) * piece
938                 c.Post(t.newMetadataExtensionMessage(c, pp.DataMetadataExtensionMsgType, piece, t.metadataBytes[start:start+t.metadataPieceSize(piece)]))
939                 return nil
940         case pp.RejectMetadataExtensionMsgType:
941                 return nil
942         default:
943                 return errors.New("unknown msg_type value")
944         }
945 }
946
947 func (cl *Client) badPeerIPPort(ip net.IP, port int) bool {
948         if port == 0 {
949                 return true
950         }
951         if cl.dopplegangerAddr(net.JoinHostPort(ip.String(), strconv.FormatInt(int64(port), 10))) {
952                 return true
953         }
954         if _, ok := cl.ipBlockRange(ip); ok {
955                 return true
956         }
957         if _, ok := cl.badPeerIPs[ip.String()]; ok {
958                 return true
959         }
960         return false
961 }
962
963 // Return a Torrent ready for insertion into a Client.
964 func (cl *Client) newTorrent(ih metainfo.Hash, specStorage storage.ClientImpl) (t *Torrent) {
965         // use provided storage, if provided
966         storageClient := cl.defaultStorage
967         if specStorage != nil {
968                 storageClient = storage.NewClient(specStorage)
969         }
970
971         t = &Torrent{
972                 cl:       cl,
973                 infoHash: ih,
974                 peers: prioritizedPeers{
975                         om: btree.New(32),
976                         getPrio: func(p Peer) peerPriority {
977                                 return bep40PriorityIgnoreError(cl.publicAddr(p.IP), p.addr())
978                         },
979                 },
980                 conns: make(map[*connection]struct{}, 2*cl.config.EstablishedConnsPerTorrent),
981
982                 halfOpen:          make(map[string]Peer),
983                 pieceStateChanges: pubsub.NewPubSub(),
984
985                 storageOpener:       storageClient,
986                 maxEstablishedConns: cl.config.EstablishedConnsPerTorrent,
987
988                 networkingEnabled: true,
989                 requestStrategy:   2,
990                 metadataChanged: sync.Cond{
991                         L: cl.locker(),
992                 },
993                 duplicateRequestTimeout: 1 * time.Second,
994         }
995         t.logger = cl.logger.Clone().AddValue(t)
996         t.setChunkSize(defaultChunkSize)
997         return
998 }
999
1000 // A file-like handle to some torrent data resource.
1001 type Handle interface {
1002         io.Reader
1003         io.Seeker
1004         io.Closer
1005         io.ReaderAt
1006 }
1007
1008 func (cl *Client) AddTorrentInfoHash(infoHash metainfo.Hash) (t *Torrent, new bool) {
1009         return cl.AddTorrentInfoHashWithStorage(infoHash, nil)
1010 }
1011
1012 // Adds a torrent by InfoHash with a custom Storage implementation.
1013 // If the torrent already exists then this Storage is ignored and the
1014 // existing torrent returned with `new` set to `false`
1015 func (cl *Client) AddTorrentInfoHashWithStorage(infoHash metainfo.Hash, specStorage storage.ClientImpl) (t *Torrent, new bool) {
1016         cl.lock()
1017         defer cl.unlock()
1018         t, ok := cl.torrents[infoHash]
1019         if ok {
1020                 return
1021         }
1022         new = true
1023
1024         t = cl.newTorrent(infoHash, specStorage)
1025         cl.eachDhtServer(func(s *dht.Server) {
1026                 go t.dhtAnnouncer(s)
1027         })
1028         cl.torrents[infoHash] = t
1029         cl.clearAcceptLimits()
1030         t.updateWantPeersEvent()
1031         // Tickle Client.waitAccept, new torrent may want conns.
1032         cl.event.Broadcast()
1033         return
1034 }
1035
1036 // Add or merge a torrent spec. If the torrent is already present, the
1037 // trackers will be merged with the existing ones. If the Info isn't yet
1038 // known, it will be set. The display name is replaced if the new spec
1039 // provides one. Returns new if the torrent wasn't already in the client.
1040 // Note that any `Storage` defined on the spec will be ignored if the
1041 // torrent is already present (i.e. `new` return value is `true`)
1042 func (cl *Client) AddTorrentSpec(spec *TorrentSpec) (t *Torrent, new bool, err error) {
1043         t, new = cl.AddTorrentInfoHashWithStorage(spec.InfoHash, spec.Storage)
1044         if spec.DisplayName != "" {
1045                 t.SetDisplayName(spec.DisplayName)
1046         }
1047         if spec.InfoBytes != nil {
1048                 err = t.SetInfoBytes(spec.InfoBytes)
1049                 if err != nil {
1050                         return
1051                 }
1052         }
1053         cl.lock()
1054         defer cl.unlock()
1055         if spec.ChunkSize != 0 {
1056                 t.setChunkSize(pp.Integer(spec.ChunkSize))
1057         }
1058         t.addTrackers(spec.Trackers)
1059         t.maybeNewConns()
1060         return
1061 }
1062
1063 func (cl *Client) dropTorrent(infoHash metainfo.Hash) (err error) {
1064         t, ok := cl.torrents[infoHash]
1065         if !ok {
1066                 err = fmt.Errorf("no such torrent")
1067                 return
1068         }
1069         err = t.close()
1070         if err != nil {
1071                 panic(err)
1072         }
1073         delete(cl.torrents, infoHash)
1074         return
1075 }
1076
1077 func (cl *Client) allTorrentsCompleted() bool {
1078         for _, t := range cl.torrents {
1079                 if !t.haveInfo() {
1080                         return false
1081                 }
1082                 if !t.haveAllPieces() {
1083                         return false
1084                 }
1085         }
1086         return true
1087 }
1088
1089 // Returns true when all torrents are completely downloaded and false if the
1090 // client is stopped before that.
1091 func (cl *Client) WaitAll() bool {
1092         cl.lock()
1093         defer cl.unlock()
1094         for !cl.allTorrentsCompleted() {
1095                 if cl.closed.IsSet() {
1096                         return false
1097                 }
1098                 cl.event.Wait()
1099         }
1100         return true
1101 }
1102
1103 // Returns handles to all the torrents loaded in the Client.
1104 func (cl *Client) Torrents() []*Torrent {
1105         cl.lock()
1106         defer cl.unlock()
1107         return cl.torrentsAsSlice()
1108 }
1109
1110 func (cl *Client) torrentsAsSlice() (ret []*Torrent) {
1111         for _, t := range cl.torrents {
1112                 ret = append(ret, t)
1113         }
1114         return
1115 }
1116
1117 func (cl *Client) AddMagnet(uri string) (T *Torrent, err error) {
1118         spec, err := TorrentSpecFromMagnetURI(uri)
1119         if err != nil {
1120                 return
1121         }
1122         T, _, err = cl.AddTorrentSpec(spec)
1123         return
1124 }
1125
1126 func (cl *Client) AddTorrent(mi *metainfo.MetaInfo) (T *Torrent, err error) {
1127         T, _, err = cl.AddTorrentSpec(TorrentSpecFromMetaInfo(mi))
1128         var ss []string
1129         slices.MakeInto(&ss, mi.Nodes)
1130         cl.AddDHTNodes(ss)
1131         return
1132 }
1133
1134 func (cl *Client) AddTorrentFromFile(filename string) (T *Torrent, err error) {
1135         mi, err := metainfo.LoadFromFile(filename)
1136         if err != nil {
1137                 return
1138         }
1139         return cl.AddTorrent(mi)
1140 }
1141
1142 func (cl *Client) DhtServers() []*dht.Server {
1143         return cl.dhtServers
1144 }
1145
1146 func (cl *Client) AddDHTNodes(nodes []string) {
1147         for _, n := range nodes {
1148                 hmp := missinggo.SplitHostMaybePort(n)
1149                 ip := net.ParseIP(hmp.Host)
1150                 if ip == nil {
1151                         log.Printf("won't add DHT node with bad IP: %q", hmp.Host)
1152                         continue
1153                 }
1154                 ni := krpc.NodeInfo{
1155                         Addr: krpc.NodeAddr{
1156                                 IP:   ip,
1157                                 Port: hmp.Port,
1158                         },
1159                 }
1160                 cl.eachDhtServer(func(s *dht.Server) {
1161                         s.AddNode(ni)
1162                 })
1163         }
1164 }
1165
1166 func (cl *Client) banPeerIP(ip net.IP) {
1167         if cl.badPeerIPs == nil {
1168                 cl.badPeerIPs = make(map[string]struct{})
1169         }
1170         cl.badPeerIPs[ip.String()] = struct{}{}
1171 }
1172
1173 func (cl *Client) newConnection(nc net.Conn, outgoing bool) (c *connection) {
1174         c = &connection{
1175                 conn:            nc,
1176                 outgoing:        outgoing,
1177                 Choked:          true,
1178                 PeerChoked:      true,
1179                 PeerMaxRequests: 250,
1180                 writeBuffer:     new(bytes.Buffer),
1181         }
1182         c.writerCond.L = cl.locker()
1183         c.setRW(connStatsReadWriter{nc, c})
1184         c.r = &rateLimitedReader{
1185                 l: cl.config.DownloadRateLimiter,
1186                 r: c.r,
1187         }
1188         return
1189 }
1190
1191 func (cl *Client) onDHTAnnouncePeer(ih metainfo.Hash, p dht.Peer) {
1192         cl.lock()
1193         defer cl.unlock()
1194         t := cl.torrent(ih)
1195         if t == nil {
1196                 return
1197         }
1198         t.addPeers([]Peer{{
1199                 IP:     p.IP,
1200                 Port:   p.Port,
1201                 Source: peerSourceDHTAnnouncePeer,
1202         }})
1203 }
1204
1205 func firstNotNil(ips ...net.IP) net.IP {
1206         for _, ip := range ips {
1207                 if ip != nil {
1208                         return ip
1209                 }
1210         }
1211         return nil
1212 }
1213
1214 func (cl *Client) eachListener(f func(socket) bool) {
1215         for _, s := range cl.conns {
1216                 if !f(s) {
1217                         break
1218                 }
1219         }
1220 }
1221
1222 func (cl *Client) findListener(f func(net.Listener) bool) (ret net.Listener) {
1223         cl.eachListener(func(l socket) bool {
1224                 ret = l
1225                 return !f(l)
1226         })
1227         return
1228 }
1229
1230 func (cl *Client) publicIp(peer net.IP) net.IP {
1231         // TODO: Use BEP 10 to determine how peers are seeing us.
1232         if peer.To4() != nil {
1233                 return firstNotNil(
1234                         cl.config.PublicIp4,
1235                         cl.findListenerIp(func(ip net.IP) bool { return ip.To4() != nil }),
1236                 )
1237         } else {
1238                 return firstNotNil(
1239                         cl.config.PublicIp6,
1240                         cl.findListenerIp(func(ip net.IP) bool { return ip.To4() == nil }),
1241                 )
1242         }
1243 }
1244
1245 func (cl *Client) findListenerIp(f func(net.IP) bool) net.IP {
1246         return missinggo.AddrIP(cl.findListener(func(l net.Listener) bool {
1247                 return f(missinggo.AddrIP(l.Addr()))
1248         }).Addr())
1249 }
1250
1251 // Our IP as a peer should see it.
1252 func (cl *Client) publicAddr(peer net.IP) ipPort {
1253         return ipPort{cl.publicIp(peer), uint16(cl.incomingPeerPort())}
1254 }
1255
1256 func (cl *Client) ListenAddrs() (ret []net.Addr) {
1257         cl.lock()
1258         defer cl.unlock()
1259         cl.eachListener(func(l socket) bool {
1260                 ret = append(ret, l.Addr())
1261                 return true
1262         })
1263         return
1264 }
1265
1266 func (cl *Client) onBadAccept(addr net.Addr) {
1267         ip := maskIpForAcceptLimiting(missinggo.AddrIP(addr))
1268         if cl.acceptLimiter == nil {
1269                 cl.acceptLimiter = make(map[ipStr]int)
1270         }
1271         cl.acceptLimiter[ipStr(ip.String())]++
1272 }
1273
1274 func maskIpForAcceptLimiting(ip net.IP) net.IP {
1275         if ip4 := ip.To4(); ip4 != nil {
1276                 return ip4.Mask(net.CIDRMask(24, 32))
1277         }
1278         return ip
1279 }
1280
1281 func (cl *Client) clearAcceptLimits() {
1282         cl.acceptLimiter = nil
1283 }
1284
1285 func (cl *Client) acceptLimitClearer() {
1286         for {
1287                 select {
1288                 case <-cl.closed.LockedChan(cl.locker()):
1289                         return
1290                 case <-time.After(15 * time.Minute):
1291                         cl.lock()
1292                         cl.clearAcceptLimits()
1293                         cl.unlock()
1294                 }
1295         }
1296 }
1297
1298 func (cl *Client) rateLimitAccept(ip net.IP) bool {
1299         if cl.config.DisableAcceptRateLimiting {
1300                 return false
1301         }
1302         return cl.acceptLimiter[ipStr(maskIpForAcceptLimiting(ip).String())] > 0
1303 }
1304
1305 func (cl *Client) rLock() {
1306         cl._mu.RLock()
1307 }
1308
1309 func (cl *Client) rUnlock() {
1310         cl._mu.RUnlock()
1311 }
1312
1313 func (cl *Client) lock() {
1314         cl._mu.Lock()
1315 }
1316
1317 func (cl *Client) unlock() {
1318         cl._mu.Unlock()
1319 }
1320
1321 func (cl *Client) locker() sync.Locker {
1322         return clientLocker{cl}
1323 }
1324
1325 type clientLocker struct {
1326         *Client
1327 }
1328
1329 func (cl clientLocker) Lock() {
1330         cl.lock()
1331 }
1332
1333 func (cl clientLocker) Unlock() {
1334         cl.unlock()
1335 }