mirror of
https://github.com/ngoduykhanh/wireguard-ui.git
synced 2025-06-08 00:56:58 +03:00
commit
5967c4a0b7
9 changed files with 62 additions and 6 deletions
|
@ -98,7 +98,7 @@ func WireGuardClients(db store.IStore) echo.HandlerFunc {
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.Render(http.StatusOK, "clients.html", map[string]interface{}{
|
return c.Render(http.StatusOK, "clients.html", map[string]interface{}{
|
||||||
"baseData": model.BaseData{Active: "", CurrentUser: currentUser(c)},
|
"baseData": model.BaseData{Active: "", CurrentUser: currentUser(c), AllowedIPs: util.AllowedIPs},
|
||||||
"clientDataList": clientDataList,
|
"clientDataList": clientDataList,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -254,6 +254,13 @@ func EmailClient(db store.IStore, mailer emailer.Emailer, emailSubject, emailCon
|
||||||
globalSettings, _ := db.GetGlobalSettings()
|
globalSettings, _ := db.GetGlobalSettings()
|
||||||
config := util.BuildClientConfig(*clientData.Client, server, globalSettings)
|
config := util.BuildClientConfig(*clientData.Client, server, globalSettings)
|
||||||
|
|
||||||
|
if globalSettings.EmailContent != "" {
|
||||||
|
emailContent = globalSettings.EmailContent
|
||||||
|
}
|
||||||
|
if globalSettings.EmailSubject != "" {
|
||||||
|
emailSubject = globalSettings.EmailSubject
|
||||||
|
}
|
||||||
|
|
||||||
cfg_att := emailer.Attachment{"wg0.conf", []byte(config)}
|
cfg_att := emailer.Attachment{"wg0.conf", []byte(config)}
|
||||||
var attachments []emailer.Attachment
|
var attachments []emailer.Attachment
|
||||||
if clientData.Client.PrivateKey != "" {
|
if clientData.Client.PrivateKey != "" {
|
||||||
|
@ -609,6 +616,7 @@ func GlobalSettingSubmit(db store.IStore) echo.HandlerFunc {
|
||||||
}
|
}
|
||||||
|
|
||||||
globalSettings.UpdatedAt = time.Now().UTC()
|
globalSettings.UpdatedAt = time.Now().UTC()
|
||||||
|
globalSettings.EmailContent = base64.StdEncoding.EncodeToString([]byte(globalSettings.EmailContent))
|
||||||
|
|
||||||
// write config to the database
|
// write config to the database
|
||||||
if err := db.SaveGlobalSettings(globalSettings); err != nil {
|
if err := db.SaveGlobalSettings(globalSettings); err != nil {
|
||||||
|
|
9
main.go
9
main.go
|
@ -3,10 +3,11 @@ package main
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/labstack/echo/v4"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/labstack/echo/v4"
|
||||||
|
|
||||||
rice "github.com/GeertJohan/go.rice"
|
rice "github.com/GeertJohan/go.rice"
|
||||||
"github.com/ngoduykhanh/wireguard-ui/emailer"
|
"github.com/ngoduykhanh/wireguard-ui/emailer"
|
||||||
"github.com/ngoduykhanh/wireguard-ui/handler"
|
"github.com/ngoduykhanh/wireguard-ui/handler"
|
||||||
|
@ -34,6 +35,8 @@ var (
|
||||||
flagEmailFrom string
|
flagEmailFrom string
|
||||||
flagEmailFromName string = "WireGuard UI"
|
flagEmailFromName string = "WireGuard UI"
|
||||||
flagSessionSecret string
|
flagSessionSecret string
|
||||||
|
flagClientMTU int
|
||||||
|
flagAllowedIPs string
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -60,6 +63,8 @@ func init() {
|
||||||
flag.StringVar(&flagEmailFrom, "email-from", util.LookupEnvOrString("EMAIL_FROM_ADDRESS", flagEmailFrom), "'From' email address.")
|
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(&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(&flagSessionSecret, "session-secret", util.LookupEnvOrString("SESSION_SECRET", flagSessionSecret), "The key used to encrypt session cookies.")
|
||||||
|
flag.IntVar(&flagClientMTU, "client-mtu", util.LookupEnvOrInt("CLIENT_MTU", flagClientMTU), "Client default MTU")
|
||||||
|
flag.StringVar(&flagAllowedIPs, "allowed-ips", util.LookupEnvOrString("ALLOWED_IPS", flagAllowedIPs), "List of default allowed IPs for the client")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
// update runtime config
|
// update runtime config
|
||||||
|
@ -75,6 +80,8 @@ func init() {
|
||||||
util.EmailFrom = flagEmailFrom
|
util.EmailFrom = flagEmailFrom
|
||||||
util.EmailFromName = flagEmailFromName
|
util.EmailFromName = flagEmailFromName
|
||||||
util.SessionSecret = []byte(flagSessionSecret)
|
util.SessionSecret = []byte(flagSessionSecret)
|
||||||
|
util.ClientMTU = flagClientMTU
|
||||||
|
util.AllowedIPs = flagAllowedIPs
|
||||||
|
|
||||||
// print app information
|
// print app information
|
||||||
fmt.Println("Wireguard UI")
|
fmt.Println("Wireguard UI")
|
||||||
|
|
|
@ -10,4 +10,5 @@ type Interface struct {
|
||||||
type BaseData struct {
|
type BaseData struct {
|
||||||
Active string
|
Active string
|
||||||
CurrentUser string
|
CurrentUser string
|
||||||
|
AllowedIPs string
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,5 +11,7 @@ type GlobalSetting struct {
|
||||||
MTU int `json:"mtu,string"`
|
MTU int `json:"mtu,string"`
|
||||||
PersistentKeepalive int `json:"persistent_keepalive,string"`
|
PersistentKeepalive int `json:"persistent_keepalive,string"`
|
||||||
ConfigFilePath string `json:"config_file_path"`
|
ConfigFilePath string `json:"config_file_path"`
|
||||||
|
EmailSubject string `json:"email_subject"`
|
||||||
|
EmailContent string `json:"email_content"`
|
||||||
UpdatedAt time.Time `json:"updated_at"`
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -115,7 +115,18 @@ func (o *JsonDB) GetUser() (model.User, error) {
|
||||||
// GetGlobalSettings func to query global settings from the database
|
// GetGlobalSettings func to query global settings from the database
|
||||||
func (o *JsonDB) GetGlobalSettings() (model.GlobalSetting, error) {
|
func (o *JsonDB) GetGlobalSettings() (model.GlobalSetting, error) {
|
||||||
settings := model.GlobalSetting{}
|
settings := model.GlobalSetting{}
|
||||||
return settings, o.conn.Read("server", "global_settings", &settings)
|
err := o.conn.Read("server", "global_settings", &settings)
|
||||||
|
if err != nil {
|
||||||
|
return settings, err
|
||||||
|
}
|
||||||
|
if settings.EmailContent != "" {
|
||||||
|
str, err := base64.StdEncoding.DecodeString(settings.EmailContent)
|
||||||
|
if err != nil {
|
||||||
|
return settings, err
|
||||||
|
}
|
||||||
|
settings.EmailContent = string(str)
|
||||||
|
}
|
||||||
|
return settings, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetServer func to query Server setting from the database
|
// GetServer func to query Server setting from the database
|
||||||
|
|
|
@ -175,7 +175,7 @@
|
||||||
</i>
|
</i>
|
||||||
</label>
|
</label>
|
||||||
<input type="text" data-role="tagsinput" class="form-control" id="client_allowed_ips"
|
<input type="text" data-role="tagsinput" class="form-control" id="client_allowed_ips"
|
||||||
value="0.0.0.0/0">
|
value="{{if .baseData.AllowedIPs}} {{.baseData.AllowedIPs}} {{else}} 0.0.0.0/0 {{end}}">
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="client_extra_allowed_ips" class="control-label">Extra Allowed IPs
|
<label for="client_extra_allowed_ips" class="control-label">Extra Allowed IPs
|
||||||
|
|
|
@ -61,6 +61,16 @@ Global Settings
|
||||||
name="config_file_path" placeholder="E.g. /etc/wireguard/wg0.conf"
|
name="config_file_path" placeholder="E.g. /etc/wireguard/wg0.conf"
|
||||||
value="{{ .globalSettings.ConfigFilePath }}">
|
value="{{ .globalSettings.ConfigFilePath }}">
|
||||||
</div>
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="email_subject">Email Subject</label>
|
||||||
|
<input type="text" class="form-control" id="email_subject"
|
||||||
|
name="email_subject" placeholder="Your new wireguard configuration"
|
||||||
|
value="{{ .globalSettings.EmailSubject }}">
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="email_content">Email Content</label>
|
||||||
|
<textarea class="form-control" id="email_content" name="email_content" placeholder="<p>Html email template</p>" rows="3"> {{ .globalSettings.EmailContent }} </textarea>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<!-- /.card-body -->
|
<!-- /.card-body -->
|
||||||
|
|
||||||
|
@ -142,7 +152,17 @@ Global Settings
|
||||||
const mtu = $("#mtu").val();
|
const mtu = $("#mtu").val();
|
||||||
const persistent_keepalive = $("#persistent_keepalive").val();
|
const persistent_keepalive = $("#persistent_keepalive").val();
|
||||||
const config_file_path = $("#config_file_path").val();
|
const config_file_path = $("#config_file_path").val();
|
||||||
const data = {"endpoint_address": endpoint_address, "dns_servers": dns_servers, "mtu": mtu, "persistent_keepalive": persistent_keepalive, "config_file_path": config_file_path};
|
const email_subject = $("#email_subject").val();
|
||||||
|
const email_content = $("#email_content").val();
|
||||||
|
const data = {
|
||||||
|
"endpoint_address": endpoint_address,
|
||||||
|
"dns_servers": dns_servers,
|
||||||
|
"mtu": mtu,
|
||||||
|
"persistent_keepalive": persistent_keepalive,
|
||||||
|
"config_file_path": config_file_path,
|
||||||
|
"email_subject": email_subject,
|
||||||
|
"email_content": email_content
|
||||||
|
};
|
||||||
|
|
||||||
$.ajax({
|
$.ajax({
|
||||||
cache: false,
|
cache: false,
|
||||||
|
|
|
@ -16,6 +16,8 @@ var (
|
||||||
EmailSubject string
|
EmailSubject string
|
||||||
EmailContent string
|
EmailContent string
|
||||||
SessionSecret []byte
|
SessionSecret []byte
|
||||||
|
ClientMTU int
|
||||||
|
AllowedIPs string
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
|
@ -27,6 +27,10 @@ func BuildClientConfig(client model.Client, server model.Server, setting model.G
|
||||||
if client.UseServerDNS {
|
if client.UseServerDNS {
|
||||||
clientDNS = fmt.Sprintf("DNS = %s\n", strings.Join(setting.DNSServers, ","))
|
clientDNS = fmt.Sprintf("DNS = %s\n", strings.Join(setting.DNSServers, ","))
|
||||||
}
|
}
|
||||||
|
clientMTU := ""
|
||||||
|
if ClientMTU > 0 {
|
||||||
|
clientMTU = fmt.Sprintf("MTU = %d\n", ClientMTU)
|
||||||
|
}
|
||||||
|
|
||||||
// Peer section
|
// Peer section
|
||||||
peerPublicKey := fmt.Sprintf("PublicKey = %s\n", server.KeyPair.PublicKey)
|
peerPublicKey := fmt.Sprintf("PublicKey = %s\n", server.KeyPair.PublicKey)
|
||||||
|
@ -60,6 +64,7 @@ func BuildClientConfig(client model.Client, server model.Server, setting model.G
|
||||||
clientAddress +
|
clientAddress +
|
||||||
clientPrivateKey +
|
clientPrivateKey +
|
||||||
clientDNS +
|
clientDNS +
|
||||||
|
clientMTU +
|
||||||
"\n[Peer]\n" +
|
"\n[Peer]\n" +
|
||||||
peerPublicKey +
|
peerPublicKey +
|
||||||
peerPresharedKey +
|
peerPresharedKey +
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue