2018-01-21 00:17:15 +00:00
|
|
|
package yggdrasil
|
|
|
|
|
|
|
|
import "net"
|
|
|
|
import "os"
|
|
|
|
import "bytes"
|
|
|
|
import "fmt"
|
2018-01-21 22:19:39 +00:00
|
|
|
import "sort"
|
2018-01-21 00:17:15 +00:00
|
|
|
|
2018-01-26 23:30:51 +00:00
|
|
|
// TODO? Make all of this JSON
|
2018-01-21 12:57:54 +00:00
|
|
|
// TODO: Add authentication
|
2018-01-21 00:17:15 +00:00
|
|
|
|
|
|
|
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) {
|
2018-01-21 17:51:51 +00:00
|
|
|
case "dot":
|
|
|
|
const mDepth = 1024
|
|
|
|
m := make(map[[mDepth]switchPort]string)
|
2018-01-21 12:57:54 +00:00
|
|
|
table := a.core.switchTable.table.Load().(lookupTable)
|
2018-01-21 17:51:51 +00:00
|
|
|
peers := a.core.peers.ports.Load().(map[switchPort]*peer)
|
|
|
|
|
|
|
|
// Add my own entry
|
|
|
|
peerID := address_addrForNodeID(getNodeID(&peers[0].box))
|
2018-01-21 21:54:50 +00:00
|
|
|
myAddr := net.IP(peerID[:]).String()
|
2018-01-21 17:51:51 +00:00
|
|
|
var index [mDepth]switchPort
|
|
|
|
copy(index[:mDepth], table.self.coords[:])
|
2018-01-21 21:54:50 +00:00
|
|
|
m[index] = myAddr
|
2018-01-21 17:51:51 +00:00
|
|
|
|
|
|
|
// Connect switch table entries to peer entries
|
|
|
|
for _, tableentry := range table.elems {
|
|
|
|
for _, peerentry := range peers {
|
|
|
|
if peerentry.port == tableentry.port {
|
|
|
|
peerID := address_addrForNodeID(getNodeID(&peerentry.box))
|
|
|
|
addr := net.IP(peerID[:]).String()
|
|
|
|
var index [mDepth]switchPort
|
2018-01-21 18:55:45 +00:00
|
|
|
copy(index[:], tableentry.locator.coords)
|
2018-01-21 17:51:51 +00:00
|
|
|
m[index] = addr
|
|
|
|
}
|
|
|
|
}
|
2018-01-21 12:57:54 +00:00
|
|
|
}
|
|
|
|
|
2018-01-21 18:55:45 +00:00
|
|
|
getPorts := func(coords []byte) []switchPort {
|
|
|
|
var ports []switchPort
|
|
|
|
for offset := 0; ; {
|
|
|
|
coord, length := wire_decode_uint64(coords[offset:])
|
|
|
|
if length == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
ports = append(ports, switchPort(coord))
|
|
|
|
offset += length
|
|
|
|
}
|
|
|
|
return ports
|
|
|
|
}
|
|
|
|
|
2018-01-21 17:51:51 +00:00
|
|
|
// Look up everything we know from DHT
|
2018-01-21 18:55:45 +00:00
|
|
|
getDHT := func() {
|
|
|
|
for i := 0; i < a.core.dht.nBuckets(); i++ {
|
|
|
|
b := a.core.dht.getBucket(i)
|
|
|
|
for _, v := range b.infos {
|
|
|
|
destPorts := getPorts(v.coords)
|
|
|
|
addr := net.IP(address_addrForNodeID(v.nodeID_hidden)[:]).String()
|
|
|
|
var index [mDepth]switchPort
|
|
|
|
copy(index[:], destPorts)
|
|
|
|
m[index] = addr
|
2018-01-21 17:53:25 +00:00
|
|
|
}
|
2018-01-21 18:55:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
a.core.router.doAdmin(getDHT)
|
|
|
|
|
|
|
|
// Look up everything we know from active sessions
|
|
|
|
getSessions := func() {
|
|
|
|
for _, sinfo := range a.core.sessions.sinfos {
|
|
|
|
destPorts := getPorts(sinfo.coords)
|
2018-01-21 17:53:25 +00:00
|
|
|
var index [mDepth]switchPort
|
2018-01-21 18:55:45 +00:00
|
|
|
copy(index[:], destPorts)
|
|
|
|
m[index] = net.IP(sinfo.theirAddr[:]).String()
|
2018-01-21 17:51:51 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-21 18:55:45 +00:00
|
|
|
a.core.router.doAdmin(getSessions)
|
2018-01-21 12:57:54 +00:00
|
|
|
|
2018-01-21 20:58:54 +00:00
|
|
|
// Start building a tree from all known nodes
|
|
|
|
type nodeInfo struct {
|
|
|
|
name string
|
|
|
|
key [mDepth]switchPort
|
|
|
|
parent [mDepth]switchPort
|
|
|
|
}
|
|
|
|
infos := make(map[[mDepth]switchPort]nodeInfo)
|
|
|
|
// First fill the tree with all known nodes, no parents
|
|
|
|
for k, n := range m {
|
|
|
|
infos[k] = nodeInfo{
|
|
|
|
name: n,
|
|
|
|
key: k,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Now go through and create placeholders for any missing nodes
|
|
|
|
for _, info := range infos {
|
|
|
|
for idx, port := range info.key {
|
|
|
|
if port == 0 {
|
|
|
|
break
|
2018-01-21 17:53:25 +00:00
|
|
|
}
|
2018-01-21 20:58:54 +00:00
|
|
|
var key [mDepth]switchPort
|
|
|
|
copy(key[:idx], info.key[:])
|
|
|
|
newInfo, isIn := infos[key]
|
|
|
|
if isIn {
|
|
|
|
continue
|
2018-01-21 17:53:25 +00:00
|
|
|
}
|
2018-01-26 23:30:51 +00:00
|
|
|
newInfo.name = "?"
|
2018-01-21 20:58:54 +00:00
|
|
|
newInfo.key = key
|
|
|
|
infos[key] = newInfo
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Now go through and attach parents
|
|
|
|
for _, info := range infos {
|
|
|
|
info.parent = info.key
|
|
|
|
for idx := len(info.parent) - 1; idx >= 0; idx-- {
|
|
|
|
if info.parent[idx] != 0 {
|
|
|
|
info.parent[idx] = 0
|
2018-01-21 17:53:25 +00:00
|
|
|
break
|
2018-01-21 17:51:51 +00:00
|
|
|
}
|
2018-01-21 12:57:54 +00:00
|
|
|
}
|
2018-01-21 20:58:54 +00:00
|
|
|
infos[info.key] = info
|
|
|
|
}
|
2018-01-21 22:19:39 +00:00
|
|
|
// Finally, get a sorted list of keys, which we use to organize the output
|
|
|
|
var keys [][mDepth]switchPort
|
|
|
|
for _, info := range infos {
|
|
|
|
keys = append(keys, info.key)
|
|
|
|
}
|
|
|
|
less := func(i, j int) bool {
|
|
|
|
for idx := range keys[i] {
|
|
|
|
if keys[i][idx] < keys[j][idx] {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if keys[i][idx] > keys[j][idx] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
sort.Slice(keys, less)
|
2018-01-21 20:58:54 +00:00
|
|
|
// Now print it all out
|
|
|
|
conn.Write([]byte(fmt.Sprintf("digraph {\n")))
|
|
|
|
// First set the labels
|
2018-01-21 22:19:39 +00:00
|
|
|
for _, key := range keys {
|
|
|
|
info := infos[key]
|
2018-01-21 21:54:50 +00:00
|
|
|
if info.name == myAddr {
|
|
|
|
conn.Write([]byte(fmt.Sprintf("\"%v\" [ style = \"filled\", label = \"%v\" ];\n", info.key, info.name)))
|
|
|
|
} else {
|
|
|
|
conn.Write([]byte(fmt.Sprintf("\"%v\" [ label = \"%v\" ];\n", info.key, info.name)))
|
|
|
|
}
|
2018-01-21 20:58:54 +00:00
|
|
|
}
|
|
|
|
// Then print the tree structure
|
2018-01-21 22:19:39 +00:00
|
|
|
for _, key := range keys {
|
|
|
|
info := infos[key]
|
2018-01-21 20:58:54 +00:00
|
|
|
if info.key == info.parent {
|
|
|
|
continue
|
|
|
|
} // happens for the root, skip it
|
2018-01-21 22:19:39 +00:00
|
|
|
for idx := len(info.key) - 1; idx >= 0; idx-- {
|
|
|
|
port := info.key[idx]
|
|
|
|
if port == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
conn.Write([]byte(fmt.Sprintf(" \"%+v\" -> \"%+v\" [ label = \"%v\" ];\n", info.parent, info.key, port)))
|
|
|
|
break
|
|
|
|
}
|
2018-01-21 00:17:15 +00:00
|
|
|
}
|
2018-01-21 17:51:51 +00:00
|
|
|
conn.Write([]byte(fmt.Sprintf("}\n")))
|
2018-01-21 00:17:15 +00:00
|
|
|
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()
|
|
|
|
}
|