Ejemplo n.º 1
0
 public TorProcessManager(TorSettings settings)
 {
     TorProcess           = null;
     TorControlClient     = null;
     Settings             = settings;
     TcpConnectionFactory = new(settings.SocksEndpoint);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a new instance of the object.
        /// </summary>
        /// <param name="settings">Tor settings.</param>
        /// <param name="torSocks5EndPoint">Valid Tor end point.</param>
        public TorProcessManager(TorSettings settings, EndPoint torSocks5EndPoint)
        {
            TorSocks5EndPoint = torSocks5EndPoint;
            TorProcess        = null;
            Settings          = settings;
            TorSocks5Client   = new TorSocks5Client(torSocks5EndPoint);

            IoHelpers.EnsureContainingDirectoryExists(Settings.LogFilePath);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates a new instance of the object.
        /// </summary>
        /// <param name="settings">Tor settings.</param>
        /// <param name="torSocks5EndPoint">Valid Tor end point.</param>
        public TorProcessManager(TorSettings settings, EndPoint torSocks5EndPoint)
        {
            TorSocks5EndPoint = torSocks5EndPoint;
            _monitorState     = StateNotStarted;
            Stop            = new CancellationTokenSource();
            TorProcess      = null;
            Settings        = settings;
            TorSocks5Client = new TorSocks5Client(torSocks5EndPoint);

            IoHelpers.EnsureContainingDirectoryExists(Settings.LogFilePath);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Creates a new instance.
 /// </summary>
 public TorInstallator(TorSettings settings)
 {
     Settings = settings;
 }
Ejemplo n.º 5
0
        public void Start(bool ensureRunning, string dataDir)
        {
            if (TorSocks5EndPoint is null)
            {
                return;
            }

            new Thread(delegate()              // Do not ask. This is the only way it worked on Win10/Ubuntu18.04/Manjuro(1 processor VM)/Fedora(1 processor VM)
            {
                try
                {
                    // 1. Is it already running?
                    // 2. Can I simply run it from output directory?
                    // 3. Can I copy and unzip it from assets?
                    // 4. Throw exception.

                    try
                    {
                        if (IsTorRunningAsync(TorSocks5EndPoint).GetAwaiter().GetResult())
                        {
                            Logger.LogInfo("Tor is already running.");
                            return;
                        }

                        var fullBaseDirectory = EnvironmentHelpers.GetFullBaseDirectory();
                        var settings          = new TorSettings(dataDir);

                        if (!File.Exists(settings.TorPath))
                        {
                            Logger.LogInfo($"Tor instance NOT found at '{settings.TorPath}'. Attempting to acquire it ...");
                            TorInstallator.InstallAsync(settings.TorDir).GetAwaiter().GetResult();
                        }
                        else if (!IoHelpers.CheckExpectedHash(settings.HashSourcePath, Path.Combine(fullBaseDirectory, "TorDaemons")))
                        {
                            Logger.LogInfo($"Updating Tor...");

                            string backupTorDir = $"{settings.TorDir}_backup";
                            if (Directory.Exists(backupTorDir))
                            {
                                Directory.Delete(backupTorDir, true);
                            }
                            Directory.Move(settings.TorDir, backupTorDir);

                            TorInstallator.InstallAsync(settings.TorDir).GetAwaiter().GetResult();
                        }
                        else
                        {
                            Logger.LogInfo($"Tor instance found at '{settings.TorPath}'.");
                        }

                        string torArguments = settings.GetCmdArguments(TorSocks5EndPoint);

                        if (!string.IsNullOrEmpty(LogFile))
                        {
                            IoHelpers.EnsureContainingDirectoryExists(LogFile);
                            var logFileFullPath = Path.GetFullPath(LogFile);
                            torArguments       += $" --Log \"notice file {logFileFullPath}\"";
                        }

                        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                        {
                            TorProcess = Process.Start(new ProcessStartInfo
                            {
                                FileName               = settings.TorPath,
                                Arguments              = torArguments,
                                UseShellExecute        = false,
                                CreateNoWindow         = true,
                                RedirectStandardOutput = true
                            });
                            Logger.LogInfo($"Starting Tor process with Process.Start.");
                        }
                        else                         // Linux and OSX
                        {
                            string runTorCmd = $"LD_LIBRARY_PATH=$LD_LIBRARY_PATH:='{settings.TorDir}/Tor' && export LD_LIBRARY_PATH && cd '{settings.TorDir}/Tor' && ./tor {torArguments}";
                            EnvironmentHelpers.ShellExecAsync(runTorCmd, waitForExit: false).GetAwaiter().GetResult();
                            Logger.LogInfo($"Started Tor process with shell command: {runTorCmd}.");
                        }

                        if (ensureRunning)
                        {
                            Task.Delay(3000).ConfigureAwait(false).GetAwaiter().GetResult();                             // dotnet brainfart, ConfigureAwait(false) IS NEEDED HERE otherwise (only on) Manjuro Linux fails, WTF?!!
                            if (!IsTorRunningAsync(TorSocks5EndPoint).GetAwaiter().GetResult())
                            {
                                throw new TorException("Attempted to start Tor, but it is not running.");
                            }
                            Logger.LogInfo("Tor is running.");
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new TorException("Could not automatically start Tor. Try running Tor manually.", ex);
                    }
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex);
                }
            }).Start();
        }