示例#1
0
        /// <summary>
        /// Verifies that Tor is installed and checksums of installed binaries are correct.
        /// </summary>
        /// <returns>Returns <c>true</c> if <see cref="TorSettings.TorBinaryFilePath"/> is present, <c>false</c> otherwise.</returns>
        public async Task <bool> VerifyInstallationAsync()
        {
            try
            {
                if (!File.Exists(Settings.TorBinaryFilePath))
                {
                    Logger.LogInfo($"Tor instance NOT found at '{Settings.TorBinaryFilePath}'. Attempting to acquire it.");
                    return(await InstallAsync().ConfigureAwait(false));
                }
                else if (!IoHelpers.CheckExpectedHash(Settings.HashSourcePath, Settings.DistributionFolder))
                {
                    Logger.LogInfo("Install the latest Tor version.");
                    string backupDir = $"{Settings.TorDir}_backup";

                    if (Directory.Exists(backupDir))
                    {
                        Directory.Delete(backupDir, recursive: true);
                    }

                    Directory.Move(Settings.TorDir, backupDir);
                    return(await InstallAsync().ConfigureAwait(false));
                }
                else
                {
                    Logger.LogInfo($"Tor instance found at '{Settings.TorBinaryFilePath}'.");
                    return(true);
                }
            }
            catch (Exception e)
            {
                Logger.LogError("Verification of Tor installation failed.", e);
                return(false);
            }
        }
示例#2
0
        public static async Task EnsureHwiInstalledAsync(string dataDir, Network network, bool logFound = true)
        {
            Network = network;

            var fullBaseDirectory = Path.GetFullPath(AppContext.BaseDirectory);

            if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                if (!fullBaseDirectory.StartsWith('/'))
                {
                    fullBaseDirectory.Insert(0, "/");
                }
            }

            var hwiDir = Path.Combine(dataDir, "hwi");

            string hwiPath;

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                hwiPath = $@"{hwiDir}\hwi.exe";
            }
            else             // Linux or OSX
            {
                hwiPath = $@"{hwiDir}/hwi";
            }

            if (!File.Exists(hwiPath))
            {
                Logger.LogInfo($"HWI instance NOT found at {hwiPath}. Attempting to acquire it...", nameof(HwiProcessManager));
                await InstallHwiAsync(fullBaseDirectory, hwiDir);
            }
            else if (!IoHelpers.CheckExpectedHash(hwiPath, Path.Combine(fullBaseDirectory, "Hwi", "Software")))
            {
                Logger.LogInfo($"Updating HWI...", nameof(HwiProcessManager));

                string backupHwiDir = $"{hwiDir}_backup";
                if (Directory.Exists(backupHwiDir))
                {
                    Directory.Delete(backupHwiDir, true);
                }
                Directory.Move(hwiDir, backupHwiDir);

                await InstallHwiAsync(fullBaseDirectory, hwiDir);
            }
            else
            {
                if (logFound)
                {
                    Logger.LogInfo($"HWI instance found at {hwiPath}.", nameof(HwiProcessManager));
                }
            }

            HwiPath = hwiPath;
        }
示例#3
0
        public void Start(bool ensureRunning, string dataDir)
        {
            if (TorSocks5EndPoint is null)
            {
                return;
            }

            new Thread(delegate()              // Don't 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 <TorProcessManager>("Tor is already running.");
                            return;
                        }

                        var fullBaseDirectory = Path.GetFullPath(AppContext.BaseDirectory);
                        if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                        {
                            if (!fullBaseDirectory.StartsWith('/'))
                            {
                                fullBaseDirectory.Insert(0, "/");
                            }
                        }

                        var torDir = Path.Combine(dataDir, "tor");

                        var torPath = "";
                        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                        {
                            torPath = $@"{torDir}\Tor\tor.exe";
                        }
                        else                         // Linux or OSX
                        {
                            torPath = $@"{torDir}/Tor/tor";
                        }

                        if (!File.Exists(torPath))
                        {
                            Logger.LogInfo <TorProcessManager>($"Tor instance NOT found at {torPath}. Attempting to acquire it...");
                            InstallTor(fullBaseDirectory, torDir);
                        }
                        else if (!IoHelpers.CheckExpectedHash(torPath, Path.Combine(fullBaseDirectory, "TorDaemons")))
                        {
                            Logger.LogInfo <TorProcessManager>($"Updating Tor...");

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

                            InstallTor(fullBaseDirectory, torDir);
                        }
                        else
                        {
                            Logger.LogInfo <TorProcessManager>($"Tor instance found at {torPath}.");
                        }

                        string torArguments = $"--SOCKSPort {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               = torPath,
                                Arguments              = torArguments,
                                UseShellExecute        = false,
                                CreateNoWindow         = true,
                                RedirectStandardOutput = true
                            });
                            Logger.LogInfo <TorProcessManager>($"Starting Tor process with Process.Start.");
                        }
                        else                         // Linux and OSX
                        {
                            string runTorCmd = $"LD_LIBRARY_PATH=$LD_LIBRARY_PATH:={torDir}/Tor && export LD_LIBRARY_PATH && cd {torDir}/Tor && ./tor {torArguments}";
                            EnvironmentHelpers.ShellExec(runTorCmd, false);
                            Logger.LogInfo <TorProcessManager>($"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 <TorProcessManager>("Tor is running.");
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new TorException("Could not automatically start Tor. Try running Tor manually.", ex);
                    }
                }
                catch (Exception ex)
                {
                    Logger.LogError <TorProcessManager>(ex);
                }
            }).Start();
        }