unix.go 3.2 KB

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