Documentation
¶
Index ¶
- Constants
- Variables
- func CompactNodeInfos(nodes []*NodeInfo) []byte
- func GetMyIP() (string, error)
- func IsOnline(ip string, port int) bool
- func LoadKRPCErrorMsg(data map[string]interface{}) error
- type KRPCMessage
- type KRPCQuery
- type KRPCResponse
- type Node
- func (node *Node) AnnouncePeer(addr *net.UDPAddr, infoHash []byte, token string, impliedPort int8, port int) error
- func (node *Node) FindNode(addr *net.UDPAddr, nid NodeID) error
- func (node *Node) GetPeers(addr *net.UDPAddr, infoHash []byte) error
- func (node *Node) Ping(addr *net.UDPAddr) error
- func (node *Node) Serve(opts ...NodeOption) error
- func (node *Node) WaitSignal() error
- type NodeID
- type NodeInfo
- type NodeOption
- type QueryType
- type TokenManager
Constants ¶
const ( NodeIDBits = 160 NodeIDBytes = NodeIDBits / 8 NodeInfoEncodedLength = 26 )
const (
RANDOM = "random"
)
Variables ¶
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") )
var ErrUnKnowQueryType = errors.New("Unknow query type")
Functions ¶
func CompactNodeInfos ¶
func LoadKRPCErrorMsg ¶
Types ¶
type KRPCMessage ¶
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 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 ¶
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 ¶
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 ¶
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 ¶
type NodeID ¶
type NodeID [NodeIDBytes]byte
func GenerateNodeID ¶
func GenerateNodeID() NodeID
func GetNeighborNID ¶
type NodeOption ¶
type NodeOption func(node *Node)
func OptionAddress ¶
func OptionAddress(addr string) NodeOption
func OptionNodeID ¶
func OptionNodeID(nid string) NodeOption
type TokenManager ¶
type TokenManager struct {
}
func (*TokenManager) GenToken ¶
func (*TokenManager) GenToken() []byte