From 4798907da44b15d057cf3d1f02b3fff38252e3b4 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Wed, 16 Dec 2015 15:13:32 +1100 Subject: [PATCH] Add NoDefaultBootstrap and use it and testify in a few tests --- dht/dht.go | 10 +++++++++- dht/dht_test.go | 26 ++++++++++++-------------- dht/server.go | 2 +- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/dht/dht.go b/dht/dht.go index 4bc4a5c6..31bef132 100644 --- a/dht/dht.go +++ b/dht/dht.go @@ -38,12 +38,19 @@ type transactionKey struct { // 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 @@ func (n *node) DefinitelyGood() bool { } 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 7dd5ef58..c3afe6a5 100644 --- a/dht/dht_test.go +++ b/dht/dht_test.go @@ -107,23 +107,23 @@ func TestDHTDefaultConfig(t *testing.T) { } 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 @@ func TestDHTSec(t *testing.T) { } { 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 e2036012..b755289d 100644 --- a/dht/server.go +++ b/dht/server.go @@ -541,7 +541,7 @@ func (s *Server) addRootNodes() error { 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 {