Write the initial wireguard config on start, if none exists (#207)

This commit is contained in:
Marcus Wichelmann 2022-07-14 08:40:16 +02:00 committed by GitHub
parent ec7db055c8
commit 1c6fb6a424
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

34
main.go
View file

@ -4,7 +4,10 @@ import (
"flag"
"fmt"
"github.com/labstack/echo/v4"
"github.com/labstack/gommon/log"
"github.com/ngoduykhanh/wireguard-ui/store"
"net/http"
"os"
"time"
rice "github.com/GeertJohan/go.rice"
@ -118,6 +121,9 @@ func main() {
// rice file server for assets. "assets" is the folder where the files come from.
assetHandler := http.FileServer(rice.MustFindBox("assets").HTTPBox())
// create the wireguard config on start, if it doesn't exist
initServerConfig(db, tmplBox)
// register routes
app := router.New(tmplBox, extraData, util.SessionSecret)
@ -164,3 +170,31 @@ func main() {
app.Logger.Fatal(app.Start(util.BindAddress))
}
func initServerConfig(db store.IStore, tmplBox *rice.Box) {
settings, err := db.GetGlobalSettings()
if err != nil {
log.Fatalf("Cannot get global settings: ", err)
}
if _, err := os.Stat(settings.ConfigFilePath); err == nil {
// file exists, don't overwrite it implicitly
return
}
server, err := db.GetServer()
if err != nil {
log.Fatalf("Cannot get server config: ", err)
}
clients, err := db.GetClients(false)
if err != nil {
log.Fatalf("Cannot get client config: ", err)
}
// write config file
err = util.WriteWireGuardServerConfig(tmplBox, server, clients, settings)
if err != nil {
log.Fatalf("Cannot create server config: ", err)
}
}