Add a NixOS peer config template / generator.

First attempt at Go, hope all is good.
This commit is contained in:
axelsimon 2020-11-27 20:21:29 +00:00
parent 13c974a260
commit d699862962
2 changed files with 42 additions and 0 deletions

41
add.go
View File

@ -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")
}

View File

@ -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