Skip to content

Commit 71ede02

Browse files
PresharedKey is now only included if set (#141)
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. Co-authored-by: Khanh Ngo <[email protected]>
1 parent af62be3 commit 71ede02

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

templates/wg.conf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ PostDown = {{ .serverConfig.Interface.PostDown }}
1919
# Update at: {{ .Client.UpdatedAt }}
2020
[Peer]
2121
PublicKey = {{ .Client.PublicKey }}
22-
PresharedKey = {{ .Client.PresharedKey }}
23-
AllowedIPs = {{$first :=true}}{{range .Client.AllocatedIPs }}{{if $first}}{{$first = false}}{{else}},{{end}}{{.}}{{end}}{{range .Client.ExtraAllowedIPs }},{{.}}{{end}}
22+
{{if .Client.PresharedKey }}PresharedKey = {{ .Client.PresharedKey }}
23+
{{end}}AllowedIPs = {{$first :=true}}{{range .Client.AllocatedIPs }}{{if $first}}{{$first = false}}{{else}},{{end}}{{.}}{{end}}{{range .Client.ExtraAllowedIPs }},{{.}}{{end}}
2424
{{end}}{{end}}

util/util.go

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,21 @@ import (
2121
// BuildClientConfig to create wireguard client config string
2222
func BuildClientConfig(client model.Client, server model.Server, setting model.GlobalSetting) string {
2323
// Interface section
24-
clientAddress := fmt.Sprintf("Address = %s", strings.Join(client.AllocatedIPs, ","))
25-
clientPrivateKey := fmt.Sprintf("PrivateKey = %s", client.PrivateKey)
24+
clientAddress := fmt.Sprintf("Address = %s\n", strings.Join(client.AllocatedIPs, ","))
25+
clientPrivateKey := fmt.Sprintf("PrivateKey = %s\n", client.PrivateKey)
2626
clientDNS := ""
2727
if client.UseServerDNS {
28-
clientDNS = fmt.Sprintf("DNS = %s", strings.Join(setting.DNSServers, ","))
28+
clientDNS = fmt.Sprintf("DNS = %s\n", strings.Join(setting.DNSServers, ","))
2929
}
3030

3131
// Peer section
32-
peerPublicKey := fmt.Sprintf("PublicKey = %s", server.KeyPair.PublicKey)
33-
peerPresharedKey := fmt.Sprintf("PresharedKey = %s", client.PresharedKey)
34-
peerAllowedIPs := fmt.Sprintf("AllowedIPs = %s", strings.Join(client.AllowedIPs, ","))
32+
peerPublicKey := fmt.Sprintf("PublicKey = %s\n", server.KeyPair.PublicKey)
33+
peerPresharedKey := ""
34+
if client.PresharedKey != "" {
35+
peerPresharedKey = fmt.Sprintf("PresharedKey = %s\n", client.PresharedKey)
36+
}
37+
38+
peerAllowedIPs := fmt.Sprintf("AllowedIPs = %s\n", strings.Join(client.AllowedIPs, ","))
3539

3640
desiredHost := setting.EndpointAddress
3741
desiredPort := server.Interface.ListenPort
@@ -44,24 +48,24 @@ func BuildClientConfig(client model.Client, server model.Server, setting model.G
4448
log.Error("Endpoint appears to be incorrectly formatted: ", err)
4549
}
4650
}
47-
peerEndpoint := fmt.Sprintf("Endpoint = %s:%d", desiredHost, desiredPort)
51+
peerEndpoint := fmt.Sprintf("Endpoint = %s:%d\n", desiredHost, desiredPort)
4852

4953
peerPersistentKeepalive := ""
5054
if setting.PersistentKeepalive > 0 {
51-
peerPersistentKeepalive = fmt.Sprintf("PersistentKeepalive = %d", setting.PersistentKeepalive)
55+
peerPersistentKeepalive = fmt.Sprintf("PersistentKeepalive = %d\n", setting.PersistentKeepalive)
5256
}
5357

5458
// build the config as string
5559
strConfig := "[Interface]\n" +
56-
clientAddress + "\n" +
57-
clientPrivateKey + "\n" +
58-
clientDNS + "\n\n" +
59-
"[Peer]" + "\n" +
60-
peerPublicKey + "\n" +
61-
peerPresharedKey + "\n" +
62-
peerAllowedIPs + "\n" +
63-
peerEndpoint + "\n" +
64-
peerPersistentKeepalive + "\n"
60+
clientAddress +
61+
clientPrivateKey +
62+
clientDNS +
63+
"\n[Peer]\n" +
64+
peerPublicKey +
65+
peerPresharedKey +
66+
peerAllowedIPs +
67+
peerEndpoint +
68+
peerPersistentKeepalive
6569

6670
return strConfig
6771
}

0 commit comments

Comments
 (0)