Example #1
0
        private static async Task StartTorAndPrivoxyAsync(int torSocksPort, int privoxyPort)
        {
            var settings = new TorSharpSettings { TorSocksPort = torSocksPort, PrivoxyPort = privoxyPort };
            var fetcher = new TorSharpToolFetcher(settings, new HttpClient());
            await fetcher.FetchAsync();

            var proxy = new TorSharpProxy(settings);
            await proxy.ConfigureAndStartAsync();
        }
Example #2
0
 public TorSharpProxy(TorSharpSettings settings)
 {
     _settings = settings;
     _torPasswordHasher = new TorPasswordHasher(new RandomFactory());
     _toolRunner =
         settings.ToolRunnerType == ToolRunnerType.VirtualDesktop
             ? (IToolRunner)new VirtualDesktopToolRunner()
             : new SimpleToolRunner();
 }
Example #3
0
        /// <summary>
        /// Read the <see cref="TorSharpSettings.ZippedToolsDirectory"/> and find the latest tool matching the criteria
        /// in the provided <paramref name="toolSettings"/>. If none is found return null.
        /// </summary>
        /// /// <param name="settings">The settings for TorSharp.</param>
        /// <param name="toolSettings">The settings for the tool.</param>
        /// <returns>The tool, or null if none was found.</returns>
        public static Tool GetLatestToolOrNull(
            TorSharpSettings settings,
            ToolSettings toolSettings)
        {
            if (!Directory.Exists(settings.ZippedToolsDirectory))
            {
                return(null);
            }

            var fileExtension = ArchiveUtility.GetFileExtension(toolSettings.ZippedToolFormat);
            var pattern       = $"{toolSettings.Prefix}*{fileExtension}";

            string[] zipPaths = Directory
                                .EnumerateFiles(settings.ZippedToolsDirectory, pattern, SearchOption.TopDirectoryOnly)
                                .ToArray();

            var versions = new List <Tool>();

            foreach (string zipPath in zipPaths)
            {
                string fileName         = Path.GetFileName(zipPath);
                string withoutExtension = fileName.Substring(0, fileName.Length - fileExtension.Length);
                string directoryPath    = Path.Combine(settings.ExtractedToolsDirectory, withoutExtension);
                string version          = withoutExtension.Substring(toolSettings.Prefix.Length);
                if (!Version.TryParse(version, out var parsedVersion))
                {
                    continue;
                }

                versions.Add(new Tool
                {
                    Settings          = toolSettings,
                    ZipPath           = zipPath,
                    DirectoryPath     = directoryPath,
                    Version           = parsedVersion,
                    ExecutablePath    = Path.Combine(directoryPath, toolSettings.ExecutablePath),
                    WorkingDirectory  = Path.Combine(directoryPath, toolSettings.WorkingDirectory),
                    ConfigurationPath = Path.Combine(directoryPath, toolSettings.ConfigurationPath),
                });
            }

            return(versions
                   .OrderByDescending(t => t.Version)
                   .FirstOrDefault());
        }
Example #4
0
        public TorSharpProxy(TorSharpSettings settings)
        {
            _settings          = settings;
            _torPasswordHasher = new TorPasswordHasher(new RandomFactory());

#if NET45
            if (settings.ToolRunnerType == ToolRunnerType.VirtualDesktop)
            {
                _toolRunner = new VirtualDesktopToolRunner();
            }
            else
            {
                _toolRunner = new SimpleToolRunner();
            }
#else
            _toolRunner = new SimpleToolRunner();
#endif
        }
Example #5
0
        /// <summary>
        /// Initializes an instance of the proxy controller.
        /// </summary>
        /// <param name="settings">The settings to dictate the proxy's behavior.</param>
        public TorSharpProxy(TorSharpSettings settings)
        {
            _settings          = settings ?? throw new ArgumentNullException(nameof(settings));
            _torPasswordHasher = new TorPasswordHasher(new RandomFactory());

            switch (settings.ToolRunnerType)
            {
            case ToolRunnerType.Simple:
                _toolRunner = new SimpleToolRunner();
                break;

            case ToolRunnerType.VirtualDesktop:
                if (settings.OSPlatform != TorSharpOSPlatform.Windows)
                {
                    settings.RejectRuntime($"use the {nameof(ToolRunnerType.VirtualDesktop)} tool runner");
                }
                _toolRunner = new VirtualDesktopToolRunner();
                break;

            default:
                throw new NotImplementedException($"The '{settings.ToolRunnerType}' tool runner is not supported.");
            }
        }
 public TorSharpToolFetcher(TorSharpSettings settings, HttpClient client)
 {
     _settings = settings;
     _privoxyFetcher = new PrivoxyFetcher(client);
     _torFetcher = new TorFetcher(client);   
 }
Example #7
0
        public static ToolSettings GetPrivoxyToolSettings(TorSharpSettings settings)
        {
            if (settings.OSPlatform == TorSharpOSPlatform.Windows)
            {
                return(new ToolSettings
                {
                    Name = PrivoxyName,
                    Prefix = "privoxy-win32",
                    ExecutablePath = "privoxy.exe",
                    WorkingDirectory = ".",
                    ConfigurationPath = "config.txt",
                    GetArguments = t => new[] { '\"' + t.ConfigurationPath + '\"' },
                    GetEnvironmentVariables = t => new Dictionary <string, string>(),
                    ZippedToolFormat = ZippedToolFormat.Zip,
                    GetEntryPath = e =>
                    {
                        var firstSeperatorIndex = e.IndexOfAny(new[] { '/', '\\' });
                        if (firstSeperatorIndex >= 0)
                        {
                            return e.Substring(firstSeperatorIndex + 1);
                        }

                        return e;
                    },
                });
            }
            else if (settings.OSPlatform == TorSharpOSPlatform.Linux)
            {
                var prefix = default(string);
                if (settings.Architecture == TorSharpArchitecture.X86)
                {
                    prefix = "privoxy-linux32-";
                }
                else if (settings.Architecture == TorSharpArchitecture.X64)
                {
                    prefix = "privoxy-linux64-";
                }
                else
                {
                    settings.RejectRuntime("determine Linux Privoxy prefix");
                }

                return(new ToolSettings
                {
                    Name = PrivoxyName,
                    Prefix = prefix,
                    ExecutablePath = Path.Combine("usr", "sbin", "privoxy"),
                    WorkingDirectory = Path.Combine("usr", "sbin"),
                    ConfigurationPath = Path.Combine("usr", "share", "privoxy", "config"),
                    GetArguments = t => new[] { "--no-daemon", '\"' + t.ConfigurationPath + '\"' },
                    GetEnvironmentVariables = t => new Dictionary <string, string>(),
                    ZippedToolFormat = ZippedToolFormat.Deb,
                    GetEntryPath = e =>
                    {
                        if (e.StartsWith("./usr/sbin"))
                        {
                            return e;
                        }
                        else if (e.StartsWith("./usr/share/privoxy"))
                        {
                            return e;
                        }
                        else if (e.StartsWith("./etc/privoxy"))
                        {
                            if (e.StartsWith("./etc/privoxy/templates"))
                            {
                                return null;
                            }

                            return e;
                        }
                        else
                        {
                            return null;
                        }
                    },
                });
            }
            else
            {
                settings.RejectRuntime("run Privoxy");
            }

            throw new NotImplementedException();
        }
Example #8
0
        public static ToolSettings GetTorToolSettings(TorSharpSettings settings)
        {
            if (settings.OSPlatform == TorSharpOSPlatform.Windows)
            {
                return(new ToolSettings
                {
                    Name = TorName,
                    Prefix = "tor-win32-",
                    ExecutablePath = Path.Combine(TorName, "tor.exe"),
                    WorkingDirectory = TorName,
                    ConfigurationPath = Path.Combine("Data", TorName, "torrc"),
                    GetArguments = t => new[] { "-f", '\"' + t.ConfigurationPath + '\"' },
                    GetEnvironmentVariables = t => new Dictionary <string, string>(),
                    ZippedToolFormat = ZippedToolFormat.Zip,
                    GetEntryPath = e => e,
                });
            }
            else if (settings.OSPlatform == TorSharpOSPlatform.Linux)
            {
                var prefix = default(string);
                if (settings.Architecture == TorSharpArchitecture.X86)
                {
                    prefix = "tor-linux32-";
                }
                else if (settings.Architecture == TorSharpArchitecture.X64)
                {
                    prefix = "tor-linux64-";
                }
                else
                {
                    settings.RejectRuntime("determine Linux Tor prefix");
                }

                return(new ToolSettings
                {
                    Name = TorName,
                    Prefix = prefix,
                    ExecutablePath = Path.Combine(TorName, "tor"),
                    WorkingDirectory = TorName,
                    ConfigurationPath = Path.Combine("Data", TorName, "torrc"),
                    GetArguments = t => new[] { "-f", '\"' + t.ConfigurationPath + '\"' },
                    GetEnvironmentVariables = t =>
                    {
                        var output = new Dictionary <string, string>();
                        const string ldLibraryPathKey = "LD_LIBRARY_PATH";
                        var ldLibraryPath = Environment.GetEnvironmentVariable(ldLibraryPathKey);
                        var addedPath = Path.GetDirectoryName(t.ExecutablePath);

                        if (string.IsNullOrWhiteSpace(ldLibraryPath))
                        {
                            ldLibraryPath = addedPath;
                        }
                        else
                        {
                            ldLibraryPath += ":" + addedPath;
                        }

                        output[ldLibraryPathKey] = ldLibraryPath;

                        return output;
                    },
                    ZippedToolFormat = ZippedToolFormat.TarXz,
                    GetEntryPath = e =>
                    {
                        const string entryPrefix = "tor-browser_en-US/Browser/TorBrowser/";
                        if (e.StartsWith(entryPrefix + "Data/Tor/"))
                        {
                            return e.Substring(entryPrefix.Length);
                        }
                        else if (e.StartsWith(entryPrefix + "Tor/"))
                        {
                            if (e.StartsWith(entryPrefix + "Tor/PluggableTransports/"))
                            {
                                return null;
                            }
                            else
                            {
                                return e.Substring(entryPrefix.Length);
                            }
                        }
                        else
                        {
                            return null;
                        }
                    },
                });
            }
            else
            {
                settings.RejectRuntime("run Tor");
            }

            throw new NotImplementedException();
        }
Example #9
0
 public TorSharpProxy(TorSharpSettings settings)
 {
     _settings = settings;
     _toolRunner = new ToolRunner();
 }
Example #10
0
 public TorSharpToolFetcher(TorSharpSettings settings, HttpClient client)
     : this(settings, client, new SimpleHttpClient(client), progress : null)
 {
 }
Example #11
0
 public TorSharpToolFetcher(TorSharpSettings settings, HttpClient client)
 {
     _settings       = settings;
     _privoxyFetcher = new PrivoxyFetcher(client);
     _torFetcher     = new TorFetcher(client);
 }