diff --git a/cli/peers.go b/cli/peers.go new file mode 100644 index 0000000..7d41bfc --- /dev/null +++ b/cli/peers.go @@ -0,0 +1,28 @@ +package cli + +import ( + "fmt" + + "github.com/DataDrake/cli-ng/v2/cmd" + "github.com/hyprspace/hyprspace/rpc" +) + +var Peers = cmd.Sub{ + Name: "peers", + Short: "List peer connections", + Args: &PeersArgs{}, + Run: PeersRun, +} + +type PeersArgs struct { + InterfaceName string +} + +func PeersRun(r *cmd.Root, c *cmd.Sub) { + args := c.Args.(*PeersArgs) + + peers := rpc.Peers(args.InterfaceName) + for _, ma := range peers.PeerAddrs { + fmt.Println(ma) + } +} diff --git a/cli/root.go b/cli/root.go index 7ef54f4..9268061 100644 --- a/cli/root.go +++ b/cli/root.go @@ -11,7 +11,7 @@ import ( var appVersion string = "develop" -//GlobalFlags contains the flags for commands. +// GlobalFlags contains the flags for commands. type GlobalFlags struct { Config string `short:"c" long:"config" desc:"Specify a custom config path."` } @@ -31,6 +31,8 @@ func init() { cmd.Register(&Init) cmd.Register(&Up) cmd.Register(&Down) + cmd.Register(&Status) + cmd.Register(&Peers) cmd.Register(&cmd.Version) } diff --git a/cli/status.go b/cli/status.go new file mode 100644 index 0000000..bd67645 --- /dev/null +++ b/cli/status.go @@ -0,0 +1,33 @@ +package cli + +import ( + "fmt" + + "github.com/DataDrake/cli-ng/v2/cmd" + "github.com/hyprspace/hyprspace/rpc" +) + +var Status = cmd.Sub{ + Name: "status", + Alias: "s", + Short: "Display Hyprspace daemon status", + Args: &StatusArgs{}, + Run: StatusRun, +} + +type StatusArgs struct { + InterfaceName string +} + +func StatusRun(r *cmd.Root, c *cmd.Sub) { + // Parse Command Args + args := c.Args.(*StatusArgs) + + status := rpc.Status(args.InterfaceName) + fmt.Println("PeerID:", status.PeerID) + fmt.Println("Swarm peers:", status.SwarmPeersCurrent) + fmt.Printf("Connected VPN nodes: %d/%d\n", status.NetPeersCurrent, status.NetPeersMax) + printList(status.NetPeerAddrsCurrent) + fmt.Println("Addresses:") + printList(status.ListenAddrs) +} diff --git a/cli/util.go b/cli/util.go new file mode 100644 index 0000000..e02f9e6 --- /dev/null +++ b/cli/util.go @@ -0,0 +1,9 @@ +package cli + +import "fmt" + +func printList(strings []string) { + for _, ma := range strings { + fmt.Printf(" %s\n", ma) + } +} diff --git a/rpc/client.go b/rpc/client.go new file mode 100644 index 0000000..2413f01 --- /dev/null +++ b/rpc/client.go @@ -0,0 +1,33 @@ +package rpc + +import ( + "fmt" + "log" + "net/rpc" +) + +func connect(ifname string) *rpc.Client { + client, err := rpc.Dial("unix", fmt.Sprintf("/run/hyprspace-rpc.%s.sock", ifname)) + if err != nil { + log.Fatal("[!] Failed to connect to RPC server: ", err) + } + return client +} + +func Status(ifname string) StatusReply { + client := connect(ifname) + var reply StatusReply + if err := client.Call("HyprspaceRPC.Status", new(Args), &reply); err != nil { + log.Fatal("[!] RPC call failed: ", err) + } + return reply +} + +func Peers(ifname string) PeersReply { + client := connect(ifname) + var reply PeersReply + if err := client.Call("HyprspaceRPC.Peers", new(Args), &reply); err != nil { + log.Fatal("[!] RPC call failed: ", err) + } + return reply +}