From fc56f8770923ec1997444a8318a18be0a8397520 Mon Sep 17 00:00:00 2001 From: Jeroen Wijenbergh Date: Mon, 21 Mar 2022 14:58:58 +0100 Subject: Wireguard: Add basic support --- src/wireguard.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/wireguard.go (limited to 'src/wireguard.go') diff --git a/src/wireguard.go b/src/wireguard.go new file mode 100644 index 0000000..9441c51 --- /dev/null +++ b/src/wireguard.go @@ -0,0 +1,26 @@ +package eduvpn + +import ( + "fmt" + "golang.zx2c4.com/wireguard/wgctrl/wgtypes" + "regexp" +) + +func WireguardGenerateKey() (wgtypes.Key, error) { + key, error := wgtypes.GeneratePrivateKey() + return key, error +} + +// FIXME: Instead of doing a regex replace, decide if we should use a parser +func WireguardConfigAddKey(config string, key wgtypes.Key) string { + interface_section := "[Interface]" + interface_section_escaped := regexp.QuoteMeta(interface_section) + + // (?m) enables multi line mode + // ^ match from beginning of line + // $ match till end of line + // So it matches [Interface] section exactly + interface_re := regexp.MustCompile(fmt.Sprintf("(?m)^%s$", interface_section_escaped)) + to_replace := fmt.Sprintf("%s\nPrivateKey = %s", interface_section, key.String()) + return interface_re.ReplaceAllString(config, to_replace) +} -- cgit v1.2.3