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