Add TorSettings & test.
ADD
WalletWasabi.Tests/UnitTests/TorSocks5/TorSettingsTests.cs
+using System;
+using System.Collections.Generic;
+using System.Net;
+using System.Runtime.InteropServices;
+using System.Text;
+using WalletWasabi.TorSocks5;
+using Xunit;
+
+namespace WalletWasabi.Tests.UnitTests.TorSocks5
+{
+ /// <summary>
+ /// Tests for <see cref="WalletWasabi.TorSocks5.TorSettings"/> class.
+ /// </summary>
+ public class TorSettingsTests
+ {
+ [Fact]
+ public void GetCmdArgumentsTest()
+ {
+ var settings = new TorSettings("temp");
+ var endpoint = new IPEndPoint(IPAddress.Loopback, WalletWasabi.Helpers.Constants.DefaultTorSocksPort);
+
+ string arguments = settings.GetCmdArguments(endpoint);
+ string expected;
+
+ if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
+ {
+ expected = @"--SOCKSPort 127.0.0.1:9050 --DataDirectory ""temp\tordata"" --GeoIPFile ""temp\tor\Data\Tor\geoip"" GeoIPv6File ""temp\tor\Data\Tor\geoip6""";
+ }
+ else
+ {
+ expected = @"--SOCKSPort 127.0.0.1:9050 --DataDirectory ""temp/tordata"" --GeoIPFile ""temp/tor/Data/Tor/geoip"" GeoIPv6File ""temp/tor/Data/Tor/geoip6""";
+ }
+
+ Assert.Equal(expected, arguments);
+ }
+ }
+}

MODIFY
WalletWasabi/TorSocks5/TorProcessManager.cs
WalletWasabi/TorSocks5/TorProcessManager.cs
@@ -75,54 +75,34 @@ namespace WalletWasabi.TorSocks5
return;
}
- var torDir = Path.Combine(dataDir, "tor");
- var torDataDir = Path.Combine(dataDir, "tordata");
- var torPath = "";
- var hashSourcePath = "";
- var geoIpPath = "";
- var geoIp6Path = "";
var fullBaseDirectory = EnvironmentHelpers.GetFullBaseDirectory();
- if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
- {
- torPath = $@"{torDir}/Tor/tor";
- hashSourcePath = RuntimeInformation.IsOSPlatform(OSPlatform.OSX)
- ? $@"{torDir}/Tor/tor.real"
- : $@"{torDir}/Tor/tor";
- geoIpPath = $@"{torDir}/Data/Tor/geoip";
- geoIp6Path = $@"{torDir}/Data/Tor/geoip6";
- }
- else // If Windows
- {
- torPath = $@"{torDir}\Tor\tor.exe";
- hashSourcePath = $@"{torDir}\Tor\tor.exe";
- geoIpPath = $@"{torDir}\Data\Tor\geoip";
- geoIp6Path = $@"{torDir}\Data\Tor\geoip6";
- }
+ var settings = new TorSettings(dataDir);
- if (!File.Exists(torPath))
+ if (!File.Exists(settings.TorPath))
{
- Logger.LogInfo($"Tor instance NOT found at '{torPath}'. Attempting to acquire it ...");
- InstallTor(torDir);
+ Logger.LogInfo($"Tor instance NOT found at '{settings.TorPath}'. Attempting to acquire it ...");
+ InstallTor(settings.TorDir);
}
- else if (!IoHelpers.CheckExpectedHash(hashSourcePath, Path.Combine(fullBaseDirectory, "TorDaemons")))
+ else if (!IoHelpers.CheckExpectedHash(settings.HashSourcePath, Path.Combine(fullBaseDirectory, "TorDaemons")))
{
Logger.LogInfo($"Updating Tor...");
- string backupTorDir = $"{torDir}_backup";
+ string backupTorDir = $"{settings.TorDir}_backup";
if (Directory.Exists(backupTorDir))
{
Directory.Delete(backupTorDir, true);
}
- Directory.Move(torDir, backupTorDir);
+ Directory.Move(settings.TorDir, backupTorDir);
- InstallTor(torDir);
+ InstallTor(settings.TorDir);
}
else
{
- Logger.LogInfo($"Tor instance found at '{torPath}'.");
+ Logger.LogInfo($"Tor instance found at '{settings.TorPath}'.");
}
- string torArguments = $"--SOCKSPort {TorSocks5EndPoint} --DataDirectory \"{torDataDir}\" --GeoIPFile \"{geoIpPath}\" GeoIPv6File \"{geoIp6Path}\"";
+ string torArguments = settings.GetCmdArguments(TorSocks5EndPoint);
+
if (!string.IsNullOrEmpty(LogFile))
{
IoHelpers.EnsureContainingDirectoryExists(LogFile);
@@ -134,7 +114,7 @@ namespace WalletWasabi.TorSocks5
{
TorProcess = Process.Start(new ProcessStartInfo
{
- FileName = torPath,
+ FileName = settings.TorPath,
Arguments = torArguments,
UseShellExecute = false,
CreateNoWindow = true,
@@ -144,7 +124,7 @@ namespace WalletWasabi.TorSocks5
}
else // Linux and OSX
{
- string runTorCmd = $"LD_LIBRARY_PATH=$LD_LIBRARY_PATH:='{torDir}/Tor' && export LD_LIBRARY_PATH && cd '{torDir}/Tor' && ./tor {torArguments}";
+ string runTorCmd = $"LD_LIBRARY_PATH=$LD_LIBRARY_PATH:='{settings.TorDir}/Tor' && export LD_LIBRARY_PATH && cd '{settings.TorDir}/Tor' && ./tor {torArguments}";
EnvironmentHelpers.ShellExecAsync(runTorCmd, false).GetAwaiter().GetResult();
Logger.LogInfo($"Started Tor process with shell command: {runTorCmd}.");
}

ADD
WalletWasabi/TorSocks5/TorSettings.cs
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Net;
+using System.Runtime.InteropServices;
+using System.Text;
+using WalletWasabi.Helpers;
+
+namespace WalletWasabi.TorSocks5
+{
+ public class TorSettings
+ {
+ public TorSettings(string dataDir)
+ {
+ TorDir = Path.Combine(dataDir, "tor");
+ TorDataDir = Path.Combine(dataDir, "tordata");
+
+ if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
+ {
+ TorPath = $@"{TorDir}\Tor\tor.exe";
+ HashSourcePath = $@"{TorDir}\Tor\tor.exe";
+ GeoIpPath = $@"{TorDir}\Data\Tor\geoip";
+ GeoIp6Path = $@"{TorDir}\Data\Tor\geoip6";
+ }
+ else
+ {
+ TorPath = $@"{TorDir}/Tor/tor";
+ HashSourcePath = RuntimeInformation.IsOSPlatform(OSPlatform.OSX)
+ ? $@"{TorDir}/Tor/tor.real"
+ : $@"{TorDir}/Tor/tor";
+ GeoIpPath = $@"{TorDir}/Data/Tor/geoip";
+ GeoIp6Path = $@"{TorDir}/Data/Tor/geoip6";
+ }
+ }
+
+ public string TorDir { get; }
+ public string TorDataDir { get; }
+ public string HashSourcePath { get; }
+ public string TorPath { get; }
+ private string GeoIpPath { get; }
+ private string GeoIp6Path { get; }
+
+ public string GetCmdArguments(EndPoint torSocks5EndPoint)
+ {
+ return $"--SOCKSPort {torSocks5EndPoint} " +
+ $"--DataDirectory \"{TorDataDir}\" " +
+ $"--GeoIPFile \"{GeoIpPath}\" GeoIPv6File \"{GeoIp6Path}\"";
+ }
+ }
+}

Add TorSettings & test.