项目作者: mh-cbon

项目描述 :
golang dht / Kademlia
高级语言: Go
项目地址: git://github.com/mh-cbon/dht.git
创建时间: 2017-07-31T20:29:42Z
项目社区:https://github.com/mh-cbon/dht

开源协议:Other

下载


dht

travis Status Go Report Card GoDoc MIT License

Package dht is a kademlia implementation of a Distributed Hash Table.

It implements bep05 / bep42 / bep43 / bep44

Useful resources

TOC

Api

> example/main2.go

  1. // to test repetitive single lookup.
  2. package main
  3. import (
  4. "flag"
  5. "fmt"
  6. "log"
  7. "github.com/mh-cbon/dht/bootstrap"
  8. "github.com/mh-cbon/dht/bucket"
  9. "github.com/mh-cbon/dht/dht"
  10. "github.com/mh-cbon/dht/logger"
  11. )
  12. func main2() {
  13. var v bool
  14. flag.BoolVar(&v, "vv", false, "verbose mode")
  15. flag.Parse()
  16. ready := func(d *dht.DHT) error {
  17. if v {
  18. d.AddLogger(logger.Text(log.Printf))
  19. }
  20. fmt.Println("Running bootstrap...")
  21. publicIP, err := d.BootstrapAuto(nil, bootstrap.Public)
  22. if err != nil {
  23. return err
  24. }
  25. if publicIP != nil {
  26. log.Printf("public IP bootstrap %v:%v\n", publicIP.IP, publicIP.Port)
  27. }
  28. selfID := []byte(d.ID())
  29. fmt.Printf("your node id %x\n", selfID)
  30. // that s good, we are ready.
  31. fmt.Println("Boostrap done...")
  32. // you can run a lookup to find nodes matching an info_hash or target.
  33. // info_hash and target are hex string.
  34. target := "faf5c61ddcc5e8a2dabede0f3b482cd9aea9434c"
  35. targetID, _ := dht.HexToBytes(target)
  36. for {
  37. fmt.Printf("Performing lookup request for %x\n", targetID)
  38. lookupErr := d.LookupStores(target, nil)
  39. if lookupErr != nil {
  40. return lookupErr
  41. }
  42. // then get the closest peers for that target
  43. closest, err := d.ClosestStores(target, 16)
  44. if err != nil {
  45. return err
  46. }
  47. fmt.Printf("Found %v nodes close to %x\n", len(closest), targetID)
  48. for _, c := range closest {
  49. fmt.Printf("%-24v %x %v\n", c.GetAddr(), c.GetID(), bucket.Distance(targetID, c.GetID()))
  50. }
  51. }
  52. return nil
  53. }
  54. if err := dht.New(dht.DefaultOps()).Serve(ready); err != nil {
  55. if err != nil {
  56. log.Fatal(err)
  57. }
  58. }
  59. }

Cli

The provided main.go program helps to play with the bittorrent/dht table.

  1. -annimplied
  2. Announce an implied port.
  3. -annport int
  4. The port of the announce query.
  5. -blocklist string
  6. A peer guardian ip list to block (Some organization:1.0.0.0-1.255.255.255)
  7. -bnodes string
  8. The boostrap node address list seprated by a coma, or public to use pre configured list of public bootstrap nodes, or 'no' to bootstrap empty
  9. -did string
  10. The id of your node in the dht network
  11. -do string
  12. The action to perform boostrap|closest_stores|closest_peers|ping|announce_peer|get_peers|find_node|get|put|genkey|sign (default "boostrap")
  13. -kconcurrency int
  14. The number of nodes to refresh on k-bucket split (default 8)
  15. -keyname string
  16. The name of the key
  17. -ksize int
  18. The size of the k-buckets (default 20)
  19. -listen string
  20. The listen address of the udp socket
  21. -pip string
  22. The public ip of your node to share with other nodes.
  23. -qconcurrency int
  24. The number of simultaneous outgoing requests (default 24)
  25. -qtimeout string
  26. The query timeout (default "1s")
  27. -remote string
  28. The remote address to query ip:port|closest_store|closest_peers
  29. -salt string
  30. The salt to use to put/sign a value.
  31. -seq int
  32. The seq value of get/put.
  33. -target string
  34. Info hash target 20 bytes string hex encoded.
  35. -tsecret string
  36. the secret of the token server
  37. -val string
  38. A value to put.
  39. -vv
  40. verbose mode
  41. -wait
  42. Enter in wait mode after the command ran.

Cli Examples

  1. go run main.go -do closest_peers -target faf5c61ddcc5e8a2dabede0f3b482cd9aea9434c -qtimeout 1s -vv -bnodes public
  2. # run a node on 127...:9091, with an empty bootstrap, verbose, wait mode
  3. go run main.go -listen 127.0.0.1:9091 -wait -bnodes no -vv
  4. # sign a value prints useful information like keys, target etc
  5. go run main.go -do sign -keyname bep44test -val "Hello World!"
  6. go run main.go -do sign -keyname bep44test -val "Hello World!" -salt foobar
  7. # put a value on a specific address (127.0.0.1:9090)
  8. go run main.go -listen 127.0.0.1:9091 -bnodes no -remote "127.0.0.1:9090" -do put -val "Hello World!" -vv
  9. # get a value from a remote
  10. go run main.go -listen 127.0.0.1:9091 -bnodes no -remote "127.0.0.1:9090" -do get -target e5f96f6f38320f0f33959cb4d3d656452117aadb -vv
  11. # put a mutable value on a specific address
  12. go run main.go -listen 127.0.0.1:9091 -bnodes no -remote "127.0.0.1:9090" -do put -keyname bep44test -val "Hello World!" -salt foobar -seq 1 -vv
  13. # get a mutable value on a specific address
  14. go run main.go -listen 127.0.0.1:9091 -bnodes no -remote "127.0.0.1:9090" -do get -keyname bep44test -target 411eba73b6f087ca51a3795d9c8c938d365e32c1 -salt foobar -seq 1 -vv

Credits && inspiration

Thanks SO!