v0.0.0-...-0cf3de4 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Apr 9, 2016 License: MPL-2.0, MIT Imports: 27 Imported by: 0



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



View Source
const CompactIPv4NodeInfoLen = 26

The size in bytes of a NodeInfo in its compact binary representation.


This section is empty.


func NodeIdSecure

func NodeIdSecure(id string, ip net.IP) bool

Returns whether the node ID is considered secure. The id is the 20 raw bytes. http://www.libtorrent.org/dht_sec.html

func SecureNodeId

func SecureNodeId(id []byte, ip net.IP)

Makes a node ID secure, in-place. The ID is 20 raw bytes. http://www.libtorrent.org/dht_sec.html


type Addr

type Addr interface {
	UDPAddr() *net.UDPAddr
	String() string

Used internally to refer to node network addresses.

func NewAddr

func NewAddr(ua *net.UDPAddr) Addr

type Announce

type Announce struct {
	Peers chan PeersValues
	// contains filtered or unexported fields

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

func (*Announce) Close

func (me *Announce) Close()

Stop the announce.

func (*Announce) NumContacted

func (me *Announce) NumContacted() int

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

type CompactIPv4NodeInfo

type CompactIPv4NodeInfo []NodeInfo

func (CompactIPv4NodeInfo) MarshalBencode

func (me CompactIPv4NodeInfo) MarshalBencode() (ret []byte, err error)

func (*CompactIPv4NodeInfo) UnmarshalBencode

func (me *CompactIPv4NodeInfo) UnmarshalBencode(_b []byte) (err error)

type KRPCError

type KRPCError struct {
	Code int
	Msg  string

Represented as a string or list in bencode.

func (KRPCError) Error

func (me KRPCError) Error() string

func (KRPCError) MarshalBencode

func (me KRPCError) MarshalBencode() (ret []byte, err error)

func (*KRPCError) UnmarshalBencode

func (me *KRPCError) UnmarshalBencode(_b []byte) (err error)

type Msg

type Msg struct {
	Q  string           `bencode:"q,omitempty"` // Query method (one of 4: "ping", "find_node", "get_peers", "announce_peer")
	A  *MsgArgs         `bencode:"a,omitempty"` // named arguments sent with a query
	T  string           `bencode:"t"`           // required: transaction ID
	Y  string           `bencode:"y"`           // required: type of the message: q for QUERY, r for RESPONSE, e for ERROR
	R  *Return          `bencode:"r,omitempty"` // RESPONSE type only
	E  *KRPCError       `bencode:"e,omitempty"` // ERROR type only
	IP util.CompactPeer `bencode:"ip,omitempty"`

Msg represents messages that nodes in the network send to each other as specified by the protocol. They are also refered to as the KRPC messages. There are three types of messages: QUERY, RESPONSE, ERROR The message is a dictonary that is then "bencoded" (serialization & compression format adopted by the BitTorrent) and sent via the UDP connection to peers.

A KRPC message is a single dictionary with two keys common to every message and additional keys depending on the type of message. Every message has a key "t" with a string value representing a transaction ID. This transaction ID is generated by the querying node and is echoed in the response, so responses may be correlated with multiple queries to the same node. The transaction ID should be encoded as a short string of binary numbers, typically 2 characters are enough as they cover 2^16 outstanding queries. The other key contained in every KRPC message is "y" with a single character value describing the type of message. The value of the "y" key is one of "q" for query, "r" for response, or "e" for error. 3 message types: QUERY, RESPONSE, ERROR

func (Msg) Error

func (m Msg) Error() *KRPCError

func (Msg) SenderID

func (m Msg) SenderID() string

The node ID of the source of this Msg.

func (Msg) String

func (m Msg) String() string

type MsgArgs

type MsgArgs struct {
	ID       string `bencode:"id"`        // ID of the quirying Node
	InfoHash string `bencode:"info_hash"` // InfoHash of the torrent
	Target   string `bencode:"target"`    // ID of the node sought

type NodeInfo

type NodeInfo struct {
	ID   [20]byte
	Addr Addr

func (*NodeInfo) PutCompact

func (ni *NodeInfo) PutCompact(b []byte) error

Writes the node info to its compact binary representation in b. See CompactNodeInfoLen.

func (*NodeInfo) UnmarshalCompactIPv4

func (cni *NodeInfo) UnmarshalCompactIPv4(b []byte) error

type Peer

type Peer struct {
	IP   net.IP
	Port int

func (*Peer) String

func (me *Peer) String() string

type PeersValues

type PeersValues struct {
	Peers    []Peer // Peers given in get_peers response.
	NodeInfo        // The node that gave the response.

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.

type Return

type Return struct {
	ID     string              `bencode:"id"` // ID of the querying node
	Nodes  CompactIPv4NodeInfo `bencode:"nodes,omitempty"`
	Token  string              `bencode:"token,omitempty"`
	Values []util.CompactPeer  `bencode:"values,omitempty"`

type Server

type Server struct {
	// contains filtered or unexported fields

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

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

NewServer initializes a new DHT node server.

func (*Server) AddNode

func (s *Server) AddNode(ni NodeInfo)

Adds directly to the node table.

func (*Server) Addr

func (s *Server) Addr() net.Addr

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

func (*Server) Announce

func (s *Server) Announce(infoHash string, port int, impliedPort bool) (*Announce, error)

This is kind of the main thing you want to do with DHT. It traverses the graph toward nodes that store peers for the infohash, streaming them to the caller, and announcing the local node to each node if allowed and specified.

func (*Server) Close

func (s *Server) Close()

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

func (*Server) ID

func (s *Server) ID() string

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

func (*Server) IPBlocklist

func (s *Server) IPBlocklist() iplist.Ranger

func (*Server) Nodes

func (s *Server) Nodes() (nis []NodeInfo)

Exports the current node table.

func (*Server) NumNodes

func (s *Server) NumNodes() int

Returns how many nodes are in the node table.

func (*Server) Ping

func (s *Server) Ping(node *net.UDPAddr) (*Transaction, error)

Sends a ping query to the address given.

func (*Server) SetIPBlockList

func (s *Server) SetIPBlockList(list iplist.Ranger)

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

func (*Server) Stats

func (s *Server) Stats() (ss ServerStats)

Stats returns statistics for the server.

func (*Server) String

func (s *Server) String() string

Returns a description of the Server. Python repr-style.

type ServerConfig

type ServerConfig struct {
	// Listen address. Used if Conn is nil.
	Addr string

	// Set NodeId Manually. Caller must ensure that, if NodeId does not
	// conform to DHT Security Extensions, that NoSecurity is also set. This
	// should be given as a HEX string.
	NodeIdHex string

	Conn net.PacketConn
	// Don't respond to queries from other nodes.
	Passive bool
	// DHT Bootstrap nodes
	BootstrapNodes []string
	// Disable bootstrapping from global servers even if given no BootstrapNodes.
	// This creates a solitary node that awaits other nodes; it's only useful if
	// you're creating your own DHT and want to avoid accidental crossover, without
	// spoofing a bootstrap node and filling your logs with connection errors.
	NoDefaultBootstrap bool

	// Disable the DHT security extension:
	// http://www.libtorrent.org/dht_sec.html.
	NoSecurity bool
	// Initial IP blocklist to use. Applied before serving and bootstrapping
	// begins.
	IPBlocklist iplist.Ranger
	// Used to secure the server's ID. Defaults to the Conn's LocalAddr().
	PublicIP net.IP

	OnQuery func(*Msg, net.Addr) bool

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

type ServerStats

type ServerStats struct {
	// Count of nodes in the node table that responded to our last query or
	// haven't yet been queried.
	GoodNodes int
	// Count of nodes in the node table.
	Nodes int
	// Transactions awaiting a response.
	OutstandingTransactions int
	// Individual announce_peer requests that got a success response.
	ConfirmedAnnounces int
	// Nodes that have been blocked.
	BadNodes uint

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

type Transaction

type Transaction struct {
	// contains filtered or unexported fields

Transaction keeps track of a message exchange between nodes, such as a query message and a response message.

func (*Transaction) Close

func (t *Transaction) Close()

Close (abandon) the transaction.

func (*Transaction) SetResponseHandler

func (t *Transaction) SetResponseHandler(f func(Msg, bool))

SetResponseHandler sets up a function to be called when query response arrives.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL