-
Notifications
You must be signed in to change notification settings - Fork 2
/
template.go
112 lines (101 loc) · 3.21 KB
/
template.go
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"bytes"
"strings"
"text/template"
model "github.com/nettica-com/nettica-admin/model"
)
var (
clientTpl = `[Interface]
Address = {{ StringsJoin .Host.Current.Address ", " }}
PrivateKey = {{ .Host.Current.PrivateKey }}
{{ if ne (len .Server.Dns) 0 -}}
DNS = {{ StringsJoin .Server.Dns ", " }}
{{- end }}
{{ if ne .Server.Mtu 0 -}}
MTU = {{.Server.Mtu}}
{{- end}}
[Peer]
PublicKey = {{ .Host.Current.PublicKey }}
PresharedKey = {{ .Host.Current.PresharedKey }}
AllowedIPs = {{ StringsJoin .Host.Current.AllowedIPs ", " }}
Endpoint = {{ .Server.Endpoint }}
PersistentKeepalive = {{.Host.Current.PersistentKeepalive}}
`
wireguardTemplate = `{{ if .Vpn.Enable }}
# {{.Vpn.Name }}
[Interface]
{{- range .Vpn.Current.Address }}
Address = {{ . }}
{{- end }}
PrivateKey = {{ .Vpn.Current.PrivateKey }}
{{ $server := .Vpn.Current.Endpoint -}}{{ $service := .Vpn.Type -}}
{{ if ne .Vpn.Current.ListenPort 0 -}}ListenPort = {{ .Vpn.Current.ListenPort }}{{- end}}
{{ if .Vpn.Current.Dns }}DNS = {{ StringsJoin .Vpn.Current.Dns ", " }}{{ end }}
{{ if .Vpn.Current.Table }}Table = {{ .Vpn.Current.Table }}{{- end}}
{{ if ne .Vpn.Current.Mtu 0 -}}MTU = {{.Vpn.Current.Mtu}}{{- end}}
{{ if .Vpn.Current.PreUp -}}PreUp = {{ .Vpn.Current.PreUp }}{{- end}}
{{ if .Vpn.Current.PostUp -}}PostUp = {{ .Vpn.Current.PostUp }}{{- end}}
{{ if .Vpn.Current.PreDown -}}PreDown = {{ .Vpn.Current.PreDown }}{{- end}}
{{ if .Vpn.Current.PostDown -}}PostDown = {{ .Vpn.Current.PostDown }}{{- end}}
{{ range .VPNs -}}
{{ if or .Enable $service -}}
{{ if $server }}
# {{.Name}}
[Peer]
PublicKey = {{ .Current.PublicKey }}
PresharedKey = {{ .Current.PresharedKey }}
AllowedIPs = {{ StringsJoin .Current.AllowedIPs ", " }}
{{ if .Current.Endpoint -}}Endpoint = {{ .Current.Endpoint }} {{- end }}
{{ if .Current.PersistentKeepalive -}}PersistentKeepalive = {{ .Current.PersistentKeepalive }}{{ end }}
{{ else -}}
{{ if .Current.Endpoint -}}
# {{.Name}}
[Peer]
PublicKey = {{ .Current.PublicKey }}
PresharedKey = {{ .Current.PresharedKey }}
AllowedIPs = {{ StringsJoin .Current.AllowedIPs ", " }}
{{ if .Current.Endpoint -}}Endpoint = {{ .Current.Endpoint }} {{- end }}
{{ if .Current.PersistentKeepalive }}PersistentKeepalive = {{ .Current.PersistentKeepalive }}{{ end }}
{{ end }}
{{ end -}}
{{ end -}}
{{ end -}}
{{ end }}`
)
// DumpWireguardConfig using go template
func DumpWireguardConfig(vpn *model.VPN, VPNs *[]model.VPN) ([]byte, error) {
t, err := template.New("wireguard").Funcs(template.FuncMap{"StringsJoin": strings.Join}).Parse(wireguardTemplate)
if err != nil {
return nil, err
}
return dump(t, struct {
Vpn *model.VPN
VPNs *[]model.VPN
}{
Vpn: vpn,
VPNs: VPNs,
})
}
// DumpClientWg dump client wg config with go template
func DumpClientWg(vpn *model.VPN, server *model.Server) ([]byte, error) {
t, err := template.New("client").Funcs(template.FuncMap{"StringsJoin": strings.Join}).Parse(clientTpl)
if err != nil {
return nil, err
}
return dump(t, struct {
Vpn *model.VPN
Server *model.Server
}{
Vpn: vpn,
Server: server,
})
}
func dump(tpl *template.Template, data interface{}) ([]byte, error) {
var tplBuff bytes.Buffer
err := tpl.Execute(&tplBuff, data)
if err != nil {
return nil, err
}
return tplBuff.Bytes(), nil
}