unix.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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/config"
  10. "github.com/gravitl/netmaker/netclient/ncutils"
  11. "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
  12. )
  13. // SetWGKeyConfig - sets the wg conf with a new private key
  14. func SetWGKeyConfig(network string, serveraddr string) error {
  15. cfg, err := config.ReadConfig(network)
  16. if err != nil {
  17. return err
  18. }
  19. node := cfg.Node
  20. privatekey, err := wgtypes.GeneratePrivateKey()
  21. if err != nil {
  22. return err
  23. }
  24. privkeystring := privatekey.String()
  25. publickey := privatekey.PublicKey()
  26. node.PublicKey = publickey.String()
  27. err = StorePrivKey(privkeystring, network)
  28. if err != nil {
  29. return err
  30. }
  31. if node.Action == models.NODE_UPDATE_KEY {
  32. node.Action = models.NODE_NOOP
  33. }
  34. err = config.ModConfig(&node)
  35. if err != nil {
  36. return err
  37. }
  38. err = SetWGConfig(network, false)
  39. if err != nil {
  40. return err
  41. }
  42. return err
  43. }
  44. // ApplyWGQuickConf - applies wg-quick commands if os supports
  45. func ApplyWGQuickConf(confPath string, ifacename string) error {
  46. if ncutils.IsWindows() {
  47. return ApplyWindowsConf(confPath)
  48. } else {
  49. _, err := os.Stat(confPath)
  50. if err != nil {
  51. logger.Log(0, confPath+" does not exist "+err.Error())
  52. return err
  53. }
  54. if ncutils.IfaceExists(ifacename) {
  55. ncutils.RunCmd("wg-quick down "+confPath, true)
  56. }
  57. _, err = ncutils.RunCmd("wg-quick up "+confPath, true)
  58. return err
  59. }
  60. }
  61. // ApplyMacOSConf - applies system commands similar to wg-quick using golang for MacOS
  62. func ApplyMacOSConf(node *models.Node, ifacename string, confPath string) error {
  63. var err error
  64. _ = WgQuickDownMac(node, ifacename)
  65. err = WgQuickUpMac(node, ifacename, confPath)
  66. return err
  67. }
  68. // SyncWGQuickConf - formats config file and runs sync command
  69. func SyncWGQuickConf(iface string, confPath string) error {
  70. var tmpConf = confPath + ".sync.tmp"
  71. var confCmd = "wg-quick strip "
  72. if ncutils.IsMac() {
  73. confCmd = "grep -v -e Address -e MTU -e PostUp -e PostDown "
  74. }
  75. confRaw, err := ncutils.RunCmd(confCmd+confPath, false)
  76. if err != nil {
  77. return err
  78. }
  79. regex := regexp.MustCompile(".*Warning.*\n")
  80. conf := regex.ReplaceAllString(confRaw, "")
  81. err = os.WriteFile(tmpConf, []byte(conf), 0600)
  82. if err != nil {
  83. return err
  84. }
  85. _, err = ncutils.RunCmd("wg syncconf "+iface+" "+tmpConf, true)
  86. if err != nil {
  87. log.Println(err.Error())
  88. logger.Log(0, "error syncing conf, resetting")
  89. err = ApplyWGQuickConf(confPath, iface)
  90. }
  91. errN := os.Remove(tmpConf)
  92. if errN != nil {
  93. logger.Log(0, errN.Error())
  94. }
  95. return err
  96. }
  97. // RemoveWGQuickConf - calls wg-quick down
  98. func RemoveWGQuickConf(confPath string, printlog bool) error {
  99. _, err := ncutils.RunCmd(fmt.Sprintf("wg-quick down %s", confPath), printlog)
  100. return err
  101. }
  102. // StorePrivKey - stores wg priv key on disk locally
  103. func StorePrivKey(key string, network string) error {
  104. var err error
  105. d1 := []byte(key)
  106. err = os.WriteFile(ncutils.GetNetclientPathSpecific()+"wgkey-"+network, d1, 0600)
  107. return err
  108. }
  109. // RetrievePrivKey - reads wg priv key from local disk
  110. func RetrievePrivKey(network string) (string, error) {
  111. dat, err := ncutils.GetFileWithRetry(ncutils.GetNetclientPathSpecific()+"wgkey-"+network, 2)
  112. return string(dat), err
  113. }