public IDictionary<string, string> GetDictionary(TorSharpSettings settings)
        {
            var dictionary = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
            {
                { "SocksPort", settings.TorSocksPort.ToString(CultureInfo.InvariantCulture) },
                { "ControlPort", settings.TorControlPort.ToString(CultureInfo.InvariantCulture) }
            };

            if (settings.HashedTorControlPassword != null)
            {
                dictionary["HashedControlPassword"] = settings.HashedTorControlPassword;
            }

            if (!string.IsNullOrWhiteSpace(settings.TorDataDirectory))
            {
                dictionary["DataDirectory"] = settings.TorDataDirectory;
            }

            if (settings.TorExitNodes != null)
            {
                dictionary["ExitNodes"] = settings.TorExitNodes;
                dictionary["GeoIPFile"] = Path.Combine(_torDirectoryPath, Path.Combine("Data", "Tor", "geoip"));
                dictionary["GeoIPv6File"] = Path.Combine(_torDirectoryPath, Path.Combine("Data", "Tor", "geoip6"));
            }

            if (settings.TorStrictNodes != null)
            {
                dictionary["StrictNodes"] = settings.TorStrictNodes.Value ? "1" : "0";
            }

            return dictionary;
        }
Beispiel #2
0
        private static async Task MainAsync()
        {
            // configure
            var settings = new TorSharpSettings
            {
                ZippedToolsDirectory = Path.Combine(Path.GetTempPath(), "TorZipped"),
                ExtractedToolsDirectory = Path.Combine(Path.GetTempPath(), "TorExtracted"),
                PrivoxyPort = 1337,
                TorSocksPort = 1338,
                TorControlPort = 1339,
                TorControlPassword = "******",
                ToolRunnerType = ToolRunnerType.Simple
            };

            // download tools
            await new TorSharpToolFetcher(settings, new HttpClient()).FetchAsync();

            // execute
            var proxy = new TorSharpProxy(settings);
            var handler = new HttpClientHandler
            {
                Proxy = new WebProxy(new Uri("http://localhost:" + settings.PrivoxyPort))
            };
            var httpClient = new HttpClient(handler);
            await proxy.ConfigureAndStartAsync();
            Console.WriteLine(await httpClient.GetStringAsync("http://icanhazip.com"));
            await proxy.GetNewIdentityAsync();
            Console.WriteLine(await httpClient.GetStringAsync("http://icanhazip.com"));
            proxy.Stop();
        }
 public IDictionary<string, string> GetDictionary(TorSharpSettings settings)
 {
     return new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
     {
         {"listen-address", $"127.0.0.1:{settings.PrivoxyPort}"},
         {"forward-socks5t", $"/ 127.0.0.1:{settings.TorSocksPort} ."},
         {"close-button-minimizes", "1"},
         {"show-on-task-bar", "0"},
         {"activity-animation", "0"}
     };
 }
Beispiel #4
0
        public static void WriteLine(this ITestOutputHelper output, TorSharpSettings settings)
        {
            var serializerSettings = new JsonSerializerSettings
            {
                Converters =
                {
                    new StringEnumConverter()
                }
            };

            var json = JsonConvert.SerializeObject(settings, Formatting.Indented, serializerSettings);

            output.WriteLine($"{nameof(TorSharpSettings)}:" + Environment.NewLine + json);
        }
Beispiel #5
0
        private async Task<IPAddress> GetCurrentIpAddressAsync(TorSharpSettings settings)
        {
            var handler = new HttpClientHandler
            {
                Proxy = new WebProxy(new Uri("http://localhost:" + settings.PrivoxyPort))
            };

            using (var httpClient = new HttpClient(handler))
            {
                var ip = (await httpClient.GetStringAsync("http://ipv4.icanhazip.com")).Trim();
                _output.WriteLine($"Get IP succeeded: {ip}");
                return IPAddress.Parse(ip);
            }
        }
Beispiel #6
0
        private async Task <IPAddress> GetCurrentIpAddressAsync(TorSharpSettings settings)
        {
            var handler = new HttpClientHandler
            {
                Proxy = new WebProxy(new Uri("http://localhost:" + settings.PrivoxyPort))
            };

            using (var httpClient = new HttpClient(handler))
            {
                var ip = (await httpClient.GetStringAsync("http://ipv4.icanhazip.com")).Trim();
                _output.WriteLine($"Get IP succeeded: {ip}");
                return(IPAddress.Parse(ip));
            }
        }
Beispiel #7
0
        private void TraceSettings(TorSharpSettings settings)
        {
            var serializerSettings = new JsonSerializerSettings
            {
                Converters =
                {
                    new StringEnumConverter()
                }
            };

            var json = JsonConvert.SerializeObject(settings, Formatting.Indented, serializerSettings);

            _output.WriteLine("TorSharpSettings:" + Environment.NewLine + json);
        }
        public IDictionary<string, string> GetDictionary(TorSharpSettings settings)
        {
            var dictionary = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
            {
                { "SocksPort", settings.TorSocksPort.ToString(CultureInfo.InvariantCulture) },
                { "ControlPort", settings.TorControlPort.ToString(CultureInfo.InvariantCulture) }
            };

            if (settings.HashedTorControlPassword != null)
            {
                dictionary["HashedControlPassword"] = settings.HashedTorControlPassword;
            }

            return dictionary;
        }
Beispiel #9
0
        public static async void InitializeTor()
        {
            _torSettings = new TorSharpSettings
            {
                ZippedToolsDirectory    = Path.Combine(Path.GetTempPath(), "TorZipped"),
                ExtractedToolsDirectory = Path.Combine(Path.GetTempPath(), "TorExtracted"),
                PrivoxyPort             = 1337,
                TorSocksPort            = 1338,
                TorControlPort          = 1339,
                TorControlPassword      = "******",
                ToolRunnerType          = ToolRunnerType.Simple
            };

            // download tools
            await new TorSharpToolFetcher(_torSettings, new HttpClient()).FetchAsync();
        }
Beispiel #10
0
        public PlatformFact(string osPlatform = null, string architecture = null)
        {
            var settings = new TorSharpSettings();

            var currentOSPlatform = settings.OSPlatform.ToString();

            if (osPlatform != null && osPlatform != currentOSPlatform)
            {
                Skip = $"This test is not run on platform '{currentOSPlatform}'.";
            }

            var currentArchitecture = settings.Architecture.ToString();

            if (architecture != null && architecture != currentArchitecture)
            {
                Skip = $"This test is not run on architecture '{currentArchitecture}'.";
            }
        }
Beispiel #11
0
        /// <summary>
        /// Setups up torsharp "tools".
        /// </summary>
        static async Task SetupTorSharpTools(TorSharpSettings settings)
        {
            using (HttpClient hc = new HttpClient())
            {
                var fetcher = new TorSharpToolFetcher(settings, hc);
                var updates = await fetcher.CheckForUpdatesAsync();

                Log.Logger.Debug($"Current Privoxy: {updates.Privoxy.LocalVersion?.ToString() ?? "(none)"}");
                Log.Logger.Debug($"Latest Privoxy: {updates.Privoxy.LatestDownload.Version}");
                Log.Logger.Debug($"Current Tor: {updates.Tor.LocalVersion?.ToString() ?? "(none)"}");
                Log.Logger.Debug($"Latest Tor: {updates.Tor.LatestDownload.Version}");

                if (updates.HasUpdate)
                {
                    await fetcher.FetchAsync(updates);
                }
            }
        }
Beispiel #12
0
        public static void MakeExecutable(TorSharpSettings settings, string path)
        {
            if (settings.OSPlatform == TorSharpOSPlatform.Windows)
            {
                return;
            }
            else if (settings.OSPlatform == TorSharpOSPlatform.Linux)
            {
                // We shell out here since invoking "stat" is non-trivial. We should invoke "chmod" but we would need
                // to know the initial permissions to perform an additive "+x" change which "stat" could tell us. Let's
                // just keep things simple.
                using (var process = new Process())
                {
                    process.StartInfo.FileName  = "chmod";
                    process.StartInfo.Arguments = $"+x \"{path}\"";
                    process.StartInfo.RedirectStandardOutput = true;
                    process.StartInfo.RedirectStandardError  = true;
                    process.StartInfo.CreateNoWindow         = true;
                    process.StartInfo.UseShellExecute        = false;

                    var output     = new StringBuilder();
                    var outputLock = new object();
                    process.OutputDataReceived += GetOutputHandler(output, outputLock);
                    process.ErrorDataReceived  += GetOutputHandler(output, outputLock);

                    process.Start();
                    process.BeginOutputReadLine();
                    process.BeginErrorReadLine();
                    process.WaitForExit();

                    if (process.ExitCode != 0)
                    {
                        throw new TorSharpException(
                                  $"Failed making file '{path}' executable with 'chmod'. Exit code: {process.ExitCode}. " +
                                  $"Output: {Environment.NewLine}{output}");
                    }
                }
            }
            else
            {
                settings.RejectRuntime("make a file executable");
            }
        }
        public IDictionary <string, string> GetDictionary(TorSharpSettings settings)
        {
            var dictionary = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase)
            {
                { "SocksPort", settings.TorSocksPort.ToString(CultureInfo.InvariantCulture) },
                { "ControlPort", settings.TorControlPort.ToString(CultureInfo.InvariantCulture) }
            };

            if (settings.HashedTorControlPassword != null)
            {
                dictionary["HashedControlPassword"] = settings.HashedTorControlPassword;
            }

            if (!string.IsNullOrWhiteSpace(settings.TorDataDirectory))
            {
                dictionary["DataDirectory"] = settings.TorDataDirectory;
            }

            if (settings.TorExitNodes != null)
            {
                dictionary["ExitNodes"]   = settings.TorExitNodes;
                dictionary["GeoIPFile"]   = Path.Combine(_torDirectoryPath, "Data\\Tor\\geoip");
                dictionary["GeoIPv6File"] = Path.Combine(_torDirectoryPath, "Data\\Tor\\geoip6");
            }

            if (settings.TorStrictNodes != null)
            {
                dictionary["StrictNodes"] = (bool)settings.TorStrictNodes ? "1" : "0";
            }

            if (settings.TorNewCircuitPeriod != null)
            {
                dictionary["NewCircuitPeriod"] = settings.TorNewCircuitPeriod?.ToString(CultureInfo.InvariantCulture);
            }

            if (settings.MaxCircuitDirtiness != null)
            {
                dictionary["MaxCircuitDirtiness"] = settings.TorNewCircuitPeriod?.ToString(CultureInfo.InvariantCulture);
            }

            return(dictionary);
        }
        public IDictionary <string, string> GetDictionary(TorSharpSettings settings)
        {
            var dictionary = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase)
            {
                { "SocksPort", settings.TorSocksPort.ToString(CultureInfo.InvariantCulture) },
                { "ControlPort", settings.TorControlPort.ToString(CultureInfo.InvariantCulture) }
            };

            if (settings.HashedTorControlPassword != null)
            {
                dictionary["HashedControlPassword"] = settings.HashedTorControlPassword;
            }

            if (!string.IsNullOrWhiteSpace(settings.TorDataDirectory))
            {
                dictionary["DataDirectory"] = settings.TorDataDirectory;
            }

            return(dictionary);
        }
Beispiel #15
0
        private async Task ExecuteEndToEndTestAsync(TorSharpSettings settings)
        {
            // Arrange
            using (var httpClient = new HttpClient())
                using (var proxy = new TorSharpProxy(settings))
                {
                    _output.WriteLine(settings);

                    // Act
                    var fetcher = _httpFixture.GetTorSharpToolFetcher(settings, httpClient);
                    await fetcher.FetchAsync();

                    _output.WriteLine("The tools have been fetched");
                    await proxy.ConfigureAndStartAsync();

                    _output.WriteLine("The proxy has been started");

                    // get the first identity
                    var ipA = await GetCurrentIpAddressAsync(proxy, settings);

                    await proxy.GetNewIdentityAsync();

                    _output.WriteLine("Get new identity succeeded");
                    var ipB = await GetCurrentIpAddressAsync(proxy, settings);

                    // Assert
                    Assert.Equal(AddressFamily.InterNetwork, ipA.AddressFamily);
                    Assert.Equal(AddressFamily.InterNetwork, ipB.AddressFamily);

                    var zippedDir = new DirectoryInfo(settings.ZippedToolsDirectory);
                    Assert.True(zippedDir.Exists, "The zipped tools directory should exist.");
                    Assert.Empty(zippedDir.EnumerateDirectories());
                    Assert.Equal(2, zippedDir.EnumerateFiles().Count());

                    var extractedDir = new DirectoryInfo(settings.ExtractedToolsDirectory);
                    Assert.True(extractedDir.Exists, "The extracted tools directory should exist.");
                    Assert.Equal(2, extractedDir.EnumerateDirectories().Count());
                    Assert.Empty(extractedDir.EnumerateFiles());
                }
        }
Beispiel #16
0
        private async Task InitializeTor()
        {
            Settings = new TorSharpSettings
            {
                ZippedToolsDirectory    = Path.Combine(Path.GetTempPath(), "TorZipped"),
                ExtractedToolsDirectory = Path.Combine(Path.GetTempPath(), "TorExtracted"),
                PrivoxyPort             = 1337,
                TorSocksPort            = 1338,
                TorControlPort          = 1339,
                TorControlPassword      = "******"
            };

            // download tools
            await new TorSharpToolFetcher(Settings, new HttpClient()).FetchAsync();

            // execute
            Proxy      = new TorSharpProxy(Settings);
            TorHandler = new HttpClientHandler
            {
                Proxy = new WebProxy(new Uri("http://localhost:" + Settings.PrivoxyPort))
            };
        }
Beispiel #17
0
        public static async Task <IPAddress> GetCurrentIpAddressAsync(TorSharpSettings settings = null)
        {
            var handler = new HttpClientHandler();

            if (settings != null)
            {
                handler.Proxy = new WebProxy(new Uri("http://localhost:" + settings.PrivoxySettings.Port));
            }
            try
            {
                using (var httpClient = new HttpClient(handler))
                {
                    var ip = (await httpClient.GetStringAsync("http://ipv4.icanhazip.com")).Trim();
                    return(IPAddress.Parse(ip));
                }
            }
            catch (Exception ex)
            {
                Logger.Write("Fehler beim Abfragen der IP-Adresse - Grund: " + ex.Message, LogLevel.Debug);
                return(IPAddress.Parse("0.0.0.0"));
            }
        }
        public async void StartProxyAsync()
        {
            var settings = new TorSharpSettings
            {
                ZippedToolsDirectory    = Path.Combine(Path.GetTempPath(), "TorZipped"),
                ExtractedToolsDirectory = Path.Combine(Path.GetTempPath(), "TorExtracted"),
                PrivoxyPort             = 1337,
                TorSocksPort            = 1338,
                TorControlPort          = 1339,
                TorControlPassword      = "******",
                TorrcLoc = Application.StartupPath + "\\torrc"
            };

            await new TorSharpToolFetcher(settings, new HttpClient()).FetchAsync();
            proxy = new TorSharpProxy(settings);
            var handler = new HttpClientHandler
            {
                Proxy = new WebProxy(new Uri("http://localhost:" + settings.PrivoxyPort))
            };
            var httpClient = new HttpClient(handler);
            await proxy.ConfigureAndStartAsync();
        }
Beispiel #19
0
        public IDictionary <string, string> GetDictionary(Tool tool, TorSharpSettings settings)
        {
            var output = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase)
            {
                { "listen-address", $"{settings.PrivoxySettings.ListenAddress}:{settings.PrivoxySettings.Port}" },
                { "forward-socks5t", $"/ 127.0.0.1:{settings.TorSettings.SocksPort} ." },
            };

            if (settings.OSPlatform == TorSharpOSPlatform.Windows)
            {
                output["close-button-minimizes"] = "1";
                output["show-on-task-bar"]       = "0";
                output["activity-animation"]     = "0";
            }
            else if (settings.OSPlatform == TorSharpOSPlatform.Linux)
            {
                output["confdir"] = Path.Combine(tool.DirectoryPath, "etc", "privoxy");
                output["logdir"]  = Path.Combine(tool.DirectoryPath, "var", "log", "privoxy");
            }

            return(output);
        }
Beispiel #20
0
        private static async Task MainAsync()
        {
            // configure
            var settings = new TorSharpSettings
            {
                ZippedToolsDirectory    = Path.Combine(Path.GetTempPath(), "TorZipped"),
                ExtractedToolsDirectory = Path.Combine(Path.GetTempPath(), "TorExtracted"),
                PrivoxyPort             = 1337,
                TorSocksPort            = 1338,
                TorControlPort          = 1339,
                TorControlPassword      = "******",
#if NET45
                ToolRunnerType = ToolRunnerType.VirtualDesktop,
#else
                ToolRunnerType = ToolRunnerType.Simple,
#endif
                WaitForTestEndpoint = true
            };

            // download tools
            await new TorSharpToolFetcher(settings, new HttpClient()).FetchAsync();

            // execute
            using (var proxy = new TorSharpProxy(settings))
                using (var handler = proxy.CreateHttpClientHandler())
                    using (var httpClient = new HttpClient(handler))
                    {
                        await proxy.ConfigureAndStartAsync();

                        Console.WriteLine(await httpClient.GetStringAsync("http://icanhazip.com"));

                        await proxy.GetNewIdentityAsync();

                        Console.WriteLine(await httpClient.GetStringAsync("http://icanhazip.com"));
                    }
        }
Beispiel #21
0
        private async Task ExecuteEndToEndTestAsync(TorSharpSettings settings)
        {
            // Arrange
            using (var proxy = new TorSharpProxy(settings))
            {
                _output.WriteLine($"Initialized proxy with tool runner type {settings.ToolRunnerType}");

                // Act
                await new TorSharpToolFetcher(settings, new HttpClient()).FetchAsync();
                _output.WriteLine("The tools have been fetched");
                await proxy.ConfigureAndStartAsync();
                _output.WriteLine("The proxy has been started");

                // get the first identity
                var ipA = await GetCurrentIpAddressAsync(settings);
                await proxy.GetNewIdentityAsync();
                _output.WriteLine("Get new identity succeeded");
                var ipB = await GetCurrentIpAddressAsync(settings);
                
                // Assert
                Assert.Equal(AddressFamily.InterNetwork, ipA.AddressFamily);
                Assert.Equal(AddressFamily.InterNetwork, ipB.AddressFamily);
            }
        }
Beispiel #22
0
 public PrivoxyFetcher(TorSharpSettings settings, HttpClient httpClient)
 {
     _settings   = settings;
     _httpClient = httpClient;
 }
Beispiel #23
0
        private static async Task MainAsync()
        {
            // configure
            var settings = new TorSharpSettings
            {
                ZippedToolsDirectory    = Path.Combine(Path.GetTempPath(), "TorZipped"),
                ExtractedToolsDirectory = Path.Combine(Path.GetTempPath(), "TorExtracted"),
                PrivoxySettings         =
                {
                    Port = 18118,
                },
                TorSettings =
                {
                    SocksPort       =    19050,
                    ControlPort     =    19051,
                    ControlPassword = "******",
                },
            };

            // output runtime information
            var message = new StringBuilder();

            message.Append($"Running the sample on {settings.OSPlatform} OS and {settings.Architecture} architecture.");
#if NETCOREAPP
            message.Append($" OS description: {RuntimeInformation.OSDescription}.");
#endif
            Console.WriteLine(message.ToString());
            Console.WriteLine();

            // download tools
            using (var httpClient = new HttpClient())
            {
                var fetcher = new TorSharpToolFetcher(settings, httpClient);
                var updates = await fetcher.CheckForUpdatesAsync();

                Console.WriteLine($"Current Privoxy: {updates.Privoxy.LocalVersion?.ToString() ?? "(none)"}");
                Console.WriteLine($" Latest Privoxy: {updates.Privoxy.LatestDownload.Version}");
                Console.WriteLine();
                Console.WriteLine($"Current Tor: {updates.Tor.LocalVersion?.ToString() ?? "(none)"}");
                Console.WriteLine($" Latest Tor: {updates.Tor.LatestDownload.Version}");
                Console.WriteLine();
                if (updates.HasUpdate)
                {
                    await fetcher.FetchAsync(updates);
                }
            }

            // execute
            using (var proxy = new TorSharpProxy(settings))
            {
                var handler = new HttpClientHandler
                {
                    Proxy = new WebProxy(new Uri("http://localhost:" + settings.PrivoxySettings.Port))
                };

                using (handler)
                    using (var httpClient = new HttpClient(handler))
                    {
                        await proxy.ConfigureAndStartAsync();

                        Console.WriteLine(await httpClient.GetStringAsync("http://api.ipify.org"));
                        await proxy.GetNewIdentityAsync();

                        Console.WriteLine(await httpClient.GetStringAsync("http://api.ipify.org"));

                        using (var controlClient = await proxy.GetControlClientAsync())
                        {
                            var read = await controlClient.GetTrafficReadAsync();

                            Console.WriteLine("Bytes read    : {0}", read);
                            var written = await controlClient.GetTrafficWrittenAsync();

                            Console.WriteLine("Bytes written : {0}", written);
                        }
                    }

                proxy.Stop();
            }
        }
        public IDictionary <string, string> GetDictionary(Tool tool, TorSharpSettings settings)
        {
            var dictionary = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase)
            {
                { "SocksPort", settings.TorSettings.SocksPort.ToString(CultureInfo.InvariantCulture) },
                { "ControlPort", settings.TorSettings.ControlPort.ToString(CultureInfo.InvariantCulture) }
            };

            if (settings.TorSettings.HashedControlPassword != null)
            {
                dictionary["HashedControlPassword"] = settings.TorSettings.HashedControlPassword;
            }
            else
            {
                dictionary["HashedControlPassword"] = null;
            }

            string dataDictionary;

            if (!string.IsNullOrWhiteSpace(settings.TorSettings.DataDirectory))
            {
                dataDictionary = settings.TorSettings.DataDirectory;
            }
            else
            {
                dataDictionary = Path.Combine(tool.DirectoryPath, "Data", "Tor");
            }

            dictionary["DataDirectory"] = dataDictionary;

            if (!string.IsNullOrWhiteSpace(settings.TorSettings.ExitNodes))
            {
                dictionary["ExitNodes"]   = settings.TorSettings.ExitNodes;
                dictionary["GeoIPFile"]   = Path.Combine(tool.DirectoryPath, "Data", "Tor", "geoip");
                dictionary["GeoIPv6File"] = Path.Combine(tool.DirectoryPath, "Data", "Tor", "geoip6");
            }

            if (settings.TorSettings.StrictNodes.HasValue)
            {
                dictionary["StrictNodes"] = settings.TorSettings.StrictNodes.Value ? "1" : "0";
            }

            if (!string.IsNullOrWhiteSpace(settings.TorSettings.HttpsProxyHost))
            {
                dictionary["HTTPSProxy"] = settings.TorSettings.HttpsProxyHost;

                if (settings.TorSettings.HttpsProxyPort.HasValue)
                {
                    dictionary["HTTPSProxy"] +=
                        ":" + settings.TorSettings.HttpsProxyPort.Value.ToString(CultureInfo.InvariantCulture);
                }
            }

            if (settings.TorSettings.HttpsProxyUsername != null ||
                settings.TorSettings.HttpsProxyPassword != null)
            {
                dictionary["HTTPSProxyAuthenticator"] =
                    $"{settings.TorSettings.HttpsProxyUsername}:{settings.TorSettings.HttpsProxyPassword}";
            }

            return(dictionary);
        }
Beispiel #25
0
        public static async Task Run(CancellationToken cancelToken, string store)
        {
            Timer.Tick    += new EventHandler(Timer_Tick);
            Timer.Interval = 1000;

            if (!Settings.Get <bool>("ScanNew") && !Settings.Get <bool>("ScanUsed"))
            {
                mf.textBox4.AppendText("Crawler wird gestoppt - Reason: Neu & Gebraucht sind Deaktiviert" + Environment.NewLine);
                mf.metroButton3.PerformClick();
                return;
            }

            mf.textBox4.AppendText("Übernehme Neu & Gebraucht Einstellungen" + Environment.NewLine);
            var sellerForNew = new List <string>();

            if (!String.IsNullOrWhiteSpace(Settings.Get <string>("SellerForNew")))
            {
                sellerForNew = Settings.Get <string>("SellerForNew").Split(',').Select(s => s.Trim()).ToList();
            }
            else
            {
                sellerForNew.Add("Amazon");
            }
            var sellerForUsed = new List <string>();

            if (!String.IsNullOrWhiteSpace(Settings.Get <string>("SellerForUsed")))
            {
                sellerForUsed = Settings.Get <string>("SellerForUsed").Split(',').Select(s => s.Trim()).ToList();
            }
            else
            {
                sellerForUsed.Add("Amazon");
            }


            TorSharpSettings torSettings = null;
            TorSharpProxy    torProxy    = null;

            if (Settings.Get <bool>("UseTorProxies"))
            {
                mf.textBox4.AppendText("Initialisiere TOR Proxy Funktion" + Environment.NewLine);
                try
                {
                    torSettings = new TorSharpSettings
                    {
                        ReloadTools             = true,
                        ZippedToolsDirectory    = Path.Combine(FoldersFilesAndPaths.Temp, "TorZipped"),
                        ExtractedToolsDirectory = Path.Combine(FoldersFilesAndPaths.Temp, "TorExtracted"),
                        PrivoxySettings         =
                        {
                            Port = 1337,
                        },
                        TorSettings =
                        {
                            SocksPort       =     1338,
                            ControlPort     =     1339,
                            ControlPassword = "******",
                        },
                    };
                    await new TorSharpToolFetcher(torSettings, new HttpClient()).FetchAsync();
                    torProxy = new TorSharpProxy(torSettings);
                    await torProxy.ConfigureAndStartAsync();
                }
                catch (Exception ex)
                {
                    mf.textBox4.AppendText("Fehler beim Initialisiere der TOR Proxy Funktion ;-(" + Environment.NewLine + "TOR Proxy Funktion wird deaktiviert!" + Environment.NewLine);
                    mf.textBox4.AppendText(ex.Message);
                    mf.metroToggle5.Checked = false;
                }

                if (Settings.IsPremium && Settings.Get <bool>("UseTorProxies") && Settings.Get <bool>("ProxyAlwaysActive"))
                {
                    mf.textBox4.AppendText("Set TOR Proxy - Reason: P. always Active" + Environment.NewLine);
                    mf.metroLabel45.Text   = Convert.ToString(await WebUtils.GetCurrentIpAddressAsync(torSettings));
                    Proxies.TorProxyActive = true;
                }
            }

            string sourcecode;
            int    sites, loops;

            while (true)
            {
                Database.OpenConnection();
                SQLiteCommand getEntry;
                switch (store)
                {
                case "ALLE":
                    getEntry = new SQLiteCommand("SELECT * FROM Products WHERE Status = '0' ORDER BY [Letzter Check] ASC LIMIT 0,1",
                                                 Database.Connection);
                    break;

                default:
                    getEntry =
                        new SQLiteCommand("SELECT * FROM Products WHERE Status = '0' AND Store = @store ORDER BY [Letzter Check] ASC LIMIT 0,1",
                                          Database.Connection);
                    getEntry.Parameters.AddWithValue("@store", store);
                    break;
                }
                SQLiteDataReader entry = getEntry.ExecuteReader();
                if (!entry.HasRows)
                {
                    mf.metroButton3.PerformClick();
                    Timer.Stop();
                    torProxy.Stop();
                    return;
                }
                while (entry.Read())
                {
                    if (cancelToken.IsCancellationRequested)
                    {
                        Timer.Stop();
                        torProxy.Stop();
                        return;
                    }
                    mf.metroLabel10.Text        = Convert.ToString(entry["Store"]);
                    mf.metroLabel11.Text        = Convert.ToString(entry["ASIN / ISBN"]);
                    mf.metroLabel12.Text        = Convert.ToString(entry["Name"]);
                    mf.metroLabel13.Text        = Convert.ToString(entry["Preis: Neu"]);
                    mf.metroLabel14.Text        = Convert.ToString(entry["Preis: Wie Neu"]);
                    mf.metroLabel15.Text        = Convert.ToString(entry["Preis: Sehr Gut"]);
                    mf.metroLabel16.Text        = Convert.ToString(entry["Preis: Gut"]);
                    mf.metroLabel17.Text        = Convert.ToString(entry["Preis: Akzeptabel"]);
                    mf.metroLabel18.Text        = Convert.ToString(entry["Letzter Check"]);
                    mf.metroLabel24.Text        = mf.metroLabel25.Text = mf.metroLabel26.Text = mf.metroLabel27.Text = mf.metroLabel28.Text = null;
                    mf.metroLabel41.Text        = null;
                    mf.webBrowser1.DocumentText = mf.webBrowser2.DocumentText = null;
                    await Task.Delay(Tools.RandomNumber(250, 500), cancelToken).ContinueWith(tsk => { });

                    string priceNew = null;
                    if (Settings.Get <bool>("ScanNew"))
                    {
                        List <string> priceNewList = new List <string>();
                        mf.tabControl2.SelectedTab = mf.TabPage4;
                        sites = 1;
                        loops = 0;
                        do
                        {
                            if (cancelToken.IsCancellationRequested)
                            {
                                Timer.Stop();
                                torProxy.Stop();
                                return;
                            }
                            try
                            {
                                string url = "https://www.amazon." + Amazon.GetTld(Convert.ToString(entry["Store"])) +
                                             "/gp/aw/ol/" + entry["ASIN / ISBN"] +
                                             "/ref=mw_dp_olp?ca=" + entry["ASIN / ISBN"] + "&o=New&op=" + (loops + 1) +
                                             "&vs=1";

                                var handler = new HttpClientHandler();
                                if (Settings.Get <bool>("UseTorProxies"))
                                {
                                    if (Settings.IsPremium && Settings.Get <bool>("ProxyAlwaysActive") ||
                                        Proxies.TorProxyActive && Proxies.RealIPnexttime > DateTime.Now)
                                    {
                                        Debug.WriteLine("Add TOR Proxy to HttpClientHandler...");
                                        handler.Proxy =
                                            new WebProxy(new Uri("http://*****:*****@"<title> (Tut uns Leid!|Toutes nos excuses)</title>");
                            Match errordetection1 = Regex.Match(sourcecode.RemoveLineEndings(),
                                                                @"<title>(503 - Service Unavailable Error)</title>");
                            if (errordetection.Success || errordetection1.Success)
                            {
                                Debug.WriteLine("Issue Found...");
                                continue;
                            }
                            //CAPTCHA Detection
                            Match captchadetection =
                                Regex.Match(sourcecode,
                                            "<title dir=\"ltr\">(Amazon CAPTCHA|Bot Check|Robot Check)</title>");
                            if (captchadetection.Success)
                            {
                                Debug.WriteLine("Captcha detected...");
                                if (Settings.Get <bool>("UseTorProxies"))
                                {
                                    await torProxy.GetNewIdentityAsync();

                                    mf.metroLabel45.Text =
                                        Convert.ToString(await WebUtils.GetCurrentIpAddressAsync(torSettings));
                                    Proxies.TorProxyActive = true;
                                    if (!Settings.Get <bool>("ProxyAlwaysActive") &&
                                        Proxies.RealIPnexttime < DateTime.Now)
                                    {
                                        Proxies.RealIPnexttime = DateTime.Now.AddMinutes(15);
                                    }
                                }
                                continue;
                            }

                            //How many Sites?
                            Match sitesNumber = Regex.Match(sourcecode, "<a name=\"New\">([^\"]*) / ([^\"]*)</a>");
                            if (sitesNumber.Success && loops == 0 && Settings.Get <int>("ScanMethod") == 0)
                            {
                                sites = Convert.ToInt32(
                                    Math.Ceiling(Convert.ToDecimal(sitesNumber.Groups[2].Value) / 10));
                            }
                            mf.metroLabel41.Text = $@"{loops + 1} / {sites} (NEU)";

                            //Get Price for NEW
                            if (sellerForNew.Contains("Amazon"))
                            {
                                Match newama = Regex.Match(sourcecode,
                                                           "<a href=(.*)ca=([a-zA-Z0-9_]*)&vs=1(.*)>(Neu|Nuovo|Neuf|Nuevo|New) - (EUR |£)(.*)</a>");
                                if (newama.Success)
                                {
                                    priceNewList.Add(await CurrencyConverter.ConvertToEuro(newama.Groups[6].Value,
                                                                                           Convert.ToString(entry["Store"])));
                                }
                            }
                            if (sellerForNew.Contains("Drittanbieter"))
                            {
                                if (sellerForNew.Contains("Versand per Amazon"))
                                {
                                    Regex new3rd =
                                        new Regex(
                                            "(Versand durch Amazon.de|Spedito da Amazon|EXPÉDIÉ PAR AMAZON|Distribuido por Amazon|Fulfilled by Amazon)(\r\n|\r|\n)</font>(\r\n|\r|\n)(\r\n|\r|\n)<br />(\r\n|\r|\n)(\r\n|\r|\n)(\r\n|\r|\n)" +
                                            "<a href=(.*)ca=([a-zA-Z0-9_]*)&eid=(.*)>(Neu|Nuovo|Neuf|Nuevo|New) - (EUR |£)(.*)</a>",
                                            RegexOptions.Compiled);
                                    foreach (Match itemMatch in new3rd.Matches(sourcecode))
                                    {
                                        priceNewList.Add(
                                            await CurrencyConverter.ConvertToEuro(itemMatch.Groups[13].Value,
                                                                                  Convert.ToString(entry["Store"])));
                                    }
                                }
                                else
                                {
                                    Regex new3rd =
                                        new Regex(
                                            @"<a href=(.*)ca=([a-zA-Z0-9_]*)&eid=(.*)>(Neu|Nuovo|Neuf|Nuevo|New) - (EUR |£)(.*)</a>",
                                            RegexOptions.Compiled);
                                    foreach (Match itemMatch in new3rd.Matches(sourcecode))
                                    {
                                        priceNewList.Add(
                                            await CurrencyConverter.ConvertToEuro(itemMatch.Groups[6].Value,
                                                                                  Convert.ToString(entry["Store"])));
                                    }
                                }
                            }
                            loops++;
                            if (sites > loops)
                            {
                                await Task.Delay(
                                    Tools.RandomNumber(Convert.ToInt32(mf.numericUpDown3.Value) * 1000 - 500,
                                                       Convert.ToInt32(mf.numericUpDown3.Value) * 1000 + 500), cancelToken)
                                .ContinueWith(tsk => { });
                            }
                        } while (sites > loops);

                        RemoveInvalidEntrys(priceNewList);
                        priceNew = priceNewList.OrderBy(price => price).FirstOrDefault();
                        if (!String.IsNullOrEmpty(priceNew))
                        {
                            mf.metroLabel24.Text = priceNew;
                            ComparePrice(entry["Preis: Neu"], priceNew, mf.metroLabel24);
                        }
                    }
                    await Task.Delay(Tools.RandomNumber(500, 1000), cancelToken).ContinueWith(tsk => { });

                    string priceLikeNew    = null;
                    string priceVeryGood   = null;
                    string priceGood       = null;
                    string priceAcceptable = null;
                    if (Settings.Get <bool>("ScanUsed"))
                    {
                        List <string> priceLikeNewList    = new List <string>();
                        List <string> priceVeryGoodList   = new List <string>();
                        List <string> priceGoodList       = new List <string>();
                        List <string> priceAcceptableList = new List <string>();
                        mf.tabControl2.SelectedTab = mf.TabPage5;
                        var conditionList = new List <Tuple <List <string>, string> >
                        {
                            new Tuple <List <string>, string>(priceLikeNewList,
                                                              "Wie neu|Condizioni pari al nuovo|Comme neuf|Como nuevo|Mint"),
                            new Tuple <List <string>, string>(priceVeryGoodList,
                                                              "Sehr gut|Ottimo|Très bon état|Muy bueno|Very good"),
                            new Tuple <List <string>, string>(priceGoodList,
                                                              "Gut|Buono|D'occasion - très bon état|Bueno|Good"),
                            new Tuple <List <string>, string>(priceAcceptableList,
                                                              "Akzeptabel|Accettabile|Acceptable|Aceptable|Acceptable")
                        };
                        sites = 1;
                        loops = 0;
                        do
                        {
                            if (cancelToken.IsCancellationRequested)
                            {
                                Timer.Stop();
                                torProxy.Stop();
                                return;
                            }
                            try
                            {
                                string url = "https://www.amazon." + Amazon.GetTld(Convert.ToString(entry["Store"])) +
                                             "/gp/aw/ol/" + entry["ASIN / ISBN"] +
                                             "/ref=mw_dp_olp?o=Used&op=" + (loops + 1);

                                var handler = new HttpClientHandler();
                                if (Settings.Get <bool>("UseTorProxies"))
                                {
                                    if (Settings.IsPremium && Settings.Get <bool>("ProxyAlwaysActive") || Proxies.TorProxyActive && Proxies.RealIPnexttime > DateTime.Now)
                                    {
                                        Debug.WriteLine("Add TOR Proxy to HttpClientHandler...");
                                        handler.Proxy = new WebProxy(new Uri("http://*****:*****@"<title> (Tut uns Leid!|Toutes nos excuses)</title>");
                            Match errordetection1 = Regex.Match(sourcecode.RemoveLineEndings(), @"<title>(503 - Service Unavailable Error)</title>");
                            if (errordetection.Success || errordetection1.Success)
                            {
                                Debug.WriteLine("Issue Found...");
                                continue;
                            }
                            //CAPTCHA Detection
                            Match captchadetection = Regex.Match(sourcecode, "<title dir=\"ltr\">(Amazon CAPTCHA|Bot Check|Robot Check)</title>");
                            if (captchadetection.Success)
                            {
                                Debug.WriteLine("Captcha detected...");
                                if (Settings.Get <bool>("UseTorProxies"))
                                {
                                    await torProxy.GetNewIdentityAsync();

                                    mf.metroLabel45.Text   = Convert.ToString(await WebUtils.GetCurrentIpAddressAsync(torSettings));
                                    Proxies.TorProxyActive = true;
                                    if (!Settings.Get <bool>("ProxyAlwaysActive") && Proxies.RealIPnexttime < DateTime.Now)
                                    {
                                        Proxies.RealIPnexttime = DateTime.Now.AddMinutes(15);
                                    }
                                }
                                continue;
                            }

                            //How many WHD Sites?
                            Match sitesNumber = Regex.Match(sourcecode, "<a name=\"Used\">([^\"]*) / ([^\"]*)</a>");
                            if (sitesNumber.Success && loops == 0 && Settings.Get <int>("ScanMethod") == 0)
                            {
                                sites = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(sitesNumber.Groups[2].Value) / 10));
                            }
                            mf.metroLabel41.Text = $@"{loops + 1} / {sites} (GEBRAUCHT)";

                            //Get Price for WHDs
                            //.de WHD Seller ID: A8KICS1PHF7ZO
                            //.it WHD Seller ID: A1HO9729ND375Y
                            //.fr WHD Seller ID: A2CVHYRTWLQO9T
                            //.es WHD Seller ID: A6T89FGPU3U0Q
                            //.co.uk WHD Seller ID: A2OAJ7377F756P

                            if (sellerForUsed.Contains("Amazon"))
                            {
                                foreach (var condition in conditionList)
                                {
                                    Regex search =
                                        new Regex(
                                            @"<a href=(.*)(A8KICS1PHF7ZO|A1HO9729ND375Y|A2CVHYRTWLQO9T|A6T89FGPU3U0Q|A2OAJ7377F756P)(.*)>(" + condition.Item2 + ") - (EUR |£)(.*)</a>",
                                            RegexOptions.Compiled);
                                    foreach (Match itemMatch in search.Matches(sourcecode))
                                    {
                                        condition.Item1.Add(await CurrencyConverter.ConvertToEuro(itemMatch.Groups[6].Value, Convert.ToString(entry["Store"])));
                                    }
                                }
                            }
                            if (sellerForUsed.Contains("Drittanbieter"))
                            {
                                if (sellerForUsed.Contains("Versand per Amazon"))
                                {
                                    foreach (var condition in conditionList)
                                    {
                                        Regex search =
                                            new Regex(
                                                "(Versand durch Amazon.de|Spedito da Amazon|EXPÉDIÉ PAR AMAZON|Distribuido por Amazon|Fulfilled by Amazon)(\r\n|\r|\n)</font>(\r\n|\r|\n)(\r\n|\r|\n)<br />(\r\n|\r|\n)(\r\n|\r|\n)(\r\n|\r|\n)" +
                                                "<a href=(.*)&me=(?!(A8KICS1PHF7ZO|A1HO9729ND375Y|A2CVHYRTWLQO9T|A6T89FGPU3U0Q|A2OAJ7377F756P).)(.*)>(" + condition.Item2 + ") - (EUR |£)(.*)</a>",
                                                RegexOptions.Compiled);
                                        foreach (Match itemMatch in search.Matches(sourcecode))
                                        {
                                            condition.Item1.Add(await CurrencyConverter.ConvertToEuro(itemMatch.Groups[13].Value, Convert.ToString(entry["Store"])));
                                        }
                                    }
                                }
                                else
                                {
                                    foreach (var condition in conditionList)
                                    {
                                        Regex search =
                                            new Regex(
                                                @"<a href=(.*)&me=(?!(A8KICS1PHF7ZO|A1HO9729ND375Y|A2CVHYRTWLQO9T|A6T89FGPU3U0Q|A2OAJ7377F756P).)(.*)>(" + condition.Item2 + ") - (EUR |£)(.*)</a>",
                                                RegexOptions.Compiled);
                                        foreach (Match itemMatch in search.Matches(sourcecode))
                                        {
                                            condition.Item1.Add(await CurrencyConverter.ConvertToEuro(itemMatch.Groups[6].Value, Convert.ToString(entry["Store"])));
                                        }
                                    }
                                }
                            }
                            loops++;
                            if (sites > loops)
                            {
                                await Task.Delay(Tools.RandomNumber(Convert.ToInt32(mf.numericUpDown3.Value) * 1000 - 500, Convert.ToInt32(mf.numericUpDown3.Value) * 1000 + 500), cancelToken).ContinueWith(tsk => { });
                            }
                        } while (sites > loops);

                        foreach (var condition in conditionList)
                        {
                            RemoveInvalidEntrys(condition.Item1);
                        }
                        priceLikeNew = priceLikeNewList.OrderBy(price => price).FirstOrDefault();
                        if (!String.IsNullOrEmpty(priceLikeNew))
                        {
                            mf.metroLabel25.Text = priceLikeNew;
                            ComparePrice(entry["Preis: Wie Neu"], priceLikeNew, mf.metroLabel25);
                        }
                        priceVeryGood = priceVeryGoodList.OrderBy(price => price).FirstOrDefault();
                        if (!String.IsNullOrEmpty(priceVeryGood))
                        {
                            mf.metroLabel26.Text = priceVeryGood;
                            ComparePrice(entry["Preis: Sehr Gut"], priceVeryGood, mf.metroLabel26);
                        }
                        priceGood = priceGoodList.OrderBy(price => price).FirstOrDefault();
                        if (!String.IsNullOrEmpty(priceGood))
                        {
                            mf.metroLabel27.Text = priceGood;
                            ComparePrice(entry["Preis: Gut"], priceGood, mf.metroLabel27);
                        }
                        priceAcceptable = priceAcceptableList.OrderBy(price => price).FirstOrDefault();
                        if (!String.IsNullOrEmpty(priceAcceptable))
                        {
                            mf.metroLabel28.Text = priceAcceptable;
                            ComparePrice(entry["Preis: Akzeptabel"], priceAcceptable, mf.metroLabel28);
                        }
                    }
                    await Task.Delay(Tools.RandomNumber(500, 1000), cancelToken).ContinueWith(tsk => { });

                    Database.OpenConnection();
                    SQLiteCommand updateEntry =
                        new SQLiteCommand(
                            "UPDATE Products SET [Preis: Neu] = @priceNew, [Preis: Wie Neu] = @priceLikenew, [Preis: Sehr Gut] = @priceVerygood, [Preis: Gut] = @priceGood, [Preis: Akzeptabel] = @priceAcceptable, [Letzter Check] = @lastcheck WHERE ID = @id",
                            Database.Connection);
                    updateEntry.Parameters.AddWithValue("@id", entry["ID"]);
                    updateEntry.Parameters.AddWithValue("@priceNew", !String.IsNullOrEmpty(priceNew) ? priceNew : null);
                    updateEntry.Parameters.AddWithValue("@priceLikenew", !String.IsNullOrEmpty(priceLikeNew) ? priceLikeNew : null);
                    updateEntry.Parameters.AddWithValue("@priceVerygood", !String.IsNullOrEmpty(priceVeryGood) ? priceVeryGood : null);
                    updateEntry.Parameters.AddWithValue("@priceGood", !String.IsNullOrEmpty(priceGood) ? priceGood : null);
                    updateEntry.Parameters.AddWithValue("@priceAcceptable", !String.IsNullOrEmpty(priceAcceptable) ? priceAcceptable : null);
                    updateEntry.Parameters.AddWithValue("@lastcheck", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    updateEntry.ExecuteNonQuery();

                    await Task.Delay(250, cancelToken).ContinueWith(tsk => { });

                    if (mf.metroComboBox2.SelectedIndex == -1 || mf.metroComboBox2.Text == "ALLE" || store == mf.metroComboBox2.Text)
                    {
                        ProductDatabase.Display(mf.metroComboBox2.SelectedIndex == -1 ? "ALLE" : mf.metroComboBox2.Text);
                    }


                    await MyReminder.DoRemindWhenPossible(Convert.ToString(entry["ID"]));


                    _randomWait = Tools.RandomNumber(Convert.ToInt32(mf.numericUpDown2.Value) - 1, Convert.ToInt32(mf.numericUpDown2.Value) + 1);
                    mf.metroProgressBar1.Value   = 0;
                    mf.metroProgressBar1.Maximum = _randomWait * 1000;
                    Timer.Start();
                    mf.metroLabel29.Text = _dt.AddSeconds(_randomWait).ToString("mm:ss");

                    await Task.Delay((_randomWait + 1) * 1000, cancelToken).ContinueWith(tsk => { });
                }
            }
        }
Beispiel #26
0
        public IDictionary <string, string> GetDictionary(Tool tool, TorSharpSettings settings)
        {
            var dictionary = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase)
            {
                { "SocksPort", settings.TorSettings.SocksPort.ToString(CultureInfo.InvariantCulture) },
                { "ControlPort", settings.TorSettings.ControlPort.ToString(CultureInfo.InvariantCulture) }
            };

            if (settings.TorSettings.UseBridges.HasValue)
            {
                dictionary["UseBridges"] = settings.TorSettings.UseBridges.Value ? "1" : "0";
            }

            if (settings.TorSettings.BridgeRelay.HasValue)
            {
                dictionary["BridgeRelay"] = settings.TorSettings.BridgeRelay.Value ? "1" : "0";
            }

            if (!string.IsNullOrWhiteSpace(settings.TorSettings.Nickname))
            {
                dictionary["Nickname"] = settings.TorSettings.Nickname;
            }

            if (!string.IsNullOrWhiteSpace(settings.TorSettings.ContactInfo))
            {
                dictionary["ContactInfo"] = settings.TorSettings.ContactInfo;
            }

            if (!string.IsNullOrWhiteSpace(settings.TorSettings.ServerTransportListenAddr))
            {
                dictionary["ServerTransportListenAddr"] = settings.TorSettings.ServerTransportListenAddr;
            }

            if (!string.IsNullOrWhiteSpace(settings.TorSettings.ExitPolicy))
            {
                dictionary["ExitPolicy"] = settings.TorSettings.ExitPolicy;
            }

            if (!string.IsNullOrWhiteSpace(settings.TorSettings.PublishServerDescriptor))
            {
                dictionary["PublishServerDescriptor"] = settings.TorSettings.PublishServerDescriptor;
            }

            if (!string.IsNullOrWhiteSpace(settings.TorSettings.ExtORPort))
            {
                dictionary["ExtORPort"] = settings.TorSettings.ExtORPort;
            }

            if (!string.IsNullOrWhiteSpace(settings.TorSettings.ORPort))
            {
                dictionary["ORPort"] = settings.TorSettings.ORPort;
            }

            if (!string.IsNullOrWhiteSpace(settings.TorSettings.ClientTransportPlugin))
            {
                dictionary["ClientTransportPlugin"] = settings.TorSettings.ClientTransportPlugin;
            }

            if (!string.IsNullOrWhiteSpace(settings.TorSettings.Bridge))
            {
                dictionary["Bridge"] = settings.TorSettings.Bridge;
            }

            if (settings.TorSettings.HashedControlPassword != null)
            {
                dictionary["HashedControlPassword"] = settings.TorSettings.HashedControlPassword;
            }
            else
            {
                dictionary["HashedControlPassword"] = null;
            }

            string dataDictionary;

            if (!string.IsNullOrWhiteSpace(settings.TorSettings.DataDirectory))
            {
                dataDictionary = settings.TorSettings.DataDirectory;
            }
            else
            {
                dataDictionary = Path.Combine(tool.DirectoryPath, "Data", "Tor");
            }

            dictionary["DataDirectory"] = dataDictionary;

            if (!string.IsNullOrWhiteSpace(settings.TorSettings.ExitNodes))
            {
                dictionary["ExitNodes"]   = settings.TorSettings.ExitNodes;
                dictionary["GeoIPFile"]   = Path.Combine(tool.DirectoryPath, "Data", "Tor", "geoip");
                dictionary["GeoIPv6File"] = Path.Combine(tool.DirectoryPath, "Data", "Tor", "geoip6");
            }

            if (settings.TorSettings.StrictNodes.HasValue)
            {
                dictionary["StrictNodes"] = settings.TorSettings.StrictNodes.Value ? "1" : "0";
            }

            if (!string.IsNullOrWhiteSpace(settings.TorSettings.HttpsProxyHost))
            {
                dictionary["HTTPSProxy"] = settings.TorSettings.HttpsProxyHost;

                if (settings.TorSettings.HttpsProxyPort.HasValue)
                {
                    dictionary["HTTPSProxy"] +=
                        ":" + settings.TorSettings.HttpsProxyPort.Value.ToString(CultureInfo.InvariantCulture);
                }
            }

            if (settings.TorSettings.HttpsProxyUsername != null ||
                settings.TorSettings.HttpsProxyPassword != null)
            {
                dictionary["HTTPSProxyAuthenticator"] =
                    $"{settings.TorSettings.HttpsProxyUsername}:{settings.TorSettings.HttpsProxyPassword}";
            }

            return(dictionary);
        }
Beispiel #27
0
        public async Task ApplySettings(string path, TorSharpSettings settings)
        {
            // convert the settings to a dictionary
            var dictionary = _configurationDictionary.GetDictionary(settings);

            // write the new settings
            string temporaryPath = null;

            try
            {
                // write first to a temporary file
                temporaryPath = Path.GetTempFileName();
                TextReader reader;

                // read the existing configuration, if there is some
                if (File.Exists(path))
                {
                    reader = new StreamReader(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None));
                }
                else
                {
                    reader = new StringReader(string.Empty);
                }

                using (reader)
                    using (var writer = new StreamWriter(new FileStream(temporaryPath, FileMode.Create, FileAccess.Write, FileShare.None)))
                    {
                        // traverse each line looking for the designed configuration
                        string originalLine;
                        while ((originalLine = await reader.ReadLineAsync().ConfigureAwait(false)) != null)
                        {
                            string newLine = dictionary.Count > 0 ? _format.UpdateLine(dictionary, originalLine) : originalLine;
                            await writer.WriteLineAsync(newLine).ConfigureAwait(false);
                        }

                        // write the remaining lines
                        foreach (var pair in dictionary.OrderBy(p => p.Key))
                        {
                            string newLine = _format.CreateLine(pair);
                            await writer.WriteLineAsync(newLine).ConfigureAwait(false);
                        }
                    }

                // If the original file exists, remove it before copying over the temporary file.
                if (File.Exists(path))
                {
                    string backupPath = path + ".bak";

                    // If there has already been a backup, delete it.
                    if (File.Exists(backupPath))
                    {
                        File.Delete(backupPath);
                    }

                    // Backup the last config.
                    File.Move(path, backupPath);
                }

                File.Move(temporaryPath, path);
            }
            finally
            {
                if (temporaryPath != null)
                {
                    try
                    {
                        File.Delete(temporaryPath);
                    }
                    catch
                    {
                        // ignored
                    }
                }
            }
        }
Beispiel #28
0
 /// <summary>
 /// Construct the default TriasConfiguration only with the Endpoint Url.
 /// </summary>
 /// <param name="triasUrl">Main Trias-Api-Endpoint Url that should be used.</param>
 public TriasConfiguration(Uri triasUrl)
 {
     TriasUrl         = triasUrl;
     TorSharpSettings = DefaultTorSharpSettings;
 }
        public async Task ApplySettings(string path, TorSharpSettings settings)
        {
            // convert the settings to a dictionary
            var dictionary = _configurationDictionary.GetDictionary(settings);
            
            // write the new settings
            string temporaryPath = null;
            try
            {
                // write first to a temporary file
                temporaryPath = Path.GetTempFileName();
                TextReader reader;

                // read the existing configuration, if there is some
                if (File.Exists(path))
                {
                    reader = new StreamReader(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None));
                }
                else
                {
                    reader = new StringReader(string.Empty);
                }

                using (reader)
                using (var writer = new StreamWriter(new FileStream(temporaryPath, FileMode.Create, FileAccess.Write, FileShare.None)))
                {
                    // traverse each line looking for the designed configuration
                    string originalLine;
                    while ((originalLine = await reader.ReadLineAsync().ConfigureAwait(false)) != null)
                    {
                        string newLine = dictionary.Count > 0 ? _format.UpdateLine(dictionary, originalLine) : originalLine;
                        await writer.WriteLineAsync(newLine).ConfigureAwait(false);
                    }

                    // write the remaining lines
                    foreach (var pair in dictionary.OrderBy(p => p.Key))
                    {
                        string newLine = _format.CreateLine(pair);
                        await writer.WriteLineAsync(newLine).ConfigureAwait(false);
                    }
                }

                // If the original file exists, remove it before copying over the temporary file.
                if (File.Exists(path))
                {
                    string backupPath = path + ".bak";

                    // If there has already been a backup, delete it.
                    if (File.Exists(backupPath))
                    {
                        File.Delete(backupPath);
                    }

                    // Backup the last config.
                    File.Move(path, backupPath);
                }

                File.Move(temporaryPath, path);
            }
            finally
            {
                if (temporaryPath != null)
                {
                    try
                    {
                        File.Delete(temporaryPath);
                    }
                    catch
                    {
                        // ignored
                    }
                }
            }

        }
Beispiel #30
0
 /// <summary>
 /// Default Constructor to load config from Json.
 /// </summary>
 public TriasConfiguration()
 {
     TriasUrl         = null !;
     TorSharpSettings = DefaultTorSharpSettings;
 }
Beispiel #31
0
        private void TraceSettings(TorSharpSettings settings)
        {
            var serializerSettings = new JsonSerializerSettings
            {
                Converters =
                {
                    new StringEnumConverter()
                }
            };

            var json = JsonConvert.SerializeObject(settings, Formatting.Indented, serializerSettings);

            _output.WriteLine("TorSharpSettings:" + Environment.NewLine + json);
        }