5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-12-22 19:55:39 +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
dht dht
tun tunDevice
admin admin
searches searches
tcp *tcpInterface
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) {
c.log = log
}

View File

@ -150,7 +150,7 @@ func (r *router) sendPacket(bs []byte) {
}
func (r *router) recvPacket(bs []byte, theirAddr *address, theirSubnet *subnet) {
// TODO? move this into the session?
// TODO? move this into the session?
//fmt.Println("Recv packet")
if len(bs) < 24 {
util_putBytes(bs)

View File

@ -44,13 +44,13 @@ type in6_aliasreq struct {
ifra_addr sockaddr_in6
ifra_dstaddr sockaddr_in6
ifra_prefixmask sockaddr_in6
ifra_flags uint32
ifra_flags uint32
ifra_lifetime in6_addrlifetime
}
type ifreq struct {
ifr_name [16]byte
ifru_mtu uint32
ifr_name [16]byte
ifru_mtu uint32
}
func (tun *tunDevice) setupAddress(addr string) error {
@ -66,7 +66,8 @@ func (tun *tunDevice) setupAddress(addr string) error {
copy(ar.ifra_name[:], tun.iface.Name())
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_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"), ":")
for i := 0; i < 8; i++ {
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))
}

View File

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