public async Task <IDisposable> SetupAsync(ClientInteraction interaction, string torPath)
        {
            var autoConfig = Proxy.Address.Equals(IPAddress.Parse("127.0.0.1"));

            if (!await TestConnectionAsync(Proxy).ConfigureAwait(false))
            {
                if (torPath != null && autoConfig)
                {
                    var args = $"-socksport {Proxy.Port}";
                    await interaction.AskConnectToTorAsync(torPath, args).ConfigureAwait(false);

                    try
                    {
                        var processInfo = new ProcessStartInfo(torPath)
                        {
                            Arguments              = args,
                            UseShellExecute        = false,
                            CreateNoWindow         = true,
                            RedirectStandardOutput = false
                        };
                        var process = new ProcessDisposable(Process.Start(processInfo));
                        try
                        {
                            await SetupAsync(interaction, null).ConfigureAwait(false);
                        }
                        catch
                        {
                            process.Dispose();
                            throw;
                        }
                        return(process);
                    }
                    catch (Exception ex)
                    {
                        Logs.Configuration.LogError($"Failed to start Tor, please verify your configuration settings \"torpath\": {ex.Message}");
                    }
                }
                throw new ConfigException($"Unable to connect to SOCKS {Proxy.Address}:{Proxy.Port}");
            }
            return(NullDisposable.Instance);
        }
Beispiel #2
0
        public async Task <IDisposable> SetupAsync(ClientInteraction interaction, string torPath)
        {
            var autoConfig    = string.IsNullOrEmpty(Password) && String.IsNullOrEmpty(CookieFile) && Server.Address.Equals(IPAddress.Parse("127.0.0.1"));
            var connectResult = await TryConnectAsync().ConfigureAwait(false);

            if (connectResult == TorConnectionSettings.ConnectionTest.SocketError)
            {
                if (torPath != null && autoConfig)
                {
                    var args = $"-controlport {Server.Port} -cookieauthentication 1";
                    await interaction.AskConnectToTorAsync(torPath, args).ConfigureAwait(false);

                    try
                    {
                        var processInfo = new ProcessStartInfo(torPath)
                        {
                            Arguments              = args,
                            UseShellExecute        = false,
                            CreateNoWindow         = true,
                            RedirectStandardOutput = false
                        };
                        var process = new ProcessDisposable(Process.Start(processInfo));
                        try
                        {
                            await SetupAsync(interaction, null).ConfigureAwait(false);
                        }
                        catch
                        {
                            process.Dispose();
                            throw;
                        }
                        return(process);
                    }
                    catch (Exception ex)
                    {
                        Logs.Configuration.LogError($"Failed to start Tor, please verify your configuration settings \"torpath\": {ex.Message}");
                    }
                }
                throw new ConfigException("Unable to connect to tor control port");
            }
            else if (connectResult == TorConnectionSettings.ConnectionTest.Success)
            {
                return(NullDisposable.Instance);
            }
            else if (!autoConfig && connectResult == TorConnectionSettings.ConnectionTest.AuthError)
            {
                throw new ConfigException("Unable to authenticate tor control port");
            }

            if (autoConfig)
            {
                AutoDetectCookieFile();
            }
            connectResult = await TryConnectAsync().ConfigureAwait(false);

            if (connectResult != TorConnectionSettings.ConnectionTest.Success)
            {
                throw new ConfigException("Unable to authenticate tor control port");
            }
            return(NullDisposable.Instance);
        }