Esempio n. 1
0
        /// <summary>
        /// Installs Tor for Wasabi Wallet use.
        /// </summary>
        /// <returns>Returns <c>true</c> if <see cref="TorSettings.TorBinaryFilePath"/> is present after installation, <c>false</c> otherwise.</returns>
        public async Task <bool> InstallAsync()
        {
            try
            {
                // Common for all platforms.
                await ExtractZipFileAsync(Path.Combine(Settings.DistributionFolder, "data-folder.zip"), Settings.TorDir).ConfigureAwait(false);

                // File differs per platform.
                await ExtractZipFileAsync(Path.Combine(Settings.DistributionFolder, $"tor-{GetPlatformIdentifier()}.zip"), Settings.TorDir).ConfigureAwait(false);

                if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    // Set sufficient file permission.
                    string shellCommand = $"chmod -R 750 {Settings.TorDir}";
                    await EnvironmentHelpers.ShellExecAsync(shellCommand, waitForExit : true).ConfigureAwait(false);

                    Logger.LogInfo($"Shell command executed: '{shellCommand}'.");
                }

                bool verification = File.Exists(Settings.TorBinaryFilePath);

                Logger.LogDebug($"Tor installation finished. Installed correctly? {verification}.");
                return(verification);
            }
            catch (Exception e)
            {
                Logger.LogError("Tor installation failed.", e);
            }

            return(false);
        }
Esempio n. 2
0
 public static async Task ModifyStartupSettingAsync(bool runOnSystemStartup)
 {
     if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
     {
         string pathToExeFile = EnvironmentHelpers.GetExecutablePath();
         if (!File.Exists(pathToExeFile))
         {
             throw new InvalidOperationException($"Path {pathToExeFile} does not exist.");
         }
         StartOnWindowsStartup(runOnSystemStartup, pathToExeFile);
     }
     else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
     {
         throw new NotImplementedException();
     }
     else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
     {
         if (runOnSystemStartup)
         {
             await EnvironmentHelpers.ShellExecAsync(AddCmd).ConfigureAwait(false);
         }
         else
         {
             await EnvironmentHelpers.ShellExecAsync(DeleteCmd).ConfigureAwait(false);
         }
     }
 }
Esempio n. 3
0
        public static async Task OpenFileInTextEditorAsync(string filePath)
        {
            if (File.Exists(filePath))
            {
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                {
                    // If no associated application/json MimeType is found xdg-open opens retrun error
                    // but it tries to open it anyway using the console editor (nano, vim, other..)
                    await EnvironmentHelpers.ShellExecAsync($"which gedit &> /dev/null && gedit {filePath} || xdg-open {filePath}", waitForExit : false).ConfigureAwait(false);
                }
                else
                {
                    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                    {
                        bool openWithNotepad = true;                         // If there is an exception with the registry read we use notepad.

                        try
                        {
                            openWithNotepad = !EnvironmentHelpers.IsFileTypeAssociated("json");
                        }
                        catch (Exception ex)
                        {
                            Logger.LogError(ex);
                        }

                        if (openWithNotepad)
                        {
                            // Open file using Notepad.
                            using Process notepadProcess = Process.Start(new ProcessStartInfo
                            {
                                FileName        = "notepad.exe",
                                Arguments       = filePath,
                                CreateNoWindow  = true,
                                UseShellExecute = false
                            });
                            return;                             // Opened with notepad, return.
                        }
                    }

                    // Open file with the default editor.
                    using Process defaultEditorProcess = Process.Start(new ProcessStartInfo
                    {
                        FileName        = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? filePath : "open",
                        Arguments       = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? $"-e {filePath}" : "",
                        CreateNoWindow  = true,
                        UseShellExecute = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
                    });
                }
            }
            else
            {
                NotificationHelpers.Error("File not found.");
            }
        }
 public static async Task AddOrRemoveLoginItemAsync(bool runOnSystemStartup)
 {
     if (runOnSystemStartup)
     {
         await EnvironmentHelpers.ShellExecAsync(AddCmd).ConfigureAwait(false);
     }
     else
     {
         await EnvironmentHelpers.ShellExecAsync(DeleteCmd).ConfigureAwait(false);
     }
 }
Esempio n. 5
0
 public static async Task OpenBrowserAsync(string url)
 {
     if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
     {
         // If no associated application/json MimeType is found xdg-open opens retrun error
         // but it tries to open it anyway using the console editor (nano, vim, other..)
         await EnvironmentHelpers.ShellExecAsync($"xdg-open {url}", waitForExit : false).ConfigureAwait(false);
     }
     else
     {
         using Process process = Process.Start(new ProcessStartInfo
         {
             FileName        = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? url : "open",
             Arguments       = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? $"-e {url}" : "",
             CreateNoWindow  = true,
             UseShellExecute = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
         });
     }
 }
Esempio n. 6
0
        public static async Task InstallAsync(string torDir)
        {
            string torDaemonsDir = Path.Combine(EnvironmentHelpers.GetFullBaseDirectory(), "TorDaemons");

            string dataZip = Path.Combine(torDaemonsDir, "data-folder.zip");
            await IoHelpers.BetterExtractZipToDirectoryAsync(dataZip, torDir).ConfigureAwait(false);

            Logger.LogInfo($"Extracted {dataZip} to {torDir}.");

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                string torWinZip = Path.Combine(torDaemonsDir, "tor-win64.zip");
                await IoHelpers.BetterExtractZipToDirectoryAsync(torWinZip, torDir).ConfigureAwait(false);

                Logger.LogInfo($"Extracted {torWinZip} to {torDir}.");
            }
            else             // Linux or OSX
            {
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                {
                    string torLinuxZip = Path.Combine(torDaemonsDir, "tor-linux64.zip");
                    await IoHelpers.BetterExtractZipToDirectoryAsync(torLinuxZip, torDir).ConfigureAwait(false);

                    Logger.LogInfo($"Extracted {torLinuxZip} to {torDir}.");
                }
                else                 // OSX
                {
                    string torOsxZip = Path.Combine(torDaemonsDir, "tor-osx64.zip");
                    await IoHelpers.BetterExtractZipToDirectoryAsync(torOsxZip, torDir).ConfigureAwait(false);

                    Logger.LogInfo($"Extracted {torOsxZip} to {torDir}.");
                }

                // Make sure there's sufficient permission.
                string chmodTorDirCmd = $"chmod -R 750 {torDir}";
                await EnvironmentHelpers.ShellExecAsync(chmodTorDirCmd, waitForExit : true).ConfigureAwait(false);

                Logger.LogInfo($"Shell command executed: {chmodTorDirCmd}.");
            }
        }
Esempio n. 7
0
        private static void InstallTor(string fullBaseDirectory, string torDir)
        {
            string torDaemonsDir = Path.Combine(fullBaseDirectory, "TorDaemons");

            string dataZip = Path.Combine(torDaemonsDir, "data-folder.zip");

            IoHelpers.BetterExtractZipToDirectoryAsync(dataZip, torDir).GetAwaiter().GetResult();
            Logger.LogInfo($"Extracted {dataZip} to {torDir}.");

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                string torWinZip = Path.Combine(torDaemonsDir, "tor-win32.zip");
                IoHelpers.BetterExtractZipToDirectoryAsync(torWinZip, torDir).GetAwaiter().GetResult();
                Logger.LogInfo($"Extracted {torWinZip} to {torDir}.");
            }
            else             // Linux or OSX
            {
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                {
                    string torLinuxZip = Path.Combine(torDaemonsDir, "tor-linux64.zip");
                    IoHelpers.BetterExtractZipToDirectoryAsync(torLinuxZip, torDir).GetAwaiter().GetResult();
                    Logger.LogInfo($"Extracted {torLinuxZip} to {torDir}.");
                }
                else                 // OSX
                {
                    string torOsxZip = Path.Combine(torDaemonsDir, "tor-osx64.zip");
                    IoHelpers.BetterExtractZipToDirectoryAsync(torOsxZip, torDir).GetAwaiter().GetResult();
                    Logger.LogInfo($"Extracted {torOsxZip} to {torDir}.");
                }

                // Make sure there's sufficient permission.
                string chmodTorDirCmd = $"chmod -R 750 {torDir}";
                EnvironmentHelpers.ShellExecAsync(chmodTorDirCmd).GetAwaiter().GetResult();
                Logger.LogInfo($"Shell command executed: {chmodTorDirCmd}.");
            }
        }
Esempio n. 8
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 torDir            = Path.Combine(dataDir, "tor");
                        var torPath           = "";
                        var fullBaseDirectory = Path.GetFullPath(AppContext.BaseDirectory);
                        if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                        {
                            if (!fullBaseDirectory.StartsWith('/'))
                            {
                                fullBaseDirectory = fullBaseDirectory.Insert(0, "/");
                            }

                            torPath = $@"{torDir}/Tor/tor";
                        }
                        else                         // If Windows
                        {
                            torPath = $@"{torDir}\Tor\tor.exe";
                        }

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

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

                            InstallTor(fullBaseDirectory, torDir);
                        }
                        else
                        {
                            Logger.LogInfo($"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($"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.ShellExecAsync(runTorCmd, 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();
        }