]> Sergey Matveev's repositories - btrtrc.git/blob - client.go
Log failure to complete metadata
[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         "net/http"
14         "strconv"
15         "strings"
16         "time"
17
18         "github.com/anacrolix/dht/v2"
19         "github.com/anacrolix/dht/v2/krpc"
20         "github.com/anacrolix/log"
21         "github.com/anacrolix/missinggo/bitmap"
22         "github.com/anacrolix/missinggo/perf"
23         "github.com/anacrolix/missinggo/pubsub"
24         "github.com/anacrolix/missinggo/slices"
25         "github.com/anacrolix/missinggo/v2/pproffd"
26         "github.com/anacrolix/sync"
27         "github.com/anacrolix/torrent/internal/limiter"
28         "github.com/anacrolix/torrent/tracker"
29         "github.com/anacrolix/torrent/webtorrent"
30         "github.com/davecgh/go-spew/spew"
31         "github.com/dustin/go-humanize"
32         "github.com/google/btree"
33         "github.com/pion/datachannel"
34         "golang.org/x/time/rate"
35         "golang.org/x/xerrors"
36
37         "github.com/anacrolix/missinggo/v2"
38         "github.com/anacrolix/missinggo/v2/conntrack"
39
40         "github.com/anacrolix/torrent/bencode"
41         "github.com/anacrolix/torrent/iplist"
42         "github.com/anacrolix/torrent/metainfo"
43         "github.com/anacrolix/torrent/mse"
44         pp "github.com/anacrolix/torrent/peer_protocol"
45         "github.com/anacrolix/torrent/storage"
46 )
47
48 // Clients contain zero or more Torrents. A Client manages a blocklist, the
49 // TCP/UDP protocol ports, and DHT as desired.
50 type Client struct {
51         // An aggregate of stats over all connections. First in struct to ensure 64-bit alignment of
52         // fields. See #262.
53         stats ConnStats
54
55         _mu    lockWithDeferreds
56         event  sync.Cond
57         closed missinggo.Event
58
59         config *ClientConfig
60         logger log.Logger
61
62         peerID         PeerID
63         defaultStorage *storage.Client
64         onClose        []func()
65         dialers        []Dialer
66         listeners      []Listener
67         dhtServers     []DhtServer
68         ipBlockList    iplist.Ranger
69
70         // Set of addresses that have our client ID. This intentionally will
71         // include ourselves if we end up trying to connect to our own address
72         // through legitimate channels.
73         dopplegangerAddrs map[string]struct{}
74         badPeerIPs        map[string]struct{}
75         torrents          map[InfoHash]*Torrent
76
77         acceptLimiter   map[ipStr]int
78         dialRateLimiter *rate.Limiter
79         numHalfOpen     int
80
81         websocketTrackers websocketTrackers
82
83         activeAnnounceLimiter limiter.Instance
84 }
85
86 type ipStr string
87
88 func (cl *Client) BadPeerIPs() []string {
89         cl.rLock()
90         defer cl.rUnlock()
91         return cl.badPeerIPsLocked()
92 }
93
94 func (cl *Client) badPeerIPsLocked() []string {
95         return slices.FromMapKeys(cl.badPeerIPs).([]string)
96 }
97
98 func (cl *Client) PeerID() PeerID {
99         return cl.peerID
100 }
101
102 // Returns the port number for the first listener that has one. No longer assumes that all port
103 // numbers are the same, due to support for custom listeners. Returns zero if no port number is
104 // found.
105 func (cl *Client) LocalPort() (port int) {
106         cl.eachListener(func(l Listener) bool {
107                 port = addrPortOrZero(l.Addr())
108                 return port == 0
109         })
110         return
111 }
112
113 func writeDhtServerStatus(w io.Writer, s DhtServer) {
114         dhtStats := s.Stats()
115         fmt.Fprintf(w, " ID: %x\n", s.ID())
116         spew.Fdump(w, dhtStats)
117 }
118
119 // Writes out a human readable status of the client, such as for writing to a
120 // HTTP status page.
121 func (cl *Client) WriteStatus(_w io.Writer) {
122         cl.rLock()
123         defer cl.rUnlock()
124         w := bufio.NewWriter(_w)
125         defer w.Flush()
126         fmt.Fprintf(w, "Listen port: %d\n", cl.LocalPort())
127         fmt.Fprintf(w, "Peer ID: %+q\n", cl.PeerID())
128         fmt.Fprintf(w, "Extension bits: %v\n", cl.config.Extensions)
129         fmt.Fprintf(w, "Announce key: %x\n", cl.announceKey())
130         fmt.Fprintf(w, "Banned IPs: %d\n", len(cl.badPeerIPsLocked()))
131         cl.eachDhtServer(func(s DhtServer) {
132                 fmt.Fprintf(w, "%s DHT server at %s:\n", s.Addr().Network(), s.Addr().String())
133                 writeDhtServerStatus(w, s)
134         })
135         spew.Fdump(w, &cl.stats)
136         fmt.Fprintf(w, "# Torrents: %d\n", len(cl.torrentsAsSlice()))
137         fmt.Fprintln(w)
138         for _, t := range slices.Sort(cl.torrentsAsSlice(), func(l, r *Torrent) bool {
139                 return l.InfoHash().AsString() < r.InfoHash().AsString()
140         }).([]*Torrent) {
141                 if t.name() == "" {
142                         fmt.Fprint(w, "<unknown name>")
143                 } else {
144                         fmt.Fprint(w, t.name())
145                 }
146                 fmt.Fprint(w, "\n")
147                 if t.info != nil {
148                         fmt.Fprintf(
149                                 w,
150                                 "%f%% of %d bytes (%s)",
151                                 100*(1-float64(t.bytesMissingLocked())/float64(t.info.TotalLength())),
152                                 *t.length,
153                                 humanize.Bytes(uint64(*t.length)))
154                 } else {
155                         w.WriteString("<missing metainfo>")
156                 }
157                 fmt.Fprint(w, "\n")
158                 t.writeStatus(w)
159                 fmt.Fprintln(w)
160         }
161 }
162
163 // Filters things that are less than warning from UPnP discovery.
164 func upnpDiscoverLogFilter(m log.Msg) bool {
165         level, ok := m.GetLevel()
166         return !m.HasValue(UpnpDiscoverLogTag) || (!level.LessThan(log.Warning) && ok)
167 }
168
169 func (cl *Client) initLogger() {
170         logger := cl.config.Logger
171         if logger.IsZero() {
172                 logger = log.Default
173                 if !cl.config.Debug {
174                         logger = logger.FilterLevel(log.Info).WithFilter(upnpDiscoverLogFilter)
175                 }
176         }
177         cl.logger = logger.WithValues(cl)
178 }
179
180 func (cl *Client) announceKey() int32 {
181         return int32(binary.BigEndian.Uint32(cl.peerID[16:20]))
182 }
183
184 func NewClient(cfg *ClientConfig) (cl *Client, err error) {
185         if cfg == nil {
186                 cfg = NewDefaultClientConfig()
187                 cfg.ListenPort = 0
188         }
189         defer func() {
190                 if err != nil {
191                         cl = nil
192                 }
193         }()
194         cl = &Client{
195                 config:            cfg,
196                 dopplegangerAddrs: make(map[string]struct{}),
197                 torrents:          make(map[metainfo.Hash]*Torrent),
198                 dialRateLimiter:   rate.NewLimiter(10, 10),
199         }
200         cl.activeAnnounceLimiter.SlotsPerKey = 2
201         go cl.acceptLimitClearer()
202         cl.initLogger()
203         defer func() {
204                 if err == nil {
205                         return
206                 }
207                 cl.Close()
208         }()
209         cl.event.L = cl.locker()
210         storageImpl := cfg.DefaultStorage
211         if storageImpl == nil {
212                 // We'd use mmap by default but HFS+ doesn't support sparse files.
213                 storageImplCloser := storage.NewFile(cfg.DataDir)
214                 cl.onClose = append(cl.onClose, func() {
215                         if err := storageImplCloser.Close(); err != nil {
216                                 cl.logger.Printf("error closing default storage: %s", err)
217                         }
218                 })
219                 storageImpl = storageImplCloser
220         }
221         cl.defaultStorage = storage.NewClient(storageImpl)
222         if cfg.IPBlocklist != nil {
223                 cl.ipBlockList = cfg.IPBlocklist
224         }
225
226         if cfg.PeerID != "" {
227                 missinggo.CopyExact(&cl.peerID, cfg.PeerID)
228         } else {
229                 o := copy(cl.peerID[:], cfg.Bep20)
230                 _, err = rand.Read(cl.peerID[o:])
231                 if err != nil {
232                         panic("error generating peer id")
233                 }
234         }
235
236         sockets, err := listenAll(cl.listenNetworks(), cl.config.ListenHost, cl.config.ListenPort, cl.firewallCallback)
237         if err != nil {
238                 return
239         }
240
241         // Check for panics.
242         cl.LocalPort()
243
244         for _, _s := range sockets {
245                 s := _s // Go is fucking retarded.
246                 cl.onClose = append(cl.onClose, func() { s.Close() })
247                 if peerNetworkEnabled(parseNetworkString(s.Addr().Network()), cl.config) {
248                         cl.dialers = append(cl.dialers, s)
249                         cl.listeners = append(cl.listeners, s)
250                         go cl.acceptConnections(s)
251                 }
252         }
253
254         go cl.forwardPort()
255         if !cfg.NoDHT {
256                 for _, s := range sockets {
257                         if pc, ok := s.(net.PacketConn); ok {
258                                 ds, err := cl.newAnacrolixDhtServer(pc)
259                                 if err != nil {
260                                         panic(err)
261                                 }
262                                 cl.dhtServers = append(cl.dhtServers, anacrolixDhtServerWrapper{ds})
263                                 cl.onClose = append(cl.onClose, func() { ds.Close() })
264                         }
265                 }
266         }
267
268         cl.websocketTrackers = websocketTrackers{
269                 PeerId: cl.peerID,
270                 Logger: cl.logger,
271                 GetAnnounceRequest: func(event tracker.AnnounceEvent, infoHash [20]byte) (tracker.AnnounceRequest, error) {
272                         cl.lock()
273                         defer cl.unlock()
274                         t, ok := cl.torrents[infoHash]
275                         if !ok {
276                                 return tracker.AnnounceRequest{}, errors.New("torrent not tracked by client")
277                         }
278                         return t.announceRequest(event), nil
279                 },
280                 OnConn: func(dc datachannel.ReadWriteCloser, dcc webtorrent.DataChannelContext) {
281                         cl.lock()
282                         defer cl.unlock()
283                         t, ok := cl.torrents[dcc.InfoHash]
284                         if !ok {
285                                 cl.logger.WithDefaultLevel(log.Warning).Printf(
286                                         "got webrtc conn for unloaded torrent with infohash %x",
287                                         dcc.InfoHash,
288                                 )
289                                 dc.Close()
290                                 return
291                         }
292                         go t.onWebRtcConn(dc, dcc)
293                 },
294         }
295
296         return
297 }
298
299 func (cl *Client) AddDhtServer(d DhtServer) {
300         cl.dhtServers = append(cl.dhtServers, d)
301 }
302
303 // Adds a Dialer for outgoing connections. All Dialers are used when attempting to connect to a
304 // given address for any Torrent.
305 func (cl *Client) AddDialer(d Dialer) {
306         cl.lock()
307         defer cl.unlock()
308         cl.dialers = append(cl.dialers, d)
309         for _, t := range cl.torrents {
310                 t.openNewConns()
311         }
312 }
313
314 // Registers a Listener, and starts Accepting on it. You must Close Listeners provided this way
315 // yourself.
316 func (cl *Client) AddListener(l Listener) {
317         cl.listeners = append(cl.listeners, l)
318         go cl.acceptConnections(l)
319 }
320
321 func (cl *Client) firewallCallback(net.Addr) bool {
322         cl.rLock()
323         block := !cl.wantConns()
324         cl.rUnlock()
325         if block {
326                 torrent.Add("connections firewalled", 1)
327         } else {
328                 torrent.Add("connections not firewalled", 1)
329         }
330         return block
331 }
332
333 func (cl *Client) listenOnNetwork(n network) bool {
334         if n.Ipv4 && cl.config.DisableIPv4 {
335                 return false
336         }
337         if n.Ipv6 && cl.config.DisableIPv6 {
338                 return false
339         }
340         if n.Tcp && cl.config.DisableTCP {
341                 return false
342         }
343         if n.Udp && cl.config.DisableUTP && cl.config.NoDHT {
344                 return false
345         }
346         return true
347 }
348
349 func (cl *Client) listenNetworks() (ns []network) {
350         for _, n := range allPeerNetworks {
351                 if cl.listenOnNetwork(n) {
352                         ns = append(ns, n)
353                 }
354         }
355         return
356 }
357
358 func (cl *Client) newAnacrolixDhtServer(conn net.PacketConn) (s *dht.Server, err error) {
359         cfg := dht.ServerConfig{
360                 IPBlocklist:    cl.ipBlockList,
361                 Conn:           conn,
362                 OnAnnouncePeer: cl.onDHTAnnouncePeer,
363                 PublicIP: func() net.IP {
364                         if connIsIpv6(conn) && cl.config.PublicIp6 != nil {
365                                 return cl.config.PublicIp6
366                         }
367                         return cl.config.PublicIp4
368                 }(),
369                 StartingNodes:      cl.config.DhtStartingNodes(conn.LocalAddr().Network()),
370                 ConnectionTracking: cl.config.ConnTracker,
371                 OnQuery:            cl.config.DHTOnQuery,
372                 Logger:             cl.logger.WithContextText(fmt.Sprintf("dht server on %v", conn.LocalAddr().String())),
373         }
374         if f := cl.config.ConfigureAnacrolixDhtServer; f != nil {
375                 f(&cfg)
376         }
377         s, err = dht.NewServer(&cfg)
378         if err == nil {
379                 go func() {
380                         ts, err := s.Bootstrap()
381                         if err != nil {
382                                 cl.logger.Printf("error bootstrapping dht: %s", err)
383                         }
384                         log.Fstr("%v completed bootstrap (%v)", s, ts).AddValues(s, ts).Log(cl.logger)
385                 }()
386         }
387         return
388 }
389
390 func (cl *Client) Closed() <-chan struct{} {
391         cl.lock()
392         defer cl.unlock()
393         return cl.closed.C()
394 }
395
396 func (cl *Client) eachDhtServer(f func(DhtServer)) {
397         for _, ds := range cl.dhtServers {
398                 f(ds)
399         }
400 }
401
402 // Stops the client. All connections to peers are closed and all activity will
403 // come to a halt.
404 func (cl *Client) Close() {
405         cl.lock()
406         defer cl.unlock()
407         cl.closed.Set()
408         for _, t := range cl.torrents {
409                 t.close()
410         }
411         for i := range cl.onClose {
412                 cl.onClose[len(cl.onClose)-1-i]()
413         }
414         cl.event.Broadcast()
415 }
416
417 func (cl *Client) ipBlockRange(ip net.IP) (r iplist.Range, blocked bool) {
418         if cl.ipBlockList == nil {
419                 return
420         }
421         return cl.ipBlockList.Lookup(ip)
422 }
423
424 func (cl *Client) ipIsBlocked(ip net.IP) bool {
425         _, blocked := cl.ipBlockRange(ip)
426         return blocked
427 }
428
429 func (cl *Client) wantConns() bool {
430         for _, t := range cl.torrents {
431                 if t.wantConns() {
432                         return true
433                 }
434         }
435         return false
436 }
437
438 func (cl *Client) waitAccept() {
439         for {
440                 if cl.closed.IsSet() {
441                         return
442                 }
443                 if cl.wantConns() {
444                         return
445                 }
446                 cl.event.Wait()
447         }
448 }
449
450 // TODO: Apply filters for non-standard networks, particularly rate-limiting.
451 func (cl *Client) rejectAccepted(conn net.Conn) error {
452         ra := conn.RemoteAddr()
453         if rip := addrIpOrNil(ra); rip != nil {
454                 if cl.config.DisableIPv4Peers && rip.To4() != nil {
455                         return errors.New("ipv4 peers disabled")
456                 }
457                 if cl.config.DisableIPv4 && len(rip) == net.IPv4len {
458                         return errors.New("ipv4 disabled")
459
460                 }
461                 if cl.config.DisableIPv6 && len(rip) == net.IPv6len && rip.To4() == nil {
462                         return errors.New("ipv6 disabled")
463                 }
464                 if cl.rateLimitAccept(rip) {
465                         return errors.New("source IP accepted rate limited")
466                 }
467                 if cl.badPeerIPPort(rip, missinggo.AddrPort(ra)) {
468                         return errors.New("bad source addr")
469                 }
470         }
471         return nil
472 }
473
474 func (cl *Client) acceptConnections(l Listener) {
475         for {
476                 conn, err := l.Accept()
477                 torrent.Add("client listener accepts", 1)
478                 conn = pproffd.WrapNetConn(conn)
479                 cl.rLock()
480                 closed := cl.closed.IsSet()
481                 var reject error
482                 if conn != nil {
483                         reject = cl.rejectAccepted(conn)
484                 }
485                 cl.rUnlock()
486                 if closed {
487                         if conn != nil {
488                                 conn.Close()
489                         }
490                         return
491                 }
492                 if err != nil {
493                         log.Fmsg("error accepting connection: %s", err).SetLevel(log.Debug).Log(cl.logger)
494                         continue
495                 }
496                 go func() {
497                         if reject != nil {
498                                 torrent.Add("rejected accepted connections", 1)
499                                 log.Fmsg("rejecting accepted conn: %v", reject).SetLevel(log.Debug).Log(cl.logger)
500                                 conn.Close()
501                         } else {
502                                 go cl.incomingConnection(conn)
503                         }
504                         log.Fmsg("accepted %q connection at %q from %q",
505                                 l.Addr().Network(),
506                                 conn.LocalAddr(),
507                                 conn.RemoteAddr(),
508                         ).SetLevel(log.Debug).Log(cl.logger)
509                         torrent.Add(fmt.Sprintf("accepted conn remote IP len=%d", len(addrIpOrNil(conn.RemoteAddr()))), 1)
510                         torrent.Add(fmt.Sprintf("accepted conn network=%s", conn.RemoteAddr().Network()), 1)
511                         torrent.Add(fmt.Sprintf("accepted on %s listener", l.Addr().Network()), 1)
512                 }()
513         }
514 }
515
516 // Creates the PeerConn.connString for a regular net.Conn PeerConn.
517 func regularNetConnPeerConnConnString(nc net.Conn) string {
518         return fmt.Sprintf("%s-%s", nc.LocalAddr(), nc.RemoteAddr())
519 }
520
521 func (cl *Client) incomingConnection(nc net.Conn) {
522         defer nc.Close()
523         if tc, ok := nc.(*net.TCPConn); ok {
524                 tc.SetLinger(0)
525         }
526         c := cl.newConnection(nc, false, nc.RemoteAddr(), nc.RemoteAddr().Network(),
527                 regularNetConnPeerConnConnString(nc))
528         defer c.close()
529         c.Discovery = PeerSourceIncoming
530         cl.runReceivedConn(c)
531 }
532
533 // Returns a handle to the given torrent, if it's present in the client.
534 func (cl *Client) Torrent(ih metainfo.Hash) (t *Torrent, ok bool) {
535         cl.lock()
536         defer cl.unlock()
537         t, ok = cl.torrents[ih]
538         return
539 }
540
541 func (cl *Client) torrent(ih metainfo.Hash) *Torrent {
542         return cl.torrents[ih]
543 }
544
545 type dialResult struct {
546         Conn    net.Conn
547         Network string
548 }
549
550 func countDialResult(err error) {
551         if err == nil {
552                 torrent.Add("successful dials", 1)
553         } else {
554                 torrent.Add("unsuccessful dials", 1)
555         }
556 }
557
558 func reducedDialTimeout(minDialTimeout, max time.Duration, halfOpenLimit int, pendingPeers int) (ret time.Duration) {
559         ret = max / time.Duration((pendingPeers+halfOpenLimit)/halfOpenLimit)
560         if ret < minDialTimeout {
561                 ret = minDialTimeout
562         }
563         return
564 }
565
566 // Returns whether an address is known to connect to a client with our own ID.
567 func (cl *Client) dopplegangerAddr(addr string) bool {
568         _, ok := cl.dopplegangerAddrs[addr]
569         return ok
570 }
571
572 // Returns a connection over UTP or TCP, whichever is first to connect.
573 func (cl *Client) dialFirst(ctx context.Context, addr string) (res dialResult) {
574         {
575                 t := perf.NewTimer(perf.CallerName(0))
576                 defer func() {
577                         if res.Conn == nil {
578                                 t.Mark(fmt.Sprintf("returned no conn (context: %v)", ctx.Err()))
579                         } else {
580                                 t.Mark("returned conn over " + res.Network)
581                         }
582                 }()
583         }
584         ctx, cancel := context.WithCancel(ctx)
585         // As soon as we return one connection, cancel the others.
586         defer cancel()
587         left := 0
588         resCh := make(chan dialResult, left)
589         func() {
590                 cl.lock()
591                 defer cl.unlock()
592                 cl.eachDialer(func(s Dialer) bool {
593                         func() {
594                                 left++
595                                 //cl.logger.Printf("dialing %s on %s/%s", addr, s.Addr().Network(), s.Addr())
596                                 go func() {
597                                         resCh <- dialResult{
598                                                 cl.dialFromSocket(ctx, s, addr),
599                                                 s.LocalAddr().Network(),
600                                         }
601                                 }()
602                         }()
603                         return true
604                 })
605         }()
606         // Wait for a successful connection.
607         func() {
608                 defer perf.ScopeTimer()()
609                 for ; left > 0 && res.Conn == nil; left-- {
610                         res = <-resCh
611                 }
612         }()
613         // There are still incompleted dials.
614         go func() {
615                 for ; left > 0; left-- {
616                         conn := (<-resCh).Conn
617                         if conn != nil {
618                                 conn.Close()
619                         }
620                 }
621         }()
622         if res.Conn != nil {
623                 go torrent.Add(fmt.Sprintf("network dialed first: %s", res.Conn.RemoteAddr().Network()), 1)
624         }
625         //if res.Conn != nil {
626         //      cl.logger.Printf("first connection for %s from %s/%s", addr, res.Conn.LocalAddr().Network(), res.Conn.LocalAddr().String())
627         //} else {
628         //      cl.logger.Printf("failed to dial %s", addr)
629         //}
630         return res
631 }
632
633 func (cl *Client) dialFromSocket(ctx context.Context, s Dialer, addr string) net.Conn {
634         network := s.LocalAddr().Network()
635         cte := cl.config.ConnTracker.Wait(
636                 ctx,
637                 conntrack.Entry{network, s.LocalAddr().String(), addr},
638                 "dial torrent client",
639                 0,
640         )
641         // Try to avoid committing to a dial if the context is complete as it's difficult to determine
642         // which dial errors allow us to forget the connection tracking entry handle.
643         if ctx.Err() != nil {
644                 if cte != nil {
645                         cte.Forget()
646                 }
647                 return nil
648         }
649         c, err := s.Dial(ctx, addr)
650         // This is a bit optimistic, but it looks non-trivial to thread this through the proxy code. Set
651         // it now in case we close the connection forthwith.
652         if tc, ok := c.(*net.TCPConn); ok {
653                 tc.SetLinger(0)
654         }
655         countDialResult(err)
656         if c == nil {
657                 if err != nil && forgettableDialError(err) {
658                         cte.Forget()
659                 } else {
660                         cte.Done()
661                 }
662                 return nil
663         }
664         return closeWrapper{c, func() error {
665                 err := c.Close()
666                 cte.Done()
667                 return err
668         }}
669 }
670
671 func forgettableDialError(err error) bool {
672         return strings.Contains(err.Error(), "no suitable address found")
673 }
674
675 func (cl *Client) noLongerHalfOpen(t *Torrent, addr string) {
676         if _, ok := t.halfOpen[addr]; !ok {
677                 panic("invariant broken")
678         }
679         delete(t.halfOpen, addr)
680         cl.numHalfOpen--
681         for _, t := range cl.torrents {
682                 t.openNewConns()
683         }
684 }
685
686 // Performs initiator handshakes and returns a connection. Returns nil *connection if no connection
687 // for valid reasons.
688 func (cl *Client) initiateProtocolHandshakes(
689         ctx context.Context,
690         nc net.Conn,
691         t *Torrent,
692         outgoing, encryptHeader bool,
693         remoteAddr PeerRemoteAddr,
694         network, connString string,
695 ) (
696         c *PeerConn, err error,
697 ) {
698         c = cl.newConnection(nc, outgoing, remoteAddr, network, connString)
699         c.headerEncrypted = encryptHeader
700         ctx, cancel := context.WithTimeout(ctx, cl.config.HandshakesTimeout)
701         defer cancel()
702         dl, ok := ctx.Deadline()
703         if !ok {
704                 panic(ctx)
705         }
706         err = nc.SetDeadline(dl)
707         if err != nil {
708                 panic(err)
709         }
710         err = cl.initiateHandshakes(c, t)
711         return
712 }
713
714 // Returns nil connection and nil error if no connection could be established for valid reasons.
715 func (cl *Client) establishOutgoingConnEx(t *Torrent, addr PeerRemoteAddr, obfuscatedHeader bool) (*PeerConn, error) {
716         dialCtx, cancel := context.WithTimeout(context.Background(), func() time.Duration {
717                 cl.rLock()
718                 defer cl.rUnlock()
719                 return t.dialTimeout()
720         }())
721         defer cancel()
722         dr := cl.dialFirst(dialCtx, addr.String())
723         nc := dr.Conn
724         if nc == nil {
725                 if dialCtx.Err() != nil {
726                         return nil, xerrors.Errorf("dialing: %w", dialCtx.Err())
727                 }
728                 return nil, errors.New("dial failed")
729         }
730         c, err := cl.initiateProtocolHandshakes(context.Background(), nc, t, true, obfuscatedHeader, addr, dr.Network, regularNetConnPeerConnConnString(nc))
731         if err != nil {
732                 nc.Close()
733         }
734         return c, err
735 }
736
737 // Returns nil connection and nil error if no connection could be established
738 // for valid reasons.
739 func (cl *Client) establishOutgoingConn(t *Torrent, addr PeerRemoteAddr) (c *PeerConn, err error) {
740         torrent.Add("establish outgoing connection", 1)
741         obfuscatedHeaderFirst := cl.config.HeaderObfuscationPolicy.Preferred
742         c, err = cl.establishOutgoingConnEx(t, addr, obfuscatedHeaderFirst)
743         if err == nil {
744                 torrent.Add("initiated conn with preferred header obfuscation", 1)
745                 return
746         }
747         //cl.logger.Printf("error establishing connection to %s (obfuscatedHeader=%t): %v", addr, obfuscatedHeaderFirst, err)
748         if cl.config.HeaderObfuscationPolicy.RequirePreferred {
749                 // We should have just tried with the preferred header obfuscation. If it was required,
750                 // there's nothing else to try.
751                 return
752         }
753         // Try again with encryption if we didn't earlier, or without if we did.
754         c, err = cl.establishOutgoingConnEx(t, addr, !obfuscatedHeaderFirst)
755         if err == nil {
756                 torrent.Add("initiated conn with fallback header obfuscation", 1)
757         }
758         //cl.logger.Printf("error establishing fallback connection to %v: %v", addr, err)
759         return
760 }
761
762 // Called to dial out and run a connection. The addr we're given is already
763 // considered half-open.
764 func (cl *Client) outgoingConnection(t *Torrent, addr PeerRemoteAddr, ps PeerSource, trusted bool) {
765         cl.dialRateLimiter.Wait(context.Background())
766         c, err := cl.establishOutgoingConn(t, addr)
767         cl.lock()
768         defer cl.unlock()
769         // Don't release lock between here and addPeerConn, unless it's for
770         // failure.
771         cl.noLongerHalfOpen(t, addr.String())
772         if err != nil {
773                 if cl.config.Debug {
774                         cl.logger.Printf("error establishing outgoing connection to %v: %v", addr, err)
775                 }
776                 return
777         }
778         defer c.close()
779         c.Discovery = ps
780         c.trusted = trusted
781         t.runHandshookConnLoggingErr(c)
782 }
783
784 // The port number for incoming peer connections. 0 if the client isn't listening.
785 func (cl *Client) incomingPeerPort() int {
786         return cl.LocalPort()
787 }
788
789 func (cl *Client) initiateHandshakes(c *PeerConn, t *Torrent) error {
790         if c.headerEncrypted {
791                 var rw io.ReadWriter
792                 var err error
793                 rw, c.cryptoMethod, err = mse.InitiateHandshake(
794                         struct {
795                                 io.Reader
796                                 io.Writer
797                         }{c.r, c.w},
798                         t.infoHash[:],
799                         nil,
800                         cl.config.CryptoProvides,
801                 )
802                 c.setRW(rw)
803                 if err != nil {
804                         return xerrors.Errorf("header obfuscation handshake: %w", err)
805                 }
806         }
807         ih, err := cl.connBtHandshake(c, &t.infoHash)
808         if err != nil {
809                 return xerrors.Errorf("bittorrent protocol handshake: %w", err)
810         }
811         if ih != t.infoHash {
812                 return errors.New("bittorrent protocol handshake: peer infohash didn't match")
813         }
814         return nil
815 }
816
817 // Calls f with any secret keys. Note that it takes the Client lock, and so must be used from code
818 // that won't also try to take the lock. This saves us copying all the infohashes everytime.
819 func (cl *Client) forSkeys(f func([]byte) bool) {
820         cl.rLock()
821         defer cl.rUnlock()
822         if false { // Emulate the bug from #114
823                 var firstIh InfoHash
824                 for ih := range cl.torrents {
825                         firstIh = ih
826                         break
827                 }
828                 for range cl.torrents {
829                         if !f(firstIh[:]) {
830                                 break
831                         }
832                 }
833                 return
834         }
835         for ih := range cl.torrents {
836                 if !f(ih[:]) {
837                         break
838                 }
839         }
840 }
841
842 func (cl *Client) handshakeReceiverSecretKeys() mse.SecretKeyIter {
843         if ret := cl.config.Callbacks.ReceiveEncryptedHandshakeSkeys; ret != nil {
844                 return ret
845         }
846         return cl.forSkeys
847 }
848
849 // Do encryption and bittorrent handshakes as receiver.
850 func (cl *Client) receiveHandshakes(c *PeerConn) (t *Torrent, err error) {
851         defer perf.ScopeTimerErr(&err)()
852         var rw io.ReadWriter
853         rw, c.headerEncrypted, c.cryptoMethod, err = handleEncryption(c.rw(), cl.handshakeReceiverSecretKeys(), cl.config.HeaderObfuscationPolicy, cl.config.CryptoSelector)
854         c.setRW(rw)
855         if err == nil || err == mse.ErrNoSecretKeyMatch {
856                 if c.headerEncrypted {
857                         torrent.Add("handshakes received encrypted", 1)
858                 } else {
859                         torrent.Add("handshakes received unencrypted", 1)
860                 }
861         } else {
862                 torrent.Add("handshakes received with error while handling encryption", 1)
863         }
864         if err != nil {
865                 if err == mse.ErrNoSecretKeyMatch {
866                         err = nil
867                 }
868                 return
869         }
870         if cl.config.HeaderObfuscationPolicy.RequirePreferred && c.headerEncrypted != cl.config.HeaderObfuscationPolicy.Preferred {
871                 err = errors.New("connection does not have required header obfuscation")
872                 return
873         }
874         ih, err := cl.connBtHandshake(c, nil)
875         if err != nil {
876                 err = xerrors.Errorf("during bt handshake: %w", err)
877                 return
878         }
879         cl.lock()
880         t = cl.torrents[ih]
881         cl.unlock()
882         return
883 }
884
885 func (cl *Client) connBtHandshake(c *PeerConn, ih *metainfo.Hash) (ret metainfo.Hash, err error) {
886         res, err := pp.Handshake(c.rw(), ih, cl.peerID, cl.config.Extensions)
887         if err != nil {
888                 return
889         }
890         ret = res.Hash
891         c.PeerExtensionBytes = res.PeerExtensionBits
892         c.PeerID = res.PeerID
893         c.completedHandshake = time.Now()
894         if cb := cl.config.Callbacks.CompletedHandshake; cb != nil {
895                 cb(c, res.Hash)
896         }
897         return
898 }
899
900 func (cl *Client) runReceivedConn(c *PeerConn) {
901         err := c.conn.SetDeadline(time.Now().Add(cl.config.HandshakesTimeout))
902         if err != nil {
903                 panic(err)
904         }
905         t, err := cl.receiveHandshakes(c)
906         if err != nil {
907                 log.Fmsg(
908                         "error receiving handshakes on %v: %s", c, err,
909                 ).SetLevel(log.Debug).
910                         Add(
911                                 "network", c.Network,
912                         ).Log(cl.logger)
913                 torrent.Add("error receiving handshake", 1)
914                 cl.lock()
915                 cl.onBadAccept(c.RemoteAddr)
916                 cl.unlock()
917                 return
918         }
919         if t == nil {
920                 torrent.Add("received handshake for unloaded torrent", 1)
921                 log.Fmsg("received handshake for unloaded torrent").SetLevel(log.Debug).Log(cl.logger)
922                 cl.lock()
923                 cl.onBadAccept(c.RemoteAddr)
924                 cl.unlock()
925                 return
926         }
927         torrent.Add("received handshake for loaded torrent", 1)
928         cl.lock()
929         defer cl.unlock()
930         t.runHandshookConnLoggingErr(c)
931 }
932
933 // Client lock must be held before entering this.
934 func (cl *Client) runHandshookConn(c *PeerConn, t *Torrent) error {
935         c.setTorrent(t)
936         if c.PeerID == cl.peerID {
937                 if c.outgoing {
938                         connsToSelf.Add(1)
939                         addr := c.conn.RemoteAddr().String()
940                         cl.dopplegangerAddrs[addr] = struct{}{}
941                 } else {
942                         // Because the remote address is not necessarily the same as its client's torrent listen
943                         // address, we won't record the remote address as a doppleganger. Instead, the initiator
944                         // can record *us* as the doppleganger.
945                 }
946                 return errors.New("local and remote peer ids are the same")
947         }
948         c.conn.SetWriteDeadline(time.Time{})
949         c.r = deadlineReader{c.conn, c.r}
950         completedHandshakeConnectionFlags.Add(c.connectionFlags(), 1)
951         if connIsIpv6(c.conn) {
952                 torrent.Add("completed handshake over ipv6", 1)
953         }
954         if err := t.addPeerConn(c); err != nil {
955                 return fmt.Errorf("adding connection: %w", err)
956         }
957         defer t.dropConnection(c)
958         go c.writer(time.Minute)
959         cl.sendInitialMessages(c, t)
960         err := c.mainReadLoop()
961         if err != nil {
962                 return fmt.Errorf("main read loop: %w", err)
963         }
964         return nil
965 }
966
967 // See the order given in Transmission's tr_peerMsgsNew.
968 func (cl *Client) sendInitialMessages(conn *PeerConn, torrent *Torrent) {
969         if conn.PeerExtensionBytes.SupportsExtended() && cl.config.Extensions.SupportsExtended() {
970                 conn.post(pp.Message{
971                         Type:       pp.Extended,
972                         ExtendedID: pp.HandshakeExtendedID,
973                         ExtendedPayload: func() []byte {
974                                 msg := pp.ExtendedHandshakeMessage{
975                                         M: map[pp.ExtensionName]pp.ExtensionNumber{
976                                                 pp.ExtensionNameMetadata: metadataExtendedId,
977                                         },
978                                         V: cl.config.ExtendedHandshakeClientVersion,
979                                         // If peer requests are buffered on read, this instructs the amount of memory
980                                         // that might be used to cache pending writes. Assuming 512KiB cached for
981                                         // sending, for 16KiB chunks.
982                                         Reqq:         1 << 5,
983                                         YourIp:       pp.CompactIp(conn.remoteIp()),
984                                         Encryption:   cl.config.HeaderObfuscationPolicy.Preferred || !cl.config.HeaderObfuscationPolicy.RequirePreferred,
985                                         Port:         cl.incomingPeerPort(),
986                                         MetadataSize: torrent.metadataSize(),
987                                         // TODO: We can figured these out specific to the socket
988                                         // used.
989                                         Ipv4: pp.CompactIp(cl.config.PublicIp4.To4()),
990                                         Ipv6: cl.config.PublicIp6.To16(),
991                                 }
992                                 if !cl.config.DisablePEX {
993                                         msg.M[pp.ExtensionNamePex] = pexExtendedId
994                                 }
995                                 return bencode.MustMarshal(msg)
996                         }(),
997                 })
998         }
999         func() {
1000                 if conn.fastEnabled() {
1001                         if torrent.haveAllPieces() {
1002                                 conn.post(pp.Message{Type: pp.HaveAll})
1003                                 conn.sentHaves.AddRange(0, bitmap.BitIndex(conn.t.NumPieces()))
1004                                 return
1005                         } else if !torrent.haveAnyPieces() {
1006                                 conn.post(pp.Message{Type: pp.HaveNone})
1007                                 conn.sentHaves.Clear()
1008                                 return
1009                         }
1010                 }
1011                 conn.postBitfield()
1012         }()
1013         if conn.PeerExtensionBytes.SupportsDHT() && cl.config.Extensions.SupportsDHT() && cl.haveDhtServer() {
1014                 conn.post(pp.Message{
1015                         Type: pp.Port,
1016                         Port: cl.dhtPort(),
1017                 })
1018         }
1019 }
1020
1021 func (cl *Client) dhtPort() (ret uint16) {
1022         cl.eachDhtServer(func(s DhtServer) {
1023                 ret = uint16(missinggo.AddrPort(s.Addr()))
1024         })
1025         return
1026 }
1027
1028 func (cl *Client) haveDhtServer() (ret bool) {
1029         cl.eachDhtServer(func(_ DhtServer) {
1030                 ret = true
1031         })
1032         return
1033 }
1034
1035 // Process incoming ut_metadata message.
1036 func (cl *Client) gotMetadataExtensionMsg(payload []byte, t *Torrent, c *PeerConn) error {
1037         var d map[string]int
1038         err := bencode.Unmarshal(payload, &d)
1039         if _, ok := err.(bencode.ErrUnusedTrailingBytes); ok {
1040         } else if err != nil {
1041                 return fmt.Errorf("error unmarshalling bencode: %s", err)
1042         }
1043         msgType, ok := d["msg_type"]
1044         if !ok {
1045                 return errors.New("missing msg_type field")
1046         }
1047         piece := d["piece"]
1048         switch msgType {
1049         case pp.DataMetadataExtensionMsgType:
1050                 c.allStats(add(1, func(cs *ConnStats) *Count { return &cs.MetadataChunksRead }))
1051                 if !c.requestedMetadataPiece(piece) {
1052                         return fmt.Errorf("got unexpected piece %d", piece)
1053                 }
1054                 c.metadataRequests[piece] = false
1055                 begin := len(payload) - metadataPieceSize(d["total_size"], piece)
1056                 if begin < 0 || begin >= len(payload) {
1057                         return fmt.Errorf("data has bad offset in payload: %d", begin)
1058                 }
1059                 t.saveMetadataPiece(piece, payload[begin:])
1060                 c.lastUsefulChunkReceived = time.Now()
1061                 err = t.maybeCompleteMetadata()
1062                 if err != nil {
1063                         // Log this at the Torrent-level, as we don't partition metadata by Peer yet, so we
1064                         // don't know who to blame. TODO: Also errors can be returned here that aren't related
1065                         // to verifying metadata, which should be fixed. This should be tagged with metadata, so
1066                         // log consumers can filter for this message.
1067                         t.logger.WithDefaultLevel(log.Warning).Printf("error completing metadata: %v", err)
1068                 }
1069                 return err
1070         case pp.RequestMetadataExtensionMsgType:
1071                 if !t.haveMetadataPiece(piece) {
1072                         c.post(t.newMetadataExtensionMessage(c, pp.RejectMetadataExtensionMsgType, d["piece"], nil))
1073                         return nil
1074                 }
1075                 start := (1 << 14) * piece
1076                 c.logger.WithDefaultLevel(log.Debug).Printf("sending metadata piece %d", piece)
1077                 c.post(t.newMetadataExtensionMessage(c, pp.DataMetadataExtensionMsgType, piece, t.metadataBytes[start:start+t.metadataPieceSize(piece)]))
1078                 return nil
1079         case pp.RejectMetadataExtensionMsgType:
1080                 return nil
1081         default:
1082                 return errors.New("unknown msg_type value")
1083         }
1084 }
1085
1086 func (cl *Client) badPeerAddr(addr PeerRemoteAddr) bool {
1087         if ipa, ok := tryIpPortFromNetAddr(addr); ok {
1088                 return cl.badPeerIPPort(ipa.IP, ipa.Port)
1089         }
1090         return false
1091 }
1092
1093 func (cl *Client) badPeerIPPort(ip net.IP, port int) bool {
1094         if port == 0 {
1095                 return true
1096         }
1097         if cl.dopplegangerAddr(net.JoinHostPort(ip.String(), strconv.FormatInt(int64(port), 10))) {
1098                 return true
1099         }
1100         if _, ok := cl.ipBlockRange(ip); ok {
1101                 return true
1102         }
1103         if _, ok := cl.badPeerIPs[ip.String()]; ok {
1104                 return true
1105         }
1106         return false
1107 }
1108
1109 // Return a Torrent ready for insertion into a Client.
1110 func (cl *Client) newTorrent(ih metainfo.Hash, specStorage storage.ClientImpl) (t *Torrent) {
1111         // use provided storage, if provided
1112         storageClient := cl.defaultStorage
1113         if specStorage != nil {
1114                 storageClient = storage.NewClient(specStorage)
1115         }
1116
1117         t = &Torrent{
1118                 cl:       cl,
1119                 infoHash: ih,
1120                 peers: prioritizedPeers{
1121                         om: btree.New(32),
1122                         getPrio: func(p PeerInfo) peerPriority {
1123                                 ipPort := p.addr()
1124                                 return bep40PriorityIgnoreError(cl.publicAddr(ipPort.IP), ipPort)
1125                         },
1126                 },
1127                 conns: make(map[*PeerConn]struct{}, 2*cl.config.EstablishedConnsPerTorrent),
1128
1129                 halfOpen:          make(map[string]PeerInfo),
1130                 pieceStateChanges: pubsub.NewPubSub(),
1131
1132                 storageOpener:       storageClient,
1133                 maxEstablishedConns: cl.config.EstablishedConnsPerTorrent,
1134
1135                 networkingEnabled: true,
1136                 metadataChanged: sync.Cond{
1137                         L: cl.locker(),
1138                 },
1139                 webSeeds: make(map[string]*Peer),
1140         }
1141         t._pendingPieces.NewSet = priorityBitmapStableNewSet
1142         t.requestStrategy = cl.config.DefaultRequestStrategy(t.requestStrategyCallbacks(), &cl._mu)
1143         t.logger = cl.logger.WithContextValue(t)
1144         t.setChunkSize(defaultChunkSize)
1145         return
1146 }
1147
1148 // A file-like handle to some torrent data resource.
1149 type Handle interface {
1150         io.Reader
1151         io.Seeker
1152         io.Closer
1153         io.ReaderAt
1154 }
1155
1156 func (cl *Client) AddTorrentInfoHash(infoHash metainfo.Hash) (t *Torrent, new bool) {
1157         return cl.AddTorrentInfoHashWithStorage(infoHash, nil)
1158 }
1159
1160 // Adds a torrent by InfoHash with a custom Storage implementation.
1161 // If the torrent already exists then this Storage is ignored and the
1162 // existing torrent returned with `new` set to `false`
1163 func (cl *Client) AddTorrentInfoHashWithStorage(infoHash metainfo.Hash, specStorage storage.ClientImpl) (t *Torrent, new bool) {
1164         cl.lock()
1165         defer cl.unlock()
1166         t, ok := cl.torrents[infoHash]
1167         if ok {
1168                 return
1169         }
1170         new = true
1171
1172         t = cl.newTorrent(infoHash, specStorage)
1173         cl.eachDhtServer(func(s DhtServer) {
1174                 go t.dhtAnnouncer(s)
1175         })
1176         cl.torrents[infoHash] = t
1177         cl.clearAcceptLimits()
1178         t.updateWantPeersEvent()
1179         // Tickle Client.waitAccept, new torrent may want conns.
1180         cl.event.Broadcast()
1181         return
1182 }
1183
1184 // Add or merge a torrent spec. Returns new if the torrent wasn't already in the client. See also
1185 // Torrent.MergeSpec.
1186 func (cl *Client) AddTorrentSpec(spec *TorrentSpec) (t *Torrent, new bool, err error) {
1187         t, new = cl.AddTorrentInfoHashWithStorage(spec.InfoHash, spec.Storage)
1188         err = t.MergeSpec(spec)
1189         if err != nil && new {
1190                 t.Drop()
1191         }
1192         return
1193 }
1194
1195 type stringAddr string
1196
1197 var _ net.Addr = stringAddr("")
1198
1199 func (stringAddr) Network() string   { return "" }
1200 func (me stringAddr) String() string { return string(me) }
1201
1202 // The trackers will be merged with the existing ones. If the Info isn't yet known, it will be set.
1203 // spec.DisallowDataDownload/Upload will be read and applied
1204 // The display name is replaced if the new spec provides one. Note that any `Storage` is ignored.
1205 func (t *Torrent) MergeSpec(spec *TorrentSpec) error {
1206         if spec.DisplayName != "" {
1207                 t.SetDisplayName(spec.DisplayName)
1208         }
1209         if spec.InfoBytes != nil {
1210                 err := t.SetInfoBytes(spec.InfoBytes)
1211                 if err != nil {
1212                         return err
1213                 }
1214         }
1215         cl := t.cl
1216         cl.AddDhtNodes(spec.DhtNodes)
1217         cl.lock()
1218         defer cl.unlock()
1219         useTorrentSources(spec.Sources, t)
1220         for _, url := range spec.Webseeds {
1221                 t.addWebSeed(url)
1222         }
1223         for _, peerAddr := range spec.PeerAddrs {
1224                 t.addPeer(PeerInfo{
1225                         Addr:    stringAddr(peerAddr),
1226                         Source:  PeerSourceDirect,
1227                         Trusted: true,
1228                 })
1229         }
1230         if spec.ChunkSize != 0 {
1231                 t.setChunkSize(pp.Integer(spec.ChunkSize))
1232         }
1233         t.addTrackers(spec.Trackers)
1234         t.maybeNewConns()
1235         t.dataDownloadDisallowed = spec.DisallowDataDownload
1236         t.dataUploadDisallowed = spec.DisallowDataUpload
1237         return nil
1238 }
1239
1240 func useTorrentSources(sources []string, t *Torrent) {
1241         for _, s := range sources {
1242                 go func(s string) {
1243                         err := useTorrentSource(s, t)
1244                         if err != nil {
1245                                 t.logger.WithDefaultLevel(log.Warning).Printf("using torrent source %q: %v", s, err)
1246                         } else {
1247                                 t.logger.Printf("successfully used source %q", s)
1248                         }
1249                 }(s)
1250         }
1251 }
1252
1253 func useTorrentSource(source string, t *Torrent) error {
1254         req, err := http.NewRequest(http.MethodGet, source, nil)
1255         if err != nil {
1256                 panic(err)
1257         }
1258         ctx, cancel := context.WithCancel(context.Background())
1259         defer cancel()
1260         go func() {
1261                 select {
1262                 case <-t.GotInfo():
1263                 case <-t.Closed():
1264                 case <-ctx.Done():
1265                 }
1266                 cancel()
1267         }()
1268         req = req.WithContext(ctx)
1269         resp, err := http.DefaultClient.Do(req)
1270         if err != nil {
1271                 return err
1272         }
1273         mi, err := metainfo.Load(resp.Body)
1274         if err != nil {
1275                 if ctx.Err() != nil {
1276                         return nil
1277                 }
1278                 return err
1279         }
1280         return t.MergeSpec(TorrentSpecFromMetaInfo(mi))
1281 }
1282
1283 func (cl *Client) dropTorrent(infoHash metainfo.Hash) (err error) {
1284         t, ok := cl.torrents[infoHash]
1285         if !ok {
1286                 err = fmt.Errorf("no such torrent")
1287                 return
1288         }
1289         err = t.close()
1290         if err != nil {
1291                 panic(err)
1292         }
1293         delete(cl.torrents, infoHash)
1294         return
1295 }
1296
1297 func (cl *Client) allTorrentsCompleted() bool {
1298         for _, t := range cl.torrents {
1299                 if !t.haveInfo() {
1300                         return false
1301                 }
1302                 if !t.haveAllPieces() {
1303                         return false
1304                 }
1305         }
1306         return true
1307 }
1308
1309 // Returns true when all torrents are completely downloaded and false if the
1310 // client is stopped before that.
1311 func (cl *Client) WaitAll() bool {
1312         cl.lock()
1313         defer cl.unlock()
1314         for !cl.allTorrentsCompleted() {
1315                 if cl.closed.IsSet() {
1316                         return false
1317                 }
1318                 cl.event.Wait()
1319         }
1320         return true
1321 }
1322
1323 // Returns handles to all the torrents loaded in the Client.
1324 func (cl *Client) Torrents() []*Torrent {
1325         cl.lock()
1326         defer cl.unlock()
1327         return cl.torrentsAsSlice()
1328 }
1329
1330 func (cl *Client) torrentsAsSlice() (ret []*Torrent) {
1331         for _, t := range cl.torrents {
1332                 ret = append(ret, t)
1333         }
1334         return
1335 }
1336
1337 func (cl *Client) AddMagnet(uri string) (T *Torrent, err error) {
1338         spec, err := TorrentSpecFromMagnetUri(uri)
1339         if err != nil {
1340                 return
1341         }
1342         T, _, err = cl.AddTorrentSpec(spec)
1343         return
1344 }
1345
1346 func (cl *Client) AddTorrent(mi *metainfo.MetaInfo) (T *Torrent, err error) {
1347         T, _, err = cl.AddTorrentSpec(TorrentSpecFromMetaInfo(mi))
1348         return
1349 }
1350
1351 func (cl *Client) AddTorrentFromFile(filename string) (T *Torrent, err error) {
1352         mi, err := metainfo.LoadFromFile(filename)
1353         if err != nil {
1354                 return
1355         }
1356         return cl.AddTorrent(mi)
1357 }
1358
1359 func (cl *Client) DhtServers() []DhtServer {
1360         return cl.dhtServers
1361 }
1362
1363 func (cl *Client) AddDhtNodes(nodes []string) {
1364         for _, n := range nodes {
1365                 hmp := missinggo.SplitHostMaybePort(n)
1366                 ip := net.ParseIP(hmp.Host)
1367                 if ip == nil {
1368                         cl.logger.Printf("won't add DHT node with bad IP: %q", hmp.Host)
1369                         continue
1370                 }
1371                 ni := krpc.NodeInfo{
1372                         Addr: krpc.NodeAddr{
1373                                 IP:   ip,
1374                                 Port: hmp.Port,
1375                         },
1376                 }
1377                 cl.eachDhtServer(func(s DhtServer) {
1378                         s.AddNode(ni)
1379                 })
1380         }
1381 }
1382
1383 func (cl *Client) banPeerIP(ip net.IP) {
1384         cl.logger.Printf("banning ip %v", ip)
1385         if cl.badPeerIPs == nil {
1386                 cl.badPeerIPs = make(map[string]struct{})
1387         }
1388         cl.badPeerIPs[ip.String()] = struct{}{}
1389 }
1390
1391 func (cl *Client) newConnection(nc net.Conn, outgoing bool, remoteAddr PeerRemoteAddr, network, connString string) (c *PeerConn) {
1392         if network == "" {
1393                 panic(remoteAddr)
1394         }
1395         c = &PeerConn{
1396                 Peer: Peer{
1397                         outgoing:        outgoing,
1398                         choking:         true,
1399                         peerChoking:     true,
1400                         PeerMaxRequests: 250,
1401
1402                         RemoteAddr: remoteAddr,
1403                         Network:    network,
1404                         callbacks:  &cl.config.Callbacks,
1405                 },
1406                 connString:  connString,
1407                 conn:        nc,
1408                 writeBuffer: new(bytes.Buffer),
1409         }
1410         c.peerImpl = c
1411         c.logger = cl.logger.WithDefaultLevel(log.Warning).WithContextValue(c)
1412         c.writerCond.L = cl.locker()
1413         c.setRW(connStatsReadWriter{nc, c})
1414         c.r = &rateLimitedReader{
1415                 l: cl.config.DownloadRateLimiter,
1416                 r: c.r,
1417         }
1418         c.logger.WithDefaultLevel(log.Debug).Printf("initialized with remote %v over network %v (outgoing=%t)", remoteAddr, network, outgoing)
1419         for _, f := range cl.config.Callbacks.NewPeer {
1420                 f(&c.Peer)
1421         }
1422         return
1423 }
1424
1425 func (cl *Client) onDHTAnnouncePeer(ih metainfo.Hash, ip net.IP, port int, portOk bool) {
1426         cl.lock()
1427         defer cl.unlock()
1428         t := cl.torrent(ih)
1429         if t == nil {
1430                 return
1431         }
1432         t.addPeers([]PeerInfo{{
1433                 Addr:   ipPortAddr{ip, port},
1434                 Source: PeerSourceDhtAnnouncePeer,
1435         }})
1436 }
1437
1438 func firstNotNil(ips ...net.IP) net.IP {
1439         for _, ip := range ips {
1440                 if ip != nil {
1441                         return ip
1442                 }
1443         }
1444         return nil
1445 }
1446
1447 func (cl *Client) eachDialer(f func(Dialer) bool) {
1448         for _, s := range cl.dialers {
1449                 if !f(s) {
1450                         break
1451                 }
1452         }
1453 }
1454
1455 func (cl *Client) eachListener(f func(Listener) bool) {
1456         for _, s := range cl.listeners {
1457                 if !f(s) {
1458                         break
1459                 }
1460         }
1461 }
1462
1463 func (cl *Client) findListener(f func(Listener) bool) (ret Listener) {
1464         cl.eachListener(func(l Listener) bool {
1465                 ret = l
1466                 return !f(l)
1467         })
1468         return
1469 }
1470
1471 func (cl *Client) publicIp(peer net.IP) net.IP {
1472         // TODO: Use BEP 10 to determine how peers are seeing us.
1473         if peer.To4() != nil {
1474                 return firstNotNil(
1475                         cl.config.PublicIp4,
1476                         cl.findListenerIp(func(ip net.IP) bool { return ip.To4() != nil }),
1477                 )
1478         }
1479
1480         return firstNotNil(
1481                 cl.config.PublicIp6,
1482                 cl.findListenerIp(func(ip net.IP) bool { return ip.To4() == nil }),
1483         )
1484 }
1485
1486 func (cl *Client) findListenerIp(f func(net.IP) bool) net.IP {
1487         l := cl.findListener(
1488                 func(l Listener) bool {
1489                         return f(addrIpOrNil(l.Addr()))
1490                 },
1491         )
1492         if l == nil {
1493                 return nil
1494         }
1495         return addrIpOrNil(l.Addr())
1496 }
1497
1498 // Our IP as a peer should see it.
1499 func (cl *Client) publicAddr(peer net.IP) IpPort {
1500         return IpPort{IP: cl.publicIp(peer), Port: uint16(cl.incomingPeerPort())}
1501 }
1502
1503 // ListenAddrs addresses currently being listened to.
1504 func (cl *Client) ListenAddrs() (ret []net.Addr) {
1505         cl.lock()
1506         defer cl.unlock()
1507         cl.eachListener(func(l Listener) bool {
1508                 ret = append(ret, l.Addr())
1509                 return true
1510         })
1511         return
1512 }
1513
1514 func (cl *Client) onBadAccept(addr PeerRemoteAddr) {
1515         ipa, ok := tryIpPortFromNetAddr(addr)
1516         if !ok {
1517                 return
1518         }
1519         ip := maskIpForAcceptLimiting(ipa.IP)
1520         if cl.acceptLimiter == nil {
1521                 cl.acceptLimiter = make(map[ipStr]int)
1522         }
1523         cl.acceptLimiter[ipStr(ip.String())]++
1524 }
1525
1526 func maskIpForAcceptLimiting(ip net.IP) net.IP {
1527         if ip4 := ip.To4(); ip4 != nil {
1528                 return ip4.Mask(net.CIDRMask(24, 32))
1529         }
1530         return ip
1531 }
1532
1533 func (cl *Client) clearAcceptLimits() {
1534         cl.acceptLimiter = nil
1535 }
1536
1537 func (cl *Client) acceptLimitClearer() {
1538         for {
1539                 select {
1540                 case <-cl.closed.LockedChan(cl.locker()):
1541                         return
1542                 case <-time.After(15 * time.Minute):
1543                         cl.lock()
1544                         cl.clearAcceptLimits()
1545                         cl.unlock()
1546                 }
1547         }
1548 }
1549
1550 func (cl *Client) rateLimitAccept(ip net.IP) bool {
1551         if cl.config.DisableAcceptRateLimiting {
1552                 return false
1553         }
1554         return cl.acceptLimiter[ipStr(maskIpForAcceptLimiting(ip).String())] > 0
1555 }
1556
1557 func (cl *Client) rLock() {
1558         cl._mu.RLock()
1559 }
1560
1561 func (cl *Client) rUnlock() {
1562         cl._mu.RUnlock()
1563 }
1564
1565 func (cl *Client) lock() {
1566         cl._mu.Lock()
1567 }
1568
1569 func (cl *Client) unlock() {
1570         cl._mu.Unlock()
1571 }
1572
1573 func (cl *Client) locker() *lockWithDeferreds {
1574         return &cl._mu
1575 }
1576
1577 func (cl *Client) String() string {
1578         return fmt.Sprintf("<%[1]T %[1]p>", cl)
1579 }
1580
1581 // Returns connection-level aggregate stats at the Client level. See the comment on
1582 // TorrentStats.ConnStats.
1583 func (cl *Client) ConnStats() ConnStats {
1584         return cl.stats.Copy()
1585 }