2
0

unix.go 2.6 KB

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