2023-09-18 16:52:28 +02:00
|
|
|
package ctrlserver
|
|
|
|
|
2023-09-19 14:45:49 +02:00
|
|
|
import (
|
2023-12-11 10:55:25 +01:00
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
2023-10-10 21:14:40 +02:00
|
|
|
"github.com/tim-beatham/wgmesh/pkg/conf"
|
2023-10-01 20:01:35 +02:00
|
|
|
"github.com/tim-beatham/wgmesh/pkg/conn"
|
2023-12-11 10:55:25 +01:00
|
|
|
"github.com/tim-beatham/wgmesh/pkg/lib"
|
2023-10-22 14:34:49 +02:00
|
|
|
"github.com/tim-beatham/wgmesh/pkg/mesh"
|
2023-10-30 20:02:28 +01:00
|
|
|
"github.com/tim-beatham/wgmesh/pkg/query"
|
2023-09-19 14:45:49 +02:00
|
|
|
"golang.zx2c4.com/wireguard/wgctrl"
|
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
|
|
|
)
|
2023-09-18 16:52:28 +02:00
|
|
|
|
2023-11-27 19:55:41 +01:00
|
|
|
type MeshRoute struct {
|
|
|
|
Destination string
|
|
|
|
Path []string
|
|
|
|
}
|
|
|
|
|
2023-12-11 10:55:25 +01:00
|
|
|
// Represents the WireGuard configuration attached to the node
|
|
|
|
type WireGuardStats struct {
|
|
|
|
AllowedIPs []string
|
|
|
|
TransmitBytes int64
|
|
|
|
ReceivedBytes int64
|
|
|
|
PersistentKeepAliveInterval time.Duration
|
|
|
|
}
|
|
|
|
|
2023-10-30 20:02:28 +01:00
|
|
|
// Represents a WireGuard MeshNode
|
2023-09-18 16:52:28 +02:00
|
|
|
type MeshNode struct {
|
2023-09-20 00:50:44 +02:00
|
|
|
HostEndpoint string
|
|
|
|
WgEndpoint string
|
|
|
|
PublicKey string
|
|
|
|
WgHost string
|
2023-10-24 17:00:46 +02:00
|
|
|
Timestamp int64
|
2023-11-27 19:55:41 +01:00
|
|
|
Routes []MeshRoute
|
2023-11-13 11:44:14 +01:00
|
|
|
Description string
|
2023-11-17 23:13:51 +01:00
|
|
|
Alias string
|
|
|
|
Services map[string]string
|
2023-12-11 10:55:25 +01:00
|
|
|
Stats WireGuardStats
|
2023-09-19 14:45:49 +02:00
|
|
|
}
|
|
|
|
|
2023-10-30 20:02:28 +01:00
|
|
|
// Represents a WireGuard Mesh
|
2023-09-19 14:45:49 +02:00
|
|
|
type Mesh struct {
|
|
|
|
SharedKey *wgtypes.Key
|
|
|
|
Nodes map[string]MeshNode
|
2023-09-18 16:52:28 +02:00
|
|
|
}
|
|
|
|
|
2023-11-05 19:03:58 +01:00
|
|
|
type CtrlServer interface {
|
2023-12-10 20:21:54 +01:00
|
|
|
GetConfiguration() *conf.DaemonConfiguration
|
2023-11-05 19:03:58 +01:00
|
|
|
GetClient() *wgctrl.Client
|
|
|
|
GetQuerier() query.Querier
|
|
|
|
GetMeshManager() mesh.MeshManager
|
|
|
|
Close() error
|
|
|
|
GetConnectionManager() conn.ConnectionManager
|
|
|
|
}
|
|
|
|
|
2023-10-30 20:02:28 +01:00
|
|
|
// Represents a ctrlserver to be used in WireGuard
|
2023-09-18 16:52:28 +02:00
|
|
|
type MeshCtrlServer struct {
|
2023-10-02 17:03:41 +02:00
|
|
|
Client *wgctrl.Client
|
2023-11-05 19:03:58 +01:00
|
|
|
MeshManager mesh.MeshManager
|
2023-10-05 18:48:54 +02:00
|
|
|
ConnectionManager conn.ConnectionManager
|
2023-10-02 17:03:41 +02:00
|
|
|
ConnectionServer *conn.ConnectionServer
|
2023-12-10 20:21:54 +01:00
|
|
|
Conf *conf.DaemonConfiguration
|
2023-10-30 20:02:28 +01:00
|
|
|
Querier query.Querier
|
2023-09-18 16:52:28 +02:00
|
|
|
}
|
2023-12-11 10:55:25 +01:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|