Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DecodePeerAddress ¶
DecodePeerAddress transforms the binary-encoded host:port address into a human-readable format. So, "abcdef" becomes 97.98.99.100:25958.
Types ¶
type DHT ¶
type DHT struct { Logger Logger // Public channels: PeersRequestResults chan map[InfoHash][]string // key = infohash, v = slice of peers. // contains filtered or unexported fields }
DHT should be created by NewDHTNode(). It provides DHT features to a torrent client, such as finding new peers for torrent downloads without requiring a tracker.
Example ¶
port := rand.Intn(10000) + 40000 d, err := NewDHTNode(port, 100, false) if err != nil { fmt.Println(err) return } go d.DoDHT() infoHash, err := DecodeInfoHash("d1c5676ae7ac98e8b19f63565905105e3c4c37a2") if err != nil { fmt.Printf("DecodeInfoHash faiure: %v", err) return } // Give the DHT some time to "warm-up" its routing table. time.Sleep(5 * time.Second) d.PeersRequest(string(infoHash), false) timeout := time.After(30 * time.Second) var infoHashPeers map[InfoHash][]string select { case infoHashPeers = <-d.PeersRequestResults: break case <-timeout: fmt.Printf("Could not find new peers: timed out") return } for ih, peers := range infoHashPeers { if len(peers) > 0 { fmt.Printf("peer found for infohash [%x]\n", ih) // Peers are encoded in binary format. Decoding example using github.com/nictuku/nettools: // for _, peer := range peers { // fmt.Println(DecodePeerAddress(peer)) // } return } }
Output: peer found for infohash [d1c5676ae7ac98e8b19f63565905105e3c4c37a2]
func NewDHTNode ¶
func (*DHT) AddNode ¶
AddNode informs the DHT of a new node it should add to its routing table. addr is a string containing the target node's "host:port" UDP address.
func (*DHT) DoDHT ¶
func (d *DHT) DoDHT()
DoDHT is the DHT node main loop and should be run as a goroutine by the torrent client.
func (*DHT) PeersRequest ¶
PeersRequest asks the DHT to search for more peers for the infoHash provided. announce should be true if the connected peer is actively downloading this infohash, which is normally the case - unless this DHT node is just a router that doesn't downloads torrents.
type DHTStore ¶
type DHTStore struct { // The rest of the stack uses string, but that confuses the json // Marshaller. []byte is more correct anyway. Id []byte Port int Remotes map[string][]byte // Key: IP, Value: node ID. // contains filtered or unexported fields }
DHTStore is used to persist the routing table on disk.
type InfoHash ¶
type InfoHash string
func DecodeInfoHash ¶
DecodeInfoHash transforms a hex-encoded 20-characters string to a binary infohash.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
examples
|
|
find_infohash_and_wait
Runs a node on UDP port 11221 that attempt to collect 100 peers for an infohash, then keeps running as a passive DHT node.
|
Runs a node on UDP port 11221 that attempt to collect 100 peers for an infohash, then keeps running as a passive DHT node. |