update & fix
This commit is contained in:
parent
a35edbde22
commit
adee6738a7
29
wg.go
29
wg.go
@ -3,13 +3,14 @@ package wgctl
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
"net"
|
||||||
|
"syscall"
|
||||||
|
"text/template"
|
||||||
|
|
||||||
"github.com/mdlayher/wireguardctrl"
|
"github.com/mdlayher/wireguardctrl"
|
||||||
"github.com/mdlayher/wireguardctrl/wgtypes"
|
"github.com/mdlayher/wireguardctrl/wgtypes"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/vishvananda/netlink"
|
"github.com/vishvananda/netlink"
|
||||||
"net"
|
|
||||||
"syscall"
|
|
||||||
"text/template"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
@ -48,6 +49,12 @@ func (cfg *Config) String() string {
|
|||||||
return string(b)
|
return string(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var cfgTemplate = template.Must(
|
||||||
|
template.
|
||||||
|
New("wg-cfg").
|
||||||
|
Funcs(template.FuncMap(map[string]interface{}{"wgKey": serializeKey})).
|
||||||
|
Parse(wgtypeTemplateSpec))
|
||||||
|
|
||||||
func (cfg *Config) MarshalText() (text []byte, err error) {
|
func (cfg *Config) MarshalText() (text []byte, err error) {
|
||||||
buff := &bytes.Buffer{}
|
buff := &bytes.Buffer{}
|
||||||
if err := cfgTemplate.Execute(buff, cfg); err != nil {
|
if err := cfgTemplate.Execute(buff, cfg); err != nil {
|
||||||
@ -57,12 +64,12 @@ func (cfg *Config) MarshalText() (text []byte, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const wgtypeTemplateSpec = `[Interface]
|
const wgtypeTemplateSpec = `[Interface]
|
||||||
{{- range := .Address }}
|
{{- range .Address }}
|
||||||
Address = {{ . }}
|
Address = {{ . }}
|
||||||
{{ end }}
|
{{- end }}
|
||||||
{{- range := .DNS }}
|
{{- range .DNS }}
|
||||||
DNS = {{ . }}
|
DNS = {{ . }}
|
||||||
{{ end }}
|
{{- end }}
|
||||||
PrivateKey = {{ .PrivateKey | wgKey }}
|
PrivateKey = {{ .PrivateKey | wgKey }}
|
||||||
{{- if .ListenPort }}{{ "\n" }}ListenPort = {{ .ListenPort }}{{ end }}
|
{{- if .ListenPort }}{{ "\n" }}ListenPort = {{ .ListenPort }}{{ end }}
|
||||||
{{- if .MTU }}{{ "\n" }}MTU = {{ .MTU }}{{ end }}
|
{{- if .MTU }}{{ "\n" }}MTU = {{ .MTU }}{{ end }}
|
||||||
@ -72,8 +79,8 @@ PrivateKey = {{ .PrivateKey | wgKey }}
|
|||||||
{{- if .PreDown }}{{ "\n" }}PreDown = {{ .PreDown }}{{ end }}
|
{{- if .PreDown }}{{ "\n" }}PreDown = {{ .PreDown }}{{ end }}
|
||||||
{{- if .PostDown }}{{ "\n" }}PostDown = {{ .PostDown }}{{ end }}
|
{{- if .PostDown }}{{ "\n" }}PostDown = {{ .PostDown }}{{ end }}
|
||||||
{{- if .SaveConfig }}{{ "\n" }}SaveConfig = {{ .SaveConfig }}{{ end }}
|
{{- if .SaveConfig }}{{ "\n" }}SaveConfig = {{ .SaveConfig }}{{ end }}
|
||||||
|
|
||||||
{{- range .Peers }}
|
{{- range .Peers }}
|
||||||
|
{{- "\n" }}
|
||||||
[Peer]
|
[Peer]
|
||||||
PublicKey = {{ .PublicKey | wgKey }}
|
PublicKey = {{ .PublicKey | wgKey }}
|
||||||
AllowedIps = {{ range $i, $el := .AllowedIPs }}{{if $i}}, {{ end }}{{ $el }}{{ end }}
|
AllowedIps = {{ range $i, $el := .AllowedIPs }}{{if $i}}, {{ end }}{{ $el }}{{ end }}
|
||||||
@ -96,12 +103,6 @@ func ParseKey(key string) (wgtypes.Key, error) {
|
|||||||
return pkey, nil
|
return pkey, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var cfgTemplate = template.Must(
|
|
||||||
template.
|
|
||||||
New("wg-cfg").
|
|
||||||
Funcs(template.FuncMap(map[string]interface{}{"wgKey": serializeKey})).
|
|
||||||
Parse(wgtypeTemplateSpec))
|
|
||||||
|
|
||||||
// Sync the config to the current setup for given interface
|
// Sync the config to the current setup for given interface
|
||||||
func (cfg *Config) Sync(iface string, logger logrus.FieldLogger) error {
|
func (cfg *Config) Sync(iface string, logger logrus.FieldLogger) error {
|
||||||
log := logger.WithField("iface", iface)
|
log := logger.WithField("iface", iface)
|
||||||
|
Loading…
Reference in New Issue
Block a user