IPv6 config to template

This commit is contained in:
Callan Bryant 2020-10-25 22:55:18 +00:00
parent f7f06f00fa
commit e7fb9dc5ce
No known key found for this signature in database
GPG Key ID: C31FA9DF3ACBFFAA

17
add.go
View File

@ -3,13 +3,13 @@ package dsnet
import ( import (
"fmt" "fmt"
"os" "os"
"strings"
"text/template" "text/template"
"time" "time"
) )
const wgQuickPeerConf = `[Interface] const wgQuickPeerConf = `[Interface]
Address={{ .Peer.IP }}/22 Address={{ .Peer.IP }}/22
Address={{ .Peer.IP6 }}/64
PrivateKey={{ .Peer.PrivateKey.Key }} PrivateKey={{ .Peer.PrivateKey.Key }}
{{- if .DsnetConfig.DNS }} {{- if .DsnetConfig.DNS }}
DNS={{ .DsnetConfig.DNS }} DNS={{ .DsnetConfig.DNS }}
@ -19,8 +19,10 @@ DNS = {{ .DsnetConfig.DNS }}
PublicKey={{ .DsnetConfig.PrivateKey.PublicKey.Key }} PublicKey={{ .DsnetConfig.PrivateKey.PublicKey.Key }}
PresharedKey={{ .Peer.PresharedKey.Key }} PresharedKey={{ .Peer.PresharedKey.Key }}
Endpoint={{ .DsnetConfig.ExternalIP }}:{{ .DsnetConfig.ListenPort }} Endpoint={{ .DsnetConfig.ExternalIP }}:{{ .DsnetConfig.ListenPort }}
AllowedIPs={{ .AllowedIPs }}
PersistentKeepalive={{ .Keepalive }} PersistentKeepalive={{ .Keepalive }}
{{ range .AllowedIPs -}}
AllowedIPs={{ . }}
{{ end }}
` `
// TODO use random wg0-wg999 to hopefully avoid conflict by default? // TODO use random wg0-wg999 to hopefully avoid conflict by default?
@ -34,9 +36,11 @@ set interfaces wireguard wg0 description {{ conf.InterfaceName }}
{{ end }} {{ end }}
set interfaces wireguard wg0 peer {{ .DsnetConfig.PrivateKey.PublicKey.Key }} endpoint {{ .DsnetConfig.ExternalIP }}:{{ .DsnetConfig.ListenPort }} set interfaces wireguard wg0 peer {{ .DsnetConfig.PrivateKey.PublicKey.Key }} endpoint {{ .DsnetConfig.ExternalIP }}:{{ .DsnetConfig.ListenPort }}
set interfaces wireguard wg0 peer {{ .DsnetConfig.PrivateKey.PublicKey.Key }} allowed-ips {{ .AllowedIPs }}
set interfaces wireguard wg0 peer {{ .DsnetConfig.PrivateKey.PublicKey.Key }} persistent-keepalive {{ .Keepalive }} 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 }} set interfaces wireguard wg0 peer {{ .DsnetConfig.PrivateKey.PublicKey.Key }} preshared-key {{ .Peer.PresharedKey.Key }}
{{ range .AllowedIPs -}}
set interfaces wireguard wg0 peer {{ .DsnetConfig.PrivateKey.PublicKey.Key }} allowed-ips {{ . }}
{{ end }}
commit; save commit; save
` `
@ -92,11 +96,12 @@ func Add() {
} }
func PrintPeerCfg(peer PeerConfig, conf *DsnetConfig) { func PrintPeerCfg(peer PeerConfig, conf *DsnetConfig) {
allowedIPsStr := make([]string, len(conf.Networks)+1) allowedIPsStr := make([]string, len(conf.Networks)+2)
allowedIPsStr[0] = conf.Network.String() allowedIPsStr[0] = conf.Network.String()
allowedIPsStr[1] = conf.Network6.String()
for i, net := range conf.Networks { for i, net := range conf.Networks {
allowedIPsStr[i+1] = net.String() allowedIPsStr[i+2] = net.String()
} }
var peerConf string var peerConf string
@ -119,7 +124,7 @@ func PrintPeerCfg(peer PeerConfig, conf *DsnetConfig) {
"Peer": peer, "Peer": peer,
"DsnetConfig": conf, "DsnetConfig": conf,
"Keepalive": time.Duration(KEEPALIVE).Seconds(), "Keepalive": time.Duration(KEEPALIVE).Seconds(),
"AllowedIPs": strings.Join(allowedIPsStr, ","), "AllowedIPs": allowedIPsStr,
"Cidrmask": cidrmask, "Cidrmask": cidrmask,
}) })
check(err) check(err)