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

Merge pull request #213 from neilalexander/admincleanup

Admin socket clean-up
This commit is contained in:
Arceliar 2018-11-26 19:18:24 -06:00 committed by GitHub
commit 05b07adba2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 25 deletions

View File

@ -52,12 +52,12 @@ func (a *admin) addHandler(name string, args []string, handler func(admin_info)
func (a *admin) init(c *Core, listenaddr string) { func (a *admin) init(c *Core, listenaddr string) {
a.core = c a.core = c
a.listenaddr = listenaddr a.listenaddr = listenaddr
a.addHandler("help", nil, func(in admin_info) (admin_info, error) { a.addHandler("list", []string{}, func(in admin_info) (admin_info, error) {
handlers := make(map[string]interface{}) handlers := make(map[string]interface{})
for _, handler := range a.handlers { for _, handler := range a.handlers {
handlers[handler.name] = admin_info{"fields": handler.args} handlers[handler.name] = admin_info{"fields": handler.args}
} }
return admin_info{"help": handlers}, nil return admin_info{"list": handlers}, nil
}) })
a.addHandler("dot", []string{}, func(in admin_info) (admin_info, error) { a.addHandler("dot", []string{}, func(in admin_info) (admin_info, error) {
return admin_info{"dot": string(a.getResponse_dot())}, nil return admin_info{"dot": string(a.getResponse_dot())}, nil
@ -202,32 +202,32 @@ func (a *admin) init(c *Core, listenaddr string) {
a.addHandler("getAllowedEncryptionPublicKeys", []string{}, func(in admin_info) (admin_info, error) { a.addHandler("getAllowedEncryptionPublicKeys", []string{}, func(in admin_info) (admin_info, error) {
return admin_info{"allowed_box_pubs": a.getAllowedEncryptionPublicKeys()}, nil return admin_info{"allowed_box_pubs": a.getAllowedEncryptionPublicKeys()}, nil
}) })
a.addHandler("addAllowedEncryptionPublicKey", []string{"key"}, func(in admin_info) (admin_info, error) { a.addHandler("addAllowedEncryptionPublicKey", []string{"box_pub_key"}, func(in admin_info) (admin_info, error) {
if a.addAllowedEncryptionPublicKey(in["key"].(string)) == nil { if a.addAllowedEncryptionPublicKey(in["box_pub_key"].(string)) == nil {
return admin_info{ return admin_info{
"added": []string{ "added": []string{
in["key"].(string), in["box_pub_key"].(string),
}, },
}, nil }, nil
} else { } else {
return admin_info{ return admin_info{
"not_added": []string{ "not_added": []string{
in["key"].(string), in["box_pub_key"].(string),
}, },
}, errors.New("Failed to add allowed key") }, errors.New("Failed to add allowed key")
} }
}) })
a.addHandler("removeAllowedEncryptionPublicKey", []string{"key"}, func(in admin_info) (admin_info, error) { a.addHandler("removeAllowedEncryptionPublicKey", []string{"box_pub_key"}, func(in admin_info) (admin_info, error) {
if a.removeAllowedEncryptionPublicKey(in["key"].(string)) == nil { if a.removeAllowedEncryptionPublicKey(in["box_pub_key"].(string)) == nil {
return admin_info{ return admin_info{
"removed": []string{ "removed": []string{
in["key"].(string), in["box_pub_key"].(string),
}, },
}, nil }, nil
} else { } else {
return admin_info{ return admin_info{
"not_removed": []string{ "not_removed": []string{
in["key"].(string), in["box_pub_key"].(string),
}, },
}, errors.New("Failed to remove allowed key") }, errors.New("Failed to remove allowed key")
} }
@ -243,15 +243,15 @@ func (a *admin) init(c *Core, listenaddr string) {
return admin_info{"not_added": []string{in["subnet"].(string)}}, errors.New("Failed to add source subnet") return admin_info{"not_added": []string{in["subnet"].(string)}}, errors.New("Failed to add source subnet")
} }
}) })
a.addHandler("addRoute", []string{"subnet", "destPubKey"}, func(in admin_info) (admin_info, error) { a.addHandler("addRoute", []string{"subnet", "box_pub_key"}, func(in admin_info) (admin_info, error) {
var err error var err error
a.core.router.doAdmin(func() { a.core.router.doAdmin(func() {
err = a.core.router.cryptokey.addRoute(in["subnet"].(string), in["destPubKey"].(string)) err = a.core.router.cryptokey.addRoute(in["subnet"].(string), in["box_pub_key"].(string))
}) })
if err == nil { if err == nil {
return admin_info{"added": []string{fmt.Sprintf("%s via %s", in["subnet"].(string), in["destPubKey"].(string))}}, nil return admin_info{"added": []string{fmt.Sprintf("%s via %s", in["subnet"].(string), in["box_pub_key"].(string))}}, nil
} else { } else {
return admin_info{"not_added": []string{fmt.Sprintf("%s via %s", in["subnet"].(string), in["destPubKey"].(string))}}, errors.New("Failed to add route") return admin_info{"not_added": []string{fmt.Sprintf("%s via %s", in["subnet"].(string), in["box_pub_key"].(string))}}, errors.New("Failed to add route")
} }
}) })
a.addHandler("getSourceSubnets", []string{}, func(in admin_info) (admin_info, error) { a.addHandler("getSourceSubnets", []string{}, func(in admin_info) (admin_info, error) {
@ -291,28 +291,28 @@ func (a *admin) init(c *Core, listenaddr string) {
return admin_info{"not_removed": []string{in["subnet"].(string)}}, errors.New("Failed to remove source subnet") return admin_info{"not_removed": []string{in["subnet"].(string)}}, errors.New("Failed to remove source subnet")
} }
}) })
a.addHandler("removeRoute", []string{"subnet", "destPubKey"}, func(in admin_info) (admin_info, error) { a.addHandler("removeRoute", []string{"subnet", "box_pub_key"}, func(in admin_info) (admin_info, error) {
var err error var err error
a.core.router.doAdmin(func() { a.core.router.doAdmin(func() {
err = a.core.router.cryptokey.removeRoute(in["subnet"].(string), in["destPubKey"].(string)) err = a.core.router.cryptokey.removeRoute(in["subnet"].(string), in["box_pub_key"].(string))
}) })
if err == nil { if err == nil {
return admin_info{"removed": []string{fmt.Sprintf("%s via %s", in["subnet"].(string), in["destPubKey"].(string))}}, nil return admin_info{"removed": []string{fmt.Sprintf("%s via %s", in["subnet"].(string), in["box_pub_key"].(string))}}, nil
} else { } else {
return admin_info{"not_removed": []string{fmt.Sprintf("%s via %s", in["subnet"].(string), in["destPubKey"].(string))}}, errors.New("Failed to remove route") return admin_info{"not_removed": []string{fmt.Sprintf("%s via %s", in["subnet"].(string), in["box_pub_key"].(string))}}, errors.New("Failed to remove route")
} }
}) })
a.addHandler("dhtPing", []string{"key", "coords", "[target]"}, func(in admin_info) (admin_info, error) { a.addHandler("dhtPing", []string{"box_pub_key", "coords", "[target]"}, func(in admin_info) (admin_info, error) {
if in["target"] == nil { if in["target"] == nil {
in["target"] = "none" in["target"] = "none"
} }
result, err := a.admin_dhtPing(in["key"].(string), in["coords"].(string), in["target"].(string)) result, err := a.admin_dhtPing(in["box_pub_key"].(string), in["coords"].(string), in["target"].(string))
if err == nil { if err == nil {
infos := make(map[string]map[string]string, len(result.Infos)) infos := make(map[string]map[string]string, len(result.Infos))
for _, dinfo := range result.Infos { for _, dinfo := range result.Infos {
info := map[string]string{ info := map[string]string{
"key": hex.EncodeToString(dinfo.key[:]), "box_pub_key": hex.EncodeToString(dinfo.key[:]),
"coords": fmt.Sprintf("%v", dinfo.coords), "coords": fmt.Sprintf("%v", dinfo.coords),
} }
addr := net.IP(address_addrForNodeID(getNodeID(&dinfo.key))[:]).String() addr := net.IP(address_addrForNodeID(getNodeID(&dinfo.key))[:]).String()
infos[addr] = info infos[addr] = info
@ -556,7 +556,7 @@ func (a *admin) getData_getSelf() *admin_nodeInfo {
table := a.core.switchTable.table.Load().(lookupTable) table := a.core.switchTable.table.Load().(lookupTable)
coords := table.self.getCoords() coords := table.self.getCoords()
self := admin_nodeInfo{ self := admin_nodeInfo{
{"key", hex.EncodeToString(a.core.boxPub[:])}, {"box_pub_key", hex.EncodeToString(a.core.boxPub[:])},
{"ip", a.core.GetAddress().String()}, {"ip", a.core.GetAddress().String()},
{"subnet", a.core.GetSubnet().String()}, {"subnet", a.core.GetSubnet().String()},
{"coords", fmt.Sprint(coords)}, {"coords", fmt.Sprint(coords)},
@ -583,6 +583,7 @@ func (a *admin) getData_getPeers() []admin_nodeInfo {
{"bytes_sent", atomic.LoadUint64(&p.bytesSent)}, {"bytes_sent", atomic.LoadUint64(&p.bytesSent)},
{"bytes_recvd", atomic.LoadUint64(&p.bytesRecvd)}, {"bytes_recvd", atomic.LoadUint64(&p.bytesRecvd)},
{"endpoint", p.endpoint}, {"endpoint", p.endpoint},
{"box_pub_key", hex.EncodeToString(p.box[:])},
} }
peerInfos = append(peerInfos, info) peerInfos = append(peerInfos, info)
} }
@ -608,6 +609,7 @@ func (a *admin) getData_getSwitchPeers() []admin_nodeInfo {
{"bytes_sent", atomic.LoadUint64(&peer.bytesSent)}, {"bytes_sent", atomic.LoadUint64(&peer.bytesSent)},
{"bytes_recvd", atomic.LoadUint64(&peer.bytesRecvd)}, {"bytes_recvd", atomic.LoadUint64(&peer.bytesRecvd)},
{"endpoint", peer.endpoint}, {"endpoint", peer.endpoint},
{"box_pub_key", hex.EncodeToString(peer.box[:])},
} }
peerInfos = append(peerInfos, info) peerInfos = append(peerInfos, info)
} }
@ -661,6 +663,7 @@ func (a *admin) getData_getDHT() []admin_nodeInfo {
{"ip", net.IP(addr[:]).String()}, {"ip", net.IP(addr[:]).String()},
{"coords", fmt.Sprint(v.coords)}, {"coords", fmt.Sprint(v.coords)},
{"last_seen", int(now.Sub(v.recv).Seconds())}, {"last_seen", int(now.Sub(v.recv).Seconds())},
{"box_pub_key", hex.EncodeToString(v.key[:])},
} }
infos = append(infos, info) infos = append(infos, info)
} }
@ -682,6 +685,7 @@ func (a *admin) getData_getSessions() []admin_nodeInfo {
{"was_mtu_fixed", sinfo.wasMTUFixed}, {"was_mtu_fixed", sinfo.wasMTUFixed},
{"bytes_sent", sinfo.bytesSent}, {"bytes_sent", sinfo.bytesSent},
{"bytes_recvd", sinfo.bytesRecvd}, {"bytes_recvd", sinfo.bytesRecvd},
{"box_pub_key", hex.EncodeToString(sinfo.theirPermPub[:])},
} }
infos = append(infos, info) infos = append(infos, info)
} }

View File

@ -18,11 +18,12 @@ type admin_info map[string]interface{}
func main() { func main() {
server := flag.String("endpoint", defaults.GetDefaults().DefaultAdminListen, "Admin socket endpoint") server := flag.String("endpoint", defaults.GetDefaults().DefaultAdminListen, "Admin socket endpoint")
injson := flag.Bool("json", false, "Output in JSON format") injson := flag.Bool("json", false, "Output in JSON format")
verbose := flag.Bool("v", false, "Verbose output (includes public keys)")
flag.Parse() flag.Parse()
args := flag.Args() args := flag.Args()
if len(args) == 0 { if len(args) == 0 {
fmt.Println("usage:", os.Args[0], "[-endpoint=proto://server] [-json] command [key=value] [...]") fmt.Println("usage:", os.Args[0], "[-endpoint=proto://server] [-v] [-json] command [key=value] [...]")
fmt.Println("example:", os.Args[0], "getPeers") fmt.Println("example:", os.Args[0], "getPeers")
fmt.Println("example:", os.Args[0], "setTunTap name=auto mtu=1500 tap_mode=false") fmt.Println("example:", os.Args[0], "setTunTap name=auto mtu=1500 tap_mode=false")
fmt.Println("example:", os.Args[0], "-endpoint=tcp://localhost:9001 getDHT") fmt.Println("example:", os.Args[0], "-endpoint=tcp://localhost:9001 getDHT")
@ -107,7 +108,7 @@ func main() {
switch strings.ToLower(req["request"].(string)) { switch strings.ToLower(req["request"].(string)) {
case "dot": case "dot":
fmt.Println(res["dot"]) fmt.Println(res["dot"])
case "help", "getpeers", "getswitchpeers", "getdht", "getsessions", "dhtping": case "list", "getpeers", "getswitchpeers", "getdht", "getsessions", "dhtping":
maxWidths := make(map[string]int) maxWidths := make(map[string]int)
var keyOrder []string var keyOrder []string
keysOrdered := false keysOrdered := false
@ -116,6 +117,11 @@ func main() {
for slk, slv := range tlv.(map[string]interface{}) { for slk, slv := range tlv.(map[string]interface{}) {
if !keysOrdered { if !keysOrdered {
for k := range slv.(map[string]interface{}) { for k := range slv.(map[string]interface{}) {
if !*verbose {
if k == "box_pub_key" || k == "box_sig_key" {
continue
}
}
keyOrder = append(keyOrder, fmt.Sprint(k)) keyOrder = append(keyOrder, fmt.Sprint(k))
} }
sort.Strings(keyOrder) sort.Strings(keyOrder)
@ -182,6 +188,14 @@ func main() {
if coords, ok := v.(map[string]interface{})["coords"].(string); ok { if coords, ok := v.(map[string]interface{})["coords"].(string); ok {
fmt.Println("Coords:", coords) fmt.Println("Coords:", coords)
} }
if *verbose {
if boxPubKey, ok := v.(map[string]interface{})["box_pub_key"].(string); ok {
fmt.Println("Public encryption key:", boxPubKey)
}
if boxSigKey, ok := v.(map[string]interface{})["box_sig_key"].(string); ok {
fmt.Println("Public signing key:", boxSigKey)
}
}
} }
case "getswitchqueues": case "getswitchqueues":
maximumqueuesize := float64(4194304) maximumqueuesize := float64(4194304)