2
0
mirror of synced 2025-02-24 14:48:27 +00:00

Add NodeIdHex config option

This commit is contained in:
Matt Joiner 2015-12-16 15:15:59 +11:00
parent 4798907da4
commit 44ec4d9bdb
3 changed files with 31 additions and 0 deletions

View File

@ -40,6 +40,12 @@ type transactionKey struct {
type ServerConfig struct { type ServerConfig struct {
// Listen address. Used if Conn is nil. // Listen address. Used if Conn is nil.
Addr string 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 Conn net.PacketConn
// Don't respond to queries from other nodes. // Don't respond to queries from other nodes.
Passive bool Passive bool

View File

@ -184,6 +184,22 @@ func TestServerDefaultNodeIdSecure(t *testing.T) {
} }
} }
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) { func TestAnnounceTimeout(t *testing.T) {
s, err := NewServer(&ServerConfig{ s, err := NewServer(&ServerConfig{
BootstrapNodes: []string{"1.2.3.4:5"}, BootstrapNodes: []string{"1.2.3.4:5"},

View File

@ -3,6 +3,7 @@ package dht
import ( import (
"crypto" "crypto"
"encoding/binary" "encoding/binary"
"encoding/hex"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@ -85,6 +86,14 @@ func NewServer(c *ServerConfig) (s *Server, err error) {
} }
} }
s.bootstrapNodes = c.BootstrapNodes 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() err = s.init()
if err != nil { if err != nil {
return return