mirror of
https://github.com/ngoduykhanh/wireguard-ui.git
synced 2025-04-21 20:12:33 +03:00
PresharedKey is now only included if set
PresharedKey is now only set in the server and client config if the key is set and not null (or empty). I added this feature because I was importing old config files from clients that did not have a preshared key set. Clients can be created without preshared keys when editing db/client/ files manually. If the field is not set, wireguard-ui creates invalid configs by producing: PresharedKey = This patch remvoes this behavior and just skips the preshared key if not set.
This commit is contained in:
parent
f3a788e3a4
commit
38bb8c343c
2 changed files with 23 additions and 19 deletions
|
@ -19,6 +19,6 @@ PostDown = {{ .serverConfig.Interface.PostDown }}
|
||||||
# Update at: {{ .Client.UpdatedAt }}
|
# Update at: {{ .Client.UpdatedAt }}
|
||||||
[Peer]
|
[Peer]
|
||||||
PublicKey = {{ .Client.PublicKey }}
|
PublicKey = {{ .Client.PublicKey }}
|
||||||
PresharedKey = {{ .Client.PresharedKey }}
|
{{if .Client.PresharedKey }}PresharedKey = {{ .Client.PresharedKey }}
|
||||||
AllowedIPs = {{$first :=true}}{{range .Client.AllocatedIPs }}{{if $first}}{{$first = false}}{{else}},{{end}}{{.}}{{end}}
|
{{end}}AllowedIPs = {{$first :=true}}{{range .Client.AllocatedIPs }}{{if $first}}{{$first = false}}{{else}},{{end}}{{.}}{{end}}
|
||||||
{{end}}{{end}}
|
{{end}}{{end}}
|
||||||
|
|
38
util/util.go
38
util/util.go
|
@ -21,17 +21,21 @@ import (
|
||||||
// BuildClientConfig to create wireguard client config string
|
// BuildClientConfig to create wireguard client config string
|
||||||
func BuildClientConfig(client model.Client, server model.Server, setting model.GlobalSetting) string {
|
func BuildClientConfig(client model.Client, server model.Server, setting model.GlobalSetting) string {
|
||||||
// Interface section
|
// Interface section
|
||||||
clientAddress := fmt.Sprintf("Address = %s", strings.Join(client.AllocatedIPs, ","))
|
clientAddress := fmt.Sprintf("Address = %s\n", strings.Join(client.AllocatedIPs, ","))
|
||||||
clientPrivateKey := fmt.Sprintf("PrivateKey = %s", client.PrivateKey)
|
clientPrivateKey := fmt.Sprintf("PrivateKey = %s\n", client.PrivateKey)
|
||||||
clientDNS := ""
|
clientDNS := ""
|
||||||
if client.UseServerDNS {
|
if client.UseServerDNS {
|
||||||
clientDNS = fmt.Sprintf("DNS = %s", strings.Join(setting.DNSServers, ","))
|
clientDNS = fmt.Sprintf("DNS = %s\n", strings.Join(setting.DNSServers, ","))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Peer section
|
// Peer section
|
||||||
peerPublicKey := fmt.Sprintf("PublicKey = %s", server.KeyPair.PublicKey)
|
peerPublicKey := fmt.Sprintf("PublicKey = %s\n", server.KeyPair.PublicKey)
|
||||||
peerPresharedKey := fmt.Sprintf("PresharedKey = %s", client.PresharedKey)
|
peerPresharedKey := ""
|
||||||
peerAllowedIPs := fmt.Sprintf("AllowedIPs = %s", strings.Join(client.AllowedIPs, ","))
|
if client.PresharedKey != "" {
|
||||||
|
peerPresharedKey = fmt.Sprintf("PresharedKey = %s\n", client.PresharedKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
peerAllowedIPs := fmt.Sprintf("AllowedIPs = %s\n", strings.Join(client.AllowedIPs, ","))
|
||||||
|
|
||||||
desiredHost := setting.EndpointAddress
|
desiredHost := setting.EndpointAddress
|
||||||
desiredPort := server.Interface.ListenPort
|
desiredPort := server.Interface.ListenPort
|
||||||
|
@ -44,24 +48,24 @@ func BuildClientConfig(client model.Client, server model.Server, setting model.G
|
||||||
log.Error("Endpoint appears to be incorrectly formatted: ", err)
|
log.Error("Endpoint appears to be incorrectly formatted: ", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
peerEndpoint := fmt.Sprintf("Endpoint = %s:%d", desiredHost, desiredPort)
|
peerEndpoint := fmt.Sprintf("Endpoint = %s:%d\n", desiredHost, desiredPort)
|
||||||
|
|
||||||
peerPersistentKeepalive := ""
|
peerPersistentKeepalive := ""
|
||||||
if setting.PersistentKeepalive > 0 {
|
if setting.PersistentKeepalive > 0 {
|
||||||
peerPersistentKeepalive = fmt.Sprintf("PersistentKeepalive = %d", setting.PersistentKeepalive)
|
peerPersistentKeepalive = fmt.Sprintf("PersistentKeepalive = %d\n", setting.PersistentKeepalive)
|
||||||
}
|
}
|
||||||
|
|
||||||
// build the config as string
|
// build the config as string
|
||||||
strConfig := "[Interface]\n" +
|
strConfig := "[Interface]\n" +
|
||||||
clientAddress + "\n" +
|
clientAddress +
|
||||||
clientPrivateKey + "\n" +
|
clientPrivateKey +
|
||||||
clientDNS + "\n\n" +
|
clientDNS +
|
||||||
"[Peer]" + "\n" +
|
"\n[Peer]\n" +
|
||||||
peerPublicKey + "\n" +
|
peerPublicKey +
|
||||||
peerPresharedKey + "\n" +
|
peerPresharedKey +
|
||||||
peerAllowedIPs + "\n" +
|
peerAllowedIPs +
|
||||||
peerEndpoint + "\n" +
|
peerEndpoint +
|
||||||
peerPersistentKeepalive + "\n"
|
peerPersistentKeepalive
|
||||||
|
|
||||||
return strConfig
|
return strConfig
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue