Move yggdrasilctl responses to separate functions

This commit is contained in:
Alex Kotov 2021-07-22 19:14:18 +05:00
parent b333c7d7f3
commit fa65253e57

View file

@ -207,8 +207,49 @@ func run() int {
switch strings.ToLower(req["request"].(string)) { switch strings.ToLower(req["request"].(string)) {
case "dot": case "dot":
fmt.Println(res["dot"]) runDot(res)
case "list", "getpeers", "getswitchpeers", "getdht", "getsessions", "dhtping": case "list", "getpeers", "getswitchpeers", "getdht", "getsessions", "dhtping":
runVariousInfo(res, verbose)
case "gettuntap", "settuntap":
runGetAndSetTunTap(res)
case "getself":
runGetSelf(res, verbose)
case "getswitchqueues":
runGetSwitchQueues(res)
case "addpeer", "removepeer", "addallowedencryptionpublickey", "removeallowedencryptionpublickey", "addsourcesubnet", "addroute", "removesourcesubnet", "removeroute":
runAddsAndRemoves(res)
case "getallowedencryptionpublickeys":
runGetAllowedEncryptionPublicKeys(res)
case "getmulticastinterfaces":
runGetMulticastInterfaces(res)
case "getsourcesubnets":
runGetSourceSubnets(res)
case "getroutes":
runGetRoutes(res)
case "settunnelrouting":
fallthrough
case "gettunnelrouting":
runGetTunnelRouting(res)
default:
if json, err := json.MarshalIndent(recv["response"], "", " "); err == nil {
fmt.Println(string(json))
}
}
} else {
logger.Println("Error receiving response:", err)
}
if v, ok := recv["status"]; ok && v != "success" {
return 1
}
return 0
}
func runDot(res map[string]interface{}) {
fmt.Println(res["dot"])
}
func runVariousInfo(res map[string]interface{}, verbose *bool) {
maxWidths := make(map[string]int) maxWidths := make(map[string]int)
var keyOrder []string var keyOrder []string
keysOrdered := false keysOrdered := false
@ -269,7 +310,9 @@ func run() int {
fmt.Println() fmt.Println()
} }
} }
case "gettuntap", "settuntap": }
func runGetAndSetTunTap(res map[string]interface{}) {
for k, v := range res { for k, v := range res {
fmt.Println("Interface name:", k) fmt.Println("Interface name:", k)
if mtu, ok := v.(map[string]interface{})["mtu"].(float64); ok { if mtu, ok := v.(map[string]interface{})["mtu"].(float64); ok {
@ -279,7 +322,9 @@ func run() int {
fmt.Println("TAP mode:", tap_mode) fmt.Println("TAP mode:", tap_mode)
} }
} }
case "getself": }
func runGetSelf(res map[string]interface{}, verbose *bool) {
for k, v := range res["self"].(map[string]interface{}) { for k, v := range res["self"].(map[string]interface{}) {
if buildname, ok := v.(map[string]interface{})["build_name"].(string); ok && buildname != "unknown" { if buildname, ok := v.(map[string]interface{})["build_name"].(string); ok && buildname != "unknown" {
fmt.Println("Build name:", buildname) fmt.Println("Build name:", buildname)
@ -309,7 +354,9 @@ func run() int {
} }
} }
} }
case "getswitchqueues": }
func runGetSwitchQueues(res map[string]interface{}) {
maximumqueuesize := float64(4194304) maximumqueuesize := float64(4194304)
portqueues := make(map[float64]float64) portqueues := make(map[float64]float64)
portqueuesize := make(map[float64]float64) portqueuesize := make(map[float64]float64)
@ -357,7 +404,9 @@ func run() int {
uint(k), uint(v), uint(queuesizepercent), uint(portqueuepackets[k])) uint(k), uint(v), uint(queuesizepercent), uint(portqueuepackets[k]))
} }
} }
case "addpeer", "removepeer", "addallowedencryptionpublickey", "removeallowedencryptionpublickey", "addsourcesubnet", "addroute", "removesourcesubnet", "removeroute": }
func runAddsAndRemoves(res map[string]interface{}) {
if _, ok := res["added"]; ok { if _, ok := res["added"]; ok {
for _, v := range res["added"].([]interface{}) { for _, v := range res["added"].([]interface{}) {
fmt.Println("Added:", fmt.Sprint(v)) fmt.Println("Added:", fmt.Sprint(v))
@ -378,7 +427,9 @@ func run() int {
fmt.Println("Not removed:", fmt.Sprint(v)) fmt.Println("Not removed:", fmt.Sprint(v))
} }
} }
case "getallowedencryptionpublickeys": }
func runGetAllowedEncryptionPublicKeys(res map[string]interface{}) {
if _, ok := res["allowed_box_pubs"]; !ok { if _, ok := res["allowed_box_pubs"]; !ok {
fmt.Println("All connections are allowed") fmt.Println("All connections are allowed")
} else if res["allowed_box_pubs"] == nil { } else if res["allowed_box_pubs"] == nil {
@ -389,7 +440,9 @@ func run() int {
fmt.Println("-", v) fmt.Println("-", v)
} }
} }
case "getmulticastinterfaces": }
func runGetMulticastInterfaces(res map[string]interface{}) {
if _, ok := res["multicast_interfaces"]; !ok { if _, ok := res["multicast_interfaces"]; !ok {
fmt.Println("No multicast interfaces found") fmt.Println("No multicast interfaces found")
} else if res["multicast_interfaces"] == nil { } else if res["multicast_interfaces"] == nil {
@ -400,7 +453,9 @@ func run() int {
fmt.Println("-", v) fmt.Println("-", v)
} }
} }
case "getsourcesubnets": }
func runGetSourceSubnets(res map[string]interface{}) {
if _, ok := res["source_subnets"]; !ok { if _, ok := res["source_subnets"]; !ok {
fmt.Println("No source subnets found") fmt.Println("No source subnets found")
} else if res["source_subnets"] == nil { } else if res["source_subnets"] == nil {
@ -411,7 +466,9 @@ func run() int {
fmt.Println("-", v) fmt.Println("-", v)
} }
} }
case "getroutes": }
func runGetRoutes(res map[string]interface{}) {
if routes, ok := res["routes"].(map[string]interface{}); !ok { if routes, ok := res["routes"].(map[string]interface{}); !ok {
fmt.Println("No routes found") fmt.Println("No routes found")
} else { } else {
@ -426,9 +483,9 @@ func run() int {
} }
} }
} }
case "settunnelrouting": }
fallthrough
case "gettunnelrouting": func runGetTunnelRouting(res map[string]interface{}) {
if enabled, ok := res["enabled"].(bool); !ok { if enabled, ok := res["enabled"].(bool); !ok {
fmt.Println("Tunnel routing is disabled") fmt.Println("Tunnel routing is disabled")
} else if !enabled { } else if !enabled {
@ -436,17 +493,4 @@ func run() int {
} else { } else {
fmt.Println("Tunnel routing is enabled") fmt.Println("Tunnel routing is enabled")
} }
default:
if json, err := json.MarshalIndent(recv["response"], "", " "); err == nil {
fmt.Println(string(json))
}
}
} else {
logger.Println("Error receiving response:", err)
}
if v, ok := recv["status"]; ok && v != "success" {
return 1
}
return 0
} }