private void startTorProcess() { // Setup Tor process StringBuilder outputBuffer = new StringBuilder();; ProcessStartInfo processStartInfo; Process process; processStartInfo = new ProcessStartInfo(); processStartInfo.CreateNoWindow = true; processStartInfo.RedirectStandardOutput = true; processStartInfo.RedirectStandardInput = true; processStartInfo.UseShellExecute = false; System.Console.WriteLine(string.Format("cp {0}, sp {1}, dd {2}", controlPort, socksPort, dataDirectory)); processStartInfo.Arguments = string.Format("--controlPort {0} --socksPort {1} --dataDirectory {2}", controlPort, socksPort, dataDirectory); //processStartInfo.Arguments = string.Format("--Socks5Proxy 127.0.0.1:1337 --controlPort {0} --socksPort {1} --dataDirectory {2}", controlPort, socksPort, dataDirectory); processStartInfo.FileName = torloc; process = new Process(); process.StartInfo = processStartInfo; // enable raising events because Process does not raise events by default process.EnableRaisingEvents = true; // attach the event handler for OutputDataReceived before starting the process process.OutputDataReceived += new DataReceivedEventHandler(parseSTDOUT); process.Start(); process.BeginOutputReadLine(); process.WaitForExit(); // block thread, await potential process termination. process.CancelOutputRead(); System.Console.WriteLine(this.ToString() + " process terminated!"); state = TorState.Terminated; }
public static async Task MakeSureCircuitEstabilishedAsync() { var ctsToken = CircuitEstabilishingJobCancel.Token; State = TorState.NotStarted; while (true) { try { if (ctsToken.IsCancellationRequested) { return; } try { var estabilished = await ControlPortClient.IsCircuitEstabilishedAsync().WithTimeout(TimeSpan.FromSeconds(3)).ConfigureAwait(false); if (ctsToken.IsCancellationRequested) { return; } if (estabilished) { State = TorState.CircuitEstabilished; } else { State = TorState.EstabilishingCircuit; } } catch (Exception ex) { if (ex is TimeoutException || ex.InnerException is TimeoutException) { // Tor messed up something internally, this sometimes happens when it creates new datadir (at first launch) // Restarting to solves the issue await RestartAsync().ConfigureAwait(false); } if (ctsToken.IsCancellationRequested) { return; } } } catch (Exception ex) { Debug.WriteLine("Ignoring Tor exception:"); Debug.WriteLine(ex); } if (State == TorState.CircuitEstabilished) { return; } var wait = TimeSpan.FromSeconds(3); await Task.Delay(wait, ctsToken).ContinueWith(tsk => { }).ConfigureAwait(false); } }
private void parseSTDOUT(object sender, DataReceivedEventArgs e) { string messageType; string message; // Parse the stdout, to determine the state. if (sender != null && e != null && e.Data != null && e.Data != "") { messageType = e.Data.Substring(21, e.Data.IndexOf(']', 21) - 21); message = e.Data.Substring(23 + messageType.Length); switch (messageType) { case "warn": break; case "notice": switch (message) { case "Bootstrapped 100%: Done.": state = TorState.Ready; break; } break; case "err": state = TorState.Error; break; } System.Console.WriteLine(string.Format("{0}: {1}", this.GetHashCode().ToString(), e.Data)); } }
public static void Kill() { TorStateJobCtsSource.Cancel(); State = TorState.NotStarted; if (TorProcess != null && !TorProcess.HasExited) { Console.WriteLine("Terminating Tor process"); TorProcess.Kill(); } }
public static void Kill() { CircuitEstabilishingJobCancel.Cancel(); State = TorState.NotStarted; if (TorProcess != null && !TorProcess.HasExited) { Console.WriteLine("Terminating Tor process"); TorProcess.Kill(); } }
public static async Task RestartAsync(CancellationToken ctsToken) { Kill(); await Task.Delay(3000, ctsToken).ContinueWith(tsk => { }).ConfigureAwait(false); if (ctsToken.IsCancellationRequested) { return; } try { Console.WriteLine("Starting Tor process..."); TorProcess.Start(); State = TorState.EstabilishingCircuit; } catch { State = TorState.NotStarted; } }
public static async Task RestartAsync() { Kill(); await Task.Delay(3000).ContinueWith(tsk => { }).ConfigureAwait(false); try { Console.WriteLine("Starting Tor process..."); TorProcess.Start(); CircuitEstabilishingJobCancel = new CancellationTokenSource(); #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed MakeSureCircuitEstabilishedAsync(); #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed } catch (Exception ex) { Debug.WriteLine(ex); State = TorState.NotStarted; } }
public TorInstance(string _torloc, int _controlPort, int _socksPort, bool useExistingTorPorts = false) { // Assign member variables // state = TorState.Bootstrapping; torloc = _torloc; controlPort = (ushort)_controlPort; socksPort = (ushort)_socksPort; dataDirectory = string.Format("./data/{0}", this.GetHashCode()); System.Console.WriteLine("TorInstance({0:d}) created. Socks: {1:d}", this.GetHashCode(), socksPort); factory = new ProxyClientFactory(); proxy = factory.CreateProxyClient(ProxyType.Socks5, "127.0.0.1", socksPort); if (!useExistingTorPorts) { StartTorThread(); } else { state = TorState.Ready; } }
private void startTorProcess() { // Setup Tor process StringBuilder outputBuffer = new StringBuilder(); ; ProcessStartInfo processStartInfo; Process process; processStartInfo = new ProcessStartInfo(); processStartInfo.CreateNoWindow = true; processStartInfo.RedirectStandardOutput = true; processStartInfo.RedirectStandardInput = true; processStartInfo.UseShellExecute = false; System.Console.WriteLine(string.Format("cp {0}, sp {1}, dd {2}", controlPort, socksPort, dataDirectory)); processStartInfo.Arguments = string.Format("--controlPort {0} --socksPort {1} --dataDirectory {2}", controlPort, socksPort, dataDirectory); //processStartInfo.Arguments = string.Format("--Socks5Proxy 127.0.0.1:1337 --controlPort {0} --socksPort {1} --dataDirectory {2}", controlPort, socksPort, dataDirectory); processStartInfo.FileName = torloc; process = new Process(); process.StartInfo = processStartInfo; // enable raising events because Process does not raise events by default process.EnableRaisingEvents = true; // attach the event handler for OutputDataReceived before starting the process process.OutputDataReceived += new DataReceivedEventHandler(parseSTDOUT); process.Start(); process.BeginOutputReadLine(); process.WaitForExit(); // block thread, await potential process termination. process.CancelOutputRead(); System.Console.WriteLine(this.ToString() + " process terminated!"); state = TorState.Terminated; }
public void cleanup() { this.proxy.TcpClient.Close(); try { while (this.proxy.TcpClient.Available == 0) Thread.Sleep(25); } catch (ObjectDisposedException) { Debug.Print("TcpClient instance disposed."); } proxy = factory.CreateProxyClient(ProxyType.Socks5, "127.0.0.1", socksPort); this.State = TorState.Ready; }