2018-05-20 22:32:17 +00:00
|
|
|
package main
|
|
|
|
|
2018-12-08 01:56:04 +00:00
|
|
|
import (
|
2018-12-10 10:54:41 +00:00
|
|
|
"bytes"
|
2018-12-08 01:56:04 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2018-12-10 11:09:10 +00:00
|
|
|
"io/ioutil"
|
2018-12-10 10:54:41 +00:00
|
|
|
"log"
|
2018-12-08 01:56:04 +00:00
|
|
|
"net"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2018-05-20 22:32:17 +00:00
|
|
|
|
2018-12-10 11:09:10 +00:00
|
|
|
"golang.org/x/text/encoding/unicode"
|
|
|
|
|
2018-12-17 10:50:57 +00:00
|
|
|
"github.com/hjson/hjson-go"
|
2018-12-08 01:56:04 +00:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/defaults"
|
2019-08-10 21:31:22 +00:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/version"
|
2018-12-08 01:56:04 +00:00
|
|
|
)
|
2018-07-07 11:08:52 +00:00
|
|
|
|
2018-05-20 22:32:17 +00:00
|
|
|
type admin_info map[string]interface{}
|
|
|
|
|
|
|
|
func main() {
|
2018-12-10 10:54:41 +00:00
|
|
|
logbuffer := &bytes.Buffer{}
|
|
|
|
logger := log.New(logbuffer, "", log.Flags())
|
2018-12-10 11:29:42 +00:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
logger.Println("Fatal error:", r)
|
|
|
|
fmt.Print(logbuffer)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}()
|
2018-12-10 10:54:41 +00:00
|
|
|
|
2018-12-10 11:12:40 +00:00
|
|
|
endpoint := defaults.GetDefaults().DefaultAdminListen
|
2018-12-10 10:54:41 +00:00
|
|
|
|
2018-12-09 13:33:18 +00:00
|
|
|
flag.Usage = func() {
|
2018-12-17 10:17:16 +00:00
|
|
|
fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [options] command [key=value] [key=value] ...\n\n", os.Args[0])
|
2018-12-09 13:33:18 +00:00
|
|
|
fmt.Println("Options:")
|
2018-12-10 10:54:41 +00:00
|
|
|
flag.PrintDefaults()
|
2018-12-17 10:17:16 +00:00
|
|
|
fmt.Println("\nPlease note that options must always specified BEFORE the command\non the command line or they will be ignored.\n")
|
|
|
|
fmt.Println("Commands:\n - Use \"list\" for a list of available commands\n")
|
2018-12-09 13:33:18 +00:00
|
|
|
fmt.Println("Examples:")
|
|
|
|
fmt.Println(" - ", os.Args[0], "list")
|
|
|
|
fmt.Println(" - ", os.Args[0], "getPeers")
|
2018-12-17 10:17:16 +00:00
|
|
|
fmt.Println(" - ", os.Args[0], "-v getSelf")
|
2018-12-09 13:33:18 +00:00
|
|
|
fmt.Println(" - ", os.Args[0], "setTunTap name=auto mtu=1500 tap_mode=false")
|
|
|
|
fmt.Println(" - ", os.Args[0], "-endpoint=tcp://localhost:9001 getDHT")
|
|
|
|
fmt.Println(" - ", os.Args[0], "-endpoint=unix:///var/run/ygg.sock getDHT")
|
|
|
|
}
|
2018-12-10 11:12:40 +00:00
|
|
|
server := flag.String("endpoint", endpoint, "Admin socket endpoint")
|
2018-12-09 13:33:18 +00:00
|
|
|
injson := flag.Bool("json", false, "Output in JSON format (as opposed to pretty-print)")
|
2018-11-26 17:55:34 +00:00
|
|
|
verbose := flag.Bool("v", false, "Verbose output (includes public keys)")
|
2019-08-10 21:31:22 +00:00
|
|
|
ver := flag.Bool("version", false, "Prints the version of this build")
|
2018-05-20 22:32:17 +00:00
|
|
|
flag.Parse()
|
|
|
|
args := flag.Args()
|
|
|
|
|
2019-08-10 21:31:22 +00:00
|
|
|
if *ver {
|
2019-08-14 19:09:02 +00:00
|
|
|
fmt.Println("Build name:", version.BuildName())
|
|
|
|
fmt.Println("Build version:", version.BuildVersion())
|
|
|
|
fmt.Println("To get the version number of the running Yggdrasil node, run", os.Args[0], "getSelf")
|
|
|
|
return
|
2019-08-10 21:31:22 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 06:28:03 +00:00
|
|
|
if len(args) == 0 {
|
2018-12-09 13:33:18 +00:00
|
|
|
flag.Usage()
|
2018-05-21 06:28:03 +00:00
|
|
|
return
|
|
|
|
}
|
2018-05-20 22:46:02 +00:00
|
|
|
|
2018-12-10 11:12:40 +00:00
|
|
|
if *server == endpoint {
|
2018-12-10 11:09:10 +00:00
|
|
|
if config, err := ioutil.ReadFile(defaults.GetDefaults().DefaultConfigFile); err == nil {
|
|
|
|
if bytes.Compare(config[0:2], []byte{0xFF, 0xFE}) == 0 ||
|
|
|
|
bytes.Compare(config[0:2], []byte{0xFE, 0xFF}) == 0 {
|
|
|
|
utf := unicode.UTF16(unicode.BigEndian, unicode.UseBOM)
|
|
|
|
decoder := utf.NewDecoder()
|
|
|
|
config, err = decoder.Bytes(config)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var dat map[string]interface{}
|
|
|
|
if err := hjson.Unmarshal(config, &dat); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if ep, ok := dat["AdminListen"].(string); ok && (ep != "none" && ep != "") {
|
2018-12-10 11:12:40 +00:00
|
|
|
endpoint = ep
|
2018-12-10 11:09:10 +00:00
|
|
|
logger.Println("Found platform default config file", defaults.GetDefaults().DefaultConfigFile)
|
2018-12-10 11:12:40 +00:00
|
|
|
logger.Println("Using endpoint", endpoint, "from AdminListen")
|
2018-12-10 11:09:10 +00:00
|
|
|
} else {
|
|
|
|
logger.Println("Configuration file doesn't contain appropriate AdminListen option")
|
|
|
|
logger.Println("Falling back to platform default", defaults.GetDefaults().DefaultAdminListen)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logger.Println("Can't open config file from default location", defaults.GetDefaults().DefaultConfigFile)
|
|
|
|
logger.Println("Falling back to platform default", defaults.GetDefaults().DefaultAdminListen)
|
|
|
|
}
|
|
|
|
} else {
|
2018-12-15 11:48:43 +00:00
|
|
|
endpoint = *server
|
2018-12-10 11:12:40 +00:00
|
|
|
logger.Println("Using endpoint", endpoint, "from command line")
|
2018-12-10 11:09:10 +00:00
|
|
|
}
|
|
|
|
|
2018-07-07 10:25:01 +00:00
|
|
|
var conn net.Conn
|
2018-12-10 11:12:40 +00:00
|
|
|
u, err := url.Parse(endpoint)
|
2018-07-07 10:25:01 +00:00
|
|
|
if err == nil {
|
|
|
|
switch strings.ToLower(u.Scheme) {
|
|
|
|
case "unix":
|
2018-12-10 11:12:40 +00:00
|
|
|
logger.Println("Connecting to UNIX socket", endpoint[7:])
|
|
|
|
conn, err = net.Dial("unix", endpoint[7:])
|
2018-07-07 10:25:01 +00:00
|
|
|
case "tcp":
|
2018-12-10 10:54:41 +00:00
|
|
|
logger.Println("Connecting to TCP socket", u.Host)
|
2018-07-07 10:25:01 +00:00
|
|
|
conn, err = net.Dial("tcp", u.Host)
|
|
|
|
default:
|
2018-12-10 11:09:10 +00:00
|
|
|
logger.Println("Unknown protocol or malformed address - check your endpoint")
|
2018-07-07 10:25:01 +00:00
|
|
|
err = errors.New("protocol not supported")
|
|
|
|
}
|
|
|
|
} else {
|
2018-12-10 10:54:41 +00:00
|
|
|
logger.Println("Connecting to TCP socket", u.Host)
|
2018-12-10 11:12:40 +00:00
|
|
|
conn, err = net.Dial("tcp", endpoint)
|
2018-07-07 10:25:01 +00:00
|
|
|
}
|
2018-05-20 22:32:17 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-12-10 10:54:41 +00:00
|
|
|
logger.Println("Connected")
|
2018-05-20 22:32:17 +00:00
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
decoder := json.NewDecoder(conn)
|
|
|
|
encoder := json.NewEncoder(conn)
|
|
|
|
send := make(admin_info)
|
|
|
|
recv := make(admin_info)
|
|
|
|
|
|
|
|
for c, a := range args {
|
|
|
|
if c == 0 {
|
2018-12-17 10:17:16 +00:00
|
|
|
if strings.HasPrefix(a, "-") {
|
|
|
|
logger.Printf("Ignoring flag %s as it should be specified before other parameters\n", a)
|
|
|
|
continue
|
|
|
|
}
|
2018-12-10 10:54:41 +00:00
|
|
|
logger.Printf("Sending request: %v\n", a)
|
2018-05-20 22:32:17 +00:00
|
|
|
send["request"] = a
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
tokens := strings.Split(a, "=")
|
2018-12-17 10:17:16 +00:00
|
|
|
if len(tokens) == 1 {
|
|
|
|
send[tokens[0]] = true
|
|
|
|
} else if len(tokens) > 2 {
|
|
|
|
send[tokens[0]] = strings.Join(tokens[1:], "=")
|
|
|
|
} else if len(tokens) == 2 {
|
|
|
|
if i, err := strconv.Atoi(tokens[1]); err == nil {
|
|
|
|
logger.Printf("Sending parameter %s: %d\n", tokens[0], i)
|
|
|
|
send[tokens[0]] = i
|
|
|
|
} else {
|
|
|
|
switch strings.ToLower(tokens[1]) {
|
|
|
|
case "true":
|
|
|
|
send[tokens[0]] = true
|
|
|
|
case "false":
|
|
|
|
send[tokens[0]] = false
|
|
|
|
default:
|
|
|
|
send[tokens[0]] = tokens[1]
|
|
|
|
}
|
|
|
|
logger.Printf("Sending parameter %s: %v\n", tokens[0], send[tokens[0]])
|
2018-05-20 22:32:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := encoder.Encode(&send); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-12-10 10:54:41 +00:00
|
|
|
logger.Printf("Request sent")
|
2018-05-20 22:32:17 +00:00
|
|
|
if err := decoder.Decode(&recv); err == nil {
|
2018-12-10 10:54:41 +00:00
|
|
|
logger.Printf("Response received")
|
2018-05-21 12:54:51 +00:00
|
|
|
if recv["status"] == "error" {
|
|
|
|
if err, ok := recv["error"]; ok {
|
2018-12-10 10:54:41 +00:00
|
|
|
fmt.Println("Admin socket returned an error:", err)
|
2018-05-21 12:54:51 +00:00
|
|
|
} else {
|
2018-12-10 10:54:41 +00:00
|
|
|
fmt.Println("Admin socket returned an error but didn't specify any error text")
|
2018-05-21 12:54:51 +00:00
|
|
|
}
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2018-05-21 06:28:03 +00:00
|
|
|
if _, ok := recv["request"]; !ok {
|
2018-05-21 12:54:51 +00:00
|
|
|
fmt.Println("Missing request in response (malformed response?)")
|
2018-12-10 10:54:41 +00:00
|
|
|
os.Exit(1)
|
2018-05-21 06:28:03 +00:00
|
|
|
}
|
|
|
|
if _, ok := recv["response"]; !ok {
|
2018-05-21 12:54:51 +00:00
|
|
|
fmt.Println("Missing response body (malformed response?)")
|
2018-12-10 10:54:41 +00:00
|
|
|
os.Exit(1)
|
2018-05-21 06:28:03 +00:00
|
|
|
}
|
|
|
|
req := recv["request"].(map[string]interface{})
|
|
|
|
res := recv["response"].(map[string]interface{})
|
2018-05-21 13:14:57 +00:00
|
|
|
|
2018-05-21 13:29:27 +00:00
|
|
|
if *injson {
|
|
|
|
if json, err := json.MarshalIndent(res, "", " "); err == nil {
|
|
|
|
fmt.Println(string(json))
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2018-07-09 18:30:41 +00:00
|
|
|
switch strings.ToLower(req["request"].(string)) {
|
2018-05-20 22:32:17 +00:00
|
|
|
case "dot":
|
|
|
|
fmt.Println(res["dot"])
|
2018-11-26 17:51:30 +00:00
|
|
|
case "list", "getpeers", "getswitchpeers", "getdht", "getsessions", "dhtping":
|
2018-05-21 12:54:51 +00:00
|
|
|
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{}) {
|
2018-11-26 17:55:34 +00:00
|
|
|
if !*verbose {
|
2019-05-29 11:59:36 +00:00
|
|
|
if k == "box_pub_key" || k == "box_sig_key" || k == "nodeinfo" || k == "was_mtu_fixed" {
|
2018-11-26 17:55:34 +00:00
|
|
|
continue
|
|
|
|
}
|
2018-11-26 17:50:31 +00:00
|
|
|
}
|
2018-05-21 12:54:51 +00:00
|
|
|
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 {
|
2018-05-21 13:14:57 +00:00
|
|
|
fmt.Printf("%-"+fmt.Sprint(maxWidths["key"])+"s ", "")
|
2018-05-21 12:54:51 +00:00
|
|
|
for _, v := range keyOrder {
|
2018-05-21 13:14:57 +00:00
|
|
|
fmt.Printf("%-"+fmt.Sprint(maxWidths[v])+"s ", v)
|
2018-05-21 12:54:51 +00:00
|
|
|
}
|
|
|
|
fmt.Println()
|
|
|
|
}
|
|
|
|
|
|
|
|
for slk, slv := range tlv.(map[string]interface{}) {
|
2018-05-21 13:14:57 +00:00
|
|
|
fmt.Printf("%-"+fmt.Sprint(maxWidths["key"])+"s ", slk)
|
2018-05-21 12:54:51 +00:00
|
|
|
for _, k := range keyOrder {
|
2018-05-21 14:21:23 +00:00
|
|
|
preformatted := slv.(map[string]interface{})[k]
|
|
|
|
var formatted string
|
|
|
|
switch k {
|
|
|
|
case "bytes_sent", "bytes_recvd":
|
|
|
|
formatted = fmt.Sprintf("%d", uint(preformatted.(float64)))
|
|
|
|
case "uptime", "last_seen":
|
|
|
|
seconds := uint(preformatted.(float64)) % 60
|
|
|
|
minutes := uint(preformatted.(float64)/60) % 60
|
|
|
|
hours := uint(preformatted.(float64) / 60 / 60)
|
|
|
|
formatted = fmt.Sprintf("%02d:%02d:%02d", hours, minutes, seconds)
|
|
|
|
default:
|
|
|
|
formatted = fmt.Sprint(preformatted)
|
|
|
|
}
|
|
|
|
fmt.Printf("%-"+fmt.Sprint(maxWidths[k])+"s ", formatted)
|
2018-05-21 12:54:51 +00:00
|
|
|
}
|
|
|
|
fmt.Println()
|
|
|
|
}
|
2018-05-20 22:32:17 +00:00
|
|
|
}
|
2018-07-09 18:30:41 +00:00
|
|
|
case "gettuntap", "settuntap":
|
2018-05-21 13:14:57 +00:00
|
|
|
for k, v := range res {
|
|
|
|
fmt.Println("Interface name:", k)
|
|
|
|
if mtu, ok := v.(map[string]interface{})["mtu"].(float64); ok {
|
|
|
|
fmt.Println("Interface MTU:", mtu)
|
|
|
|
}
|
|
|
|
if tap_mode, ok := v.(map[string]interface{})["tap_mode"].(bool); ok {
|
|
|
|
fmt.Println("TAP mode:", tap_mode)
|
|
|
|
}
|
|
|
|
}
|
2018-07-09 18:30:41 +00:00
|
|
|
case "getself":
|
2018-05-23 21:13:52 +00:00
|
|
|
for k, v := range res["self"].(map[string]interface{}) {
|
2018-12-08 11:01:05 +00:00
|
|
|
if buildname, ok := v.(map[string]interface{})["build_name"].(string); ok && buildname != "unknown" {
|
2018-12-07 22:22:46 +00:00
|
|
|
fmt.Println("Build name:", buildname)
|
|
|
|
}
|
2018-12-08 11:01:05 +00:00
|
|
|
if buildversion, ok := v.(map[string]interface{})["build_version"].(string); ok && buildversion != "unknown" {
|
2018-12-07 22:22:46 +00:00
|
|
|
fmt.Println("Build version:", buildversion)
|
|
|
|
}
|
2018-05-23 21:21:37 +00:00
|
|
|
fmt.Println("IPv6 address:", k)
|
2018-05-23 21:13:52 +00:00
|
|
|
if subnet, ok := v.(map[string]interface{})["subnet"].(string); ok {
|
2018-05-23 21:21:37 +00:00
|
|
|
fmt.Println("IPv6 subnet:", subnet)
|
2018-05-23 21:13:52 +00:00
|
|
|
}
|
|
|
|
if coords, ok := v.(map[string]interface{})["coords"].(string); ok {
|
2018-05-23 21:21:37 +00:00
|
|
|
fmt.Println("Coords:", coords)
|
2018-05-23 21:13:52 +00:00
|
|
|
}
|
2018-11-26 17:55:34 +00:00
|
|
|
if *verbose {
|
2019-04-18 22:38:23 +00:00
|
|
|
if nodeID, ok := v.(map[string]interface{})["node_id"].(string); ok {
|
|
|
|
fmt.Println("Node ID:", nodeID)
|
|
|
|
}
|
2018-11-26 17:55:34 +00:00
|
|
|
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)
|
|
|
|
}
|
2018-11-26 17:50:31 +00:00
|
|
|
}
|
2018-05-23 21:13:52 +00:00
|
|
|
}
|
2018-09-27 09:53:19 +00:00
|
|
|
case "getswitchqueues":
|
2018-09-27 15:19:47 +00:00
|
|
|
maximumqueuesize := float64(4194304)
|
|
|
|
portqueues := make(map[float64]float64)
|
2018-09-27 14:51:17 +00:00
|
|
|
portqueuesize := make(map[float64]float64)
|
|
|
|
portqueuepackets := make(map[float64]float64)
|
2018-09-27 09:53:19 +00:00
|
|
|
v := res["switchqueues"].(map[string]interface{})
|
|
|
|
if queuecount, ok := v["queues_count"].(float64); ok {
|
2018-09-27 15:19:47 +00:00
|
|
|
fmt.Printf("Active queue count: %d queues\n", uint(queuecount))
|
2018-09-27 09:53:19 +00:00
|
|
|
}
|
|
|
|
if queuesize, ok := v["queues_size"].(float64); ok {
|
2018-09-27 15:19:47 +00:00
|
|
|
fmt.Printf("Active queue size: %d bytes\n", uint(queuesize))
|
2018-09-27 09:53:19 +00:00
|
|
|
}
|
2018-09-27 15:19:47 +00:00
|
|
|
if highestqueuecount, ok := v["highest_queues_count"].(float64); ok {
|
|
|
|
fmt.Printf("Highest queue count: %d queues\n", uint(highestqueuecount))
|
2018-09-27 09:53:19 +00:00
|
|
|
}
|
2018-09-27 15:19:47 +00:00
|
|
|
if highestqueuesize, ok := v["highest_queues_size"].(float64); ok {
|
|
|
|
fmt.Printf("Highest queue size: %d bytes\n", uint(highestqueuesize))
|
|
|
|
}
|
|
|
|
if m, ok := v["maximum_queues_size"].(float64); ok {
|
|
|
|
maximumqueuesize = m
|
2018-12-02 22:49:27 +00:00
|
|
|
fmt.Printf("Maximum queue size: %d bytes\n", uint(maximumqueuesize))
|
2018-09-27 09:53:19 +00:00
|
|
|
}
|
2018-09-27 11:14:55 +00:00
|
|
|
if queues, ok := v["queues"].([]interface{}); ok {
|
2018-09-27 15:19:47 +00:00
|
|
|
if len(queues) != 0 {
|
2018-09-27 11:14:55 +00:00
|
|
|
fmt.Println("Active queues:")
|
2018-09-27 11:55:41 +00:00
|
|
|
for _, v := range queues {
|
2018-09-27 14:51:17 +00:00
|
|
|
queueport := v.(map[string]interface{})["queue_port"].(float64)
|
|
|
|
queuesize := v.(map[string]interface{})["queue_size"].(float64)
|
|
|
|
queuepackets := v.(map[string]interface{})["queue_packets"].(float64)
|
|
|
|
queueid := v.(map[string]interface{})["queue_id"].(string)
|
2018-12-10 10:54:41 +00:00
|
|
|
portqueues[queueport]++
|
2018-09-27 14:51:17 +00:00
|
|
|
portqueuesize[queueport] += queuesize
|
|
|
|
portqueuepackets[queueport] += queuepackets
|
2018-09-27 15:19:47 +00:00
|
|
|
queuesizepercent := (100 / maximumqueuesize) * queuesize
|
|
|
|
fmt.Printf("- Switch port %d, Stream ID: %v, size: %d bytes (%d%% full), %d packets\n",
|
|
|
|
uint(queueport), []byte(queueid), uint(queuesize),
|
|
|
|
uint(queuesizepercent), uint(queuepackets))
|
2018-09-27 11:14:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-27 14:51:17 +00:00
|
|
|
if len(portqueuesize) > 0 && len(portqueuepackets) > 0 {
|
2018-09-27 15:19:47 +00:00
|
|
|
fmt.Println("Aggregated statistics by switchport:")
|
2018-09-27 14:51:17 +00:00
|
|
|
for k, v := range portqueuesize {
|
2018-09-27 15:19:47 +00:00
|
|
|
queuesizepercent := (100 / (portqueues[k] * maximumqueuesize)) * v
|
|
|
|
fmt.Printf("- Switch port %d, size: %d bytes (%d%% full), %d packets\n",
|
|
|
|
uint(k), uint(v), uint(queuesizepercent), uint(portqueuepackets[k]))
|
2018-09-27 14:51:17 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-23 03:30:56 +00:00
|
|
|
case "addpeer", "removepeer", "addallowedencryptionpublickey", "removeallowedencryptionpublickey", "addsourcesubnet", "addroute", "removesourcesubnet", "removeroute":
|
2018-05-21 13:25:11 +00:00
|
|
|
if _, ok := res["added"]; ok {
|
|
|
|
for _, v := range res["added"].([]interface{}) {
|
2018-05-21 17:12:36 +00:00
|
|
|
fmt.Println("Added:", fmt.Sprint(v))
|
2018-05-21 13:25:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, ok := res["not_added"]; ok {
|
|
|
|
for _, v := range res["not_added"].([]interface{}) {
|
2018-05-21 17:12:36 +00:00
|
|
|
fmt.Println("Not added:", fmt.Sprint(v))
|
2018-05-21 13:25:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, ok := res["removed"]; ok {
|
|
|
|
for _, v := range res["removed"].([]interface{}) {
|
2018-05-21 17:12:36 +00:00
|
|
|
fmt.Println("Removed:", fmt.Sprint(v))
|
2018-05-21 13:25:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, ok := res["not_removed"]; ok {
|
|
|
|
for _, v := range res["not_removed"].([]interface{}) {
|
2018-05-21 17:12:36 +00:00
|
|
|
fmt.Println("Not removed:", fmt.Sprint(v))
|
|
|
|
}
|
|
|
|
}
|
2018-07-09 18:30:41 +00:00
|
|
|
case "getallowedencryptionpublickeys":
|
2018-05-21 17:12:36 +00:00
|
|
|
if _, ok := res["allowed_box_pubs"]; !ok {
|
|
|
|
fmt.Println("All connections are allowed")
|
|
|
|
} else if res["allowed_box_pubs"] == nil {
|
|
|
|
fmt.Println("All connections are allowed")
|
|
|
|
} else {
|
|
|
|
fmt.Println("Connections are allowed only from the following public box keys:")
|
|
|
|
for _, v := range res["allowed_box_pubs"].([]interface{}) {
|
|
|
|
fmt.Println("-", v)
|
2018-05-21 13:25:11 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-09 18:30:41 +00:00
|
|
|
case "getmulticastinterfaces":
|
2018-05-23 21:13:52 +00:00
|
|
|
if _, ok := res["multicast_interfaces"]; !ok {
|
|
|
|
fmt.Println("No multicast interfaces found")
|
|
|
|
} else if res["multicast_interfaces"] == nil {
|
|
|
|
fmt.Println("No multicast interfaces found")
|
|
|
|
} else {
|
|
|
|
fmt.Println("Multicast peer discovery is active on:")
|
|
|
|
for _, v := range res["multicast_interfaces"].([]interface{}) {
|
|
|
|
fmt.Println("-", v)
|
|
|
|
}
|
|
|
|
}
|
2018-11-23 03:37:57 +00:00
|
|
|
case "getsourcesubnets":
|
|
|
|
if _, ok := res["source_subnets"]; !ok {
|
|
|
|
fmt.Println("No source subnets found")
|
|
|
|
} else if res["source_subnets"] == nil {
|
|
|
|
fmt.Println("No source subnets found")
|
|
|
|
} else {
|
|
|
|
fmt.Println("Source subnets:")
|
|
|
|
for _, v := range res["source_subnets"].([]interface{}) {
|
|
|
|
fmt.Println("-", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case "getroutes":
|
2019-03-06 17:32:25 +00:00
|
|
|
if routes, ok := res["routes"].(map[string]interface{}); !ok {
|
2018-11-23 03:37:57 +00:00
|
|
|
fmt.Println("No routes found")
|
|
|
|
} else {
|
2019-03-06 17:32:25 +00:00
|
|
|
if res["routes"] == nil || len(routes) == 0 {
|
|
|
|
fmt.Println("No routes found")
|
|
|
|
} else {
|
|
|
|
fmt.Println("Routes:")
|
|
|
|
for k, v := range routes {
|
|
|
|
if pv, ok := v.(string); ok {
|
|
|
|
fmt.Println("-", k, " via ", pv)
|
|
|
|
}
|
|
|
|
}
|
2018-11-23 03:37:57 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-06 17:37:48 +00:00
|
|
|
case "settunnelrouting":
|
|
|
|
fallthrough
|
|
|
|
case "gettunnelrouting":
|
|
|
|
if enabled, ok := res["enabled"].(bool); !ok {
|
|
|
|
fmt.Println("Tunnel routing is disabled")
|
|
|
|
} else if !enabled {
|
|
|
|
fmt.Println("Tunnel routing is disabled")
|
|
|
|
} else {
|
|
|
|
fmt.Println("Tunnel routing is enabled")
|
|
|
|
}
|
2018-05-21 13:14:57 +00:00
|
|
|
default:
|
|
|
|
if json, err := json.MarshalIndent(recv["response"], "", " "); err == nil {
|
|
|
|
fmt.Println(string(json))
|
|
|
|
}
|
2018-05-20 22:32:17 +00:00
|
|
|
}
|
2018-12-10 10:54:41 +00:00
|
|
|
} else {
|
|
|
|
logger.Println("Error receiving response:", err)
|
2018-05-20 22:32:17 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 10:54:41 +00:00
|
|
|
if v, ok := recv["status"]; ok && v != "success" {
|
2018-05-21 06:28:03 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
2018-05-20 22:32:17 +00:00
|
|
|
}
|