Skip to content

Commit 8808f5b

Browse files
authored
Merge pull request Place1#153 from DasSkelett/feature/envvar-comma-separator
2 parents 82ad4d8 + 0a56bd6 commit 8808f5b

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

cmd/serve/main.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,6 @@ func (cmd *servecmd) Name() string {
7474
func (cmd *servecmd) Run() {
7575
conf := cmd.ReadConfig()
7676

77-
if conf.VPN.CIDR == "0" {
78-
conf.VPN.CIDR = ""
79-
}
80-
if conf.VPN.CIDRv6 == "0" {
81-
conf.VPN.CIDRv6 = ""
82-
}
83-
if conf.DNS.Domain == "0" {
84-
conf.DNS.Domain = ""
85-
}
86-
8777
// Get the server's IP addresses within the VPN
8878
var vpnip, vpnipv6 *net.IPNet
8979
var err error
@@ -284,6 +274,7 @@ func (cmd *servecmd) Run() {
284274
}
285275
}
286276

277+
// ReadConfig reads the config file from disk if specified and overrides any env vars or cmdline options
287278
func (cmd *servecmd) ReadConfig() *config.AppConfig {
288279
if cmd.ConfigFilePath != "" {
289280
if b, err := ioutil.ReadFile(cmd.ConfigFilePath); err == nil {
@@ -335,6 +326,24 @@ func (cmd *servecmd) ReadConfig() *config.AppConfig {
335326
cmd.AppConfig.WireGuard.PrivateKey = key.String()
336327
}
337328

329+
// The empty string can be hard to pass through an env var, so we accept '0' too
330+
if cmd.AppConfig.VPN.CIDR == "0" {
331+
cmd.AppConfig.VPN.CIDR = ""
332+
}
333+
if cmd.AppConfig.VPN.CIDRv6 == "0" {
334+
cmd.AppConfig.VPN.CIDRv6 = ""
335+
}
336+
if cmd.AppConfig.DNS.Domain == "0" {
337+
cmd.AppConfig.DNS.Domain = ""
338+
}
339+
// kingpin only splits env vars by \n, let's split at commas as well
340+
if len(cmd.AppConfig.VPN.AllowedIPs) == 1 {
341+
cmd.AppConfig.VPN.AllowedIPs = strings.Split(cmd.AppConfig.VPN.AllowedIPs[0], ",")
342+
}
343+
if len(cmd.AppConfig.DNS.Upstream) == 1 {
344+
cmd.AppConfig.DNS.Upstream = strings.Split(cmd.AppConfig.DNS.Upstream[0], ",")
345+
}
346+
338347
return &cmd.AppConfig
339348
}
340349

docs/2-configuration.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ wg genkey
1616

1717
The config file format is `yaml` and an example is provided [below](#the-config-file-configyaml).
1818

19+
The format for specifying multiple values for options that allow it is:
20+
* as commandline flags:
21+
* repeat the flag (e.g. `--dns-upstream 2001:db8::1 --dns-upstream 192.0.2.1`)
22+
* separate the values with a comma (e.g. `--dns-upstream 2001:db8::1,192.0.2.1`)
23+
* as environment variables:
24+
* separate with a comma (e.g. `WG_DNS_UPSTREAM="2001:db8::1,192.0.2.1"`)
25+
* separate with a new line char (e.g. `WG_DNS_UPSTREAM=$'2001:db8::1\n192.0.2.1'`)
26+
* in the config file as YAML list.
27+
1928
Here's what you can configure:
2029

2130
| Environment Variable | CLI Flag | Config File Path | Required | Default (docker) | Description |
@@ -40,7 +49,7 @@ Here's what you can configure:
4049
| `WG_VPN_GATEWAY_INTERFACE` | `--vpn-gateway-interface` | `vpn.gatewayInterface` | | _default gateway interface (e.g. eth0)_ | The VPN gateway interface. VPN client traffic will be forwarded to this interface. |
4150
| `WG_VPN_ALLOWED_IPS` | `--vpn-allowed-ips` | `vpn.allowedIPs` | | `0.0.0.0/0, ::/0` | Allowed IPs that clients may route through this VPN. This will be set in the client's WireGuard connection file and routing is also enforced by the server using iptables. |
4251
| `WG_DNS_ENABLED` | `--[no-]dns-enabled` | `dns.enabled` | | `true` | Enable/disable the embedded DNS proxy server. This is enabled by default and allows VPN clients to avoid DNS leaks by sending all DNS requests to wg-access-server itself. |
43-
| `WG_DNS_UPSTREAM` | `--dns-upstream` | `dns.upstream` | | _resolvconf autodetection or Cloudflare DNS_ | The upstream DNS server to proxy DNS requests to. By default the host machine's resolveconf configuration is used to find it's upstream DNS server, with a fallback to Cloudflare. |
52+
| `WG_DNS_UPSTREAM` | `--dns-upstream` | `dns.upstream` | | _resolvconf autodetection or Cloudflare DNS_ | The upstream DNS servers to proxy DNS requests to. By default the host machine's resolveconf configuration is used to find its upstream DNS server, with a fallback to Cloudflare. |
4453
| `WG_DNS_DOMAIN` | `--dns-domain` | `dns.domain` | | | A domain to serve configured devices authoritatively. Queries for names in the format <device>.<user>.<domain> will be answered with the device's IP addresses. |
4554

4655
## The Config File (config.yaml)

0 commit comments

Comments
 (0)