Exemple #1
0
        public static async Task MakeSureCircuitEstablishedAsync()
        {
            var ctsToken = CircuitEstablishingJobCancel.Token;

            State = TorState.NotStarted;
            while (true)
            {
                try
                {
                    if (ctsToken.IsCancellationRequested)
                    {
                        return;
                    }

                    try
                    {
                        var established = await ControlPortClient.IsCircuitEstablishedAsync().WithTimeoutAsync(TimeSpan.FromSeconds(21));

                        if (ctsToken.IsCancellationRequested)
                        {
                            return;
                        }

                        if (established)
                        {
                            State = TorState.CircuitEstablished;
                        }
                        else
                        {
                            State = TorState.EstablishingCircuit;
                        }
                    }
                    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();
                        }
                        if (ctsToken.IsCancellationRequested)
                        {
                            return;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Ignoring Tor exception:");
                    Debug.WriteLine(ex);
                }

                if (State == TorState.CircuitEstablished)
                {
                    return;
                }
                var wait = TimeSpan.FromSeconds(3);
                await Task.Delay(wait, ctsToken).ContinueWith(tsk => { });
            }
        }
Exemple #2
0
        public async Task <bool> CircuitEstablished(SecureString password)
        {
            if (password == null)
            {
                throw new ArgumentNullException(nameof(password));
            }

            bool hasCircuit = false;

            try
            {
                using (var insecurePassword = password.Insecure())
                {
                    var controlPortClient = new DotNetTor.ControlPort.Client(controlHost, ControlPort, insecurePassword.Value);
                    hasCircuit = await controlPortClient.IsCircuitEstablishedAsync();
                }
            }
            catch (DotNetTor.TorException ex)
            {
                console.WriteLine(ex.Message);
            }

            return(hasCircuit);
        }