packages/hyprspace: implement RPC client and new CLI commands

This commit is contained in:
Max Headroom 2023-01-20 23:37:26 +01:00
parent 88f402dbaa
commit 6bc8b856ed
5 changed files with 106 additions and 1 deletions

View file

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

View file

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

View file

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

View file

@ -0,0 +1,9 @@
package cli
import "fmt"
func printList(strings []string) {
for _, ma := range strings {
fmt.Printf(" %s\n", ma)
}
}

View file

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