package util import ( "bytes" "gitlab.127-0-0-1.fr/vx3r/wg-gen-web/model" "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 = {{.Client.Address}} PrivateKey = {{.Client.PrivateKey}} DNS = {{.Server.Dns}} [Peer] PublicKey = {{.Server.PublicKey}} PresharedKey = {{.Server.PresharedKey}} AllowedIPs = {{.Client.AllowedIPs}} Endpoint = {{.Server.Endpoint}} PersistentKeepalive = {{.Server.PersistentKeepalive}}` wgTpl = ` # {{.Server.Name}} / Updated: {{.Server.Updated}} / Created: {{.Server.Created}} [Interface] {{range .ServerAdresses}} Address = {{.}} {{end}} ListenPort = {{.Server.ListenPort}} PrivateKey = {{.Server.PrivateKey}} {{$server := .Server}} {{range .Clients}} {{if .Enable}} # {{.Name}} / {{.Email}} / Updated: {{.Updated}} / Created: {{.Created}} [Peer] PublicKey = {{.PublicKey}} PresharedKey = {{$server.PresharedKey}} AllowedIPs = {{.Address}} {{end}} {{end}}` ) // DumpClient dump client wg config with go template func DumpClient(client *model.Client, server *model.Server) (bytes.Buffer, error) { var tplBuff bytes.Buffer t, err := template.New("client").Parse(clientTpl) if err != nil { return tplBuff, err } return dump(t, struct { Client *model.Client Server *model.Server }{ Client: client, Server: server, }) } // DumpServerWg dump server wg config with go template func DumpServerWg(clients []*model.Client, server *model.Server) (bytes.Buffer, error) { var tplBuff bytes.Buffer t, err := template.New("server").Parse(wgTpl) if err != nil { return tplBuff, err } return dump(t, struct { Clients []*model.Client Server *model.Server ServerAdresses []string }{ ServerAdresses: strings.Split(server.Address, ","), Clients: clients, Server: server, }) } // DumpEmail dump server wg config with go template func DumpEmail(client *model.Client, qrcodePngName string) (bytes.Buffer, error) { var tplBuff bytes.Buffer t, err := template.New("email").Parse(emailTpl) if err != nil { return tplBuff, err } return dump(t, struct { Client *model.Client QrcodePngName string }{ Client: client, QrcodePngName: qrcodePngName, }) } func dump(tpl *template.Template, data interface{}) (bytes.Buffer, error) { var tplBuff bytes.Buffer err := tpl.Execute(&tplBuff, data) if err != nil { return tplBuff, err } return tplBuff, nil }