Exemplo n.º 1
0
        public string ExecuteCommand(WireGuardCommand command, out int exitCode)
        {
            string result = default;

            exitCode = 1;

            switch (command.WhichExe)
            {
            case WhichExe.WireGuardExe:
            case WhichExe.WGExe:
                var cmd = command.StandardInput | Cli.Wrap(GetPath(command.WhichExe)).WithArguments(a => a
                                                                                                    .Add(command.Switch)
                                                                                                    .Add(command.Args)).WithValidation(CommandResultValidation.None);

                // For some reason, awaiting this can hang, so this method must do everything synchronously.
                var bufferedResult = cmd.ExecuteBufferedAsync().Task.Result;
                result   = bufferedResult.StandardOutput.Trim();
                exitCode = bufferedResult.ExitCode;

                if (exitCode != 0)
                {
                    result += bufferedResult.StandardError.Trim();
                }

                break;

            case WhichExe.Custom:
                Process process = Process.Start(new ProcessStartInfo
                {
                    FileName               = command.Args[0],
                    Arguments              = string.Join(' ', command.Args.Skip(1)),
                    CreateNoWindow         = true,
                    RedirectStandardOutput = true
                });
                process?.WaitForExit((int)TimeSpan.FromSeconds(30).TotalMilliseconds);
                result   = process?.StandardOutput.ReadToEnd();
                exitCode = process?.ExitCode ?? 1;
                break;

            case WhichExe.CustomInteractive:
                process = Process.Start(new ProcessStartInfo
                {
                    FileName  = command.Args[0],
                    Arguments = string.Join(' ', command.Args.Skip(1)),
                });
                process?.WaitForExit();
                exitCode = process?.ExitCode ?? 1;
                break;
            }

            return(result);
        }
Exemplo n.º 2
0
 public string ExecuteCommand(WireGuardCommand command)
 {
     return(ExecuteCommand(command, out _));
 }