2023-11-30 03:02:38 +01:00
|
|
|
package automerge
|
2023-10-28 17:38:25 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
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-10-28 17:38:25 +02:00
|
|
|
)
|
|
|
|
|
2024-01-04 14:10:08 +01:00
|
|
|
// CrdtProviderFactory: abstracts the instantiation of an automerge
|
|
|
|
// datastore
|
2023-10-28 17:38:25 +02:00
|
|
|
type CrdtProviderFactory struct{}
|
|
|
|
|
2024-01-04 14:10:08 +01:00
|
|
|
// CreateMesh: create a new mesh datastore
|
2023-10-28 17:38:25 +02:00
|
|
|
func (f *CrdtProviderFactory) CreateMesh(params *mesh.MeshProviderFactoryParams) (mesh.MeshProvider, error) {
|
|
|
|
return NewCrdtNodeManager(&NewCrdtNodeMangerParams{
|
|
|
|
MeshId: params.MeshId,
|
|
|
|
DevName: params.DevName,
|
2023-12-10 20:21:54 +01:00
|
|
|
Conf: params.Conf,
|
2023-10-28 17:38:25 +02:00
|
|
|
Client: params.Client,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-01-04 14:10:08 +01:00
|
|
|
// MeshNodeFactory: abstracts the instnatiation of a node
|
2023-10-28 17:38:25 +02:00
|
|
|
type MeshNodeFactory struct {
|
2023-12-10 20:21:54 +01:00
|
|
|
Config conf.DaemonConfiguration
|
2023-10-28 17:38:25 +02:00
|
|
|
}
|
|
|
|
|
2024-01-04 14:10:08 +01:00
|
|
|
// Build: builds the mesh node that represents the host machine to add
|
2023-10-28 17:38:25 +02:00
|
|
|
// to the mesh
|
|
|
|
func (f *MeshNodeFactory) Build(params *mesh.MeshNodeFactoryParams) mesh.MeshNode {
|
|
|
|
hostName := f.getAddress(params)
|
|
|
|
|
2023-12-22 22:49:47 +01:00
|
|
|
grpcEndpoint := fmt.Sprintf("%s:%d", hostName, f.Config.GrpcPort)
|
2023-11-24 13:07:03 +01:00
|
|
|
|
2023-12-10 20:21:54 +01:00
|
|
|
if *params.MeshConfig.Role == conf.CLIENT_ROLE {
|
2023-11-24 13:07:03 +01:00
|
|
|
grpcEndpoint = "-"
|
|
|
|
}
|
|
|
|
|
2023-10-28 17:38:25 +02:00
|
|
|
return &MeshNodeCrdt{
|
2023-11-24 13:07:03 +01:00
|
|
|
HostEndpoint: grpcEndpoint,
|
2023-10-28 17:38:25 +02:00
|
|
|
PublicKey: params.PublicKey.String(),
|
|
|
|
WgEndpoint: fmt.Sprintf("%s:%d", hostName, params.WgPort),
|
|
|
|
WgHost: fmt.Sprintf("%s/128", params.NodeIP.String()),
|
|
|
|
// Always set the routes as empty.
|
|
|
|
// Routes handled by external component
|
2023-11-25 04:15:58 +01:00
|
|
|
Routes: make(map[string]Route),
|
2023-11-17 20:05:21 +01:00
|
|
|
Description: "",
|
|
|
|
Alias: "",
|
2023-12-10 20:21:54 +01:00
|
|
|
Type: string(*params.MeshConfig.Role),
|
2023-10-28 17:38:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-04 14:10:08 +01:00
|
|
|
// getAddress: returns the routable address of the machine.
|
2023-10-28 17:38:25 +02:00
|
|
|
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 len(*params.MeshConfig.Endpoint) != 0 {
|
|
|
|
hostName = *params.MeshConfig.Endpoint
|
2023-10-28 17:38:25 +02:00
|
|
|
} else {
|
2023-11-24 13:07:03 +01:00
|
|
|
ipFunc := lib.GetPublicIP
|
|
|
|
|
2023-12-10 20:21:54 +01:00
|
|
|
if *params.MeshConfig.IPDiscovery == conf.DNS_IP_DISCOVERY {
|
2023-11-24 13:07:03 +01:00
|
|
|
ipFunc = lib.GetOutboundIP
|
|
|
|
}
|
|
|
|
|
|
|
|
ip, err := ipFunc()
|
2023-11-22 11:41:54 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
hostName = ip.String()
|
2023-10-28 17:38:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return hostName
|
|
|
|
}
|