netmaker/netclient/wireguard/windows.go

51 lines
1.4 KiB
Go
Raw Normal View History

2021-09-20 02:03:47 +08:00
package wireguard
2021-11-18 10:57:27 +08:00
import (
"fmt"
2021-12-12 23:16:53 +08:00
"os"
"strings"
"time"
2021-11-18 10:57:27 +08:00
"github.com/gravitl/netmaker/netclient/ncutils"
)
2021-09-20 02:03:47 +08:00
2021-12-12 23:16:53 +08:00
// ApplyWindowsConf - applies the WireGuard configuration file on Windows
2021-09-20 02:03:47 +08:00
func ApplyWindowsConf(confPath string) error {
2021-12-12 23:16:53 +08:00
pathStrings := strings.Split(confPath, ncutils.GetWGPathSpecific())
if len(pathStrings) == 2 {
copyConfPath := fmt.Sprintf("%s\\%s", ncutils.WINDOWS_WG_DPAPI_PATH, pathStrings[1])
err := ncutils.Copy(confPath, copyConfPath)
if err != nil {
ncutils.PrintLog(err.Error(), 1)
}
}
2021-11-18 10:57:27 +08:00
var commandLine = fmt.Sprintf(`wireguard.exe /installtunnelservice "%s"`, confPath)
if _, err := ncutils.RunCmdFormatted(commandLine, false); err != nil {
2021-09-20 02:03:47 +08:00
return err
}
return nil
}
2021-12-12 23:16:53 +08:00
// RemoveWindowsConf - removes the WireGuard configuration file on Windows and dpapi file
2021-09-20 02:03:47 +08:00
func RemoveWindowsConf(ifacename string, printlog bool) error {
if _, err := ncutils.RunCmd("wireguard.exe /uninstalltunnelservice "+ifacename, printlog); err != nil {
2021-12-12 23:16:53 +08:00
ncutils.PrintLog(err.Error(), 1)
}
dpapipath := fmt.Sprintf("%s\\%s.conf.dpapi", ncutils.WINDOWS_WG_DPAPI_PATH, ifacename)
confpath := fmt.Sprintf("%s\\%s.conf", ncutils.WINDOWS_WG_DPAPI_PATH, ifacename)
if ncutils.FileExists(confpath) {
err := os.Remove(confpath)
if err != nil {
ncutils.PrintLog(err.Error(), 1)
}
}
time.Sleep(time.Second >> 2)
if ncutils.FileExists(dpapipath) {
err := os.Remove(dpapipath)
if err != nil {
ncutils.PrintLog(err.Error(), 1)
}
2021-09-20 02:03:47 +08:00
}
return nil
}