wgcentos.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package wg
  2. import (
  3. "bytes"
  4. _ "embed"
  5. "fmt"
  6. "log"
  7. "text/template"
  8. )
  9. //go:embed wireguard.conf.templ
  10. var confTmpl string
  11. type WireguardTemplateSeed struct {
  12. VpnClientPrivateKey string
  13. VpnClientAddress string
  14. Peers []WireguardTemplatePeer
  15. }
  16. type WireguardTemplatePeer struct {
  17. Pubkey string
  18. Address string
  19. Port int
  20. }
  21. /*
  22. Render out a client configuration file, utilizing the data provided from Semaphore and the daemon keyring
  23. :param tmpl: a template.Template that will be populated with the VPN data
  24. :param wgData: a WireGuardTemplateSeed struct that contains all the info needed to populate a wireguard config file
  25. */
  26. func RenderClientConfiguration(wgData WireguardTemplateSeed) ([]byte, error) {
  27. buff := bytes.NewBuffer([]byte{})
  28. tmpl, err := template.New("wireguard.conf.templ").Parse(confTmpl)
  29. if err != nil {
  30. log.Fatal(err)
  31. }
  32. err = tmpl.Execute(buff, wgData)
  33. if err != nil {
  34. return buff.Bytes(), &TemplatingError{TemplateData: wgData, Msg: err.Error()}
  35. }
  36. return buff.Bytes(), nil
  37. }
  38. type TemplatingError struct {
  39. TemplateData WireguardTemplateSeed
  40. Msg string
  41. }
  42. func (t *TemplatingError) Error() string {
  43. return fmt.Sprintf("There was an error executing the template file: %s, Seed data: %+v\n", t.Msg, t.TemplateData)
  44. }