Add ability to use custom wg.conf template

This commit is contained in:
slch 2022-03-29 23:07:50 +04:00
parent ad4ca4d9bb
commit 2c8252ff6e
No known key found for this signature in database
GPG key ID: 41F39A9F62B8AD06
3 changed files with 22 additions and 4 deletions

View file

@ -34,6 +34,7 @@ var (
flagEmailFrom string
flagEmailFromName string = "WireGuard UI"
flagSessionSecret string
flagWgConfTemplate string
)
const (
@ -60,6 +61,7 @@ func init() {
flag.StringVar(&flagEmailFrom, "email-from", util.LookupEnvOrString("EMAIL_FROM_ADDRESS", flagEmailFrom), "'From' email address.")
flag.StringVar(&flagEmailFromName, "email-from-name", util.LookupEnvOrString("EMAIL_FROM_NAME", flagEmailFromName), "'From' email name.")
flag.StringVar(&flagSessionSecret, "session-secret", util.LookupEnvOrString("SESSION_SECRET", flagSessionSecret), "The key used to encrypt session cookies.")
flag.StringVar(&flagWgConfTemplate, "wg-conf-template", util.LookupEnvOrString("WG_CONF_TEMPLATE", flagWgConfTemplate), "Path to custom wg.conf template.")
flag.Parse()
// update runtime config
@ -75,6 +77,7 @@ func init() {
util.EmailFrom = flagEmailFrom
util.EmailFromName = flagEmailFromName
util.SessionSecret = []byte(flagSessionSecret)
util.WgConfTemplate = flagWgConfTemplate
// print app information
fmt.Println("Wireguard UI")
@ -89,6 +92,7 @@ func init() {
fmt.Println("Email from\t:", util.EmailFrom)
fmt.Println("Email from name\t:", util.EmailFromName)
//fmt.Println("Session secret\t:", util.SessionSecret)
fmt.Println("Custom wg.conf\t:", util.WgConfTemplate)
}

View file

@ -16,6 +16,7 @@ var (
EmailSubject string
EmailContent string
SessionSecret []byte
WgConfTemplate string
)
const (

View file

@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net"
"os"
"strconv"
@ -359,10 +360,22 @@ 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, globalSettings model.GlobalSetting) error {
// read wg.conf template file to string
tmplWireguardConf, err := tmplBox.String("wg.conf")
if err != nil {
return err
var tmplWireguardConf string
// if set, read wg.conf template from WgConfTemplate
if len(WgConfTemplate) > 0 {
fileContentBytes, err := ioutil.ReadFile(WgConfTemplate)
if err != nil {
return err
}
tmplWireguardConf = string(fileContentBytes)
} else {
// read default wg.conf template file to string
fileContent, err := tmplBox.String("wg.conf")
if err != nil {
return err
}
tmplWireguardConf = fileContent
}
// parse the template