unix.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. // SyncWGQuickConf - formats config file and runs sync command
  61. func SyncWGQuickConf(iface string, confPath string) error {
  62. var tmpConf = confPath + ".sync.tmp"
  63. confRaw, err := ncutils.RunCmd("wg-quick strip "+confPath, false)
  64. if err != nil {
  65. return err
  66. }
  67. regex := regexp.MustCompile(".*Warning.*\n")
  68. conf := regex.ReplaceAllString(confRaw, "")
  69. err = os.WriteFile(tmpConf, []byte(conf), 0644)
  70. if err != nil {
  71. return err
  72. }
  73. _, err = ncutils.RunCmd("wg syncconf "+iface+" "+tmpConf, true)
  74. if err != nil {
  75. log.Println(err.Error())
  76. ncutils.Log("error syncing conf, resetting")
  77. err = ApplyWGQuickConf(confPath)
  78. }
  79. errN := os.Remove(tmpConf)
  80. if errN != nil {
  81. ncutils.Log(errN.Error())
  82. }
  83. return err
  84. }
  85. // RemoveWGQuickConf - calls wg-quick down
  86. func RemoveWGQuickConf(confPath string, printlog bool) error {
  87. _, err := ncutils.RunCmd(fmt.Sprintf("wg-quick down %s", confPath), printlog)
  88. return err
  89. }
  90. // StorePrivKey - stores wg priv key on disk locally
  91. func StorePrivKey(key string, network string) error {
  92. var err error
  93. d1 := []byte(key)
  94. err = os.WriteFile(ncutils.GetNetclientPathSpecific()+"wgkey-"+network, d1, 0644)
  95. return err
  96. }
  97. // RetrievePrivKey - reads wg priv key from local disk
  98. func RetrievePrivKey(network string) (string, error) {
  99. dat, err := os.ReadFile(ncutils.GetNetclientPathSpecific() + "wgkey-" + network)
  100. return string(dat), err
  101. }