From 2b48fd1fcee7099398f5f22ab1ef7c7f43a4e814 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Wed, 28 Feb 2018 13:43:06 +0000 Subject: [PATCH] Add addPeer to admin socket --- src/yggdrasil/admin.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/yggdrasil/admin.go b/src/yggdrasil/admin.go index ab0e33ed..f24da17a 100644 --- a/src/yggdrasil/admin.go +++ b/src/yggdrasil/admin.go @@ -55,6 +55,13 @@ func (a *admin) init(c *Core, listenaddr string) { a.addHandler("getSessions", nil, func(out *[]byte, _ ...string) { *out = []byte(a.printInfos(a.getData_getSessions())) }) + a.addHandler("addPeer", nil, func(out *[]byte, saddr ...string) { + if a.addPeer(saddr[0]) == nil { + *out = []byte("Adding peer: " + saddr[0] + "\n") + } else { + *out = []byte("Failed to add peer: " + saddr[0] + "\n") + } + }) go a.listen() } @@ -143,6 +150,39 @@ func (a *admin) printInfos(infos []admin_nodeInfo) string { return strings.Join(out, "\n") } +func (a *admin) addPeer(p string) error { + pAddr := p + if p[:4] == "tcp:" || p[:4] == "udp:" { + pAddr = p[4:] + } + switch { + case len(p) >= 4 && p[:4] == "udp:": + // Connect to peer over UDP + udpAddr, err := net.ResolveUDPAddr("udp", pAddr) + if err != nil { + return err + } + var addr connAddr + addr.fromUDPAddr(udpAddr) + a.core.udp.mutex.RLock() + _, isIn := a.core.udp.conns[addr] + a.core.udp.mutex.RUnlock() + if !isIn { + a.core.udp.sendKeys(addr) + } + return nil + case len(p) >= 4 && p[:4] == "tcp:": + default: + // Connect to peer over TCP + _, err := net.ResolveTCPAddr("tcp", pAddr) + if err != nil { + return err + } + a.core.tcp.call(p) + } + return nil +} + func (a *admin) getData_getSelf() *admin_nodeInfo { table := a.core.switchTable.table.Load().(lookupTable) addr := a.core.router.addr