unix.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package wireguard
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "os"
  7. "regexp"
  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) error {
  46. _, _ = ncutils.RunCmd("wg-quick down "+confPath, false)
  47. _, err := ncutils.RunCmd("wg-quick up "+confPath, false)
  48. return err
  49. }
  50. // SyncWGQuickConf - formats config file and runs sync command
  51. func SyncWGQuickConf(iface string, confPath string) error {
  52. var tmpConf = confPath + ".sync.tmp"
  53. confRaw, err := ncutils.RunCmd("wg-quick strip "+confPath, false)
  54. if err != nil {
  55. return err
  56. }
  57. regex := regexp.MustCompile(".*Warning.*\n")
  58. conf := regex.ReplaceAllString(confRaw, "")
  59. err = ioutil.WriteFile(tmpConf, []byte(conf), 0644)
  60. if err != nil {
  61. return err
  62. }
  63. _, err = ncutils.RunCmd("wg syncconf "+iface+" "+tmpConf, true)
  64. if err != nil {
  65. log.Println(err.Error())
  66. ncutils.Log("error syncing conf, resetting")
  67. err = ApplyWGQuickConf(confPath)
  68. }
  69. errN := os.Remove(tmpConf)
  70. if errN != nil {
  71. ncutils.Log(errN.Error())
  72. }
  73. return err
  74. }
  75. // RemoveWGQuickConf - calls wg-quick down
  76. func RemoveWGQuickConf(confPath string, printlog bool) error {
  77. _, err := ncutils.RunCmd(fmt.Sprintf("wg-quick down %s", confPath), printlog)
  78. return err
  79. }
  80. // StorePrivKey - stores wg priv key on disk locally
  81. func StorePrivKey(key string, network string) error {
  82. var err error
  83. d1 := []byte(key)
  84. err = ioutil.WriteFile(ncutils.GetNetclientPathSpecific()+"wgkey-"+network, d1, 0644)
  85. return err
  86. }
  87. // RetrievePrivKey - reads wg priv key from local disk
  88. func RetrievePrivKey(network string) (string, error) {
  89. dat, err := ioutil.ReadFile(ncutils.GetNetclientPathSpecific() + "wgkey-" + network)
  90. return string(dat), err
  91. }