From d6998629627ca0ccb0bea25f6a7de59d2ab8cafc Mon Sep 17 00:00:00 2001 From: axelsimon Date: Fri, 27 Nov 2020 20:21:29 +0000 Subject: [PATCH] =?UTF-8?q?Add=20a=20NixOS=C2=A0peer=20config=20template?= =?UTF-8?q?=20/=20generator.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit First attempt at Go, hope all is good. --- add.go | 41 +++++++++++++++++++++++++++++++++++++++++ cmd/dsnet.go | 1 + 2 files changed, 42 insertions(+) diff --git a/add.go b/add.go index ac39f96..87d6ca3 100644 --- a/add.go +++ b/add.go @@ -65,6 +65,44 @@ set interfaces wireguard {{ .Wgif }} peer {{ .DsnetConfig.PrivateKey.PublicKey.K commit; save ` +const nixosPeerConf = `networking.wireguard.interfaces = {{ "{" }} + {{ .Wgif }} = {{ "{" }} + ips = [ + {{ if gt (.DsnetConfig.Network.IPNet.IP | len) 0 -}} + "{{ .Peer.IP }}/{{ .CidrSize }}" + {{ end -}} + {{ if gt (.DsnetConfig.Network6.IPNet.IP | len) 0 -}} + "{{ .Peer.IP6 }}/{{ .CidrSize6 }}" + {{ end -}} + ]; + privateKey = "{{ .Peer.PrivateKey.Key }}"; + {{- if .DsnetConfig.DNS }} + dns = [ "{{ .DsnetConfig.DNS }}" ]; + {{ end }} + peers= [ + {{ "{" }} + publicKey = "{{ .DsnetConfig.PrivateKey.PublicKey.Key }}"; + presharedKey = "{{ .Peer.PresharedKey.Key }}"; + allowedIPs = [ + {{ if gt (.DsnetConfig.Network.IPNet.IP | len) 0 -}} + "{{ .DsnetConfig.Network }}" + {{ end -}} + {{ if gt (.DsnetConfig.Network6.IPNet.IP | len) 0 -}} + "{{ .DsnetConfig.Network6 }}" + {{ end -}} + ]; + {{ if gt (.DsnetConfig.ExternalIP | len) 0 -}} + endpoint = "{{ .DsnetConfig.ExternalIP }}:{{ .DsnetConfig.ListenPort }}"; + {{ else -}} + endpoint = "{{ .DsnetConfig.ExternalIP6 }}:{{ .DsnetConfig.ListenPort }}"; + {{ end -}} + persistentKeepalive = {{ .Keepalive }}; + {{ "}" }} + ]; + {{ "};" }} +{{ "};" }} +` + func Add() { if len(os.Args) != 3 { // TODO non-red @@ -126,6 +164,9 @@ func PrintPeerCfg(peer PeerConfig, conf *DsnetConfig) { // https://github.com/WireGuard/wireguard-vyatta-ubnt/ case "vyatta": peerConf = vyattaPeerConf + // https://nixos.wiki/wiki/Wireguard + case "nixos": + peerConf = nixosPeerConf default: ExitFail("Unrecognised DSNET_OUTPUT type") } diff --git a/cmd/dsnet.go b/cmd/dsnet.go index 48b97c4..a593dfb 100644 --- a/cmd/dsnet.go +++ b/cmd/dsnet.go @@ -62,6 +62,7 @@ Environment variables: DSNET_OUTPUT=wg-quick : The add command will generate a wg-quick config (default) DSNET_OUTPUT=vyatta : The add command will generate a vyatta (edgeos) config + DSNET_OUTPUT=nixos : The add command will generate a NixOS config dsnet version %[2]s commit %[3]s