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
You probably requested VPN configuration. Here is {{.Client.Name}} configuration created {{.Client.Created.Format "Jan 02, 2006 15:04:05 UTC"}}. Scan the Qrcode or open attached configuration file in VPN client.
About WireGuard
WireGuard is an extremely simple yet fast and modern VPN that utilizes state-of-the-art cryptography. It aims to be faster, simpler, leaner, and more useful than IPsec, while avoiding the massive headache. It intends to be considerably more performant than OpenVPN.
Download WireGuard VPN Client
Wg Gen Web - Simple Web based configuration generator for WireGuard
More info on Github
` 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 = {{ .Server.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 }} {{ $server := .Server }} {{- range .Clients }} {{ if .Enable -}} # {{.Name}} / {{.Email}} / Updated: {{.Updated}} / Created: {{.Created}} [Peer] PublicKey = {{ .PublicKey }} PresharedKey = {{ $server.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 }