dht

package module
v0.0.0-...-feeebe0 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2016 License: MIT Imports: 20 Imported by: 0

README

DHT

See the video on the Youtube.

中文版README

Introduction

DHT implements the bittorrent DHT protocol in Go. Now it includes:

It contains two modes, the standard mode and the crawling mode. The standard mode follows the BEPs, and you can use it as a standard dht server. The crawling mode aims to crawl as more metadata info as possiple. It doesn't follow the standard BEPs protocol. With the crawling mode, you can build another BTDigg.

bthub.io is a BT search engine based on the crawling mode.

Installation

go get github.com/shiyanhui/dht

Example

Below is a simple spider. You can move here to see more samples.

import (
    "fmt"
    "github.com/shiyanhui/dht"
)

func main() {
    downloader := dht.NewWire(65535)
    go func() {
        // once we got the request result
        for resp := range downloader.Response() {
            fmt.Println(resp.InfoHash, resp.MetadataInfo)
        }
    }()
    go downloader.Run()

    config := dht.NewCrawlConfig()
    config.OnAnnouncePeer = func(infoHash, ip string, port int) {
        // request to download the metadata info
        downloader.Request([]byte(infoHash), ip, port)
    }
    d := dht.New(config)

    d.Run()
}

Download

You can download the demo compiled binary file here.

TODO

  • NAT Traversal.
  • Implements the full BEP-3.
  • Optimization.

License

MIT, read more here

Documentation

Overview

Package dht implements the bittorrent dht protocol. For more information see http://www.bittorrent.org/beps/bep_0005.html.

Index

Constants

View Source
const (
	// StandardMode follows the standard protocol
	StandardMode = iota
	// CrawlMode for crawling the dht network.
	CrawlMode
)
View Source
const (
	// REQUEST represents request message type
	REQUEST = iota
	// DATA represents data message type
	DATA
	// REJECT represents reject message type
	REJECT
)
View Source
const (
	// BLOCK is 2 ^ 14
	BLOCK = 16384
	// MaxMetadataSize represents the max medata it can accept
	MaxMetadataSize = BLOCK * 1000
	// EXTENDED represents it is a extended message
	EXTENDED = 20
	// HANDSHAKE represents handshake bit
	HANDSHAKE = 0
)

Variables

This section is empty.

Functions

func Decode

func Decode(data []byte) (result interface{}, err error)

Decode decodes a bencoded string to string, int, list or map.

func DecodeDict

func DecodeDict(data []byte, start int) (
	result interface{}, index int, err error)

DecodeDict decodes a map value.

func DecodeInt

func DecodeInt(data []byte, start int) (
	result interface{}, index int, err error)

DecodeInt decodes int value in the data.

func DecodeList

func DecodeList(data []byte, start int) (
	result interface{}, index int, err error)

DecodeList decodes a list value.

func DecodeString

func DecodeString(data []byte, start int) (
	result interface{}, index int, err error)

DecodeString decodes a string in the data. It returns a tuple (decoded result, the end position, error).

func Encode

func Encode(data interface{}) string

Encode encodes a string, int, dict or list value to a bencoded string.

func EncodeDict

func EncodeDict(data map[string]interface{}) string

EncodeDict encodes a dict value.

func EncodeInt

func EncodeInt(data int) string

EncodeInt encodes a int value.

func EncodeList

func EncodeList(data []interface{}) string

EncodeList encodes a list value.

func EncodeString

func EncodeString(data string) string

EncodeString encodes a string value.

Types

type Config

type Config struct {
	K                    int
	KBucketSize          int
	Network              string
	Address              string
	PrimeNodes           []string
	KBucketExpiredAfter  time.Duration
	NodeExpriedAfter     time.Duration
	CheckKBucketPeriod   time.Duration
	TokenExpiredAfter    time.Duration
	MaxTransactionCursor uint64
	MaxNodes             int
	OnGetPeers           func(string, string, int)
	OnAnnouncePeer       func(string, string, int)
	BlockedIPs           []string
	BlackListMaxSize     int
	Mode                 int
	Try                  int
}

Config represents the configure of dht.

func NewCrawlConfig

func NewCrawlConfig() *Config

NewCrawlConfig returns a config in crawling mode.

func NewStandardConfig

func NewStandardConfig() *Config

NewStandardConfig returns a Config pointer with default values.

type DHT

type DHT struct {
	*Config

	Ready bool
	// contains filtered or unexported fields
}

DHT represents a DHT node.

func New

func New(config *Config) *DHT

New returns a DHT pointer. If config is nil, then config will be set to the default config.

func (*DHT) GetPeers

func (dht *DHT) GetPeers(infoHash string) ([]*Peer, error)

GetPeers returns peers who have announced having infoHash.

func (*DHT) IsCrawlMode

func (dht *DHT) IsCrawlMode() bool

IsCrawlMode returns whether mode is CrawlMode.

func (*DHT) IsStandardMode

func (dht *DHT) IsStandardMode() bool

IsStandardMode returns whether mode is StandardMode.

func (*DHT) Run

func (dht *DHT) Run()

Run starts the dht.

type Peer

type Peer struct {
	IP   net.IP
	Port int
	// contains filtered or unexported fields
}

Peer represents a peer contact.

func (*Peer) CompactIPPortInfo

func (p *Peer) CompactIPPortInfo() string

CompactIPPortInfo returns "Compact node info". See http://www.bittorrent.org/beps/bep_0005.html.

type Request

type Request struct {
	InfoHash []byte
	IP       string
	Port     int
}

Request represents the request context.

type Response

type Response struct {
	Request
	MetadataInfo []byte
}

Response contains the request context and the metadata info.

type Wire

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

Wire represents the wire protocol.

func NewWire

func NewWire(blackListMaxSize int) *Wire

NewWire returns a Wire pointer.

func (*Wire) Request

func (wire *Wire) Request(infoHash []byte, ip string, port int)

Request pushes the request to the queue.

func (*Wire) Response

func (wire *Wire) Response() <-chan Response

Response returns a chan of Response.

func (*Wire) Run

func (wire *Wire) Run()

Run starts the peer wire protocol.

Directories

Path Synopsis
sample
getpeers command
spider command

Jump to

Keyboard shortcuts

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