mirror of
https://github.com/tim-beatham/smegmesh.git
synced 2025-08-17 16:41:02 +02:00
main - adding WireGuard stats to JSON objects
- Adding WireGuard stats through to IPC calls so that they can be used by the API
This commit is contained in:
@ -65,6 +65,12 @@ func (s *SmegServer) meshNodeToAPIMeshNode(meshNode ctrlserver.MeshNode) *SmegNo
|
|||||||
PublicKey: meshNode.PublicKey,
|
PublicKey: meshNode.PublicKey,
|
||||||
Alias: alias,
|
Alias: alias,
|
||||||
Services: meshNode.Services,
|
Services: meshNode.Services,
|
||||||
|
Stats: SmegStats{
|
||||||
|
TotalTransmit: meshNode.Stats.TransmitBytes,
|
||||||
|
TotalReceived: meshNode.Stats.ReceivedBytes,
|
||||||
|
KeepAliveInterval: meshNode.Stats.PersistentKeepAliveInterval,
|
||||||
|
AllowedIps: meshNode.Stats.AllowedIPs,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,10 +1,19 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
type Route struct {
|
type Route struct {
|
||||||
Prefix string `json:"prefix"`
|
Prefix string `json:"prefix"`
|
||||||
Path []string `json:"path"`
|
Path []string `json:"path"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SmegStats struct {
|
||||||
|
TotalTransmit int64 `json:"totalTransmit"`
|
||||||
|
TotalReceived int64 `json:"totalReceived"`
|
||||||
|
KeepAliveInterval time.Duration `json:"keepaliveInterval"`
|
||||||
|
AllowedIps []string `json:"allowedIps"`
|
||||||
|
}
|
||||||
|
|
||||||
type SmegNode struct {
|
type SmegNode struct {
|
||||||
Alias string `json:"alias"`
|
Alias string `json:"alias"`
|
||||||
WgHost string `json:"wgHost"`
|
WgHost string `json:"wgHost"`
|
||||||
@ -15,6 +24,7 @@ type SmegNode struct {
|
|||||||
PublicKey string `json:"publicKey"`
|
PublicKey string `json:"publicKey"`
|
||||||
Routes []Route `json:"routes"`
|
Routes []Route `json:"routes"`
|
||||||
Services map[string]string `json:"services"`
|
Services map[string]string `json:"services"`
|
||||||
|
Stats SmegStats `json:"stats"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type SmegMesh struct {
|
type SmegMesh struct {
|
||||||
|
@ -89,6 +89,7 @@ func NewCtrlServer(params *NewCtrlServerParams) (*MeshCtrlServer, error) {
|
|||||||
return ctrlServer, nil
|
return ctrlServer, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (s *MeshCtrlServer) GetConfiguration() *conf.DaemonConfiguration {
|
func (s *MeshCtrlServer) GetConfiguration() *conf.DaemonConfiguration {
|
||||||
return s.Conf
|
return s.Conf
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,12 @@
|
|||||||
package ctrlserver
|
package ctrlserver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/tim-beatham/wgmesh/pkg/conf"
|
"github.com/tim-beatham/wgmesh/pkg/conf"
|
||||||
"github.com/tim-beatham/wgmesh/pkg/conn"
|
"github.com/tim-beatham/wgmesh/pkg/conn"
|
||||||
|
"github.com/tim-beatham/wgmesh/pkg/lib"
|
||||||
"github.com/tim-beatham/wgmesh/pkg/mesh"
|
"github.com/tim-beatham/wgmesh/pkg/mesh"
|
||||||
"github.com/tim-beatham/wgmesh/pkg/query"
|
"github.com/tim-beatham/wgmesh/pkg/query"
|
||||||
"golang.zx2c4.com/wireguard/wgctrl"
|
"golang.zx2c4.com/wireguard/wgctrl"
|
||||||
@ -14,6 +18,14 @@ type MeshRoute struct {
|
|||||||
Path []string
|
Path []string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Represents the WireGuard configuration attached to the node
|
||||||
|
type WireGuardStats struct {
|
||||||
|
AllowedIPs []string
|
||||||
|
TransmitBytes int64
|
||||||
|
ReceivedBytes int64
|
||||||
|
PersistentKeepAliveInterval time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
// Represents a WireGuard MeshNode
|
// Represents a WireGuard MeshNode
|
||||||
type MeshNode struct {
|
type MeshNode struct {
|
||||||
HostEndpoint string
|
HostEndpoint string
|
||||||
@ -25,6 +37,7 @@ type MeshNode struct {
|
|||||||
Description string
|
Description string
|
||||||
Alias string
|
Alias string
|
||||||
Services map[string]string
|
Services map[string]string
|
||||||
|
Stats WireGuardStats
|
||||||
}
|
}
|
||||||
|
|
||||||
// Represents a WireGuard Mesh
|
// Represents a WireGuard Mesh
|
||||||
@ -51,3 +64,53 @@ type MeshCtrlServer struct {
|
|||||||
Conf *conf.DaemonConfiguration
|
Conf *conf.DaemonConfiguration
|
||||||
Querier query.Querier
|
Querier query.Querier
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewCtrlNode create an instance of a ctrl node to send over an
|
||||||
|
// IPC call
|
||||||
|
func NewCtrlNode(provider mesh.MeshProvider, node mesh.MeshNode) *MeshNode {
|
||||||
|
pubKey, _ := node.GetPublicKey()
|
||||||
|
|
||||||
|
ctrlNode := MeshNode{
|
||||||
|
HostEndpoint: node.GetHostEndpoint(),
|
||||||
|
WgEndpoint: node.GetWgEndpoint(),
|
||||||
|
PublicKey: pubKey.String(),
|
||||||
|
WgHost: node.GetWgHost().String(),
|
||||||
|
Timestamp: node.GetTimeStamp(),
|
||||||
|
Routes: lib.Map(node.GetRoutes(), func(r mesh.Route) MeshRoute {
|
||||||
|
return MeshRoute{
|
||||||
|
Destination: r.GetDestination().String(),
|
||||||
|
Path: r.GetPath(),
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
Description: node.GetDescription(),
|
||||||
|
Alias: node.GetAlias(),
|
||||||
|
Services: node.GetServices(),
|
||||||
|
}
|
||||||
|
|
||||||
|
device, err := provider.GetDevice()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return &ctrlNode
|
||||||
|
}
|
||||||
|
|
||||||
|
peers := lib.Filter(device.Peers, func(p wgtypes.Peer) bool {
|
||||||
|
return p.PublicKey.String() == pubKey.String()
|
||||||
|
})
|
||||||
|
|
||||||
|
if len(peers) > 0 {
|
||||||
|
peer := peers[0]
|
||||||
|
|
||||||
|
stats := WireGuardStats{
|
||||||
|
AllowedIPs: lib.Map(peer.AllowedIPs, func(i net.IPNet) string {
|
||||||
|
return i.String()
|
||||||
|
}),
|
||||||
|
TransmitBytes: peer.TransmitBytes,
|
||||||
|
ReceivedBytes: peer.ReceiveBytes,
|
||||||
|
PersistentKeepAliveInterval: peer.PersistentKeepaliveInterval,
|
||||||
|
}
|
||||||
|
|
||||||
|
ctrlNode.Stats = stats
|
||||||
|
}
|
||||||
|
|
||||||
|
return &ctrlNode
|
||||||
|
}
|
||||||
|
@ -11,7 +11,6 @@ import (
|
|||||||
"github.com/tim-beatham/wgmesh/pkg/conf"
|
"github.com/tim-beatham/wgmesh/pkg/conf"
|
||||||
"github.com/tim-beatham/wgmesh/pkg/ctrlserver"
|
"github.com/tim-beatham/wgmesh/pkg/ctrlserver"
|
||||||
"github.com/tim-beatham/wgmesh/pkg/ipc"
|
"github.com/tim-beatham/wgmesh/pkg/ipc"
|
||||||
"github.com/tim-beatham/wgmesh/pkg/lib"
|
|
||||||
"github.com/tim-beatham/wgmesh/pkg/mesh"
|
"github.com/tim-beatham/wgmesh/pkg/mesh"
|
||||||
"github.com/tim-beatham/wgmesh/pkg/query"
|
"github.com/tim-beatham/wgmesh/pkg/query"
|
||||||
"github.com/tim-beatham/wgmesh/pkg/rpc"
|
"github.com/tim-beatham/wgmesh/pkg/rpc"
|
||||||
@ -171,30 +170,9 @@ func (n *IpcHandler) GetMesh(meshId string, reply *ipc.GetMeshReply) error {
|
|||||||
|
|
||||||
i := 0
|
i := 0
|
||||||
for _, node := range meshSnapshot.GetNodes() {
|
for _, node := range meshSnapshot.GetNodes() {
|
||||||
pubKey, _ := node.GetPublicKey()
|
node := ctrlserver.NewCtrlNode(theMesh, node)
|
||||||
|
|
||||||
if err != nil {
|
nodes[i] = *node
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
node := ctrlserver.MeshNode{
|
|
||||||
HostEndpoint: node.GetHostEndpoint(),
|
|
||||||
WgEndpoint: node.GetWgEndpoint(),
|
|
||||||
PublicKey: pubKey.String(),
|
|
||||||
WgHost: node.GetWgHost().String(),
|
|
||||||
Timestamp: node.GetTimeStamp(),
|
|
||||||
Routes: lib.Map(node.GetRoutes(), func(r mesh.Route) ctrlserver.MeshRoute {
|
|
||||||
return ctrlserver.MeshRoute{
|
|
||||||
Destination: r.GetDestination().String(),
|
|
||||||
Path: r.GetPath(),
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
Description: node.GetDescription(),
|
|
||||||
Alias: node.GetAlias(),
|
|
||||||
Services: node.GetServices(),
|
|
||||||
}
|
|
||||||
|
|
||||||
nodes[i] = node
|
|
||||||
i += 1
|
i += 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user