mirror of
https://github.com/h44z/wg-portal
synced 2025-02-26 05:49:14 +00:00
Initial alpha codebase for version 2 of WireGuard Portal. This version is considered unstable and incomplete (for example, no public REST API)! Use with care! Fixes/Implements the following issues: - OAuth support #154, #1 - New Web UI with internationalisation support #98, #107, #89, #62 - Postgres Support #49 - Improved Email handling #47, #119 - DNS Search Domain support #46 - Bugfixes #94, #48 --------- Co-authored-by: Fabian Wechselberger <wechselbergerf@hotmail.com>
69 lines
1.5 KiB
Go
69 lines
1.5 KiB
Go
package configfile
|
|
|
|
import (
|
|
"bytes"
|
|
"embed"
|
|
"fmt"
|
|
"io"
|
|
"text/template"
|
|
|
|
"github.com/h44z/wg-portal/internal/domain"
|
|
)
|
|
|
|
//go:embed tpl_files/*
|
|
var TemplateFiles embed.FS
|
|
|
|
type TemplateHandler struct {
|
|
templates *template.Template
|
|
}
|
|
|
|
func newTemplateHandler() (*TemplateHandler, error) {
|
|
tplFuncs := template.FuncMap{
|
|
"CidrsToString": domain.CidrsToString,
|
|
}
|
|
|
|
templateCache, err := template.New("WireGuard").Funcs(tplFuncs).ParseFS(TemplateFiles, "tpl_files/*.tpl")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
handler := &TemplateHandler{
|
|
templates: templateCache,
|
|
}
|
|
|
|
return handler, nil
|
|
}
|
|
|
|
func (c TemplateHandler) GetInterfaceConfig(cfg *domain.Interface, peers []domain.Peer) (io.Reader, error) {
|
|
var tplBuff bytes.Buffer
|
|
|
|
err := c.templates.ExecuteTemplate(&tplBuff, "wg_interface.tpl", map[string]interface{}{
|
|
"Interface": cfg,
|
|
"Peers": peers,
|
|
"Portal": map[string]interface{}{
|
|
"Version": "unknown",
|
|
},
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to execute interface template for %s: %w", cfg.Identifier, err)
|
|
}
|
|
|
|
return &tplBuff, nil
|
|
}
|
|
|
|
func (c TemplateHandler) GetPeerConfig(peer *domain.Peer) (io.Reader, error) {
|
|
var tplBuff bytes.Buffer
|
|
|
|
err := c.templates.ExecuteTemplate(&tplBuff, "wg_peer.tpl", map[string]interface{}{
|
|
"Peer": peer,
|
|
"Portal": map[string]interface{}{
|
|
"Version": "unknown",
|
|
},
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to execute peer template for %s: %w", peer.Identifier, err)
|
|
}
|
|
|
|
return &tplBuff, nil
|
|
}
|