1
0
forked from extern/smegmesh
smegmesh/cmd/wgmeshd/main.go

109 lines
2.3 KiB
Go
Raw Normal View History

package main
import (
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
2023-09-19 14:45:49 +02:00
2023-10-01 20:01:35 +02:00
"github.com/tim-beatham/wgmesh/pkg/conf"
ctrlserver "github.com/tim-beatham/wgmesh/pkg/ctrlserver"
2023-09-29 16:00:20 +02:00
"github.com/tim-beatham/wgmesh/pkg/ipc"
logging "github.com/tim-beatham/wgmesh/pkg/log"
"github.com/tim-beatham/wgmesh/pkg/mesh"
2023-09-29 16:00:20 +02:00
"github.com/tim-beatham/wgmesh/pkg/robin"
"github.com/tim-beatham/wgmesh/pkg/sync"
"golang.zx2c4.com/wireguard/wgctrl"
)
func main() {
if len(os.Args) != 2 {
logging.Log.WriteErrorf("Need to provide configuration.yaml")
return
}
conf, err := conf.ParseConfiguration(os.Args[1])
2023-09-19 14:45:49 +02:00
if err != nil {
logging.Log.WriteInfof("Could not parse configuration")
return
2023-09-19 14:45:49 +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
if conf.Profile {
go func() {
http.ListenAndServe("localhost:6060", nil)
}()
}
var robinRpc robin.WgRpc
var robinIpc robin.IpcHandler
var syncProvider sync.SyncServiceImpl
var syncRequester sync.SyncRequester
var syncer sync.Syncer
ctrlServerParams := ctrlserver.NewCtrlServerParams{
Conf: conf,
CtrlProvider: &robinRpc,
SyncProvider: &syncProvider,
Client: client,
OnDelete: func(mp mesh.MeshProvider) {
syncer.SyncMeshes()
},
2023-10-01 20:01:35 +02:00
}
ctrlServer, err := ctrlserver.NewCtrlServer(&ctrlServerParams)
syncProvider.Server = ctrlServer
syncRequester = sync.NewSyncRequester(ctrlServer)
syncer = sync.NewSyncer(ctrlServer.MeshManager, conf, syncRequester)
syncScheduler := sync.NewSyncScheduler(ctrlServer, syncRequester, syncer)
2023-10-05 18:48:54 +02:00
robinIpcParams := robin.RobinIpcParams{
CtrlServer: ctrlServer,
}
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
}
logging.Log.WriteInfof("Running IPC Handler")
go ipc.RunIpcHandler(&robinIpc)
go syncScheduler.Run()
2023-09-29 16:00:20 +02:00
closeResources := func() {
logging.Log.WriteInfof("Closing resources")
syncScheduler.Stop()
ctrlServer.Close()
client.Close()
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for range c {
closeResources()
os.Exit(0)
}
}()
err = ctrlServer.ConnectionServer.Listen()
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-09-20 15:34:34 +02:00
go closeResources()
}