5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-25 20:41:36 +00:00

Add an admin socket

This commit is contained in:
Neil Alexander 2018-01-21 00:17:15 +00:00
parent 502ab3cfaa
commit b754d68068
6 changed files with 92 additions and 15 deletions

61
src/yggdrasil/admin.go Normal file
View File

@ -0,0 +1,61 @@
package yggdrasil
import "net"
import "os"
import "bytes"
import "fmt"
// TODO: Make all of this JSON
type admin struct {
core *Core
listenaddr string
}
func (a *admin) init(c *Core, listenaddr string) {
a.core = c
a.listenaddr = listenaddr
go a.listen()
}
func (a *admin) listen() {
l, err := net.Listen("tcp", a.listenaddr)
if err != nil {
a.core.log.Printf("Admin socket failed to listen: %v", err)
os.Exit(1)
}
defer l.Close()
a.core.log.Printf("Admin socket listening on %s", l.Addr().String())
for {
conn, err := l.Accept()
if err == nil {
a.handleRequest(conn)
}
}
}
func (a *admin) handleRequest(conn net.Conn) {
buf := make([]byte, 1024)
_, err := conn.Read(buf)
if err != nil {
a.core.log.Printf("Admin socket failed to read: %v", err)
conn.Close()
return
}
buf = bytes.Trim(buf, "\x00\r\n\t")
switch string(buf) {
case "ports":
ports := a.core.peers.getPorts()
for _, v := range ports {
conn.Write([]byte(fmt.Sprintf("Found switch port %d\n", v.port)))
}
break
default:
conn.Write([]byte("I didn't understand that!\n"))
}
if err != nil {
a.core.log.Printf("Admin socket error: %v", err)
}
conn.Close()
}

View File

@ -20,6 +20,7 @@ type Core struct {
router router router router
dht dht dht dht
tun tunDevice tun tunDevice
admin admin
searches searches searches searches
tcp *tcpInterface tcp *tcpInterface
udp *udpInterface udp *udpInterface

View File

@ -336,6 +336,14 @@ func (c *Core) DEBUG_addKCPConn(saddr string) {
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
func (c *Core) DEBUG_setupAndStartAdminInterface(addrport string) {
a := admin{}
a.init(c, addrport)
c.admin = a
}
////////////////////////////////////////////////////////////////////////////////
func (c *Core) DEBUG_setLogger(log *log.Logger) { func (c *Core) DEBUG_setLogger(log *log.Logger) {
c.log = log c.log = log
} }

View File

@ -66,7 +66,8 @@ func (tun *tunDevice) setupAddress(addr string) error {
copy(ar.ifra_name[:], tun.iface.Name()) copy(ar.ifra_name[:], tun.iface.Name())
ar.ifra_prefixmask.sin6_len = uint8(unsafe.Sizeof(ar.ifra_prefixmask)) ar.ifra_prefixmask.sin6_len = uint8(unsafe.Sizeof(ar.ifra_prefixmask))
b := make([]byte, 16); binary.LittleEndian.PutUint16(b, uint16(0xFF00)) b := make([]byte, 16)
binary.LittleEndian.PutUint16(b, uint16(0xFF00))
ar.ifra_prefixmask.sin6_addr[0] = uint16(binary.BigEndian.Uint16(b)) ar.ifra_prefixmask.sin6_addr[0] = uint16(binary.BigEndian.Uint16(b))
ar.ifra_addr.sin6_len = uint8(unsafe.Sizeof(ar.ifra_addr)) ar.ifra_addr.sin6_len = uint8(unsafe.Sizeof(ar.ifra_addr))
@ -74,7 +75,8 @@ func (tun *tunDevice) setupAddress(addr string) error {
parts := strings.Split(strings.TrimRight(addr, "/8"), ":") parts := strings.Split(strings.TrimRight(addr, "/8"), ":")
for i := 0; i < 8; i++ { for i := 0; i < 8; i++ {
addr, _ := strconv.ParseUint(parts[i], 16, 16) addr, _ := strconv.ParseUint(parts[i], 16, 16)
b := make([]byte, 16); binary.LittleEndian.PutUint16(b, uint16(addr)) b := make([]byte, 16)
binary.LittleEndian.PutUint16(b, uint16(addr))
ar.ifra_addr.sin6_addr[i] = uint16(binary.BigEndian.Uint16(b)) ar.ifra_addr.sin6_addr[i] = uint16(binary.BigEndian.Uint16(b))
} }

View File

@ -30,6 +30,7 @@ import . "yggdrasil"
type nodeConfig struct { type nodeConfig struct {
Listen string Listen string
AdminListen string
Peers []string Peers []string
BoxPub string BoxPub string
BoxPriv string BoxPriv string
@ -72,6 +73,9 @@ func (n *node) init(cfg *nodeConfig, logger *log.Logger) {
logger.Println("Starting interface...") logger.Println("Starting interface...")
n.core.DEBUG_setupAndStartGlobalUDPInterface(cfg.Listen) n.core.DEBUG_setupAndStartGlobalUDPInterface(cfg.Listen)
logger.Println("Started interface") logger.Println("Started interface")
logger.Println("Starting admin socket...")
n.core.DEBUG_setupAndStartAdminInterface(cfg.AdminListen)
logger.Println("Started admin socket")
go func() { go func() {
if len(cfg.Peers) == 0 { if len(cfg.Peers) == 0 {
return return
@ -92,6 +96,7 @@ func generateConfig() *nodeConfig {
spub, spriv := core.DEBUG_newSigKeys() spub, spriv := core.DEBUG_newSigKeys()
cfg := nodeConfig{} cfg := nodeConfig{}
cfg.Listen = "[::]:0" cfg.Listen = "[::]:0"
cfg.AdminListen = "[::]:0"
cfg.BoxPub = hex.EncodeToString(bpub[:]) cfg.BoxPub = hex.EncodeToString(bpub[:])
cfg.BoxPriv = hex.EncodeToString(bpriv[:]) cfg.BoxPriv = hex.EncodeToString(bpriv[:])
cfg.SigPub = hex.EncodeToString(spub[:]) cfg.SigPub = hex.EncodeToString(spub[:])