From db750afe0d6f85b2be14003f00db72fd818d592d Mon Sep 17 00:00:00 2001 From: Kiminuo Date: Fri, 11 Sep 2020 10:59:39 +0200 Subject: [PATCH] Add TorInstallator class. --- WalletWasabi/Tor/TorInstallator.cs | 50 +++++++++++++++++++++++++++ WalletWasabi/Tor/TorProcessManager.cs | 40 ++------------------- 2 files changed, 52 insertions(+), 38 deletions(-) create mode 100644 WalletWasabi/Tor/TorInstallator.cs diff --git a/WalletWasabi/Tor/TorInstallator.cs b/WalletWasabi/Tor/TorInstallator.cs new file mode 100644 index 00000000000..7081753fdb4 --- /dev/null +++ b/WalletWasabi/Tor/TorInstallator.cs @@ -0,0 +1,50 @@ +using System.IO; +using System.Runtime.InteropServices; +using System.Threading.Tasks; +using WalletWasabi.Helpers; +using WalletWasabi.Logging; + +namespace WalletWasabi.Tor +{ + /// + /// Installs Tor from data-folder.zip and tor-PLATFORM.zip files which are part of Wasabi Wallet distribution. + /// + public class TorInstallator + { + public async static 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}."); + } + } + } +} diff --git a/WalletWasabi/Tor/TorProcessManager.cs b/WalletWasabi/Tor/TorProcessManager.cs index 91efde6e17f..459a73bb7a9 100644 --- a/WalletWasabi/Tor/TorProcessManager.cs +++ b/WalletWasabi/Tor/TorProcessManager.cs @@ -95,7 +95,7 @@ public void Start(bool ensureRunning, string dataDir) if (!File.Exists(settings.TorPath)) { Logger.LogInfo($"Tor instance NOT found at '{settings.TorPath}'. Attempting to acquire it ..."); - InstallTor(settings.TorDir); + TorInstallator.InstallAsync(settings.TorDir).GetAwaiter().GetResult(); } else if (!IoHelpers.CheckExpectedHash(settings.HashSourcePath, Path.Combine(fullBaseDirectory, "TorDaemons"))) { @@ -108,7 +108,7 @@ public void Start(bool ensureRunning, string dataDir) } Directory.Move(settings.TorDir, backupTorDir); - InstallTor(settings.TorDir); + TorInstallator.InstallAsync(settings.TorDir).GetAwaiter().GetResult(); } else { @@ -165,42 +165,6 @@ public void Start(bool ensureRunning, string dataDir) }).Start(); } - private static void InstallTor(string torDir) - { - string torDaemonsDir = Path.Combine(EnvironmentHelpers.GetFullBaseDirectory(), "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-win64.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, waitForExit: true).GetAwaiter().GetResult(); - Logger.LogInfo($"Shell command executed: {chmodTorDirCmd}."); - } - } - /// Opt out Tor with null. public static async Task IsTorRunningAsync(EndPoint torSocks5EndPoint) {