Skip to content

Commit

Permalink
Add TorInstallator class.
Browse files Browse the repository at this point in the history
  • Loading branch information
kiminuo committed Sep 11, 2020
1 parent 68ffd20 commit fc9218f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 38 deletions.
50 changes: 50 additions & 0 deletions 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
{
/// <summary>
/// Installs Tor from <c>data-folder.zip</c> and <c>tor-PLATFORM.zip</c> files which are part of Wasabi Wallet distribution.
/// </summary>
public class TorInstallator
{
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}.");
}
}
}
}
40 changes: 2 additions & 38 deletions WalletWasabi/Tor/TorProcessManager.cs
Expand Up @@ -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")))
{
Expand All @@ -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
{
Expand Down Expand Up @@ -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}.");
}
}

/// <param name="torSocks5EndPoint">Opt out Tor with null.</param>
public static async Task<bool> IsTorRunningAsync(EndPoint torSocks5EndPoint)
{
Expand Down

0 comments on commit fc9218f

Please sign in to comment.