unix.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package wireguard
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. "regexp"
  7. "github.com/gravitl/netmaker/logger"
  8. "github.com/gravitl/netmaker/models"
  9. "github.com/gravitl/netmaker/netclient/ncutils"
  10. )
  11. // ApplyWGQuickConf - applies wg-quick commands if os supports
  12. func ApplyWGQuickConf(confPath string, ifacename string) error {
  13. if ncutils.IsWindows() {
  14. return ApplyWindowsConf(confPath)
  15. } else {
  16. _, err := os.Stat(confPath)
  17. if err != nil {
  18. logger.Log(0, confPath+" does not exist "+err.Error())
  19. return err
  20. }
  21. if ncutils.IfaceExists(ifacename) {
  22. ncutils.RunCmd("wg-quick down "+confPath, true)
  23. }
  24. _, err = ncutils.RunCmd("wg-quick up "+confPath, true)
  25. return err
  26. }
  27. }
  28. // ApplyMacOSConf - applies system commands similar to wg-quick using golang for MacOS
  29. func ApplyMacOSConf(node *models.Node, ifacename string, confPath string) error {
  30. var err error
  31. _ = WgQuickDownMac(node, ifacename)
  32. err = WgQuickUpMac(node, ifacename, confPath)
  33. return err
  34. }
  35. // SyncWGQuickConf - formats config file and runs sync command
  36. func SyncWGQuickConf(iface string, confPath string) error {
  37. var tmpConf = confPath + ".sync.tmp"
  38. var confCmd = "wg-quick strip "
  39. if ncutils.IsMac() {
  40. confCmd = "grep -v -e Address -e MTU -e PostUp -e PostDown "
  41. }
  42. confRaw, err := ncutils.RunCmd(confCmd+confPath, false)
  43. if err != nil {
  44. return err
  45. }
  46. regex := regexp.MustCompile(".*Warning.*\n")
  47. conf := regex.ReplaceAllString(confRaw, "")
  48. err = os.WriteFile(tmpConf, []byte(conf), 0600)
  49. if err != nil {
  50. return err
  51. }
  52. _, err = ncutils.RunCmd("wg syncconf "+iface+" "+tmpConf, true)
  53. if err != nil {
  54. log.Println(err.Error())
  55. logger.Log(0, "error syncing conf, resetting")
  56. err = ApplyWGQuickConf(confPath, iface)
  57. }
  58. errN := os.Remove(tmpConf)
  59. if errN != nil {
  60. logger.Log(0, errN.Error())
  61. }
  62. return err
  63. }
  64. // RemoveWGQuickConf - calls wg-quick down
  65. func RemoveWGQuickConf(confPath string, printlog bool) error {
  66. _, err := ncutils.RunCmd(fmt.Sprintf("wg-quick down %s", confPath), printlog)
  67. return err
  68. }
  69. // StorePrivKey - stores wg priv key on disk locally
  70. func StorePrivKey(key string, network string) error {
  71. var err error
  72. d1 := []byte(key)
  73. err = os.WriteFile(ncutils.GetNetclientPathSpecific()+"wgkey-"+network, d1, 0600)
  74. return err
  75. }
  76. // RetrievePrivKey - reads wg priv key from local disk
  77. func RetrievePrivKey(network string) (string, error) {
  78. dat, err := ncutils.GetFileWithRetry(ncutils.GetNetclientPathSpecific()+"wgkey-"+network, 2)
  79. return string(dat), err
  80. }