wireguard.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package config
  2. import (
  3. "encoding/json"
  4. "goseg/defaults"
  5. "goseg/structs"
  6. "os"
  7. "path/filepath"
  8. )
  9. // write a hardcoded default conf to disk
  10. func CreateDefaultWGConf() error {
  11. defaultConfig := defaults.WgConfig
  12. path := filepath.Join(BasePath, "settings", "wireguard.json")
  13. if err := os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil {
  14. return err
  15. }
  16. file, err := os.Create(path)
  17. if err != nil {
  18. return err
  19. }
  20. defer file.Close()
  21. encoder := json.NewEncoder(file)
  22. encoder.SetIndent("", " ")
  23. if err := encoder.Encode(&defaultConfig); err != nil {
  24. return err
  25. }
  26. return nil
  27. }
  28. // write a conf to disk from version server info
  29. func UpdateWGConf() error {
  30. conf := Conf()
  31. releaseChannel := conf.UpdateBranch
  32. wgRepo := VersionInfo.Wireguard.Repo
  33. amdHash := VersionInfo.Wireguard.Amd64Sha256
  34. armHash := VersionInfo.Wireguard.Arm64Sha256
  35. newConfig := structs.WgConfig{
  36. WireguardName: "wireguard",
  37. WireguardVersion: releaseChannel,
  38. Repo: wgRepo,
  39. Amd64Sha256: amdHash,
  40. Arm64Sha256: armHash,
  41. CapAdd: []string{"NET_ADMIN", "SYS_MODULE"},
  42. Volumes: []string{"/lib/modules:/lib/modules"},
  43. Sysctls: struct {
  44. NetIpv4ConfAllSrcValidMark int `json:"net.ipv4.conf.all.src_valid_mark"`
  45. }{
  46. NetIpv4ConfAllSrcValidMark: 1,
  47. },
  48. }
  49. path := filepath.Join(BasePath, "settings", "wireguard.json")
  50. if err := os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil {
  51. return err
  52. }
  53. file, err := os.Create(path)
  54. if err != nil {
  55. return err
  56. }
  57. defer file.Close()
  58. encoder := json.NewEncoder(file)
  59. encoder.SetIndent("", " ")
  60. if err := encoder.Encode(&newConfig); err != nil {
  61. return err
  62. }
  63. return nil
  64. }