dht package - github.com/anacrolix/dht/v2 - Go Packages

Package dht implements a Distributed Hash Table (DHT) part of the BitTorrent protocol, as specified by BEP 5: http://www.bittorrent.org/beps/bep_0005.html

BitTorrent uses a "distributed hash table" (DHT) for storing peer contact information for "trackerless" torrents. In effect, each peer becomes a tracker. The protocol is based on Kademila DHT protocol and is implemented over UDP.

Please note the terminology used to avoid confusion. A "peer" is a client/server listening on a TCP port that implements the BitTorrent protocol. A "node" is a client/server listening on a UDP port implementing the distributed hash table protocol. The DHT is composed of nodes and stores the location of peers. BitTorrent clients include a DHT node, which is used to contact other nodes in the DHT to get the location of peers to download from using the BitTorrent protocol.

Standard use involves creating a Server, and calling Announce on it with the details of your local torrent client and infohash of interest.

This section is empty.

View Source

var DefaultGlobalBootstrapHostPorts = []string{
	"router.utorrent.com:6881",
	"router.bittorrent.com:6881",
	"dht.transmissionbt.com:6881",
	"dht.aelitis.com:6881",
	"router.silotis.us:6881",
	"dht.libtorrent.org:25401",
	"dht.anacrolix.link:42069",
	"router.bittorrent.cloud:42069",
}
func HashTuple(bs ...[]byte) (ret [20]byte)

func RandomNodeID deprecated

func RandomNodeID() (id krpc.ID)

Deprecated: Use function from krpc.

Used internally to refer to node network addresses. String() is called a lot, and so can be optimized. Network() is not exposed, so that the interface does not satisfy net.Addr, as the underlying type must be passed to any OS-level function that take net.Addr.

Returns the resolved addresses of the default global bootstrap nodes. Network is unused but was historically passed by anacrolix/torrent.

func ResolveHostPorts(hostPorts []string) (addrs []Addr, err error)

Resolves host:port strings to dht.Addrs, using the dht DNS resolver cache. Suitable for use with ServerConfig.BootstrapAddrs.

type Announce struct {
	Peers chan PeersValues
	
}

Maintains state for an ongoing Announce operation. An Announce is started by calling Server.Announce.

func (a *Announce) Close()

Stop the announce.

Traversal and peer announcing steps are done.

Returns the number of distinct remote addresses the announce has queried.

func (a *Announce) StopTraversing()

Halts traversal, but won't block peer announcing.

func (a *Announce) TraversalStats() TraversalStats
type AnnounceOpt func(a *Announce)

Server.Announce option

func AnnouncePeer(opts AnnouncePeerOpts) AnnounceOpt

Finish an Announce get_peers traversal with an announce of a local peer.

func Scrape() AnnounceOpt

Scrape BEP 33 bloom filters in queries.

type AnnouncePeerOpts struct {
	
	Port int
	
	ImpliedPort bool
}

Arguments for announce_peer from a Server.Announce.

Corresponds to the "values" key in a get_peers KRPC response. A list of peers that a node has reported as being in the swarm for a queried info hash.

The zero value for this uses reasonable/traditional defaults on Server methods.

type QueryRateLimiting struct {
	
	NotFirst bool
	
	NotAny        bool
	WaitOnRetries bool
	NoWaitFirst   bool
}

Rate-limiting to be applied to writes for a given query. Queries occur inside transactions that will attempt to send several times. If the STM rate-limiting helpers are used, the first send is often already accounted for in the rate-limiting machinery before the query method that does the IO is invoked.

type QueryResult struct {
	Reply  krpc.Msg
	Writes numWrites
	Err    error
}

Converts a Server QueryResult to a traversal.QueryResult.

A Server defines parameters for a DHT node server that is able to send queries, and respond to the ones from the network. Each node has a globally unique identifier known as the "node ID." Node IDs are chosen at random from the same 160-bit space as BitTorrent infohashes and define the behaviour of the node. Zero valued Server does not have a valid ID and thus is unable to function properly. Use `NewServer(nil)` to initialize a default node.

func NewServer(c *ServerConfig) (s *Server, err error)

NewServer initializes a new DHT node server.

Adds directly to the node table.

Addr returns the listen address for the server. Packets arriving to this address are processed by the server (unless aliens are involved).

func (s *Server) Announce(infoHash [20]byte, port int, impliedPort bool, opts ...AnnounceOpt) (_ *Announce, err error)

Deprecated: Use Server.AnnounceTraversal. Traverses the DHT graph toward nodes that store peers for the infohash, streaming them to the caller, and announcing the local node to each responding node if port is non-zero or impliedPort is true.

func (s *Server) AnnounceTraversal(infoHash [20]byte, opts ...AnnounceOpt) (_ *Announce, err error)

Traverses the DHT graph toward nodes that store peers for the infohash, streaming them to the caller.

Populates the node table.

Stops the server network activity. This is all that's required to clean-up a Server.

func (s *Server) FindNode(addr Addr, targetID int160.T, rl QueryRateLimiting) (ret QueryResult)

Sends a find_node query to addr. targetID is the node we're looking for. The Server makes use of some of the response fields.

Get gets item information from a specific target ID. If seq is set to a specific value, only items with seq bigger than the one provided will return a V, K and Sig, if any. Get must be used to get a Put write token, when you want to write an item instead of read it.

func (s *Server) ID() [20]byte

ID returns the 20-byte server ID. This is the ID used to communicate with the DHT network.

func (s *Server) IsGood(n *node) bool

Per the spec in BEP 5.

func (s *Server) IsQuestionable(n *node) bool
func (s *Server) NodeRespondedToPing(addr Addr, id int160.T)

Returns non-bad nodes from the routing table.

func (s *Server) NumNodes() int

Returns how many nodes are in the node table.

Sends a ping query to the address given.

Sends a ping query to the address given.

Put adds a new item to node. You need to call Get first for a write token.

Performs an arbitrary query. `q` is the query value, defined by the DHT BEP. `a` should contain the appropriate argument values, if any. `a.ID` is clobbered by the Server. Responses to queries made this way are not interpreted by the Server. More specific methods like FindNode and GetPeers may make use of the response internally before passing it back to the caller.

Packets to and from any address matching a range in the list are dropped.

func (s *Server) Stats() ServerStats

Stats returns statistics for the server.

Returns a description of the Server.

func (*Server) TableMaintainer ΒΆ added in v2.10.0

func (s *Server) TableMaintainer()

A routine that maintains the Server's routing table, by pinging questionable nodes, and refreshing buckets. This should be invoked on a running Server when the caller is satisfied with having set it up. It is not necessary to explicitly Bootstrap the Server once this routine has started.

func (s *Server) TraversalNodeFilter(node addrMaybeId) bool

Whether we should consider a node for contact based on its address and possible ID.

func (s *Server) TraversalStartingNodes() (nodes []addrMaybeId, err error)

ServerConfig allows setting up a configuration of the `Server` instance to be created with NewServer.

func NewDefaultServerConfig() *ServerConfig
func (c *ServerConfig) InitNodeId() (deterministic bool)

If the NodeId hasn't been specified, generate a suitable one. deterministic if c.Conn and c.PublicIP are non-nil.

type ServerStats struct {
	
	
	GoodNodes int
	
	Nodes int
	
	OutstandingTransactions int
	
	SuccessfulOutboundAnnouncePeerQueries int64
	
	BadNodes                 uint
	OutboundQueriesAttempted int64
}

ServerStats instance is returned by Server.Stats() and stores Server metrics

type StartingNodesGetter func() ([]Addr, error)