dht/dht.go | 10 +++++++++- dht/dht_test.go | 26 ++++++++++++-------------- dht/server.go | 2 +- diff --git a/dht/dht.go b/dht/dht.go index 4bc4a5c61f592a2ddaba326c066e06a6a283372c..31bef132b41891bece6d4f8dc90ae26e94925ae2 100644 --- a/dht/dht.go +++ b/dht/dht.go @@ -38,12 +38,19 @@ // ServerConfig allows to set up a configuration of the `Server` instance // to be created with NewServer type ServerConfig struct { - Addr string // Listen address. Used if Conn is nil. + // Listen address. Used if Conn is nil. + Addr string Conn net.PacketConn // Don't respond to queries from other nodes. Passive bool // DHT Bootstrap nodes BootstrapNodes []string + // Disable bootstrapping from global servers even if given no BootstrapNodes. + // This creates a solitary node that awaits other nodes; it's only useful if + // you're creating your own DHT and want to avoid accidental crossover, without + // spoofing a bootstrap node and filling your logs with connection errors. + NoDefaultBootstrap bool + // Disable the DHT security extension: // http://www.libtorrent.org/dht_sec.html. NoSecurity bool @@ -171,6 +178,7 @@ return true } return true } + func jitterDuration(average time.Duration, plusMinus time.Duration) time.Duration { return average - plusMinus/2 + time.Duration(rand.Int63n(int64(plusMinus))) } diff --git a/dht/dht_test.go b/dht/dht_test.go index 7dd5ef58657e5c9a9a2765880f473c4ef7ced019..c3afe6a5547fc2a3fd18489ba44d18a54ee4a5dc 100644 --- a/dht/dht_test.go +++ b/dht/dht_test.go @@ -107,23 +107,23 @@ s.Close() } func TestPing(t *testing.T) { - srv, err := NewServer(nil) - if err != nil { - t.Fatal(err) - } + srv, err := NewServer(&ServerConfig{ + Addr: "127.0.0.1:5680", + NoDefaultBootstrap: true, + }) + require.NoError(t, err) defer srv.Close() - srv0, err := NewServer(nil) - if err != nil { - t.Fatal(err) - } + srv0, err := NewServer(&ServerConfig{ + Addr: "127.0.0.1:5681", + BootstrapNodes: []string{"127.0.0.1:5680"}, + }) + require.NoError(t, err) defer srv0.Close() tn, err := srv.Ping(&net.UDPAddr{ IP: []byte{127, 0, 0, 1}, Port: srv0.Addr().(*net.UDPAddr).Port, }) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) defer tn.Close() ok := make(chan bool) tn.SetResponseHandler(func(msg Msg, msgOk bool) { @@ -159,9 +159,7 @@ {"43.213.53.83", "e56f6cbf5b7c4be0237986d5243b87aa6d51303e", false}, } { ip := net.ParseIP(case_.ipStr) id, err := hex.DecodeString(case_.nodeIDHex) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) secure := NodeIdSecure(string(id), ip) if secure != case_.valid { t.Fatalf("case failed: %v", case_) diff --git a/dht/server.go b/dht/server.go index e20360126f9d05749e5c8c7902720b363ea1e6ec..b755289d6684dca2fe5f77caa832210d6bb21f93 100644 --- a/dht/server.go +++ b/dht/server.go @@ -541,7 +541,7 @@ // Populates the node table. func (s *Server) bootstrap() (err error) { s.mu.Lock() defer s.mu.Unlock() - if len(s.nodes) == 0 { + if len(s.nodes) == 0 && !s.config.NoDefaultBootstrap { err = s.addRootNodes() } if err != nil {