Added IPC handler to wgmesh

This commit is contained in:
Tim Beatham 2023-09-18 17:00:43 +01:00
parent 01238aca59
commit b679b7fb6e
5 changed files with 75 additions and 8 deletions

View File

@ -2,12 +2,26 @@ package main
import (
"fmt"
"net/rpc"
"os"
"github.com/akamensky/argparse"
meshtypes "github.com/tim-beatham/wgmesh/pkg/wg-mesh"
)
const SockAddr = "/tmp/wgmesh_ipc.sock"
func createNewMesh(client *rpc.Client) {
var reply string
err := client.Call("Mesh.CreateNewMesh", "", &reply)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(reply)
}
func main() {
parser := argparse.NewParser("wg-mesh",
"wg-mesh Manipulate WireGuard meshes")
@ -20,13 +34,13 @@ func main() {
return
}
if newMeshCmd.Happened() {
mesh, err := meshtypes.NewWgMesh()
client, err := rpc.DialHTTP("unix", SockAddr)
if err != nil {
fmt.Println("Could not generate new WgMesh")
} else {
fmt.Println(mesh.SharedKey.String())
}
fmt.Println(err.Error())
return
}
if newMeshCmd.Happened() {
createNewMesh(client)
}
}

View File

@ -3,10 +3,12 @@ package main
import (
ctrlserver "github.com/tim-beatham/wgmesh/pkg/ctrlserver"
"github.com/tim-beatham/wgmesh/pkg/ctrlserver/api"
"github.com/tim-beatham/wgmesh/pkg/ctrlserver/ipc"
)
func main() {
ctrlServer := ctrlserver.NewCtrlServer("0.0.0.0", 21910)
r := api.RunAPI(ctrlServer)
ipc.RunIpcHandler()
r.Run(ctrlServer.GetEndpoint())
}

1
go.mod
View File

@ -39,4 +39,5 @@ require (
golang.zx2c4.com/wireguard v0.0.0-20230325221338-052af4a8072b // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gopkg.in/zeromq/goczmq.v4 v4.1.0 // indirect
)

2
go.sum
View File

@ -106,5 +106,7 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/zeromq/goczmq.v4 v4.1.0 h1:CE+FE81mGVs2aSlnbfLuS1oAwdcVywyMM2AC1g33imI=
gopkg.in/zeromq/goczmq.v4 v4.1.0/go.mod h1:h4IlfePEYMpFdywGr5gAwKhBBj+hiBl/nF4VoSE4k+0=
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=

View File

@ -0,0 +1,48 @@
package ipc
import (
"errors"
"net"
"net/http"
"net/rpc"
"os"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)
const SockAddr = "/tmp/wgmesh_ipc.sock"
type Mesh struct {
}
/*
* Create a new WireGuard mesh network
*/
func (n Mesh) CreateNewMesh(name *string, reply *string) error {
key, err := wgtypes.GenerateKey()
if err != nil {
return err
}
*reply = key.String()
return nil
}
func RunIpcHandler() error {
if err := os.RemoveAll(SockAddr); err != nil {
return errors.New("Could not find to address")
}
newMeshIpc := new(Mesh)
rpc.Register(newMeshIpc)
rpc.HandleHTTP()
l, e := net.Listen("unix", SockAddr)
if e != nil {
return e
}
http.Serve(l, nil)
return nil
}