diff --git a/dht_connection.go b/dht_connection.go index 23a6a362..44f10df5 100644 --- a/dht_connection.go +++ b/dht_connection.go @@ -19,13 +19,14 @@ var ( // DHTConnection to a DHT bootstrap node type DHTConnection struct { - routers []*DHTRouter // Routers - lock sync.Mutex // Mutex for register/unregister - instances map[string]*P2PInstance // Instances - registered []string // List of registered swarm IDs - incoming chan *ptp.DHTPacket // Packets received by routers - ip string // Our outbound IP - isActive bool // Whether DHT connection is active or not + routers []*DHTRouter // Routers + routersList string // Comma-separated list of routers + lock sync.Mutex // Mutex for register/unregister + instances map[string]*P2PInstance // Instances + registered []string // List of registered swarm IDs + incoming chan *ptp.DHTPacket // Packets received by routers + ip string // Our outbound IP + isActive bool // Whether DHT connection is active or not } func (dht *DHTConnection) init(routersSrc string) error { @@ -50,6 +51,7 @@ func (dht *DHTConnection) init(routersSrc string) error { router.data = dht.incoming dht.routers = append(dht.routers, router) } + dht.routersList = routersSrc dht.instances = make(map[string]*P2PInstance) return nil } diff --git a/lib/p2p.go b/lib/p2p.go index ff2bd247..1c4505c9 100644 --- a/lib/p2p.go +++ b/lib/p2p.go @@ -158,11 +158,12 @@ func (p *PeerToPeer) IsIPv4(ip string) bool { } // New is an entry point of a P2P library. -func New(argIP, argMac, argDev, argDirect, argHash, argKeyfile, argKey, argTTL, argLog string, fwd bool, port int, ignoreIPs []string, outboundIP net.IP) *PeerToPeer { +func New(argIP, argMac, argDev, argDirect, argHash, argKeyfile, argKey, argTTL, argLog string, fwd bool, port int, ignoreIPs []string, outboundIP net.IP, routers string) *PeerToPeer { Log(Debug, "Starting new P2P Instance: %s", argHash) Log(Debug, "IP: %s", argIP) Log(Debug, "Mac: %s", argMac) p := new(PeerToPeer) + p.Routers = routers p.outboundIP = outboundIP p.Init() var err error @@ -206,7 +207,7 @@ func New(argIP, argMac, argDev, argDirect, argHash, argKeyfile, argKey, argTTL, p.UDPSocket = new(Network) p.UDPSocket.Init("", port) go p.UDPSocket.Listen(p.HandleP2PMessage) - go p.UDPSocket.KeepAlive(p.retrieveFirstDHTRouter()) + go p.UDPSocket.KeepAlive(p.extractBestDHTRouter()) p.waitForRemotePort() // Create new DHT Client, configure it and initialize @@ -268,7 +269,7 @@ func (p *PeerToPeer) waitForRemotePort() { Log(Warning, "Remote port received: %d", p.UDPSocket.remotePort) } -func (p *PeerToPeer) retrieveFirstDHTRouter() *net.UDPAddr { +func (p *PeerToPeer) extractBestDHTRouter() *net.UDPAddr { Log(Debug, "Routers: %s", p.Routers) routers := strings.Split(p.Routers, ",") if len(routers) == 0 { diff --git a/lib/p2p_test.go b/lib/p2p_test.go index 901d5179..b0d78073 100644 --- a/lib/p2p_test.go +++ b/lib/p2p_test.go @@ -58,19 +58,19 @@ func TestIsIPv4(t *testing.T) { } } -func TestRetrieveFirstDHTRouters(t *testing.T) { +func TestExtractBestDHTRouters(t *testing.T) { ptp := new(PeerToPeer) wait, err := net.ResolveUDPAddr("udp4", "192.168.11.5:6882") if err != nil { t.Error("error") } ptp.Routers = "" - get := ptp.retrieveFirstDHTRouter() + get := ptp.extractBestDHTRouter() if get != nil { t.Error("Length of ptp routers is nil") } ptp.Routers = "192.168.11.5:24,192.168.22.1:22" - get2 := ptp.retrieveFirstDHTRouter() + get2 := ptp.extractBestDHTRouter() if bytes.EqualFold(get2.IP, wait.IP) && get2.Port != wait.Port && get2.Zone != wait.Zone { t.Errorf("Error.Wait %v, get %v", wait, get2) diff --git a/start.go b/start.go index 731e27ab..dc27f558 100644 --- a/start.go +++ b/start.go @@ -128,7 +128,7 @@ func (d *Daemon) run(args *RunArgs, resp *Response) error { newInst := new(P2PInstance) newInst.ID = args.Hash newInst.Args = *args - newInst.PTP = ptp.New(args.IP, args.Mac, args.Dev, "", args.Hash, args.Keyfile, args.Key, args.TTL, "", args.Fwd, args.Port, usedIPs, OutboundIP) + newInst.PTP = ptp.New(args.IP, args.Mac, args.Dev, "", args.Hash, args.Keyfile, args.Key, args.TTL, "", args.Fwd, args.Port, usedIPs, OutboundIP, bootstrap.routersList) if newInst.PTP == nil { resp.Output = resp.Output + "Failed to create P2P Instance" resp.ExitCode = 1