From f57fa3473dd9cbdcab57247e378d976cc68cf6a5 Mon Sep 17 00:00:00 2001 From: Callan Bryant Date: Mon, 26 Oct 2020 17:58:15 +0000 Subject: [PATCH] fix lookup of v6: do not require --- add.go | 21 ++++++++++++++++----- configtypes.go | 8 ++++---- init.go | 19 ++++++++++--------- 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/add.go b/add.go index eca1885..736fb9b 100644 --- a/add.go +++ b/add.go @@ -21,9 +21,15 @@ PublicKey={{ .DsnetConfig.PrivateKey.PublicKey.Key }} PresharedKey={{ .Peer.PresharedKey.Key }} Endpoint={{ .DsnetConfig.ExternalIP }}:{{ .DsnetConfig.ListenPort }} PersistentKeepalive={{ .Keepalive }} -{{ range .AllowedIPs -}} +{{ with .DsnetConfig.Network -}} AllowedIPs={{ . }} -{{ end }} +{{ end -}} +{{ with .DsnetConfig.Network6 -}} +AllowedIPs={{ . }} +{{ end -}} +{{ range .DsnetConfig.Networks -}} +AllowedIPs={{ . }} +{{ end -}} ` // TODO use random wg0-wg999 to hopefully avoid conflict by default? @@ -39,9 +45,15 @@ set interfaces wireguard wg0 description {{ conf.InterfaceName }} set interfaces wireguard wg0 peer {{ .DsnetConfig.PrivateKey.PublicKey.Key }} endpoint {{ .DsnetConfig.ExternalIP }}:{{ .DsnetConfig.ListenPort }} set interfaces wireguard wg0 peer {{ .DsnetConfig.PrivateKey.PublicKey.Key }} persistent-keepalive {{ .Keepalive }} set interfaces wireguard wg0 peer {{ .DsnetConfig.PrivateKey.PublicKey.Key }} preshared-key {{ .Peer.PresharedKey.Key }} -{{ range .AllowedIPs -}} +{{ with .DsnetConfig.Network -}} set interfaces wireguard wg0 peer {{ .DsnetConfig.PrivateKey.PublicKey.Key }} allowed-ips {{ . }} -{{ end }} +{{ end -}} +{{ with .DsnetConfig.Network6 -}} +set interfaces wireguard wg0 peer {{ .DsnetConfig.PrivateKey.PublicKey.Key }} allowed-ips {{ . }} +{{ end -}} +{{ range .DsnetConfig.Networks -}} +set interfaces wireguard wg0 peer {{ .DsnetConfig.PrivateKey.PublicKey.Key }} allowed-ips {{ . }} +{{ end -}} commit; save ` @@ -122,7 +134,6 @@ func PrintPeerCfg(peer PeerConfig, conf *DsnetConfig) { "Peer": peer, "DsnetConfig": conf, "Keepalive": time.Duration(KEEPALIVE).Seconds(), - "AllowedIPs": allowedIPs, "Cidrmask": cidrmask, "Address": net.IPNet{ IP: peer.IP, diff --git a/configtypes.go b/configtypes.go index ae8549a..4aedf0d 100644 --- a/configtypes.go +++ b/configtypes.go @@ -21,8 +21,8 @@ type PeerConfig struct { // Description of what the host is and/or does Description string `validate:"required,gte=1,lte=255"` // Internal VPN IP address. Added to AllowedIPs in server config as a /32 - IP net.IP `validate:"required` - IP6 net.IP `validate:"required` + IP net.IP + IP6 net.IP Added time.Time `validate:"required"` // TODO ExternalIP support (Endpoint) //ExternalIP net.UDPAddr `validate:"required,udp4_addr"` @@ -45,8 +45,8 @@ type DsnetConfig struct { // Network is chosen randomly when not specified Network JSONIPNet `validate:"required"` Network6 JSONIPNet `validate:"required"` - IP net.IP `validate:"required"` - IP6 net.IP `validate:"required"` + IP net.IP + IP6 net.IP DNS net.IP // extra networks available, will be added to AllowedIPs Networks []JSONIPNet `validate:"required"` diff --git a/init.go b/init.go index dd368e4..328f783 100644 --- a/init.go +++ b/init.go @@ -77,7 +77,7 @@ func getExternalIP() net.IP { // arbitrary external IP is used (one that's guaranteed to route outside. // In this case, Google's DNS server. Doesn't actually need to be online.) conn, err := net.Dial("udp", "8.8.8.8:53") - if err != nil { + if err == nil { defer conn.Close() localAddr := conn.LocalAddr().String() @@ -111,7 +111,7 @@ func getExternalIP() net.IP { func getExternalIP6() net.IP { var IP net.IP conn, err := net.Dial("udp", "2001:4860:4860::8888:53") - if err != nil { + if err == nil { defer conn.Close() localAddr := conn.LocalAddr().String() @@ -123,14 +123,15 @@ func getExternalIP6() net.IP { Timeout: 5 * time.Second, } resp, err := client.Get("https://ipv6.icanhazip.com/") - check(err) - defer resp.Body.Close() + if err == nil { + defer resp.Body.Close() - if resp.StatusCode == http.StatusOK { - body, err := ioutil.ReadAll(resp.Body) - check(err) - IP = net.ParseIP(strings.TrimSpace(string(body))) - return IP + if resp.StatusCode == http.StatusOK { + body, err := ioutil.ReadAll(resp.Body) + check(err) + IP = net.ParseIP(strings.TrimSpace(string(body))) + return IP + } } return net.IP{}