diff --git a/src/yggdrasil/admin.go b/src/yggdrasil/admin.go index 0509a30..35bdee0 100644 --- a/src/yggdrasil/admin.go +++ b/src/yggdrasil/admin.go @@ -44,17 +44,20 @@ func (a *admin) init(c *Core, listenaddr string) { a.core = c a.listenaddr = listenaddr a.addHandler("help", nil, func(in admin_info) (admin_info, error) { - handlers := make(map[string][]string) + handlers := make(map[string]interface{}) for _, handler := range a.handlers { - handlers[handler.name] = handler.args + handlers[handler.name] = admin_info{ "fields": handler.args } } - return admin_info{"handlers": handlers}, nil + return admin_info{"help": handlers}, nil }) a.addHandler("dot", []string{}, func(in admin_info) (admin_info, error) { return admin_info{"dot": string(a.getResponse_dot())}, nil }) a.addHandler("getSelf", []string{}, func(in admin_info) (admin_info, error) { - return admin_info{"self": a.getData_getSelf().asMap()}, nil + self := a.getData_getSelf().asMap() + ip := fmt.Sprint(self["ip"]) + delete(self, "ip") + return admin_info{"self": admin_info{ ip: self }}, nil }) a.addHandler("getPeers", []string{}, func(in admin_info) (admin_info, error) { sort := "ip" @@ -133,14 +136,15 @@ func (a *admin) init(c *Core, listenaddr string) { a.addHandler("getTunTap", []string{}, func(in admin_info) (r admin_info, e error) { defer func() { recover() - r = admin_info{"name": "none"} + r = admin_info{ "tuntap": admin_info{ "none": admin_info{ } } } e = nil }() return admin_info{ - "name": a.core.tun.iface.Name(), - "tap_mode": a.core.tun.iface.IsTAP(), - "mtu": a.core.tun.mtu, + a.core.tun.iface.Name(): admin_info{ + "tap_mode": a.core.tun.iface.IsTAP(), + "mtu": a.core.tun.mtu, + }, }, nil }) a.addHandler("setTunTap", []string{"name", "[tap_mode]", "[mtu]"}, func(in admin_info) (admin_info, error) { @@ -162,9 +166,10 @@ func (a *admin) init(c *Core, listenaddr string) { return admin_info{}, errors.New("Failed to configure adapter") } else { return admin_info{ - "name": a.core.tun.iface.Name(), - "tap_mode": a.core.tun.iface.IsTAP(), - "mtu": ifmtu, + a.core.tun.iface.Name(): admin_info{ + "tap_mode": a.core.tun.iface.IsTAP(), + "mtu": ifmtu, + }, }, nil } }) diff --git a/yggdrasilctl.go b/yggdrasilctl.go index ad05963..ba42bea 100644 --- a/yggdrasilctl.go +++ b/yggdrasilctl.go @@ -4,6 +4,7 @@ import "flag" import "fmt" import "strings" import "net" +import "sort" import "encoding/json" import "strconv" import "os" @@ -58,23 +59,77 @@ func main() { panic(err) } if err := decoder.Decode(&recv); err == nil { + if recv["status"] == "error" { + if err, ok := recv["error"]; ok { + fmt.Println(err) + } else { + fmt.Println("Unspecified error occured") + } + os.Exit(1) + } if _, ok := recv["request"]; !ok { - fmt.Println("Missing request") + fmt.Println("Missing request in response (malformed response?)") return } if _, ok := recv["response"]; !ok { - fmt.Println("Missing response") + fmt.Println("Missing response body (malformed response?)") return } req := recv["request"].(map[string]interface{}) res := recv["response"].(map[string]interface{}) + defer func() { + recover() + if json, err := json.MarshalIndent(recv["response"], "", " "); err == nil { + fmt.Println(string(json)) + } + }() switch req["request"] { case "dot": fmt.Println(res["dot"]) default: - if json, err := json.MarshalIndent(recv["response"], "", " "); err == nil { - fmt.Println(string(json)) + maxWidths := make(map[string]int) + var keyOrder []string + keysOrdered := false + + for _, tlv := range res { + for slk, slv := range tlv.(map[string]interface{}) { + if !keysOrdered { + for k := range slv.(map[string]interface{}) { + keyOrder = append(keyOrder, fmt.Sprint(k)) + } + sort.Strings(keyOrder) + keysOrdered = true + } + for k, v := range slv.(map[string]interface{}) { + if len(fmt.Sprint(slk)) > maxWidths["key"] { + maxWidths["key"] = len(fmt.Sprint(slk)) + } + if len(fmt.Sprint(v)) > maxWidths[k] { + maxWidths[k] = len(fmt.Sprint(v)) + if maxWidths[k] < len(k) { + maxWidths[k] = len(k) + } + } + } + } + + if len(keyOrder) > 0 { + fmt.Printf("%-" + fmt.Sprint(maxWidths["key"]) + "s ", "") + for _, v := range keyOrder { + fmt.Printf("%-" + fmt.Sprint(maxWidths[v]) + "s ", v) + } + fmt.Println() + } + + for slk, slv := range tlv.(map[string]interface{}) { + fmt.Printf("%-" + fmt.Sprint(maxWidths["key"]) + "s ", slk) + for _, k := range keyOrder { + fmt.Printf("%-" + fmt.Sprint(maxWidths[k]) + "s ", fmt.Sprint(slv.(map[string]interface{})[k])) + } + fmt.Println() + } } + } }