Added ability to set interface as up

This commit is contained in:
Tim Beatham 2023-09-21 18:43:29 +01:00
parent 6725a193aa
commit ea2a073875
6 changed files with 76 additions and 4 deletions

View File

@ -29,7 +29,7 @@ func listMeshes(client *ipcRpc.Client) {
err := client.Call("Mesh.ListMeshes", "", &reply)
if err != nil {
err.Error()
fmt.Println(err.Error())
return
}
@ -71,6 +71,19 @@ func getMesh(client *ipcRpc.Client, meshId string) {
}
}
func enableInterface(client *ipcRpc.Client, meshId string) {
var reply string
err := client.Call("Mesh.EnableInterface", &meshId, &reply)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(reply)
}
func main() {
parser := argparse.NewParser("wg-mesh",
"wg-mesh Manipulate WireGuard meshes")
@ -79,11 +92,13 @@ func main() {
listMeshCmd := parser.NewCommand("list-meshes", "List meshes the node is connected to")
joinMeshCmd := parser.NewCommand("join-mesh", "Join a mesh network")
getMeshCmd := parser.NewCommand("get-mesh", "Get a mesh network")
enableInterfaceCmd := parser.NewCommand("enable-interface", "Enable A Specific Mesh Interface")
var meshId *string = joinMeshCmd.String("m", "mesh", &argparse.Options{Required: true})
var ipAddress *string = joinMeshCmd.String("i", "ip", &argparse.Options{Required: true})
var getMeshId *string = getMeshCmd.String("m", "mesh", &argparse.Options{Required: true})
var enableInterfaceMeshId *string = enableInterfaceCmd.String("m", "mesh", &argparse.Options{Required: true})
err := parser.Parse(os.Args)
@ -113,4 +128,8 @@ func main() {
if getMeshCmd.Happened() {
getMesh(client, *getMeshId)
}
if enableInterfaceCmd.Happened() {
enableInterface(client, *enableInterfaceMeshId)
}
}

View File

@ -7,6 +7,7 @@ import (
"strconv"
"github.com/tim-beatham/wgmesh/pkg/lib"
"github.com/tim-beatham/wgmesh/pkg/wg"
"golang.zx2c4.com/wireguard/wgctrl"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)
@ -75,7 +76,7 @@ func (server *MeshCtrlServer) AddHost(args AddHostArgs) error {
nodes, contains := server.Meshes[args.MeshId]
if !contains {
return errors.New("Node does not exist in the mesh")
return errors.New("The mesh: " + args.MeshId + " does not exist")
}
_, contains = nodes.Nodes[args.HostEndpoint]
@ -165,3 +166,21 @@ func AddWgPeer(ifName string, client *wgctrl.Client, node MeshNode) error {
return nil
}
func (s *MeshCtrlServer) EnableInterface(meshId string) error {
mesh, contains := s.Meshes[meshId]
if !contains {
return errors.New("Mesh does not exist")
}
endPoint := lib.GetOutboundIP().String() + ":8080"
node, contains := mesh.Nodes[endPoint]
if !contains {
return errors.New("Node does not exist in the mesh")
}
return wg.EnableInterface(s.IfName, node.WgHost)
}

View File

@ -25,8 +25,6 @@ type Mesh struct {
* is running
*/
type MeshCtrlServer struct {
Host string
Port int
Client *wgctrl.Client
Meshes map[string]Mesh
IfName string

View File

@ -212,6 +212,17 @@ func (n Mesh) GetMesh(meshId string, reply *ipc.GetMeshReply) error {
return nil
}
func (n Mesh) EnableInterface(meshId string, reply *string) error {
err := n.Server.EnableInterface(meshId)
if err != nil {
return err
}
*reply = "up"
return nil
}
func RunIpcHandler(server *ctrlserver.MeshCtrlServer) error {
if err := os.RemoveAll(SockAddr); err != nil {
return errors.New("Could not find to address")

View File

@ -29,6 +29,7 @@ message JoinMeshRequest {
int32 hostPort = 2;
string publicKey = 3;
int32 wgPort = 4;
string wgIp = 5;
}
message JoinMeshReply {

View File

@ -59,3 +59,27 @@ func CreateClient(ifName string) (*wgctrl.Client, error) {
client.ConfigureDevice(ifName, cfg)
return client, nil
}
func EnableInterface(ifName string, ip string) error {
cmd := exec.Command("/usr/bin/ip", "link", "set", "up", "dev", ifName)
if err := cmd.Run(); err != nil {
fmt.Println(err.Error())
return err
}
hostIp, _, err := net.ParseCIDR(ip)
if err != nil {
return err
}
cmd = exec.Command("/usr/bin/ip", "addr", "add", hostIp.String()+"/24", "dev", "wgmesh")
if err := cmd.Run(); err != nil {
fmt.Println(err.Error())
return err
}
return nil
}