]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Add NodeIdHex config option
authorMatt Joiner <anacrolix@gmail.com>
Wed, 16 Dec 2015 04:15:59 +0000 (15:15 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Wed, 16 Dec 2015 04:15:59 +0000 (15:15 +1100)
dht/dht.go
dht/dht_test.go
dht/server.go

index 31bef132b41891bece6d4f8dc90ae26e94925ae2..ec00c1faa73e704e22cc7f2b4a0448597d98ac99 100644 (file)
@@ -40,6 +40,12 @@ type transactionKey struct {
 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
index c3afe6a5547fc2a3fd18489ba44d18a54ee4a5dc..08a9697af1b8e919b1da63954c5a14638ef11f69 100644 (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) {
        s, err := NewServer(&ServerConfig{
                BootstrapNodes: []string{"1.2.3.4:5"},
index b755289d6684dca2fe5f77caa832210d6bb21f93..b25579c1f5eed7849965f016e1fe488b1681106c 100644 (file)
@@ -3,6 +3,7 @@ package dht
 import (
        "crypto"
        "encoding/binary"
+       "encoding/hex"
        "errors"
        "fmt"
        "io"
@@ -85,6 +86,14 @@ func NewServer(c *ServerConfig) (s *Server, err error) {
                }
        }
        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