dht

package
v0.0.0-...-2c5efcf Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2019 License: MIT Imports: 12 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PackNodes

func PackNodes(nodes []*Node) (string, error)

PackNodes packs nodes

Types

type AnnouncePeerArgs

type AnnouncePeerArgs struct {
	NodeID   string `bencode:"id"`
	InfoHash string `bencode:"info_hash"`
	Port     int    `bencode:"port"`
	Token    string `bencode:"token"`
}

AnnouncePeerArgs is announce_peer query "a" field

type CrawCallback

type CrawCallback func(infoHash string, peerIP net.IP, peerPort int)

CrawCallback will call when Crawler craw a infohash

type Crawler

type Crawler interface {
	Crawl(ctx context.Context, callback CrawCallback) error
}

Crawler can crawl infohash from dht.

type FindNodeArgs

type FindNodeArgs struct {
	NodeID string `bencode:"id"`
	Target string `bencode:"target"`
}

FindNodeArgs is find_node query "a" field

type GetPeersArgs

type GetPeersArgs struct {
	NodeID   string `bencode:"id"`
	InfoHash string `bencode:"info_hash"`
}

GetPeersArgs is get_peers query "a" field

type Handle

type Handle interface {
	NodeID() string
	SendMessage(dst *net.UDPAddr, msg interface{}) error
}

Handle used for operate dht.

type Host

type Host struct {
	// contains filtered or unexported fields
}

Host is the core of dht.

func NewHost

func NewHost(host string) (*Host, error)

NewHost returns a new Core instance.

func (*Host) Addr

func (core *Host) Addr() net.Addr

Addr returns the Addr of self.

func (*Host) Handle

func (core *Host) Handle(nodeID string) Handle

Handle returns a handle

func (*Host) SendMessage

func (core *Host) SendMessage(dst *net.UDPAddr, msg interface{}) error

SendMessage will send message to the node.

func (*Host) Serv

func (core *Host) Serv(ctx context.Context, disposer MessageDisposer) (err error)

Serv starts serving.

func (*Host) SetMaxWorkers

func (core *Host) SetMaxWorkers(n int)

SetMaxWorkers set the max goroutine will be create to dispose dht message. If maxWorkers smaller than 0. it won't set upper limit.

type KRPCErr

type KRPCErr struct {
	// contains filtered or unexported fields
}

KRPCErr is krpc errors

func (*KRPCErr) Code

func (kerr *KRPCErr) Code() int

Code returns code

func (*KRPCErr) Describe

func (kerr *KRPCErr) Describe() string

Describe returns describe

func (*KRPCErr) Error

func (kerr *KRPCErr) Error() string

func (KRPCErr) MarshalBencode

func (kerr KRPCErr) MarshalBencode() ([]byte, error)

MarshalBencode implements bencode.Marshaler

func (*KRPCErr) UnmarshalBencode

func (kerr *KRPCErr) UnmarshalBencode(b []byte) error

UnmarshalBencode implements bencode.Unmarshaler

type Message

type Message struct {
	TransactionID string             `bencode:"t"`
	Y             string             `bencode:"y"`
	Q             string             `bencode:"q,omitempty"`
	Args          bencode.RawMessage `bencode:"a,omitempty"`
	Resp          bencode.RawMessage `bencode:"r,omitempty"`
	Err           *KRPCErr           `bencode:"e,omitempty"`
}

Message is KRPC message

func MakeError

func MakeError(transactionID string, err *KRPCErr) (*Message, error)

MakeError make a krpc error Message

func MakeQuery

func MakeQuery(transactionID string, args interface{}) (*Message, error)

MakeQuery make a krpc query Message

func MakeQueryEx

func MakeQueryEx(q string, transactionID string, args interface{}) (*Message, error)

MakeQueryEx can make query optional q

func MakeResponse

func MakeResponse(transactionID string, resp interface{}) (*Message, error)

MakeResponse make a krpc resp Message

type MessageDisposer

type MessageDisposer interface {
	DisposeQuery(src *net.UDPAddr, transactionID string, q string, args bencode.RawMessage) error
	DisposeResponse(src *net.UDPAddr, transactionID string, resp bencode.RawMessage) error
	DisposeError(src *net.UDPAddr, transactionID string, code int, describe string) error
	DisposeUnknownMessage(src *net.UDPAddr, message bencode.RawMessage) error
}

MessageDisposer is used for dispose message.

type Node

type Node struct {
	Addr *net.UDPAddr
	ID   string
}

Node is dht node.

type NodePtrSlice

type NodePtrSlice []*Node

NodePtrSlice is []*Node

func (NodePtrSlice) MarshalBencode

func (nds NodePtrSlice) MarshalBencode() ([]byte, error)

MarshalBencode implements bencode.Marshaler

func (*NodePtrSlice) UnmarshalBencode

func (nds *NodePtrSlice) UnmarshalBencode(data []byte) error

UnmarshalBencode implemts bencode.Unmarshaler

type PingArgs

type PingArgs struct {
	NodeID string `bencode:"id"`
}

PingArgs is ping query "a" field

type Response

type Response struct {
	NodeID string        `bencode:"id"`
	Nodes  *NodePtrSlice `bencode:"nodes,omitempty"`
	Token  string        `bencode:"token,omitempty"`
	Values []string      `bencode:"values,omitempty"`
}

Response is reponse "r" field

type Stimulater

type Stimulater interface {
	Stimulate()
}

Stimulater could be stimulate

type SybilCrawler

type SybilCrawler struct {
	// contains filtered or unexported fields
}

SybilCrawler can crawl info hash from DHT.

func NewSybilCrawler

func NewSybilCrawler(host string) *SybilCrawler

NewSybilCrawler returns a new Crawler instance.

func (*SybilCrawler) Crawl

func (crawler *SybilCrawler) Crawl(ctx context.Context, callback CrawCallback) error

Crawl ovrride Crawler.Crawl

func (*SybilCrawler) SetMaxWorkers

func (crawler *SybilCrawler) SetMaxWorkers(n int)

SetMaxWorkers set the max goroutine will be create to dispose dht message. If maxWorkers smaller than 0. it won't set upper limit.

func (*SybilCrawler) Stimulate

func (crawler *SybilCrawler) Stimulate()

Stimulate implements Stimulater

type Transaction

type Transaction interface {
	ID() string
	ShelfLife() time.Duration

	OnLaunch(handle Handle)
	OnFinish(handle Handle)
	OnResponse(handle Handle, src *net.UDPAddr, resp bencode.RawMessage) bool
	OnError(handle Handle, src *net.UDPAddr, code int, describe string) bool
	OnTimeout(handle Handle) bool
}

Transaction is KRPC transaction.

type TransactionDispatcher

type TransactionDispatcher struct {
	// contains filtered or unexported fields
}

TransactionDispatcher can dispatch transactions

func NewTransactionDispatcher

func NewTransactionDispatcher(handle Handle) *TransactionDispatcher

NewTransactionDispatcher creates a new TransactionDispatcher

func (*TransactionDispatcher) Add

func (dispatcher *TransactionDispatcher) Add(t Transaction) error

Add the transaction into dispathcher and launch it

func (*TransactionDispatcher) DisposeError

func (dispatcher *TransactionDispatcher) DisposeError(src *net.UDPAddr, transactionID string, code int, describe string)

DisposeError dispose error

func (*TransactionDispatcher) DisposeResponse

func (dispatcher *TransactionDispatcher) DisposeResponse(src *net.UDPAddr, transactionID string, resp bencode.RawMessage)

DisposeResponse dispose response.

func (*TransactionDispatcher) Remove

func (dispatcher *TransactionDispatcher) Remove(transactionID string)

Remove the transaction from the dispatcher.

Jump to

Keyboard shortcuts

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