package template
import (
"bytes"
"gitlab.127-0-0-1.fr/vx3r/wg-gen-web/model"
"gitlab.127-0-0-1.fr/vx3r/wg-gen-web/util"
"os"
"path/filepath"
"strings"
"text/template"
)
var (
emailTpl = `
Email Template
Hello
Vous avez probablement demandé une configuration VPN. Voici la configuration {{.Client.Name}} créée le {{.Client.Created.Format "02/01/2006 15:4"}} . Scannez le Qrcode ou ouvrez le fichier de configuration joint dans le client VPN.
A propos de WireGuard
WireGuard est un VPN extrêmement simple, rapide et moderne qui utilise un chiffrement de pointe. Il vise à être plus rapide, plus simple, plus léger et plus utile tout en évitant le casse-tête. Il est beaucoup plus performant comparé à OpenVPN.
`
clientTpl = `[Interface]
Address = {{ StringsJoin .Client.Address ", " }}
PrivateKey = {{ .Client.PrivateKey }}
{{ if ne (len .Server.Dns) 0 -}}
DNS = {{ StringsJoin .Server.Dns ", " }}
{{- end }}
{{ if ne .Server.Mtu 0 -}}
MTU = {{.Server.Mtu}}
{{- end}}
[Peer]
PublicKey = {{ .Server.PublicKey }}
PresharedKey = {{ .Client.PresharedKey }}
AllowedIPs = {{ StringsJoin .Client.AllowedIPs ", " }}
Endpoint = {{ .Server.Endpoint }}
{{ if and (ne .Server.PersistentKeepalive 0) (not .Client.IgnorePersistentKeepalive) -}}
PersistentKeepalive = {{.Server.PersistentKeepalive}}
{{- end}}
`
wgTpl = `# Updated: {{ .Server.Updated }} / Created: {{ .Server.Created }}
[Interface]
{{- range .Server.Address }}
Address = {{ . }}
{{- end }}
ListenPort = {{ .Server.ListenPort }}
PrivateKey = {{ .Server.PrivateKey }}
{{ if ne .Server.Mtu 0 -}}
MTU = {{.Server.Mtu}}
{{- end}}
PreUp = {{ .Server.PreUp }}
PostUp = {{ .Server.PostUp }}
PreDown = {{ .Server.PreDown }}
PostDown = {{ .Server.PostDown }}
{{- range .Clients }}
{{ if .Enable -}}
# {{.Name}} / {{.Email}} / Updated: {{.Updated}} / Created: {{.Created}}
[Peer]
PublicKey = {{ .PublicKey }}
PresharedKey = {{ .PresharedKey }}
AllowedIPs = {{ StringsJoin .Address ", " }}
{{- end }}
{{ end }}`
)
// DumpClientWg dump client wg config with go template
func DumpClientWg(client *model.Client, 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 {
Client *model.Client
Server *model.Server
}{
Client: client,
Server: server,
})
}
// DumpServerWg dump server wg config with go template, write it to file and return bytes
func DumpServerWg(clients []*model.Client, server *model.Server) ([]byte, error) {
t, err := template.New("server").Funcs(template.FuncMap{"StringsJoin": strings.Join}).Parse(wgTpl)
if err != nil {
return nil, err
}
configDataWg, err := dump(t, struct {
Clients []*model.Client
Server *model.Server
}{
Clients: clients,
Server: server,
})
if err != nil {
return nil, err
}
err = util.WriteFile(filepath.Join(os.Getenv("WG_CONF_DIR"), os.Getenv("WG_INTERFACE_NAME")), configDataWg)
if err != nil {
return nil, err
}
return configDataWg, nil
}
// DumpEmail dump server wg config with go template
func DumpEmail(client *model.Client, qrcodePngName string) ([]byte, error) {
t, err := template.New("email").Parse(emailTpl)
if err != nil {
return nil, err
}
return dump(t, struct {
Client *model.Client
QrcodePngName string
}{
Client: client,
QrcodePngName: qrcodePngName,
})
}
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
}