mirror of
https://github.com/tim-beatham/smegmesh.git
synced 2025-08-09 13:24:52 +02:00
IPV6 SLAAC
This commit is contained in:
@ -2,9 +2,7 @@ package ctrlserver
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
|
||||
"github.com/tim-beatham/wgmesh/pkg/lib"
|
||||
"github.com/tim-beatham/wgmesh/pkg/wg"
|
||||
@ -33,20 +31,6 @@ func (server *MeshCtrlServer) IsInMesh(meshId string) bool {
|
||||
return inMesh
|
||||
}
|
||||
|
||||
func (server *MeshCtrlServer) addSelfToMesh(meshId string) error {
|
||||
ipAddr := lib.GetOutboundIP()
|
||||
|
||||
node := MeshNode{
|
||||
HostEndpoint: ipAddr.String() + ":8080",
|
||||
PublicKey: server.GetDevice().PublicKey.String(),
|
||||
WgEndpoint: ipAddr.String() + ":51820",
|
||||
WgHost: "10.0.0.1/32",
|
||||
}
|
||||
|
||||
server.Meshes[meshId].Nodes[node.HostEndpoint] = node
|
||||
return nil
|
||||
}
|
||||
|
||||
func (server *MeshCtrlServer) CreateMesh() (*Mesh, error) {
|
||||
key, err := wgtypes.GenerateKey()
|
||||
|
||||
@ -60,7 +44,6 @@ func (server *MeshCtrlServer) CreateMesh() (*Mesh, error) {
|
||||
}
|
||||
|
||||
server.Meshes[key.String()] = mesh
|
||||
server.addSelfToMesh(mesh.SharedKey.String())
|
||||
return &mesh, nil
|
||||
}
|
||||
|
||||
@ -96,8 +79,6 @@ func (server *MeshCtrlServer) AddHost(args AddHostArgs) error {
|
||||
|
||||
if err == nil {
|
||||
nodes.Nodes[args.HostEndpoint] = node
|
||||
} else {
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
|
||||
return err
|
||||
@ -117,8 +98,6 @@ func AddWgPeer(ifName string, client *wgctrl.Client, node MeshNode) error {
|
||||
peer := make([]wgtypes.PeerConfig, 1)
|
||||
|
||||
peerPublic, err := wgtypes.ParseKey(node.PublicKey)
|
||||
fmt.Println("node.PublicKey: " + node.PublicKey)
|
||||
fmt.Println("peerPublic: " + peerPublic.String())
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
@ -127,7 +106,6 @@ func AddWgPeer(ifName string, client *wgctrl.Client, node MeshNode) error {
|
||||
peerEndpoint, err := net.ResolveUDPAddr("udp", node.WgEndpoint)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("err")
|
||||
return err
|
||||
}
|
||||
|
||||
@ -152,14 +130,6 @@ func AddWgPeer(ifName string, client *wgctrl.Client, node MeshNode) error {
|
||||
|
||||
err = client.ConfigureDevice(ifName, cfg)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
|
||||
dev, err := client.Device(ifName)
|
||||
|
||||
fmt.Println("Number of peers: " + strconv.Itoa(len(dev.Peers)))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -3,7 +3,6 @@ package ipc
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
ipcRpc "net/rpc"
|
||||
@ -16,6 +15,9 @@ import (
|
||||
"github.com/tim-beatham/wgmesh/pkg/ctrlserver/rpc"
|
||||
"github.com/tim-beatham/wgmesh/pkg/ipc"
|
||||
ipctypes "github.com/tim-beatham/wgmesh/pkg/ipc"
|
||||
"github.com/tim-beatham/wgmesh/pkg/lib"
|
||||
logging "github.com/tim-beatham/wgmesh/pkg/log"
|
||||
"github.com/tim-beatham/wgmesh/pkg/slaac"
|
||||
"github.com/tim-beatham/wgmesh/pkg/wg"
|
||||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
||||
"google.golang.org/grpc"
|
||||
@ -28,13 +30,28 @@ type Mesh struct {
|
||||
Server *ctrlserver.MeshCtrlServer
|
||||
}
|
||||
|
||||
const MeshIfName = "wgmesh"
|
||||
|
||||
/*
|
||||
* Create a new WireGuard mesh network
|
||||
*/
|
||||
func (n Mesh) CreateNewMesh(name *string, reply *string) error {
|
||||
wg.CreateInterface("wgmesh")
|
||||
wg.CreateInterface(MeshIfName)
|
||||
|
||||
mesh, err := n.Server.CreateMesh()
|
||||
ula, _ := slaac.NewULA(n.Server.GetDevice().PublicKey, "0")
|
||||
|
||||
outBoundIp := lib.GetOutboundIP().String()
|
||||
|
||||
addHostArgs := ctrlserver.AddHostArgs{
|
||||
HostEndpoint: outBoundIp + ":8080",
|
||||
PublicKey: n.Server.GetDevice().PublicKey.String(),
|
||||
WgEndpoint: outBoundIp + ":51820",
|
||||
WgIp: ula.CGA.GetIpv6().String() + "/128",
|
||||
MeshId: mesh.SharedKey.String(),
|
||||
}
|
||||
|
||||
n.Server.AddHost(addHostArgs)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
@ -113,6 +130,7 @@ func updatePeer(n *Mesh, node ctrlserver.MeshNode, wgHost string, meshId string)
|
||||
defer cancel()
|
||||
|
||||
dev := n.Server.GetDevice()
|
||||
|
||||
joinMeshReq := rpc.JoinMeshRequest{
|
||||
MeshId: meshId,
|
||||
HostPort: 8080,
|
||||
@ -143,7 +161,7 @@ func updatePeers(n *Mesh, meshId string, wgHost string, nodesToExclude []string)
|
||||
err := updatePeer(n, node, wgHost, meshId)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -166,14 +184,16 @@ func (n Mesh) JoinMesh(args *ipctypes.JoinMeshArgs, reply *string) error {
|
||||
defer cancel()
|
||||
|
||||
dev := n.Server.GetDevice()
|
||||
ula, _ := slaac.NewULA(dev.PublicKey, "0")
|
||||
|
||||
fmt.Print("Pub Key:" + dev.PublicKey.String())
|
||||
logging.InfoLog.Println("WgIP: " + ula.CGA.GetIpv6().String())
|
||||
|
||||
joinMeshReq := rpc.JoinMeshRequest{
|
||||
MeshId: args.MeshId,
|
||||
HostPort: 8080,
|
||||
PublicKey: dev.PublicKey.String(),
|
||||
WgPort: int32(dev.ListenPort),
|
||||
WgIp: ula.CGA.GetIpv6().String() + "/128",
|
||||
}
|
||||
|
||||
r, err := c.JoinMesh(ctx, &joinMeshReq)
|
||||
@ -200,7 +220,6 @@ func (n Mesh) GetMesh(meshId string, reply *ipc.GetMeshReply) error {
|
||||
|
||||
i := 0
|
||||
for _, n := range mesh.Nodes {
|
||||
fmt.Println(n.PublicKey)
|
||||
nodes[i] = n
|
||||
i += 1
|
||||
}
|
||||
|
@ -3,8 +3,6 @@ package rpc
|
||||
import (
|
||||
context "context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net"
|
||||
"strconv"
|
||||
|
||||
@ -58,7 +56,6 @@ func (m *meshCtrlServer) GetMesh(ctx context.Context, request *GetMeshRequest) (
|
||||
|
||||
func (m *meshCtrlServer) JoinMesh(ctx context.Context, request *JoinMeshRequest) (*JoinMeshReply, error) {
|
||||
p, _ := peer.FromContext(ctx)
|
||||
fmt.Println(p.Addr.String())
|
||||
|
||||
hostIp, _, err := net.SplitHostPort(p.Addr.String())
|
||||
|
||||
@ -69,12 +66,9 @@ func (m *meshCtrlServer) JoinMesh(ctx context.Context, request *JoinMeshRequest)
|
||||
wgIp := request.WgIp
|
||||
|
||||
if wgIp == "" {
|
||||
wgIp = "10.0.0." + strconv.Itoa(rand.Intn(253)+1) + "/32"
|
||||
return nil, errors.New("Haven't provided a valid IP address")
|
||||
}
|
||||
|
||||
fmt.Println("Join server public key: " + request.PublicKey)
|
||||
fmt.Println("Request: " + request.MeshId)
|
||||
|
||||
addHostArgs := ctrlserver.AddHostArgs{
|
||||
HostEndpoint: hostIp + ":" + strconv.Itoa(int(request.HostPort)),
|
||||
PublicKey: request.PublicKey,
|
||||
|
Reference in New Issue
Block a user