MCPcopy Index your code
hub / github.com/WireGuard/wireguard-windows / runScriptCommand

Function runScriptCommand

tunnel/scriptrunner.go:21–74  ·  view source on GitHub ↗
(command, interfaceName string)

Source from the content-addressed store, hash-verified

19)
20
21func runScriptCommand(command, interfaceName string) error {
22 if len(command) == 0 {
23 return nil
24 }
25 if !conf.AdminBool("DangerousScriptExecution") {
26 log.Printf("Skipping execution of script, because dangerous script execution is safely disabled: %#q", command)
27 return nil
28 }
29 log.Printf("Executing: %#q", command)
30 system32, err := windows.GetSystemDirectory()
31 if err != nil {
32 return err
33 }
34 comspec := filepath.Join(system32, "cmd.exe")
35
36 devNull, err := os.OpenFile(os.DevNull, os.O_RDWR, 0)
37 if err != nil {
38 return err
39 }
40 defer devNull.Close()
41 reader, writer, err := os.Pipe()
42 if err != nil {
43 return err
44 }
45 process, err := os.StartProcess(comspec, nil /* CmdLine below */, &os.ProcAttr{
46 Files: []*os.File{devNull, writer, writer},
47 Env: append(os.Environ(), "WIREGUARD_TUNNEL_NAME="+interfaceName),
48 Sys: &syscall.SysProcAttr{
49 HideWindow: true,
50 CmdLine: fmt.Sprintf("cmd /c %s", command),
51 },
52 })
53 writer.Close()
54 if err != nil {
55 reader.Close()
56 return err
57 }
58 go func() {
59 scanner := bufio.NewScanner(reader)
60 for scanner.Scan() {
61 log.Printf("cmd> %s", scanner.Text())
62 }
63 }()
64 state, err := process.Wait()
65 reader.Close()
66 if err != nil {
67 return err
68 }
69 if state.ExitCode() == 0 {
70 return nil
71 }
72 log.Printf("Command error exit status: %d", state.ExitCode())
73 return windows.ERROR_GENERIC_COMMAND_FAILED
74}

Callers 1

ExecuteMethod · 0.85

Calls 4

AdminBoolFunction · 0.92
TextMethod · 0.80
WaitMethod · 0.80
CloseMethod · 0.45

Tested by

no test coverage detected