5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-10 04:00:37 +00:00

Add addPeer to admin socket

This commit is contained in:
Neil Alexander 2018-02-28 13:43:06 +00:00
parent 33c9f74f48
commit 2b48fd1fce

View File

@ -55,6 +55,13 @@ func (a *admin) init(c *Core, listenaddr string) {
a.addHandler("getSessions", nil, func(out *[]byte, _ ...string) { a.addHandler("getSessions", nil, func(out *[]byte, _ ...string) {
*out = []byte(a.printInfos(a.getData_getSessions())) *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() go a.listen()
} }
@ -143,6 +150,39 @@ func (a *admin) printInfos(infos []admin_nodeInfo) string {
return strings.Join(out, "\n") 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 { func (a *admin) getData_getSelf() *admin_nodeInfo {
table := a.core.switchTable.table.Load().(lookupTable) table := a.core.switchTable.table.Load().(lookupTable)
addr := a.core.router.addr addr := a.core.router.addr