summaryrefslogtreecommitdiff
path: root/src/wireguard.go
blob: 9441c51babca27dee32c78876502d3bfcce7c59c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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)
}