windows.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package wireguard
  2. import (
  3. "fmt"
  4. "os"
  5. "strings"
  6. "time"
  7. "github.com/gravitl/netmaker/netclient/ncutils"
  8. )
  9. // ApplyWindowsConf - applies the WireGuard configuration file on Windows
  10. func ApplyWindowsConf(confPath string) error {
  11. pathStrings := strings.Split(confPath, ncutils.GetWGPathSpecific())
  12. if len(pathStrings) == 2 {
  13. copyConfPath := fmt.Sprintf("%s\\%s", ncutils.WINDOWS_WG_DPAPI_PATH, pathStrings[1])
  14. err := ncutils.Copy(confPath, copyConfPath)
  15. if err != nil {
  16. ncutils.PrintLog(err.Error(), 1)
  17. }
  18. }
  19. var commandLine = fmt.Sprintf(`wireguard.exe /installtunnelservice "%s"`, confPath)
  20. if _, err := ncutils.RunCmdFormatted(commandLine, false); err != nil {
  21. return err
  22. }
  23. return nil
  24. }
  25. // RemoveWindowsConf - removes the WireGuard configuration file on Windows and dpapi file
  26. func RemoveWindowsConf(ifacename string, printlog bool) error {
  27. if _, err := ncutils.RunCmd("wireguard.exe /uninstalltunnelservice "+ifacename, printlog); err != nil {
  28. ncutils.PrintLog(err.Error(), 1)
  29. }
  30. dpapipath := fmt.Sprintf("%s\\%s.conf.dpapi", ncutils.WINDOWS_WG_DPAPI_PATH, ifacename)
  31. confpath := fmt.Sprintf("%s\\%s.conf", ncutils.WINDOWS_WG_DPAPI_PATH, ifacename)
  32. if ncutils.FileExists(confpath) {
  33. err := os.Remove(confpath)
  34. if err != nil {
  35. ncutils.PrintLog(err.Error(), 1)
  36. }
  37. }
  38. time.Sleep(time.Second >> 2)
  39. if ncutils.FileExists(dpapipath) {
  40. err := os.Remove(dpapipath)
  41. if err != nil {
  42. ncutils.PrintLog(err.Error(), 1)
  43. }
  44. }
  45. return nil
  46. }