Merge branch 'master' into client-default-settings

This commit is contained in:
Arminas 2023-05-19 01:30:17 +03:00 committed by GitHub
commit 97e7ead5fd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 81 additions and 82 deletions

View file

@ -7,6 +7,7 @@ import (
"github.com/ngoduykhanh/wireguard-ui/store"
"golang.org/x/mod/sumdb/dirhash"
"io"
"io/fs"
"io/ioutil"
"net"
"os"
@ -17,7 +18,6 @@ import (
"text/template"
"time"
rice "github.com/GeertJohan/go.rice"
externalip "github.com/glendc/go-external-ip"
"github.com/labstack/gommon/log"
"github.com/ngoduykhanh/wireguard-ui/model"
@ -400,7 +400,7 @@ func ValidateIPAllocation(serverAddresses []string, ipAllocatedList []string, ip
}
// WriteWireGuardServerConfig to write Wireguard server config. e.g. wg0.conf
func WriteWireGuardServerConfig(tmplBox *rice.Box, serverConfig model.Server, clientDataList []model.ClientData, usersList []model.User, globalSettings model.GlobalSetting) error {
func WriteWireGuardServerConfig(tmplDir fs.FS, serverConfig model.Server, clientDataList []model.ClientData, usersList []model.User, globalSettings model.GlobalSetting) error {
var tmplWireguardConf string
// if set, read wg.conf template from WgConfTemplate
@ -412,7 +412,7 @@ func WriteWireGuardServerConfig(tmplBox *rice.Box, serverConfig model.Server, cl
tmplWireguardConf = string(fileContentBytes)
} else {
// read default wg.conf template file to string
fileContent, err := tmplBox.String("wg.conf")
fileContent, err := StringFromEmbedFile(tmplDir, "wg.conf")
if err != nil {
return err
}
@ -483,6 +483,18 @@ func LookupEnvOrStrings(key string, defaultVal []string) []string {
return defaultVal
}
func StringFromEmbedFile(embed fs.FS, filename string) (string, error) {
file, err := embed.Open(filename)
if err != nil {
return "", err
}
content, err := io.ReadAll(file)
if err != nil {
return "", err
}
return string(content), nil
}
func ParseLogLevel(lvl string) (log.Lvl, error) {
switch strings.ToLower(lvl) {
case "debug":
@ -520,11 +532,11 @@ func HashesChanged(db store.IStore) bool {
newClient, newServer := GetCurrentHash(db)
if oldClient != newClient {
fmt.Println("Hash for client differs")
//fmt.Println("Hash for client differs")
return true
}
if oldServer != newServer {
fmt.Println("Hash for server differs")
//fmt.Println("Hash for server differs")
return true
}
return false