dht

package module
v0.0.0-...-996c4d4 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2019 License: MIT Imports: 17 Imported by: 1

README

dht

golang dht(Distributed Hash Table) node

Developing!!!
Install
go get -u github.com/bttown/dht
Usage
node := dht.NewNode(dht.OptionAddress("0.0.0.0:8661"))
	node.PeerHandler = func(ip string, port int, hashInfo, peerID string) {
		log.Println("new announce_peer query", hashInfo)
	}
node.Serve()

Documentation

Index

Constants

View Source
const (
	NodeIDBits            = 160
	NodeIDBytes           = NodeIDBits / 8
	NodeInfoEncodedLength = 26
)
View Source
const (
	RANDOM = "random"
)

Variables

View Source
var (
	// KRPCErrGeneric ...
	KRPCErrGeneric = newKRPCError(201, "A Generic Error Ocurred")
	// KRPCErrServer ...
	KRPCErrServer = newKRPCError(201, "A Server Error Ocurred")
	// KPRCErrProtocol ...
	KPRCErrProtocol = newKRPCError(203, "A Protocol Error Ocurred")
	// KPRCErrMalformedPacket ...
	KPRCErrMalformedPacket = newKRPCError(203, "A Protocol Error Ocurred")
	// KRPCErrMethodUnknown ...
	KRPCErrMethodUnknown = newKRPCError(204, "Method Unknown")
)
View Source
var ErrUnKnowQueryType = errors.New("Unknow query type")

Functions

func CompactNodeInfos

func CompactNodeInfos(nodes []*NodeInfo) []byte

func GetMyIP

func GetMyIP() (string, error)

func IsOnline

func IsOnline(ip string, port int) bool

func LoadKRPCErrorMsg

func LoadKRPCErrorMsg(data map[string]interface{}) error

Types

type KRPCMessage

type KRPCMessage struct {
	T string
	Y string
	// contains filtered or unexported fields
}

func NewKRPCMessage

func NewKRPCMessage(b []byte) (*KRPCMessage, error)

func (*KRPCMessage) IsError

func (msg *KRPCMessage) IsError() bool

func (*KRPCMessage) IsQuery

func (msg *KRPCMessage) IsQuery() bool

func (*KRPCMessage) IsResponse

func (msg *KRPCMessage) IsResponse() bool

type KRPCQuery

type KRPCQuery struct {
	T []byte // krpc query token
	Q QueryType

	NID         NodeID
	TargetNID   NodeID
	InfoHash    []byte
	ImpliedPort int8
	Port        int
	Token       string
}

func (*KRPCQuery) Encode

func (query *KRPCQuery) Encode() ([]byte, error)

func (*KRPCQuery) Loads

func (query *KRPCQuery) Loads(data map[string]interface{}) error

type KRPCResponse

type KRPCResponse struct {
	T         []byte
	Q         QueryType
	QueriedID NodeID
	Token     string
	Nodes     []*NodeInfo
	Values    []string
}

func (*KRPCResponse) Encode

func (resp *KRPCResponse) Encode() ([]byte, error)

func (*KRPCResponse) Loads

func (resp *KRPCResponse) Loads(data map[string]interface{}) error

type Node

type Node struct {
	NodeInfo

	NetWork string

	PeerHandler func(ip string, port int, infoHash, peerID string)
	// contains filtered or unexported fields
}

func NewNode

func NewNode(opts ...NodeOption) *Node

func (*Node) AnnouncePeer

func (node *Node) AnnouncePeer(addr *net.UDPAddr, infoHash []byte, token string, impliedPort int8, port int) error

AnnouncePeer announces that the peer, controlling the querying node, is downloading a torrent on a port. announce_peer has four arguments: "id" containing the node ID of the querying node, "info_hash" containing the infohash of the torrent, "port" containing the port as an integer, and the "token" received in response to a previous get_peers query. There is an optional argument called implied_port which value is either 0 or 1. If it is present and non-zero, the port argument should be ignored and the source port of the UDP packet should be used as the peer's port instead. This is useful for peers behind a NAT that may not know their external port, and supporting uTP, they accept incoming. arguments: {"id" : "<querying nodes id>",

"implied_port": <0 or 1>,
"info_hash" : "<20-byte infohash of target torrent>",
"port" : <port number>,
"token" : "<opaque token>"}

reference: http://www.bittorrent.org/beps/bep_0005.html#announce_peer

func (*Node) FindNode

func (node *Node) FindNode(addr *net.UDPAddr, nid NodeID) error

FindNode is used to find the contact information for a node given its ID. "q" == "find_node" A find_node query has two arguments, "id" containing the node ID of the querying node, and "target" containing the ID of the node sought by the queryer. When a node receives a find_node query, it should respond with a key "nodes" and value of a string containing the compact node info for the target node or the K (8) closest good nodes in its own routing table. arguments: {"id" : "<querying nodes id>", "target" : "<id of target node>"} http://www.bittorrent.org/beps/bep_0005.html#find-node

func (*Node) GetPeers

func (node *Node) GetPeers(addr *net.UDPAddr, infoHash []byte) error

GetPeers gets peers associated with a torrent infohash. "q" = "get_peers" A get_peers query has two arguments, "id" containing the node ID of the querying node, and "info_hash" containing the infohash of the torrent. If the queried node has peers for the infohash, they are returned in a key "values" as a list of strings. Each string containing "compact" format peer information for a single peer. If the queried node has no peers for the infohash, a key "nodes" is returned containing the K nodes in the queried nodes routing table closest to the infohash supplied in the query. In either case a "token" key is also

included in the return value. The token value is a required argument for a

future announce_peer query. The token value should be a short binary string. arguments: {"id" : "<querying nodes id>", "info_hash" : "<20-byte infohash of target torrent>"} http://www.bittorrent.org/beps/bep_0005.html#get-peers

func (*Node) Ping

func (node *Node) Ping(addr *net.UDPAddr) error

Ping is the most basic query. "q" = "ping" A ping query has a single argument, "id" the value is a 20-byte string containing the senders node ID in network byte order. The appropriate response to a ping has a single key "id" containing the node ID of the responding node. arguments: {"id" : "<querying nodes id>"} http://www.bittorrent.org/beps/bep_0005.html#ping

func (*Node) Serve

func (node *Node) Serve(opts ...NodeOption) error

func (*Node) WaitSignal

func (node *Node) WaitSignal() error

type NodeID

type NodeID [NodeIDBytes]byte

func GenerateNodeID

func GenerateNodeID() NodeID

func GetNeighborNID

func GetNeighborNID(id NodeID, hash []byte) NodeID

type NodeInfo

type NodeInfo struct {
	ID NodeID
	net.UDPAddr
}

func UnCompactNodeInfos

func UnCompactNodeInfos(b []byte) []*NodeInfo

func (*NodeInfo) GetID

func (info *NodeInfo) GetID() NodeID

func (*NodeInfo) GetStringID

func (info *NodeInfo) GetStringID() string

func (*NodeInfo) String

func (info *NodeInfo) String() string

type NodeOption

type NodeOption func(node *Node)

func OptionAddress

func OptionAddress(addr string) NodeOption

func OptionNodeID

func OptionNodeID(nid string) NodeOption

type QueryType

type QueryType string
var (
	PingType         QueryType = "ping"
	FindNodeType     QueryType = "find_node"
	GetPeersType     QueryType = "get_peers"
	AnnouncePeerType QueryType = "announce_peer"
)

type TokenManager

type TokenManager struct {
}

func (*TokenManager) GenToken

func (*TokenManager) GenToken() []byte

Directories

Path Synopsis
examples
bencode command
find_node command
infohash command

Jump to

Keyboard shortcuts

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