dht.go 1.6 KB

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