unix.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package wireguard
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. "regexp"
  7. "github.com/gravitl/netmaker/models"
  8. "github.com/gravitl/netmaker/netclient/config"
  9. "github.com/gravitl/netmaker/netclient/ncutils"
  10. "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
  11. )
  12. // SetWGKeyConfig - sets the wg conf with a new private key
  13. func SetWGKeyConfig(network string, serveraddr string) error {
  14. cfg, err := config.ReadConfig(network)
  15. if err != nil {
  16. return err
  17. }
  18. node := cfg.Node
  19. privatekey, err := wgtypes.GeneratePrivateKey()
  20. if err != nil {
  21. return err
  22. }
  23. privkeystring := privatekey.String()
  24. publickey := privatekey.PublicKey()
  25. node.PublicKey = publickey.String()
  26. err = StorePrivKey(privkeystring, network)
  27. if err != nil {
  28. return err
  29. }
  30. if node.Action == models.NODE_UPDATE_KEY {
  31. node.Action = models.NODE_NOOP
  32. }
  33. err = config.ModConfig(&node)
  34. if err != nil {
  35. return err
  36. }
  37. err = SetWGConfig(network, false)
  38. if err != nil {
  39. return err
  40. }
  41. return err
  42. }
  43. // ApplyWGQuickConf - applies wg-quick commands if os supports
  44. func ApplyWGQuickConf(confPath string) error {
  45. _, _ = ncutils.RunCmd("wg-quick down "+confPath, false)
  46. _, err := ncutils.RunCmd("wg-quick up "+confPath, false)
  47. return err
  48. }
  49. // SyncWGQuickConf - formats config file and runs sync command
  50. func SyncWGQuickConf(iface string, confPath string) error {
  51. var tmpConf = confPath + ".sync.tmp"
  52. confRaw, err := ncutils.RunCmd("wg-quick strip "+confPath, false)
  53. if err != nil {
  54. return err
  55. }
  56. regex := regexp.MustCompile(".*Warning.*\n")
  57. conf := regex.ReplaceAllString(confRaw, "")
  58. err = os.WriteFile(tmpConf, []byte(conf), 0644)
  59. if err != nil {
  60. return err
  61. }
  62. _, err = ncutils.RunCmd("wg syncconf "+iface+" "+tmpConf, true)
  63. if err != nil {
  64. log.Println(err.Error())
  65. ncutils.Log("error syncing conf, resetting")
  66. err = ApplyWGQuickConf(confPath)
  67. }
  68. errN := os.Remove(tmpConf)
  69. if errN != nil {
  70. ncutils.Log(errN.Error())
  71. }
  72. return err
  73. }
  74. // RemoveWGQuickConf - calls wg-quick down
  75. func RemoveWGQuickConf(confPath string, printlog bool) error {
  76. _, err := ncutils.RunCmd(fmt.Sprintf("wg-quick down %s", confPath), printlog)
  77. return err
  78. }
  79. // StorePrivKey - stores wg priv key on disk locally
  80. func StorePrivKey(key string, network string) error {
  81. var err error
  82. d1 := []byte(key)
  83. err = os.WriteFile(ncutils.GetNetclientPathSpecific()+"wgkey-"+network, d1, 0644)
  84. return err
  85. }
  86. // RetrievePrivKey - reads wg priv key from local disk
  87. func RetrievePrivKey(network string) (string, error) {
  88. dat, err := os.ReadFile(ncutils.GetNetclientPathSpecific() + "wgkey-" + network)
  89. return string(dat), err
  90. }