mirror of
https://github.com/ngoduykhanh/wireguard-ui.git
synced 2025-05-03 22:02:23 +03:00
Write the initial wireguard config on start, if none exists (#207)
This commit is contained in:
parent
ec7db055c8
commit
1c6fb6a424
1 changed files with 34 additions and 0 deletions
34
main.go
34
main.go
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue