unix.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package wireguard
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/gravitl/netmaker/logger"
  6. "github.com/gravitl/netmaker/netclient/ncutils"
  7. )
  8. // ApplyWGQuickConf - applies wg-quick commands if os supports
  9. func ApplyWGQuickConf(confPath, ifacename string, isConnected bool) error {
  10. if ncutils.IsWindows() {
  11. return ApplyWindowsConf(confPath, ifacename, isConnected)
  12. } else {
  13. _, err := os.Stat(confPath)
  14. if err != nil {
  15. logger.Log(0, confPath+" does not exist "+err.Error())
  16. return err
  17. }
  18. if ncutils.IfaceExists(ifacename) {
  19. ncutils.RunCmd("wg-quick down "+confPath, true)
  20. }
  21. if !isConnected {
  22. return nil
  23. }
  24. _, err = ncutils.RunCmd("wg-quick up "+confPath, true)
  25. return err
  26. }
  27. }
  28. // RemoveWGQuickConf - calls wg-quick down
  29. func RemoveWGQuickConf(confPath string, printlog bool) error {
  30. _, err := ncutils.RunCmd(fmt.Sprintf("wg-quick down %s", confPath), printlog)
  31. return err
  32. }
  33. // StorePrivKey - stores wg priv key on disk locally
  34. func StorePrivKey(key string, network string) error {
  35. var err error
  36. d1 := []byte(key)
  37. err = os.WriteFile(ncutils.GetNetclientPathSpecific()+"wgkey-"+network, d1, 0600)
  38. return err
  39. }
  40. // RetrievePrivKey - reads wg priv key from local disk
  41. func RetrievePrivKey(network string) (string, error) {
  42. dat, err := ncutils.GetFileWithRetry(ncutils.GetNetclientPathSpecific()+"wgkey-"+network, 2)
  43. return string(dat), err
  44. }