unix.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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, ifacename string) error {
  45. if ncutils.IsWindows() {
  46. return ApplyWindowsConf(confPath)
  47. } else {
  48. _, err := os.Stat(confPath)
  49. if err != nil {
  50. ncutils.Log(confPath + " does not exist " + err.Error())
  51. return err
  52. }
  53. if ncutils.IfaceExists(ifacename) {
  54. ncutils.RunCmd("wg-quick down "+confPath, true)
  55. }
  56. _, err = ncutils.RunCmd("wg-quick up "+confPath, true)
  57. // if err != nil {
  58. // return err
  59. // }
  60. return err
  61. }
  62. }
  63. // ApplyMacOSConf - applies system commands similar to wg-quick using golang for MacOS
  64. func ApplyMacOSConf(node models.Node, ifacename string, confPath string) error {
  65. var err error
  66. _ = WgQuickDownMac(node, ifacename)
  67. err = WgQuickUpMac(node, ifacename, confPath)
  68. return err
  69. }
  70. // SyncWGQuickConf - formats config file and runs sync command
  71. func SyncWGQuickConf(iface string, confPath string) error {
  72. var tmpConf = confPath + ".sync.tmp"
  73. var confCmd = "wg-quick strip "
  74. if ncutils.IsMac() {
  75. confCmd = "grep -v -e Address -e MTU -e PostUp -e PostDown "
  76. }
  77. confRaw, err := ncutils.RunCmd(confCmd+confPath, false)
  78. if err != nil {
  79. return err
  80. }
  81. regex := regexp.MustCompile(".*Warning.*\n")
  82. conf := regex.ReplaceAllString(confRaw, "")
  83. err = os.WriteFile(tmpConf, []byte(conf), 0600)
  84. if err != nil {
  85. return err
  86. }
  87. _, err = ncutils.RunCmd("wg syncconf "+iface+" "+tmpConf, true)
  88. if err != nil {
  89. log.Println(err.Error())
  90. ncutils.Log("error syncing conf, resetting")
  91. err = ApplyWGQuickConf(confPath, iface)
  92. }
  93. errN := os.Remove(tmpConf)
  94. if errN != nil {
  95. ncutils.Log(errN.Error())
  96. }
  97. return err
  98. }
  99. // RemoveWGQuickConf - calls wg-quick down
  100. func RemoveWGQuickConf(confPath string, printlog bool) error {
  101. _, err := ncutils.RunCmd(fmt.Sprintf("wg-quick down %s", confPath), printlog)
  102. return err
  103. }
  104. // StorePrivKey - stores wg priv key on disk locally
  105. func StorePrivKey(key string, network string) error {
  106. var err error
  107. d1 := []byte(key)
  108. err = os.WriteFile(ncutils.GetNetclientPathSpecific()+"wgkey-"+network, d1, 0600)
  109. return err
  110. }
  111. // RetrievePrivKey - reads wg priv key from local disk
  112. func RetrievePrivKey(network string) (string, error) {
  113. dat, err := os.ReadFile(ncutils.GetNetclientPathSpecific() + "wgkey-" + network)
  114. return string(dat), err
  115. }