Merge pull request #2 from tim-beatham/1-log-key-events-in-the-mesh

1 log key events in the mesh
This commit is contained in:
Tim Beatham 2023-10-31 10:37:59 +00:00 committed by GitHub
commit a1caf2e8ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 63 additions and 12 deletions

View File

@ -144,6 +144,19 @@ func getGraph(client *ipcRpc.Client, meshId string) {
fmt.Println(reply)
}
func queryMesh(client *ipcRpc.Client, meshId, query string) {
var reply string
err := client.Call("IpcHandler.Query", &ipc.QueryMesh{MeshId: meshId, Query: query}, &reply)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(reply)
}
func main() {
parser := argparse.NewParser("wg-mesh",
"wg-mesh Manipulate WireGuard meshes")
@ -155,6 +168,7 @@ func main() {
enableInterfaceCmd := parser.NewCommand("enable-interface", "Enable A Specific Mesh Interface")
getGraphCmd := parser.NewCommand("get-graph", "Convert a mesh into DOT format")
leaveMeshCmd := parser.NewCommand("leave-mesh", "Leave a mesh network")
queryMeshCmd := parser.NewCommand("query-mesh", "Query a mesh network using JMESPath")
var newMeshIfName *string = newMeshCmd.String("f", "ifname", &argparse.Options{Required: true})
var newMeshPort *int = newMeshCmd.Int("p", "wgport", &argparse.Options{Required: true})
@ -172,6 +186,9 @@ func main() {
var leaveMeshMeshId *string = leaveMeshCmd.String("m", "mesh", &argparse.Options{Required: true})
var queryMeshMeshId *string = queryMeshCmd.String("m", "mesh", &argparse.Options{Required: true})
var queryMeshQuery *string = queryMeshCmd.String("q", "query", &argparse.Options{Required: true})
err := parser.Parse(os.Args)
if err != nil {
@ -224,4 +241,8 @@ func main() {
if leaveMeshCmd.Happened() {
leaveMesh(client, *leaveMeshMeshId)
}
if queryMeshCmd.Happened() {
queryMesh(client, *queryMeshMeshId, *queryMeshQuery)
}
}

1
go.mod
View File

@ -16,6 +16,7 @@ require (
require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/josharian/native v1.1.0 // indirect
github.com/mdlayher/genetlink v1.3.2 // indirect
github.com/mdlayher/netlink v1.7.2 // indirect

View File

@ -139,10 +139,6 @@ func (m *CrdtMeshManager) HasChanges() bool {
return len(changes) > 0
}
func (m *CrdtMeshManager) HasFailed(endpoint string) bool {
return false
}
func (m *CrdtMeshManager) SaveChanges() {
hashes := m.doc.Heads()
hash := hashes[len(hashes)-1]

View File

@ -7,6 +7,7 @@ import (
"github.com/tim-beatham/wgmesh/pkg/ip"
"github.com/tim-beatham/wgmesh/pkg/lib"
"github.com/tim-beatham/wgmesh/pkg/mesh"
"github.com/tim-beatham/wgmesh/pkg/query"
"github.com/tim-beatham/wgmesh/pkg/rpc"
"github.com/tim-beatham/wgmesh/pkg/wg"
"golang.zx2c4.com/wireguard/wgctrl"
@ -19,6 +20,7 @@ type NewCtrlServerParams struct {
AuthProvider rpc.AuthenticationServer
CtrlProvider rpc.MeshCtrlServerServer
SyncProvider rpc.SyncServiceServer
Querier query.Querier
}
// Create a new instance of the MeshCtrlServer or error if the
@ -73,7 +75,9 @@ func NewCtrlServer(params *NewCtrlServerParams) (*MeshCtrlServer, error) {
return nil, err
}
ctrlServer.Querier = query.NewJmesQuerier(ctrlServer.MeshManager)
ctrlServer.ConnectionServer = connServer
return ctrlServer, nil
}

View File

@ -4,13 +4,12 @@ import (
"github.com/tim-beatham/wgmesh/pkg/conf"
"github.com/tim-beatham/wgmesh/pkg/conn"
"github.com/tim-beatham/wgmesh/pkg/mesh"
"github.com/tim-beatham/wgmesh/pkg/query"
"golang.zx2c4.com/wireguard/wgctrl"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)
/*
* Represents a WireGuard node
*/
// Represents a WireGuard MeshNode
type MeshNode struct {
HostEndpoint string
WgEndpoint string
@ -20,19 +19,18 @@ type MeshNode struct {
Routes []string
}
// Represents a WireGuard Mesh
type Mesh struct {
SharedKey *wgtypes.Key
Nodes map[string]MeshNode
}
/*
* Defines the mesh control server this node
* is running
*/
// Represents a ctrlserver to be used in WireGuard
type MeshCtrlServer struct {
Client *wgctrl.Client
MeshManager *mesh.MeshManager
ConnectionManager conn.ConnectionManager
ConnectionServer *conn.ConnectionServer
Conf *conf.WgMeshConfiguration
Querier query.Querier
}

View File

@ -42,6 +42,11 @@ type ListMeshReply struct {
Meshes []string
}
type QueryMesh struct {
MeshId string
Query string
}
type MeshIpc interface {
CreateMesh(args *NewMeshArgs, reply *string) error
ListMeshes(name string, reply *ListMeshReply) error
@ -50,6 +55,7 @@ type MeshIpc interface {
GetMesh(meshId string, reply *GetMeshReply) error
EnableInterface(meshId string, reply *string) error
GetDOT(meshId string, reply *string) error
Query(query QueryMesh, reply *string) error
}
const SockAddr = "/tmp/wgmesh_ipc.sock"

View File

@ -43,3 +43,15 @@ func MapKeys[K comparable, V any](m map[K]V) []K {
return values
}
type convert[V1 any, V2 any] func(V1) V2
func Map[V1 any, V2 any](list []V1, f convert[V1, V2]) []V2 {
newList := make([]V2, len(list))
for i, elem := range list {
newList[i] = f(elem)
}
return newList
}

View File

@ -176,6 +176,17 @@ func (n *IpcHandler) GetDOT(meshId string, reply *string) error {
return nil
}
func (n *IpcHandler) Query(params ipc.QueryMesh, reply *string) error {
queryResponse, err := n.Server.Querier.Query(params.MeshId, params.Query)
if err != nil {
return err
}
*reply = string(queryResponse)
return nil
}
type RobinIpcParams struct {
CtrlServer *ctrlserver.MeshCtrlServer
}

View File

@ -87,10 +87,12 @@ func (s *SyncServiceImpl) SyncMesh(stream rpc.SyncService_SyncMeshServer) error
if syncer != nil {
syncer.Complete()
}
return nil
}
}
}
func NewSyncService(server *ctrlserver.MeshCtrlServer) *SyncServiceImpl {
return &SyncServiceImpl{Server: server}
}