2023-01-23 20:27:20 +02:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/DataDrake/cli-ng/v2/cmd"
|
|
|
|
"github.com/hyprspace/hyprspace/rpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
var Route = cmd.Sub{
|
|
|
|
Name: "route",
|
|
|
|
Alias: "r",
|
|
|
|
Short: "Control routing",
|
|
|
|
Args: &RouteArgs{},
|
|
|
|
Run: RouteRun,
|
|
|
|
}
|
|
|
|
|
|
|
|
type RouteArgs struct {
|
2023-10-23 00:39:39 +03:00
|
|
|
Action string
|
|
|
|
Args []string `zero:"true"`
|
2023-01-23 20:27:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func RouteRun(r *cmd.Root, c *cmd.Sub) {
|
|
|
|
// Parse Command Args
|
|
|
|
args := c.Args.(*RouteArgs)
|
2023-10-23 00:39:39 +03:00
|
|
|
ifName := r.Flags.(*GlobalFlags).InterfaceName
|
|
|
|
if ifName == "" {
|
|
|
|
ifName = "hyprspace"
|
|
|
|
}
|
2023-01-23 20:27:20 +02:00
|
|
|
|
|
|
|
action := rpc.RouteAction(args.Action)
|
|
|
|
rArgs := rpc.RouteArgs{
|
|
|
|
Action: action,
|
|
|
|
Args: args.Args,
|
|
|
|
}
|
2023-10-23 00:39:39 +03:00
|
|
|
reply := rpc.Route(ifName, rArgs)
|
2023-01-23 20:27:20 +02:00
|
|
|
for _, r := range reply.Routes {
|
|
|
|
var target string
|
|
|
|
connectStatus := ""
|
|
|
|
if r.IsRelay {
|
2023-10-23 00:39:39 +03:00
|
|
|
target = fmt.Sprintf("/p2p/%s/p2p-circuit/p2p/%s", r.RelayAddr, r.TargetAddr)
|
2023-01-23 20:27:20 +02:00
|
|
|
} else {
|
2023-10-23 00:39:39 +03:00
|
|
|
target = fmt.Sprintf("/p2p/%s", r.TargetAddr)
|
2023-01-23 20:27:20 +02:00
|
|
|
}
|
|
|
|
if r.IsConnected {
|
|
|
|
connectStatus = " connected"
|
|
|
|
}
|
|
|
|
fmt.Printf("%s via %s%s\n", &r.Network, target, connectStatus)
|
|
|
|
}
|
|
|
|
}
|