]> Sergey Matveev's repositories - btrtrc.git/blob - dht.go
Drop support for go 1.20
[btrtrc.git] / dht.go
1 package torrent
2
3 import (
4         "io"
5         "net"
6
7         "github.com/anacrolix/dht/v2"
8         "github.com/anacrolix/dht/v2/krpc"
9         peer_store "github.com/anacrolix/dht/v2/peer-store"
10 )
11
12 // DHT server interface for use by a Torrent or Client. It's reasonable for this to make assumptions
13 // for torrent-use that might not be the default behaviour for the DHT server.
14 type DhtServer interface {
15         Stats() interface{}
16         ID() [20]byte
17         Addr() net.Addr
18         AddNode(ni krpc.NodeInfo) error
19         // This is called asynchronously when receiving PORT messages.
20         Ping(addr *net.UDPAddr)
21         Announce(hash [20]byte, port int, impliedPort bool) (DhtAnnounce, error)
22         WriteStatus(io.Writer)
23 }
24
25 // Optional interface for DhtServer's that can expose their peer store (if any).
26 type PeerStorer interface {
27         PeerStore() peer_store.Interface
28 }
29
30 type DhtAnnounce interface {
31         Close()
32         Peers() <-chan dht.PeersValues
33 }
34
35 type AnacrolixDhtServerWrapper struct {
36         *dht.Server
37 }
38
39 func (me AnacrolixDhtServerWrapper) Stats() interface{} {
40         return me.Server.Stats()
41 }
42
43 type anacrolixDhtAnnounceWrapper struct {
44         *dht.Announce
45 }
46
47 func (me anacrolixDhtAnnounceWrapper) Peers() <-chan dht.PeersValues {
48         return me.Announce.Peers
49 }
50
51 func (me AnacrolixDhtServerWrapper) Announce(hash [20]byte, port int, impliedPort bool) (DhtAnnounce, error) {
52         ann, err := me.Server.Announce(hash, port, impliedPort)
53         return anacrolixDhtAnnounceWrapper{ann}, err
54 }
55
56 func (me AnacrolixDhtServerWrapper) Ping(addr *net.UDPAddr) {
57         me.Server.PingQueryInput(addr, dht.QueryInput{
58                 RateLimiting: dht.QueryRateLimiting{NoWaitFirst: true},
59         })
60 }
61
62 var _ DhtServer = AnacrolixDhtServerWrapper{}