implement get routes cli

This commit is contained in:
mykola2312 2025-01-24 20:01:49 +02:00
parent ba05370fcf
commit f14a95d032
3 changed files with 70 additions and 6 deletions

53
main.go
View file

@ -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
}
}

View file

@ -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(),

View file

@ -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"`