implement RPC node get-hosts

This commit is contained in:
mykola2312 2025-01-29 06:52:33 +02:00
parent dddc024f64
commit 74fbfe16d2
2 changed files with 60 additions and 7 deletions

52
main.go
View file

@ -561,6 +561,18 @@ signaling:
host.Stop()
}
func printRpcHost(host rpc.LuxRpcHost) {
fmt.Printf("%s\n", host.HostID)
fmt.Printf("|hostname: %s\n", host.Hostname)
fmt.Printf("|wan:\n|\taddr4\t%s\n|\taddr6\t%s\n", host.State.WAN.Addr4, host.State.WAN.Addr6)
for _, netif := range host.State.NetIf.Interfaces {
fmt.Printf("|netif %d: %s\n", netif.Index, netif.Name)
for _, addr := range netif.Addrs {
fmt.Printf("|\t%s\t%s\t\n", addr.Type, addr.Addr)
}
}
}
var rpcPath string
var rpcNewHost string
var rpcNewNode string
@ -568,6 +580,7 @@ var rpcQueryHost string
var rpcQueryHostname string
var rpcGetRoutes bool
var rpcGetKeys bool
var rpcGetHosts bool
var rpcXml bool
func rpcMain() {
@ -780,15 +793,39 @@ func rpcMain() {
}
// print state
host := rpcRes.Hosts[0]
printRpcHost(rpcRes.Hosts[0])
}
fmt.Printf("host %s hostname %s\n", host.HostID, host.Hostname)
fmt.Printf("wan addr4 %s addr6 %s\n", host.State.WAN.Addr4, host.State.WAN.Addr6)
for _, netif := range host.State.NetIf.Interfaces {
fmt.Printf("netif %s idx %d\n", netif.Name, netif.Index)
for _, addr := range netif.Addrs {
fmt.Printf("\t%s\t%s\n", addr.Type, addr.Addr)
if rpcGetHosts {
rpcRes, rpcErr, err := cl.Execute(rpc.LuxRpcRequest{
RequestID: counter,
Controller: "node",
Command: "get-hosts",
})
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)
}
if rpcXml {
xmlBytes, err := xml.Marshal(&rpcRes)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to marshal rpc output: %v\n", err)
os.Exit(1)
}
fmt.Println(string(xmlBytes))
return
}
for _, host := range rpcRes.Hosts {
printRpcHost(host)
}
}
}
@ -810,6 +847,7 @@ func main() {
flag.StringVar(&rpcQueryHostname, "rpc-query-hostname", "", "RPC node querty host state by hostname")
flag.BoolVar(&rpcGetRoutes, "rpc-get-routes", false, "RPC node list established routes")
flag.BoolVar(&rpcGetKeys, "rpc-get-keys", false, "RPC node list keys")
flag.BoolVar(&rpcGetHosts, "rpc-get-hosts", false, "RPC node list hosts")
flag.BoolVar(&rpcXml, "rpc-xml", false, "output RPC results in XML")
flag.Parse()

View file

@ -415,6 +415,21 @@ func (node *LuxNode) Handle(request rpc.LuxRpcRequest, rpcType rpc.LuxRpcType) (
})
}
return rpc.LuxRpcResponse{Hosts: foundHosts}, rpc.LuxRpcError{}, true
} else if request.Command == "get-hosts" {
node.stateLock.RLock()
defer node.stateLock.RUnlock()
foundHosts := make([]rpc.LuxRpcHost, 0)
for _, item := range node.state.hosts {
foundHosts = append(foundHosts, rpc.LuxRpcHost{
HostID: item.HostId.String(),
Hostname: item.State.Hostname,
State: item.State.IntoRpc(),
})
}
return rpc.LuxRpcResponse{Hosts: foundHosts}, rpc.LuxRpcError{}, true
}