Got it working with peer-to-peer network. Does not mark nodes as stale

This commit is contained in:
Tim Beatham
2023-09-21 13:50:59 +01:00
parent d9dc0ba6b3
commit 6725a193aa
6 changed files with 110 additions and 666 deletions

View File

@ -3,7 +3,6 @@ package ctrlserver
import (
"errors"
"fmt"
"math/rand"
"net"
"strconv"
@ -38,7 +37,7 @@ func (server *MeshCtrlServer) addSelfToMesh(meshId string) error {
node := MeshNode{
HostEndpoint: ipAddr.String() + ":8080",
PublicKey: server.GetDevice().PrivateKey.String(),
PublicKey: server.GetDevice().PublicKey.String(),
WgEndpoint: ipAddr.String() + ":51820",
WgHost: "10.0.0.1/32",
}
@ -69,6 +68,7 @@ type AddHostArgs struct {
PublicKey string
MeshId string
WgEndpoint string
WgIp string
}
func (server *MeshCtrlServer) AddHost(args AddHostArgs) error {
@ -84,13 +84,11 @@ func (server *MeshCtrlServer) AddHost(args AddHostArgs) error {
return errors.New("The node already has an endpoint in the mesh network")
}
fmt.Println(args.WgEndpoint)
node := MeshNode{
HostEndpoint: args.HostEndpoint,
WgEndpoint: args.WgEndpoint,
PublicKey: args.PublicKey,
WgHost: "10.0.0." + strconv.Itoa(rand.Intn(253)+1) + "/32",
WgHost: args.WgIp,
}
err := AddWgPeer(server.IfName, server.Client, node)
@ -118,6 +116,8 @@ 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

View File

@ -8,6 +8,7 @@ import (
"net/http"
ipcRpc "net/rpc"
"os"
"slices"
"strconv"
"time"
@ -90,15 +91,65 @@ func updateMesh(n *Mesh, meshId string, endPoint string) error {
WgHost: node.WgHost,
}
n.Server.Meshes[meshId].Nodes[node.Endpoint] = meshNode
n.Server.Meshes[meshId].Nodes[meshNode.HostEndpoint] = meshNode
ctrlserver.AddWgPeer(n.Server.IfName, n.Server.Client, meshNode)
}
return nil
}
func (n Mesh) JoinOtherMesh
func updatePeer(n *Mesh, node ctrlserver.MeshNode, wgHost string, meshId string) error {
conn, err := grpc.Dial(node.HostEndpoint, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return err
}
defer conn.Close()
c := rpc.NewMeshCtrlServerClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
dev := n.Server.GetDevice()
joinMeshReq := rpc.JoinMeshRequest{
MeshId: meshId,
HostPort: 8080,
PublicKey: dev.PublicKey.String(),
WgPort: int32(dev.ListenPort),
WgIp: wgHost,
}
r, err := c.JoinMesh(ctx, &joinMeshReq)
if err != nil {
return err
}
if !r.GetSuccess() {
return errors.New("Could not join the mesh")
}
return nil
}
func updatePeers(n *Mesh, meshId string, wgHost string, nodesToExclude []string) error {
for _, node := range n.Server.Meshes[meshId].Nodes {
nodeEndpoint := node.HostEndpoint
if !slices.Contains(nodesToExclude, nodeEndpoint) {
// Best effort service
err := updatePeer(n, node, wgHost, meshId)
if err != nil {
fmt.Println(err.Error())
}
}
}
return nil
}
func (n Mesh) JoinMesh(args *ipctypes.JoinMeshArgs, reply *string) error {
conn, err := grpc.Dial(args.IpAdress+":8080", grpc.WithTransportCredentials(insecure.NewCredentials()))
@ -116,6 +167,8 @@ func (n Mesh) JoinMesh(args *ipctypes.JoinMeshArgs, reply *string) error {
dev := n.Server.GetDevice()
fmt.Print("Pub Key:" + dev.PublicKey.String())
joinMeshReq := rpc.JoinMeshRequest{
MeshId: args.MeshId,
HostPort: 8080,
@ -133,6 +186,8 @@ func (n Mesh) JoinMesh(args *ipctypes.JoinMeshArgs, reply *string) error {
updateMesh(&n, args.MeshId, args.IpAdress+":8080")
}
err = updatePeers(&n, args.MeshId, r.GetMeshIp(), make([]string, 0))
*reply = strconv.FormatBool(r.GetSuccess())
return nil
}
@ -152,6 +207,7 @@ func (n Mesh) GetMesh(meshId string, reply *ipc.GetMeshReply) error {
*reply = ipc.GetMeshReply{Nodes: nodes}
} else {
return errors.New("mesh does not exist")
}
return nil
}

View File

@ -202,6 +202,7 @@ type JoinMeshRequest struct {
HostPort int32 `protobuf:"varint,2,opt,name=hostPort,proto3" json:"hostPort,omitempty"`
PublicKey string `protobuf:"bytes,3,opt,name=publicKey,proto3" json:"publicKey,omitempty"`
WgPort int32 `protobuf:"varint,4,opt,name=wgPort,proto3" json:"wgPort,omitempty"`
WgIp string `protobuf:"bytes,5,opt,name=wgIp,proto3" json:"wgIp,omitempty"`
}
func (x *JoinMeshRequest) Reset() {
@ -264,6 +265,13 @@ func (x *JoinMeshRequest) GetWgPort() int32 {
return 0
}
func (x *JoinMeshRequest) GetWgIp() string {
if x != nil {
return x.WgIp
}
return ""
}
type JoinMeshReply struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
@ -341,31 +349,32 @@ var file_pkg_grpc_ctrlserver_ctrlserver_proto_rawDesc = []byte{
0x12, 0x2e, 0x0a, 0x08, 0x6d, 0x65, 0x73, 0x68, 0x4e, 0x6f, 0x64, 0x65, 0x18, 0x02, 0x20, 0x03,
0x28, 0x0b, 0x32, 0x12, 0x2e, 0x72, 0x70, 0x63, 0x74, 0x79, 0x70, 0x65, 0x73, 0x2e, 0x4d, 0x65,
0x73, 0x68, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x08, 0x6d, 0x65, 0x73, 0x68, 0x4e, 0x6f, 0x64, 0x65,
0x22, 0x7b, 0x0a, 0x0f, 0x4a, 0x6f, 0x69, 0x6e, 0x4d, 0x65, 0x73, 0x68, 0x52, 0x65, 0x71, 0x75,
0x65, 0x73, 0x74, 0x12, 0x16, 0x0a, 0x06, 0x6d, 0x65, 0x73, 0x68, 0x49, 0x64, 0x18, 0x01, 0x20,
0x01, 0x28, 0x09, 0x52, 0x06, 0x6d, 0x65, 0x73, 0x68, 0x49, 0x64, 0x12, 0x1a, 0x0a, 0x08, 0x68,
0x6f, 0x73, 0x74, 0x50, 0x6f, 0x72, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x08, 0x68,
0x6f, 0x73, 0x74, 0x50, 0x6f, 0x72, 0x74, 0x12, 0x1c, 0x0a, 0x09, 0x70, 0x75, 0x62, 0x6c, 0x69,
0x63, 0x4b, 0x65, 0x79, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x70, 0x75, 0x62, 0x6c,
0x69, 0x63, 0x4b, 0x65, 0x79, 0x12, 0x16, 0x0a, 0x06, 0x77, 0x67, 0x50, 0x6f, 0x72, 0x74, 0x18,
0x04, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x77, 0x67, 0x50, 0x6f, 0x72, 0x74, 0x22, 0x51, 0x0a,
0x0d, 0x4a, 0x6f, 0x69, 0x6e, 0x4d, 0x65, 0x73, 0x68, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x12, 0x18,
0x0a, 0x07, 0x73, 0x75, 0x63, 0x63, 0x65, 0x73, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52,
0x07, 0x73, 0x75, 0x63, 0x63, 0x65, 0x73, 0x73, 0x12, 0x1b, 0x0a, 0x06, 0x6d, 0x65, 0x73, 0x68,
0x49, 0x70, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x48, 0x00, 0x52, 0x06, 0x6d, 0x65, 0x73, 0x68,
0x49, 0x70, 0x88, 0x01, 0x01, 0x42, 0x09, 0x0a, 0x07, 0x5f, 0x6d, 0x65, 0x73, 0x68, 0x49, 0x70,
0x32, 0x91, 0x01, 0x0a, 0x0e, 0x4d, 0x65, 0x73, 0x68, 0x43, 0x74, 0x72, 0x6c, 0x53, 0x65, 0x72,
0x76, 0x65, 0x72, 0x12, 0x3d, 0x0a, 0x07, 0x47, 0x65, 0x74, 0x4d, 0x65, 0x73, 0x68, 0x12, 0x18,
0x2e, 0x72, 0x70, 0x63, 0x74, 0x79, 0x70, 0x65, 0x73, 0x2e, 0x47, 0x65, 0x74, 0x4d, 0x65, 0x73,
0x68, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x16, 0x2e, 0x72, 0x70, 0x63, 0x74, 0x79,
0x70, 0x65, 0x73, 0x2e, 0x47, 0x65, 0x74, 0x4d, 0x65, 0x73, 0x68, 0x52, 0x65, 0x70, 0x6c, 0x79,
0x22, 0x00, 0x12, 0x40, 0x0a, 0x08, 0x4a, 0x6f, 0x69, 0x6e, 0x4d, 0x65, 0x73, 0x68, 0x12, 0x19,
0x22, 0x8f, 0x01, 0x0a, 0x0f, 0x4a, 0x6f, 0x69, 0x6e, 0x4d, 0x65, 0x73, 0x68, 0x52, 0x65, 0x71,
0x75, 0x65, 0x73, 0x74, 0x12, 0x16, 0x0a, 0x06, 0x6d, 0x65, 0x73, 0x68, 0x49, 0x64, 0x18, 0x01,
0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x6d, 0x65, 0x73, 0x68, 0x49, 0x64, 0x12, 0x1a, 0x0a, 0x08,
0x68, 0x6f, 0x73, 0x74, 0x50, 0x6f, 0x72, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x08,
0x68, 0x6f, 0x73, 0x74, 0x50, 0x6f, 0x72, 0x74, 0x12, 0x1c, 0x0a, 0x09, 0x70, 0x75, 0x62, 0x6c,
0x69, 0x63, 0x4b, 0x65, 0x79, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x70, 0x75, 0x62,
0x6c, 0x69, 0x63, 0x4b, 0x65, 0x79, 0x12, 0x16, 0x0a, 0x06, 0x77, 0x67, 0x50, 0x6f, 0x72, 0x74,
0x18, 0x04, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x77, 0x67, 0x50, 0x6f, 0x72, 0x74, 0x12, 0x12,
0x0a, 0x04, 0x77, 0x67, 0x49, 0x70, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x77, 0x67,
0x49, 0x70, 0x22, 0x51, 0x0a, 0x0d, 0x4a, 0x6f, 0x69, 0x6e, 0x4d, 0x65, 0x73, 0x68, 0x52, 0x65,
0x70, 0x6c, 0x79, 0x12, 0x18, 0x0a, 0x07, 0x73, 0x75, 0x63, 0x63, 0x65, 0x73, 0x73, 0x18, 0x01,
0x20, 0x01, 0x28, 0x08, 0x52, 0x07, 0x73, 0x75, 0x63, 0x63, 0x65, 0x73, 0x73, 0x12, 0x1b, 0x0a,
0x06, 0x6d, 0x65, 0x73, 0x68, 0x49, 0x70, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x48, 0x00, 0x52,
0x06, 0x6d, 0x65, 0x73, 0x68, 0x49, 0x70, 0x88, 0x01, 0x01, 0x42, 0x09, 0x0a, 0x07, 0x5f, 0x6d,
0x65, 0x73, 0x68, 0x49, 0x70, 0x32, 0x91, 0x01, 0x0a, 0x0e, 0x4d, 0x65, 0x73, 0x68, 0x43, 0x74,
0x72, 0x6c, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x12, 0x3d, 0x0a, 0x07, 0x47, 0x65, 0x74, 0x4d,
0x65, 0x73, 0x68, 0x12, 0x18, 0x2e, 0x72, 0x70, 0x63, 0x74, 0x79, 0x70, 0x65, 0x73, 0x2e, 0x47,
0x65, 0x74, 0x4d, 0x65, 0x73, 0x68, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x16, 0x2e,
0x72, 0x70, 0x63, 0x74, 0x79, 0x70, 0x65, 0x73, 0x2e, 0x47, 0x65, 0x74, 0x4d, 0x65, 0x73, 0x68,
0x52, 0x65, 0x70, 0x6c, 0x79, 0x22, 0x00, 0x12, 0x40, 0x0a, 0x08, 0x4a, 0x6f, 0x69, 0x6e, 0x4d,
0x65, 0x73, 0x68, 0x12, 0x19, 0x2e, 0x72, 0x70, 0x63, 0x74, 0x79, 0x70, 0x65, 0x73, 0x2e, 0x4a,
0x6f, 0x69, 0x6e, 0x4d, 0x65, 0x73, 0x68, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x17,
0x2e, 0x72, 0x70, 0x63, 0x74, 0x79, 0x70, 0x65, 0x73, 0x2e, 0x4a, 0x6f, 0x69, 0x6e, 0x4d, 0x65,
0x73, 0x68, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x17, 0x2e, 0x72, 0x70, 0x63, 0x74,
0x79, 0x70, 0x65, 0x73, 0x2e, 0x4a, 0x6f, 0x69, 0x6e, 0x4d, 0x65, 0x73, 0x68, 0x52, 0x65, 0x70,
0x6c, 0x79, 0x22, 0x00, 0x42, 0x14, 0x5a, 0x12, 0x70, 0x6b, 0x67, 0x2f, 0x63, 0x74, 0x72, 0x6c,
0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x2f, 0x72, 0x70, 0x63, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74,
0x6f, 0x33,
0x73, 0x68, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x22, 0x00, 0x42, 0x14, 0x5a, 0x12, 0x70, 0x6b, 0x67,
0x2f, 0x63, 0x74, 0x72, 0x6c, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x2f, 0x72, 0x70, 0x63, 0x62,
0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (

View File

@ -4,6 +4,7 @@ import (
context "context"
"errors"
"fmt"
"math/rand"
"net"
"strconv"
@ -65,21 +66,30 @@ func (m *meshCtrlServer) JoinMesh(ctx context.Context, request *JoinMeshRequest)
return nil, err
}
wgIp := request.WgIp
if wgIp == "" {
wgIp = "10.0.0." + strconv.Itoa(rand.Intn(253)+1) + "/32"
}
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,
MeshId: request.MeshId,
WgEndpoint: hostIp + ":" + strconv.Itoa(int(request.WgPort)),
WgIp: wgIp,
}
err = m.server.AddHost(addHostArgs)
if err != nil {
return &JoinMeshReply{Success: false}, nil
return nil, err
}
fmt.Println("success!")
return &JoinMeshReply{Success: true}, nil
return &JoinMeshReply{Success: true, MeshIp: &wgIp}, nil
}
func NewRpcServer(ctlServer *ctrlserver.MeshCtrlServer) *grpc.Server {