Beispiel #1
0
        private static void RedirectStd(ITorSharpProxy proxy)
        {
            // Source: https://stackoverflow.com/a/59387440/3286975

            byte[] out_buf = new byte[BUFSIZE];
            byte[] err_buf = new byte[BUFSIZE];

            int dwRead = 0;

            string out_str = "";
            string err_str = "";

            bool isOutputSet = proxy.GetHandler(true) != null;
            bool isErrorSet  = proxy.GetHandler(false) != null;

            while (true)
            {
                bool bSuccess;

                if (isOutputSet)
                {
                    bSuccess = WindowsApi.ReadFile(out_read, out_buf, BUFSIZE, ref dwRead, IntPtr.Zero);
                    if (!bSuccess || dwRead == 0)
                    {
                        break;
                    }

                    out_str += System.Text.Encoding.Default.GetString(out_buf);
                    out_str  = PushCallback(proxy, out_str, true);
                }

                if (isErrorSet)
                {
                    bSuccess = WindowsApi.ReadFile(out_read, err_buf, BUFSIZE, ref dwRead, IntPtr.Zero);
                    if (!bSuccess || dwRead == 0)
                    {
                        break;
                    }

                    err_str += System.Text.Encoding.Default.GetString(err_buf);
                    err_str  = PushCallback(proxy, err_str, true);
                }
            }

            WindowsApi.CloseHandle(out_read);
            WindowsApi.CloseHandle(err_read);
            WindowsApi.CloseHandle(out_write);
            WindowsApi.CloseHandle(err_write);
        }
Beispiel #2
0
        public Task StartAsync(Tool tool, ITorSharpProxy proxy)
        {
            // start the desired process
            var arguments            = string.Join(" ", tool.Settings.GetArguments(tool));
            var environmentVariables = tool.Settings.GetEnvironmentVariables(tool);
            var startInfo            = new ProcessStartInfo
            {
                FileName               = tool.ExecutablePath,
                Arguments              = arguments,
                WorkingDirectory       = tool.WorkingDirectory,
                RedirectStandardOutput = true,
                RedirectStandardError  = true,
                UseShellExecute        = false,
                CreateNoWindow         = true,
                WindowStyle            = ProcessWindowStyle.Hidden,
            };

            foreach (var pair in environmentVariables)
            {
                startInfo.EnvironmentVariables[pair.Key] = pair.Value;
            }

            var process = Process.Start(startInfo);

            var onOutput = proxy.GetHandler(true);

            if (onOutput != null)
            {
                process.OutputDataReceived += onOutput;
            }
            else
            {
                process.OutputDataReceived += (sender, e) =>
                {
                    Console.WriteLine(e.Data);
                }
            };

            var onError = proxy.GetHandler(false);

            if (onError != null)
            {
                process.ErrorDataReceived += onError;
            }
            else
            {
                process.ErrorDataReceived += (sender, e) =>
                {
                    Console.Error.WriteLine(e.Data);
                }
            };

            process.BeginOutputReadLine();
            process.BeginErrorReadLine();

            if (process == null)
            {
                throw new TorSharpException($"Unable to start the process '{tool.ExecutablePath}'.");
            }

            _processes.Add(process);

            return(CompletedTask);
        }