2018-05-20 23:32:17 +01:00
|
|
|
package main
|
|
|
|
|
2018-12-07 19:56:04 -06:00
|
|
|
import (
|
2018-12-10 10:54:41 +00:00
|
|
|
"bytes"
|
2018-12-07 19:56:04 -06:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2018-12-10 10:54:41 +00:00
|
|
|
"log"
|
2018-12-07 19:56:04 -06:00
|
|
|
"net"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2022-09-03 10:50:43 +01:00
|
|
|
"time"
|
2018-05-20 23:32:17 +01:00
|
|
|
|
2022-09-03 10:50:43 +01:00
|
|
|
"github.com/olekukonko/tablewriter"
|
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/admin"
|
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/core"
|
2022-09-24 12:22:38 +01:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/multicast"
|
2022-09-24 14:41:47 +01:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/tun"
|
2019-08-11 00:31:22 +03:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/version"
|
2018-12-07 19:56:04 -06:00
|
|
|
)
|
2018-07-07 12:08:52 +01:00
|
|
|
|
2018-05-20 23:32:17 +01:00
|
|
|
func main() {
|
2019-12-25 17:45:24 -06:00
|
|
|
// makes sure we can use defer and still return an error code to the OS
|
|
|
|
os.Exit(run())
|
|
|
|
}
|
|
|
|
|
|
|
|
func run() int {
|
2018-12-10 10:54:41 +00:00
|
|
|
logbuffer := &bytes.Buffer{}
|
|
|
|
logger := log.New(logbuffer, "", log.Flags())
|
2021-08-03 02:47:38 +05:00
|
|
|
|
2019-12-25 17:45:24 -06:00
|
|
|
defer func() int {
|
2018-12-10 11:29:42 +00:00
|
|
|
if r := recover(); r != nil {
|
|
|
|
logger.Println("Fatal error:", r)
|
|
|
|
fmt.Print(logbuffer)
|
2019-12-25 17:45:24 -06:00
|
|
|
return 1
|
2018-12-10 11:29:42 +00:00
|
|
|
}
|
2019-12-25 17:45:24 -06:00
|
|
|
return 0
|
2018-12-10 11:29:42 +00:00
|
|
|
}()
|
2018-12-10 10:54:41 +00:00
|
|
|
|
2021-08-03 02:47:38 +05:00
|
|
|
cmdLineEnv := newCmdLineEnv()
|
|
|
|
cmdLineEnv.parseFlagsAndArgs()
|
|
|
|
|
|
|
|
if cmdLineEnv.ver {
|
2019-08-14 20:09:02 +01: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")
|
2019-12-25 17:45:24 -06:00
|
|
|
return 0
|
2019-08-11 00:31:22 +03:00
|
|
|
}
|
|
|
|
|
2021-08-03 02:47:38 +05:00
|
|
|
if len(cmdLineEnv.args) == 0 {
|
2018-12-09 13:33:18 +00:00
|
|
|
flag.Usage()
|
2019-12-25 17:45:24 -06:00
|
|
|
return 0
|
2018-05-21 07:28:03 +01:00
|
|
|
}
|
2018-05-20 23:46:02 +01:00
|
|
|
|
2021-08-03 02:47:38 +05:00
|
|
|
cmdLineEnv.setEndpoint(logger)
|
2018-12-10 11:09:10 +00:00
|
|
|
|
2021-08-03 02:47:38 +05:00
|
|
|
var conn net.Conn
|
2022-09-03 10:50:43 +01:00
|
|
|
u, err := url.Parse(cmdLineEnv.endpoint)
|
2021-08-03 02:47:38 +05:00
|
|
|
if err == nil {
|
|
|
|
switch strings.ToLower(u.Scheme) {
|
|
|
|
case "unix":
|
2022-09-03 10:50:43 +01:00
|
|
|
logger.Println("Connecting to UNIX socket", cmdLineEnv.endpoint[7:])
|
|
|
|
conn, err = net.Dial("unix", cmdLineEnv.endpoint[7:])
|
2021-08-03 02:47:38 +05:00
|
|
|
case "tcp":
|
|
|
|
logger.Println("Connecting to TCP socket", u.Host)
|
|
|
|
conn, err = net.Dial("tcp", u.Host)
|
|
|
|
default:
|
|
|
|
logger.Println("Unknown protocol or malformed address - check your endpoint")
|
|
|
|
err = errors.New("protocol not supported")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logger.Println("Connecting to TCP socket", u.Host)
|
2022-09-03 10:50:43 +01:00
|
|
|
conn, err = net.Dial("tcp", cmdLineEnv.endpoint)
|
2021-08-03 02:47:38 +05:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-09-03 10:50:43 +01:00
|
|
|
logger.Println("Connected")
|
|
|
|
defer conn.Close()
|
2021-08-03 02:47:38 +05:00
|
|
|
|
2022-09-03 10:50:43 +01:00
|
|
|
decoder := json.NewDecoder(conn)
|
|
|
|
encoder := json.NewEncoder(conn)
|
|
|
|
send := &admin.AdminSocketRequest{}
|
|
|
|
recv := &admin.AdminSocketResponse{}
|
2022-09-24 21:28:09 +01:00
|
|
|
args := map[string]string{}
|
2022-09-03 10:50:43 +01:00
|
|
|
for c, a := range cmdLineEnv.args {
|
|
|
|
if c == 0 {
|
|
|
|
if strings.HasPrefix(a, "-") {
|
|
|
|
logger.Printf("Ignoring flag %s as it should be specified before other parameters\n", a)
|
|
|
|
continue
|
2018-05-21 14:25:11 +01:00
|
|
|
}
|
2022-09-03 10:50:43 +01:00
|
|
|
logger.Printf("Sending request: %v\n", a)
|
|
|
|
send.Name = a
|
|
|
|
continue
|
2021-08-03 02:47:38 +05:00
|
|
|
}
|
2022-09-24 21:28:09 +01:00
|
|
|
tokens := strings.SplitN(a, "=", 2)
|
2022-09-03 10:50:43 +01:00
|
|
|
switch {
|
|
|
|
case len(tokens) == 1:
|
2022-09-24 21:28:09 +01:00
|
|
|
logger.Println("Ignoring invalid argument:", a)
|
2022-09-03 10:50:43 +01:00
|
|
|
default:
|
2022-09-24 21:28:09 +01:00
|
|
|
args[tokens[0]] = tokens[1]
|
2021-08-03 02:47:38 +05:00
|
|
|
}
|
|
|
|
}
|
2022-09-24 21:28:09 +01:00
|
|
|
if send.Arguments, err = json.Marshal(args); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-09-03 10:50:43 +01:00
|
|
|
if err := encoder.Encode(&send); err != nil {
|
|
|
|
panic(err)
|
2021-08-03 02:47:38 +05:00
|
|
|
}
|
2022-09-03 10:50:43 +01:00
|
|
|
logger.Printf("Request sent")
|
|
|
|
if err := decoder.Decode(&recv); err != nil {
|
|
|
|
panic(err)
|
2021-08-03 02:47:38 +05:00
|
|
|
}
|
2022-09-03 10:50:43 +01:00
|
|
|
if recv.Status == "error" {
|
|
|
|
if err := recv.Error; err != "" {
|
|
|
|
fmt.Println("Admin socket returned an error:", err)
|
|
|
|
} else {
|
|
|
|
fmt.Println("Admin socket returned an error but didn't specify any error text")
|
2021-08-03 02:47:38 +05:00
|
|
|
}
|
2022-09-03 10:50:43 +01:00
|
|
|
return 1
|
2021-08-03 02:47:38 +05:00
|
|
|
}
|
2022-09-03 10:50:43 +01:00
|
|
|
if cmdLineEnv.injson {
|
|
|
|
if json, err := json.MarshalIndent(recv.Response, "", " "); err == nil {
|
|
|
|
fmt.Println(string(json))
|
2021-08-03 02:47:38 +05:00
|
|
|
}
|
2022-09-03 10:50:43 +01:00
|
|
|
return 0
|
2021-08-03 02:47:38 +05:00
|
|
|
}
|
|
|
|
|
2022-09-03 10:50:43 +01:00
|
|
|
table := tablewriter.NewWriter(os.Stdout)
|
|
|
|
table.SetAlignment(tablewriter.ALIGN_LEFT)
|
|
|
|
table.SetAutoFormatHeaders(false)
|
|
|
|
table.SetCenterSeparator("")
|
|
|
|
table.SetColumnSeparator("")
|
|
|
|
table.SetRowSeparator("")
|
|
|
|
table.SetHeaderLine(false)
|
|
|
|
table.SetBorder(false)
|
|
|
|
table.SetTablePadding("\t") // pad with tabs
|
|
|
|
table.SetNoWhiteSpace(true)
|
2022-09-24 12:22:38 +01:00
|
|
|
table.SetAutoWrapText(false)
|
2021-08-03 02:47:38 +05:00
|
|
|
|
2022-09-24 21:28:09 +01:00
|
|
|
switch strings.ToLower(send.Name) {
|
2022-09-03 10:50:43 +01:00
|
|
|
case "list":
|
|
|
|
var resp admin.ListResponse
|
|
|
|
if err := json.Unmarshal(recv.Response, &resp); err != nil {
|
|
|
|
panic(err)
|
2021-08-03 02:47:38 +05:00
|
|
|
}
|
2022-09-24 12:22:38 +01:00
|
|
|
table.SetHeader([]string{"Command", "Arguments", "Description"})
|
2022-09-03 10:50:43 +01:00
|
|
|
for _, entry := range resp.List {
|
2022-09-24 12:22:38 +01:00
|
|
|
for i := range entry.Fields {
|
|
|
|
entry.Fields[i] = entry.Fields[i] + "=..."
|
|
|
|
}
|
|
|
|
table.Append([]string{entry.Command, strings.Join(entry.Fields, ", "), entry.Description})
|
2021-08-03 02:47:38 +05:00
|
|
|
}
|
2022-09-03 10:50:43 +01:00
|
|
|
table.Render()
|
2021-08-03 02:47:38 +05:00
|
|
|
|
2022-09-03 10:50:43 +01:00
|
|
|
case "getself":
|
|
|
|
var resp admin.GetSelfResponse
|
|
|
|
if err := json.Unmarshal(recv.Response, &resp); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
table.Append([]string{"Build name:", resp.BuildName})
|
|
|
|
table.Append([]string{"Build version:", resp.BuildVersion})
|
|
|
|
table.Append([]string{"IPv6 address:", resp.IPAddress})
|
|
|
|
table.Append([]string{"IPv6 subnet:", resp.Subnet})
|
2023-03-19 10:33:07 +00:00
|
|
|
table.Append([]string{"Routing table size:", fmt.Sprintf("%d", resp.RoutingEntries)})
|
2022-09-03 10:50:43 +01:00
|
|
|
table.Append([]string{"Public key:", resp.PublicKey})
|
|
|
|
table.Render()
|
|
|
|
|
|
|
|
case "getpeers":
|
|
|
|
var resp admin.GetPeersResponse
|
|
|
|
if err := json.Unmarshal(recv.Response, &resp); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-11-19 08:42:27 +00:00
|
|
|
table.SetHeader([]string{"URI", "State", "Dir", "IP Address", "Uptime", "RTT", "RX", "TX", "Down", "Up", "Pr", "Cost", "Last Error"})
|
2022-09-03 10:50:43 +01:00
|
|
|
for _, peer := range resp.Peers {
|
2024-11-19 08:42:27 +00:00
|
|
|
state, lasterr, dir, rtt, rxr, txr := "Up", "-", "Out", "-", "-", "-"
|
2023-04-06 21:45:49 +01:00
|
|
|
if !peer.Up {
|
2023-05-19 20:57:14 +01:00
|
|
|
state, lasterr = "Down", fmt.Sprintf("%s ago: %s", peer.LastErrorTime.Round(time.Second), peer.LastError)
|
2024-05-30 22:46:06 +01:00
|
|
|
} else if rttms := float64(peer.Latency.Microseconds()) / 1000; rttms > 0 {
|
|
|
|
rtt = fmt.Sprintf("%.02fms", rttms)
|
2023-04-06 21:45:49 +01:00
|
|
|
}
|
|
|
|
if peer.Inbound {
|
|
|
|
dir = "In"
|
|
|
|
}
|
2024-01-15 23:14:43 +00:00
|
|
|
uristring := peer.URI
|
|
|
|
if uri, err := url.Parse(peer.URI); err == nil {
|
|
|
|
uri.RawQuery = ""
|
|
|
|
uristring = uri.String()
|
2023-04-06 21:45:49 +01:00
|
|
|
}
|
2024-11-19 08:42:27 +00:00
|
|
|
if peer.RXRate > 0 {
|
|
|
|
rxr = peer.RXRate.String() + "/s"
|
|
|
|
}
|
|
|
|
if peer.TXRate > 0 {
|
|
|
|
txr = peer.TXRate.String() + "/s"
|
|
|
|
}
|
2022-09-03 10:50:43 +01:00
|
|
|
table.Append([]string{
|
2024-01-15 23:14:43 +00:00
|
|
|
uristring,
|
2023-04-06 21:45:49 +01:00
|
|
|
state,
|
|
|
|
dir,
|
2022-09-03 10:50:43 +01:00
|
|
|
peer.IPAddress,
|
|
|
|
(time.Duration(peer.Uptime) * time.Second).String(),
|
2024-05-30 22:46:06 +01:00
|
|
|
rtt,
|
2022-09-03 10:50:43 +01:00
|
|
|
peer.RXBytes.String(),
|
|
|
|
peer.TXBytes.String(),
|
2024-11-19 08:42:27 +00:00
|
|
|
rxr,
|
|
|
|
txr,
|
2022-10-26 18:25:48 +01:00
|
|
|
fmt.Sprintf("%d", peer.Priority),
|
2024-09-21 23:05:23 +01:00
|
|
|
fmt.Sprintf("%d", peer.Cost),
|
2023-04-06 21:45:49 +01:00
|
|
|
lasterr,
|
2022-09-03 10:50:43 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
table.Render()
|
|
|
|
|
2023-05-13 14:44:38 -05:00
|
|
|
case "gettree":
|
|
|
|
var resp admin.GetTreeResponse
|
2022-09-03 10:50:43 +01:00
|
|
|
if err := json.Unmarshal(recv.Response, &resp); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-03-26 16:49:40 -05:00
|
|
|
//table.SetHeader([]string{"Public Key", "IP Address", "Port", "Rest"})
|
|
|
|
table.SetHeader([]string{"Public Key", "IP Address", "Parent", "Sequence"})
|
2023-05-13 14:44:38 -05:00
|
|
|
for _, tree := range resp.Tree {
|
2022-09-03 10:50:43 +01:00
|
|
|
table.Append([]string{
|
2023-05-13 14:44:38 -05:00
|
|
|
tree.PublicKey,
|
|
|
|
tree.IPAddress,
|
|
|
|
tree.Parent,
|
|
|
|
fmt.Sprintf("%d", tree.Sequence),
|
2023-03-26 16:49:40 -05:00
|
|
|
//fmt.Sprintf("%d", dht.Port),
|
|
|
|
//fmt.Sprintf("%d", dht.Rest),
|
2022-09-03 10:50:43 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
table.Render()
|
|
|
|
|
2023-05-13 14:44:38 -05:00
|
|
|
case "getpaths":
|
|
|
|
var resp admin.GetPathsResponse
|
|
|
|
if err := json.Unmarshal(recv.Response, &resp); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
table.SetHeader([]string{"Public Key", "IP Address", "Path", "Seq"})
|
|
|
|
for _, p := range resp.Paths {
|
|
|
|
table.Append([]string{
|
|
|
|
p.PublicKey,
|
|
|
|
p.IPAddress,
|
|
|
|
fmt.Sprintf("%v", p.Path),
|
|
|
|
fmt.Sprintf("%d", p.Sequence),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
table.Render()
|
2022-09-03 10:50:43 +01:00
|
|
|
|
|
|
|
case "getsessions":
|
|
|
|
var resp admin.GetSessionsResponse
|
|
|
|
if err := json.Unmarshal(recv.Response, &resp); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-09-03 16:55:57 +01:00
|
|
|
table.SetHeader([]string{"Public Key", "IP Address", "Uptime", "RX", "TX"})
|
2022-09-03 10:50:43 +01:00
|
|
|
for _, p := range resp.Sessions {
|
|
|
|
table.Append([]string{
|
|
|
|
p.PublicKey,
|
|
|
|
p.IPAddress,
|
2022-09-03 16:55:57 +01:00
|
|
|
(time.Duration(p.Uptime) * time.Second).String(),
|
|
|
|
p.RXBytes.String(),
|
|
|
|
p.TXBytes.String(),
|
2022-09-03 10:50:43 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
table.Render()
|
|
|
|
|
|
|
|
case "getnodeinfo":
|
|
|
|
var resp core.GetNodeInfoResponse
|
|
|
|
if err := json.Unmarshal(recv.Response, &resp); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
for _, v := range resp {
|
|
|
|
fmt.Println(string(v))
|
|
|
|
break
|
2021-08-03 02:47:38 +05:00
|
|
|
}
|
|
|
|
|
2022-09-24 12:22:38 +01:00
|
|
|
case "getmulticastinterfaces":
|
|
|
|
var resp multicast.GetMulticastInterfacesResponse
|
|
|
|
if err := json.Unmarshal(recv.Response, &resp); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-11-23 14:49:48 +00:00
|
|
|
fmtBool := func(b bool) string {
|
|
|
|
if b {
|
|
|
|
return "Yes"
|
|
|
|
}
|
|
|
|
return "-"
|
|
|
|
}
|
|
|
|
table.SetHeader([]string{"Name", "Listen Address", "Beacon", "Listen", "Password"})
|
2022-09-24 12:22:38 +01:00
|
|
|
for _, p := range resp.Interfaces {
|
2024-11-23 14:49:48 +00:00
|
|
|
table.Append([]string{
|
|
|
|
p.Name,
|
|
|
|
p.Address,
|
|
|
|
fmtBool(p.Beacon),
|
|
|
|
fmtBool(p.Listen),
|
|
|
|
fmtBool(p.Password),
|
|
|
|
})
|
2022-09-24 12:22:38 +01:00
|
|
|
}
|
|
|
|
table.Render()
|
|
|
|
|
|
|
|
case "gettun":
|
2022-09-24 14:41:47 +01:00
|
|
|
var resp tun.GetTUNResponse
|
2022-09-24 12:22:38 +01:00
|
|
|
if err := json.Unmarshal(recv.Response, &resp); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
table.Append([]string{"TUN enabled:", fmt.Sprintf("%#v", resp.Enabled)})
|
|
|
|
if resp.Enabled {
|
|
|
|
table.Append([]string{"Interface name:", resp.Name})
|
|
|
|
table.Append([]string{"Interface MTU:", fmt.Sprintf("%d", resp.MTU)})
|
|
|
|
}
|
|
|
|
table.Render()
|
|
|
|
|
2022-10-02 13:15:11 +01:00
|
|
|
case "addpeer", "removepeer":
|
|
|
|
|
2022-09-03 10:50:43 +01:00
|
|
|
default:
|
2022-09-24 12:22:38 +01:00
|
|
|
fmt.Println(string(recv.Response))
|
2018-05-20 23:32:17 +01:00
|
|
|
}
|
|
|
|
|
2022-09-03 10:50:43 +01:00
|
|
|
return 0
|
2018-05-20 23:32:17 +01:00
|
|
|
}
|