2023-11-30 03:02:38 +01:00
|
|
|
package crdt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-12-07 19:18:13 +01:00
|
|
|
"hash/fnv"
|
2023-11-30 03:02:38 +01:00
|
|
|
|
2024-01-02 00:55:50 +01:00
|
|
|
"github.com/tim-beatham/smegmesh/pkg/conf"
|
|
|
|
"github.com/tim-beatham/smegmesh/pkg/lib"
|
|
|
|
"github.com/tim-beatham/smegmesh/pkg/mesh"
|
2023-11-30 03:02:38 +01:00
|
|
|
)
|
|
|
|
|
2024-01-04 14:10:08 +01:00
|
|
|
// TwoPhaseMapFactory: instantiate a new twophasemap
|
|
|
|
// datastore
|
2023-12-10 20:21:54 +01:00
|
|
|
type TwoPhaseMapFactory struct {
|
|
|
|
Config *conf.DaemonConfiguration
|
|
|
|
}
|
2023-11-30 03:02:38 +01:00
|
|
|
|
2024-01-04 14:10:08 +01:00
|
|
|
// CreateMesh: create a new mesh network
|
2023-11-30 03:02:38 +01:00
|
|
|
func (f *TwoPhaseMapFactory) CreateMesh(params *mesh.MeshProviderFactoryParams) (mesh.MeshProvider, error) {
|
|
|
|
return &TwoPhaseStoreMeshManager{
|
2023-12-10 20:21:54 +01:00
|
|
|
MeshId: params.MeshId,
|
|
|
|
IfName: params.DevName,
|
|
|
|
Client: params.Client,
|
2023-12-29 23:05:05 +01:00
|
|
|
Conf: params.Conf,
|
|
|
|
DaemonConf: params.DaemonConf,
|
2023-12-07 19:18:13 +01:00
|
|
|
store: NewTwoPhaseMap[string, MeshNode](params.NodeID, func(s string) uint64 {
|
2023-12-08 21:02:57 +01:00
|
|
|
h := fnv.New64a()
|
2023-12-07 19:18:13 +01:00
|
|
|
h.Write([]byte(s))
|
2023-12-08 21:02:57 +01:00
|
|
|
return h.Sum64()
|
2024-01-05 13:59:13 +01:00
|
|
|
}, uint64(3*f.Config.Heartbeat)),
|
2023-11-30 03:02:38 +01:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-01-04 14:10:08 +01:00
|
|
|
// MeshNodeFactory: create a new node in the mesh network
|
2023-11-30 03:02:38 +01:00
|
|
|
type MeshNodeFactory struct {
|
2023-12-10 20:21:54 +01:00
|
|
|
Config conf.DaemonConfiguration
|
2023-11-30 03:02:38 +01:00
|
|
|
}
|
|
|
|
|
2024-01-04 14:10:08 +01:00
|
|
|
// Build: build a new mesh network
|
2023-11-30 03:02:38 +01:00
|
|
|
func (f *MeshNodeFactory) Build(params *mesh.MeshNodeFactoryParams) mesh.MeshNode {
|
|
|
|
hostName := f.getAddress(params)
|
|
|
|
|
2023-12-10 20:21:54 +01:00
|
|
|
grpcEndpoint := fmt.Sprintf("%s:%d", hostName, f.Config.GrpcPort)
|
|
|
|
wgEndpoint := fmt.Sprintf("%s:%d", hostName, params.WgPort)
|
2023-11-30 03:02:38 +01:00
|
|
|
|
2023-12-10 20:21:54 +01:00
|
|
|
if *params.MeshConfig.Role == conf.CLIENT_ROLE {
|
2023-11-30 03:02:38 +01:00
|
|
|
grpcEndpoint = "-"
|
2023-12-10 20:21:54 +01:00
|
|
|
wgEndpoint = "-"
|
2023-11-30 03:02:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return &MeshNode{
|
|
|
|
HostEndpoint: grpcEndpoint,
|
|
|
|
PublicKey: params.PublicKey.String(),
|
2023-12-10 20:21:54 +01:00
|
|
|
WgEndpoint: wgEndpoint,
|
2023-11-30 03:02:38 +01:00
|
|
|
WgHost: fmt.Sprintf("%s/128", params.NodeIP.String()),
|
|
|
|
Routes: make(map[string]Route),
|
|
|
|
Description: "",
|
|
|
|
Alias: "",
|
2023-12-10 20:21:54 +01:00
|
|
|
Type: string(*params.MeshConfig.Role),
|
2023-11-30 03:02:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// getAddress returns the routable address of the machine.
|
|
|
|
func (f *MeshNodeFactory) getAddress(params *mesh.MeshNodeFactoryParams) string {
|
|
|
|
var hostName string = ""
|
|
|
|
|
|
|
|
if params.Endpoint != "" {
|
|
|
|
hostName = params.Endpoint
|
2023-12-10 20:21:54 +01:00
|
|
|
} else if params.MeshConfig.Endpoint != nil && len(*params.MeshConfig.Endpoint) != 0 {
|
|
|
|
hostName = *params.MeshConfig.Endpoint
|
2023-11-30 03:02:38 +01:00
|
|
|
} else {
|
|
|
|
ipFunc := lib.GetPublicIP
|
|
|
|
|
2024-01-05 13:59:13 +01:00
|
|
|
if *params.MeshConfig.IPDiscovery == conf.OUTGOING_IP_DISCOVERY {
|
2023-11-30 03:02:38 +01:00
|
|
|
ipFunc = lib.GetOutboundIP
|
|
|
|
}
|
|
|
|
|
|
|
|
ip, err := ipFunc()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
hostName = ip.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
return hostName
|
|
|
|
}
|