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