dht/dht.go | 6 ++++++ dht/dht_test.go | 16 ++++++++++++++++ dht/server.go | 9 +++++++++ diff --git a/dht/dht.go b/dht/dht.go index 31bef132b41891bece6d4f8dc90ae26e94925ae2..ec00c1faa73e704e22cc7f2b4a0448597d98ac99 100644 --- a/dht/dht.go +++ b/dht/dht.go @@ -40,6 +40,12 @@ // to be created with NewServer type ServerConfig struct { // Listen address. Used if Conn is nil. Addr string + + // Set NodeId Manually. Caller must ensure that, if NodeId does not + // conform to DHT Security Extensions, that NoSecurity is also set. This + // should be given as a HEX string. + NodeIdHex string + Conn net.PacketConn // Don't respond to queries from other nodes. Passive bool diff --git a/dht/dht_test.go b/dht/dht_test.go index c3afe6a5547fc2a3fd18489ba44d18a54ee4a5dc..08a9697af1b8e919b1da63954c5a14638ef11f69 100644 --- a/dht/dht_test.go +++ b/dht/dht_test.go @@ -184,6 +184,22 @@ t.Fatal("not secure") } } +func TestServerCustomNodeId(t *testing.T) { + customId := "5a3ce1c14e7a08645677bbd1cfe7d8f956d53256" + id, err := hex.DecodeString(customId) + assert.NoError(t, err) + // How to test custom *secure* Id when tester computers will have + // different Ids? Generate custom ids for local IPs and use + // mini-Id? + s, err := NewServer(&ServerConfig{ + NodeIdHex: customId, + NoDefaultBootstrap: true, + }) + require.NoError(t, err) + defer s.Close() + assert.Equal(t, string(id), s.ID()) +} + func TestAnnounceTimeout(t *testing.T) { s, err := NewServer(&ServerConfig{ BootstrapNodes: []string{"1.2.3.4:5"}, diff --git a/dht/server.go b/dht/server.go index b755289d6684dca2fe5f77caa832210d6bb21f93..b25579c1f5eed7849965f016e1fe488b1681106c 100644 --- a/dht/server.go +++ b/dht/server.go @@ -3,6 +3,7 @@ import ( "crypto" "encoding/binary" + "encoding/hex" "errors" "fmt" "io" @@ -85,6 +86,14 @@ return } } s.bootstrapNodes = c.BootstrapNodes + if c.NodeIdHex != "" { + var rawID []byte + rawID, err = hex.DecodeString(c.NodeIdHex) + if err != nil { + return + } + s.id = string(rawID) + } err = s.init() if err != nil { return