Skip to content

Commit

Permalink
fix: (windows) wireguard command permission elevation
Browse files Browse the repository at this point in the history
  • Loading branch information
l-hellmann committed May 4, 2024
1 parent f799087 commit ad5a3ed
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.idea
.sandbox
include/
.sandbox/
.tool-versions
Expand Down
28 changes: 26 additions & 2 deletions src/wg/windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"io"
"os/exec"
"strings"
"text/template"

"github.com/pkg/errors"
Expand Down Expand Up @@ -34,11 +35,23 @@ func GenerateConfig(f io.Writer, privateKey wgtypes.Key, vpnSettings output.Proj
}

func UpCmd(ctx context.Context, filePath string) (err *exec.Cmd) {
return exec.CommandContext(ctx, "wireguard", "/installtunnelservice", filePath)
return exec.CommandContext(ctx,
"powershell",
"-Command",
"Start-Process", "wireguard",
"-Verb", "RunAs",
"-ArgumentList "+formatArgumentList("/installtunnelservice", filePath),
)
}

func DownCmd(ctx context.Context, _, interfaceName string) (err *exec.Cmd) {
return exec.CommandContext(ctx, "wireguard", "/uninstalltunnelservice", interfaceName)
return exec.CommandContext(ctx,
"powershell",
"-Command",
"Start-Process", "wireguard",
"-Verb", "RunAs",
"-ArgumentList "+formatArgumentList("/uninstalltunnelservice", interfaceName),
)
}

var vpnTmpl = `
Expand All @@ -60,3 +73,14 @@ Endpoint = {{.ProjectIpv4SharedEndpoint}}
PersistentKeepalive = 5
`

func formatArgumentList(args ...string) string {
for i, a := range args {
args[i] = quote(a)
}
return strings.Join(args, ", ")
}

func quote(in string) string {
return `"` + in + `"`
}

0 comments on commit ad5a3ed

Please sign in to comment.