2023-09-18 16:52:28 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-11-20 12:28:12 +01:00
|
|
|
"net/http"
|
|
|
|
_ "net/http/pprof"
|
2023-10-30 17:49:02 +01:00
|
|
|
"os"
|
2023-11-01 12:58:10 +01:00
|
|
|
"os/signal"
|
2023-09-19 14:45:49 +02:00
|
|
|
|
2024-01-02 01:09:31 +01:00
|
|
|
"github.com/tim-beatham/smegmesh/pkg/conf"
|
|
|
|
ctrlserver "github.com/tim-beatham/smegmesh/pkg/ctrlserver"
|
|
|
|
"github.com/tim-beatham/smegmesh/pkg/ipc"
|
|
|
|
logging "github.com/tim-beatham/smegmesh/pkg/log"
|
|
|
|
"github.com/tim-beatham/smegmesh/pkg/robin"
|
|
|
|
"github.com/tim-beatham/smegmesh/pkg/sync"
|
2023-10-24 17:00:46 +02:00
|
|
|
"golang.zx2c4.com/wireguard/wgctrl"
|
2023-09-18 16:52:28 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2023-10-30 17:49:02 +01:00
|
|
|
if len(os.Args) != 2 {
|
2023-12-10 20:21:54 +01:00
|
|
|
logging.Log.WriteErrorf("Did not provide configuration")
|
2023-10-30 17:49:02 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-10 20:21:54 +01:00
|
|
|
conf, err := conf.ParseDaemonConfiguration(os.Args[1])
|
2023-09-19 14:45:49 +02:00
|
|
|
if err != nil {
|
2023-12-10 20:21:54 +01:00
|
|
|
logging.Log.WriteErrorf("Could not parse configuration: %s", err.Error())
|
2023-10-30 17:49:02 +01:00
|
|
|
return
|
2023-09-19 14:45:49 +02:00
|
|
|
}
|
|
|
|
|
2023-10-24 17:00:46 +02:00
|
|
|
client, err := wgctrl.New()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logging.Log.WriteErrorf("Failed to create wgctrl client")
|
|
|
|
return
|
|
|
|
}
|
2023-10-01 20:01:35 +02:00
|
|
|
|
2023-11-20 12:28:12 +01:00
|
|
|
if conf.Profile {
|
|
|
|
go func() {
|
|
|
|
http.ListenAndServe("localhost:6060", nil)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
var robinRpc robin.WgRpc
|
|
|
|
var robinIpc robin.IpcHandler
|
2023-10-20 13:41:06 +02:00
|
|
|
var syncProvider sync.SyncServiceImpl
|
2023-10-02 17:03:41 +02:00
|
|
|
|
|
|
|
ctrlServerParams := ctrlserver.NewCtrlServerParams{
|
|
|
|
Conf: conf,
|
|
|
|
CtrlProvider: &robinRpc,
|
2023-10-20 13:41:06 +02:00
|
|
|
SyncProvider: &syncProvider,
|
2023-10-24 17:00:46 +02:00
|
|
|
Client: client,
|
2023-10-01 20:01:35 +02:00
|
|
|
}
|
2023-11-06 14:37:28 +01:00
|
|
|
|
2023-11-17 20:05:21 +01:00
|
|
|
ctrlServer, err := ctrlserver.NewCtrlServer(&ctrlServerParams)
|
2023-12-31 13:44:50 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2024-01-04 14:10:08 +01:00
|
|
|
syncProvider.MeshManager = ctrlServer.MeshManager
|
2023-10-05 18:48:54 +02:00
|
|
|
|
|
|
|
robinIpcParams := robin.RobinIpcParams{
|
|
|
|
CtrlServer: ctrlServer,
|
|
|
|
}
|
|
|
|
|
2023-10-02 17:03:41 +02:00
|
|
|
robinRpc.Server = ctrlServer
|
2023-10-05 18:48:54 +02:00
|
|
|
robinIpc = robin.NewRobinIpc(robinIpcParams)
|
2023-10-01 20:01:35 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2023-10-24 01:12:38 +02:00
|
|
|
logging.Log.WriteErrorf(err.Error())
|
|
|
|
return
|
2023-10-01 20:01:35 +02:00
|
|
|
}
|
|
|
|
|
2024-01-04 14:10:08 +01:00
|
|
|
logging.Log.WriteInfof("running ipc handler")
|
2023-10-02 17:03:41 +02:00
|
|
|
go ipc.RunIpcHandler(&robinIpc)
|
2023-09-29 16:00:20 +02:00
|
|
|
|
2023-11-01 12:58:10 +01:00
|
|
|
closeResources := func() {
|
2024-01-04 14:10:08 +01:00
|
|
|
logging.Log.WriteInfof("closing resources")
|
2023-11-01 12:58:10 +01:00
|
|
|
ctrlServer.Close()
|
|
|
|
client.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
c := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(c, os.Interrupt)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for range c {
|
|
|
|
closeResources()
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-10-02 17:03:41 +02:00
|
|
|
err = ctrlServer.ConnectionServer.Listen()
|
2023-10-01 20:01:35 +02:00
|
|
|
|
2023-10-02 17:03:41 +02:00
|
|
|
if err != nil {
|
2023-10-24 01:12:38 +02:00
|
|
|
logging.Log.WriteErrorf(err.Error())
|
|
|
|
return
|
2023-09-19 19:29:35 +02:00
|
|
|
}
|
2023-09-20 15:34:34 +02:00
|
|
|
|
2023-11-01 12:58:10 +01:00
|
|
|
go closeResources()
|
2023-09-18 16:52:28 +02:00
|
|
|
}
|