2018-05-20 22:32:17 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import "flag"
|
|
|
|
import "fmt"
|
|
|
|
import "strings"
|
|
|
|
import "net"
|
2018-05-21 12:54:51 +00:00
|
|
|
import "sort"
|
2018-05-20 22:32:17 +00:00
|
|
|
import "encoding/json"
|
|
|
|
import "strconv"
|
|
|
|
import "os"
|
|
|
|
|
|
|
|
type admin_info map[string]interface{}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
server := flag.String("endpoint", "localhost:9001", "Admin socket endpoint")
|
2018-05-21 13:29:27 +00:00
|
|
|
injson := flag.Bool("json", false, "Output in JSON format")
|
2018-05-20 22:32:17 +00:00
|
|
|
flag.Parse()
|
|
|
|
args := flag.Args()
|
|
|
|
|
2018-05-21 06:28:03 +00:00
|
|
|
if len(args) == 0 {
|
2018-05-21 13:29:27 +00:00
|
|
|
fmt.Println("usage:", os.Args[0], "[-endpoint=localhost:9001] [-json] command [key=value] [...]")
|
2018-05-21 06:28:03 +00:00
|
|
|
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], "-endpoint=localhost:9001 getDHT")
|
|
|
|
return
|
|
|
|
}
|
2018-05-20 22:46:02 +00:00
|
|
|
|
2018-05-20 22:32:17 +00:00
|
|
|
conn, err := net.Dial("tcp", *server)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
send["request"] = a
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
tokens := strings.Split(a, "=")
|
|
|
|
if i, err := strconv.Atoi(tokens[1]); err == nil {
|
|
|
|
send[tokens[0]] = i
|
|
|
|
} else {
|
|
|
|
switch tokens[1] {
|
|
|
|
case "true":
|
|
|
|
send[tokens[0]] = true
|
|
|
|
case "false":
|
|
|
|
send[tokens[0]] = false
|
|
|
|
default:
|
|
|
|
send[tokens[0]] = tokens[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := encoder.Encode(&send); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if err := decoder.Decode(&recv); err == nil {
|
2018-05-21 12:54:51 +00:00
|
|
|
if recv["status"] == "error" {
|
|
|
|
if err, ok := recv["error"]; ok {
|
2018-05-21 13:14:57 +00:00
|
|
|
fmt.Println("Error:", err)
|
2018-05-21 12:54:51 +00:00
|
|
|
} else {
|
|
|
|
fmt.Println("Unspecified error occured")
|
|
|
|
}
|
|
|
|
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-05-21 06:28:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, ok := recv["response"]; !ok {
|
2018-05-21 12:54:51 +00:00
|
|
|
fmt.Println("Missing response body (malformed response?)")
|
2018-05-21 06:28:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
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-05-20 22:32:17 +00:00
|
|
|
switch req["request"] {
|
|
|
|
case "dot":
|
|
|
|
fmt.Println(res["dot"])
|
2018-05-21 13:14:57 +00:00
|
|
|
case "help", "getPeers", "getSwitchPeers", "getDHT", "getSessions":
|
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{}) {
|
|
|
|
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 13:14:57 +00:00
|
|
|
fmt.Printf("%-"+fmt.Sprint(maxWidths[k])+"s ", fmt.Sprint(slv.(map[string]interface{})[k]))
|
2018-05-21 12:54:51 +00:00
|
|
|
}
|
|
|
|
fmt.Println()
|
|
|
|
}
|
2018-05-20 22:32:17 +00:00
|
|
|
}
|
2018-05-21 13:14:57 +00:00
|
|
|
case "getTunTap", "setTunTap":
|
|
|
|
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-05-21 13:25:11 +00:00
|
|
|
case "addPeer", "removePeer":
|
|
|
|
if _, ok := res["added"]; ok {
|
|
|
|
for _, v := range res["added"].([]interface{}) {
|
|
|
|
fmt.Println("Peer added:", fmt.Sprint(v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, ok := res["not_added"]; ok {
|
|
|
|
for _, v := range res["not_added"].([]interface{}) {
|
|
|
|
fmt.Println("Peer not added:", fmt.Sprint(v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, ok := res["removed"]; ok {
|
|
|
|
for _, v := range res["removed"].([]interface{}) {
|
|
|
|
fmt.Println("Peer removed:", fmt.Sprint(v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, ok := res["not_removed"]; ok {
|
|
|
|
for _, v := range res["not_removed"].([]interface{}) {
|
|
|
|
fmt.Println("Peer not removed:", fmt.Sprint(v))
|
|
|
|
}
|
|
|
|
}
|
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-05-21 06:28:03 +00:00
|
|
|
if v, ok := recv["status"]; ok && v == "error" {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
2018-05-20 22:32:17 +00:00
|
|
|
}
|