unix.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package wireguard
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "github.com/gravitl/netmaker/models"
  6. "github.com/gravitl/netmaker/netclient/config"
  7. "github.com/gravitl/netmaker/netclient/ncutils"
  8. "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
  9. )
  10. // SetWGKeyConfig - sets the wg conf with a new private key
  11. func SetWGKeyConfig(network string, serveraddr string) error {
  12. cfg, err := config.ReadConfig(network)
  13. if err != nil {
  14. return err
  15. }
  16. node := cfg.Node
  17. privatekey, err := wgtypes.GeneratePrivateKey()
  18. if err != nil {
  19. return err
  20. }
  21. privkeystring := privatekey.String()
  22. publickey := privatekey.PublicKey()
  23. node.PublicKey = publickey.String()
  24. err = StorePrivKey(privkeystring, network)
  25. if err != nil {
  26. return err
  27. }
  28. if node.Action == models.NODE_UPDATE_KEY {
  29. node.Action = models.NODE_NOOP
  30. }
  31. err = config.ModConfig(&node)
  32. if err != nil {
  33. return err
  34. }
  35. err = SetWGConfig(network, false)
  36. if err != nil {
  37. return err
  38. }
  39. return err
  40. }
  41. // ApplyWGQuickConf - applies wg-quick commands if os supports
  42. func ApplyWGQuickConf(confPath string) error {
  43. _, _ = ncutils.RunCmd("wg-quick down "+confPath, false)
  44. _, err := ncutils.RunCmd("wg-quick up "+confPath, false)
  45. return err
  46. }
  47. // ApplyWGQuickConf - applies wg-quick commands if os supports
  48. func SyncWGQuickConf(confPath string) error {
  49. var tmpConf = confPath + ".sync.tmp"
  50. conf, err := ncutils.RunCmd("wg-quick strip "+confPath, false)
  51. if err != nil {
  52. return err
  53. }
  54. err = ioutil.WriteFile(tmpConf, []byte(conf), 0644)
  55. if err != nil {
  56. return err
  57. }
  58. _, err = ncutils.RunCmd("wg sync "+confPath, false)
  59. if err != nil {
  60. ncutils.Log("error syncing conf, resetting")
  61. err = ApplyWGQuickConf(confPath)
  62. }
  63. errN := os.Remove(tmpConf)
  64. if errN != nil {
  65. ncutils.Log(errN.Error())
  66. }
  67. return err
  68. }
  69. // RemoveWGQuickConf - calls wg-quick down
  70. func RemoveWGQuickConf(confPath string, printlog bool) error {
  71. _, err := ncutils.RunCmd("wg-quick down "+confPath, printlog)
  72. return err
  73. }
  74. // StorePrivKey - stores wg priv key on disk locally
  75. func StorePrivKey(key string, network string) error {
  76. var err error
  77. d1 := []byte(key)
  78. err = ioutil.WriteFile(ncutils.GetNetclientPathSpecific()+"wgkey-"+network, d1, 0644)
  79. return err
  80. }
  81. // RetrievePrivKey - reads wg priv key from local disk
  82. func RetrievePrivKey(network string) (string, error) {
  83. dat, err := ioutil.ReadFile(ncutils.GetNetclientPathSpecific() + "wgkey-" + network)
  84. return string(dat), err
  85. }