From f14a95d032f461ac95bbc884a021a9959a71cd7c Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Fri, 24 Jan 2025 20:01:49 +0200 Subject: [PATCH] implement get routes cli --- main.go | 53 ++++++++++++++++++++++++++++++++++++++++----- net/lux_router.go | 1 + rpc/lux_rpc_data.go | 22 ++++++++++++++++++- 3 files changed, 70 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 490b45d..2961e79 100644 --- a/main.go +++ b/main.go @@ -206,7 +206,51 @@ var rpcQueryHostname string var rpcGetRoutes bool func rpcMain() { + var cl rpc.LuxRpcClient + var err error + if strings.HasPrefix(rpcPath, "unix://") { + cl, err = rpc.LuxDialRpc("unix", rpcPath[7:]) + } else if strings.HasPrefix(rpcPath, "tcp://") { + cl, err = rpc.LuxDialRpc("tcp", rpcPath[6:]) + } else { + fmt.Fprintln(os.Stderr, "unknown RPC network (must be unix:// or tcp://)") + os.Exit(1) + } + + if err != nil { + fmt.Fprintf(os.Stderr, "failed to dial RPC: %v\n", err) + os.Exit(1) + } + + defer cl.Close() + + // now we send requests + counter := 0 + + if rpcGetRoutes { + rpcRes, rpcErr, err := cl.Execute(rpc.LuxRpcRequest{ + RequestID: counter, + Controller: "router", + Command: "get", + }) + if err != nil { + fmt.Fprintf(os.Stderr, "failed to send request: %v\n", err) + os.Exit(1) + } + counter++ + + if rpcErr.ErrorCode != 0 { + // we got error + fmt.Fprintf(os.Stderr, "RPC error %d: %s\n", rpcErr.ErrorCode, rpcErr.Message) + os.Exit(1) + } + + // pretty print routes + for _, route := range rpcRes.Routes { + fmt.Println(route) + } + } } func main() { @@ -215,14 +259,13 @@ func main() { // or by explicit cli flag (--node, --host, --rpc) flag.BoolVar(&isNode, "node", false, "LUX node") flag.BoolVar(&isHost, "host", false, "LUX host") - flag.BoolVar(&isRpc, "rpc", false, "RPC tool") flag.StringVar(&configPath, "config", "", "node or host config") flag.BoolVar(&bootstrap, "bootstrap", false, "bootstrap node keystore. config must be specified") flag.BoolVar(&justNodeId, "just-node-id", false, "when bootstrapping only output node id to stdout") flag.BoolVar(&daemonize, "daemonize", false, "run LUX as daemon in background") flag.StringVar(&pidPath, "pid", "", "after daemonization LUX will write its PID here") - flag.StringVar(&rpcPath, "rpc-path", "", "path to RPC UNIX socket or TCP socket, must be in unix:// or tcp:// form") + flag.StringVar(&rpcPath, "rpc", "", "Run as RPC client, specify path to RPC UNIX socket or TCP socket, must be in unix:// or tcp:// form") flag.StringVar(&rpcNewHost, "rpc-new-host", "", "RPC node create new host, specifies path for new keystore") flag.StringVar(&rpcNewNode, "rpc-new-node", "", "RPC node create new node, specifies path for new keystore") flag.StringVar(&rpcQueryHost, "rpc-query-host", "", "RPC node query host state by ID") @@ -230,14 +273,14 @@ func main() { flag.BoolVar(&rpcGetRoutes, "rpc-get-routes", false, "RPC node list established routes") flag.Parse() - if !isNode && !isHost && !isRpc { + if rpcPath != "" { + isRpc = true + } else if !isNode && !isHost { // determine by argv[0] if strings.Contains(os.Args[0], "node") { isNode = true } else if strings.Contains(os.Args[0], "host") { isHost = true - } else if strings.Contains(os.Args[0], "rpc") { - isRpc = true } } diff --git a/net/lux_router.go b/net/lux_router.go index df2ea23..c414c19 100644 --- a/net/lux_router.go +++ b/net/lux_router.go @@ -467,6 +467,7 @@ func (r *LuxRouter) Multicast(packet LuxPacket, group proto.LuxType) error { func (route *LuxRoute) IntoRpc() rpc.LuxRpcRoute { return rpc.LuxRpcRoute{ Type: int(route.Type), + ChannelType: route.Associated.Type.String(), Target: route.Target.String(), Source: route.Source.String(), Destination: route.Destination.String(), diff --git a/rpc/lux_rpc_data.go b/rpc/lux_rpc_data.go index fed00b3..a6ffe7e 100644 --- a/rpc/lux_rpc_data.go +++ b/rpc/lux_rpc_data.go @@ -1,6 +1,9 @@ package rpc -import "encoding/xml" +import ( + "encoding/xml" + "fmt" +) type LuxRpcWAN struct { XMLName xml.Name `xml:"wan"` @@ -64,11 +67,28 @@ type LuxRpcKeyStore struct { type LuxRpcRoute struct { XMLName xml.Name `xml:"route"` Type int `xml:"type,attr"` + ChannelType string `xml:"channelType,attr"` Target string `xml:"target"` Source string `xml:"source"` Destination string `xml:"destination"` } +func (route *LuxRpcRoute) String() string { + var dir string + switch route.Type { + case 0: + dir = " <-> *" + case 1: + dir = "* <-> " + default: + dir = " " + } + + return fmt.Sprintf("%s%s%s %s %s", + route.Target, dir, route.Source, + route.ChannelType, route.Destination) +} + type LuxRpcResponse struct { XMLName xml.Name `xml:"response"` RequestID int `xml:"id,attr"`