-
Notifications
You must be signed in to change notification settings - Fork 492
/
TorSettings.cs
120 lines (98 loc) · 4.62 KB
/
TorSettings.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using WalletWasabi.Extensions;
using WalletWasabi.Helpers;
using WalletWasabi.Microservices;
namespace WalletWasabi.Tor;
/// <summary>
/// All Tor-related settings.
/// </summary>
public class TorSettings
{
/// <summary>Tor binary file name without extension.</summary>
private const string TorBinaryFileName = "tor";
/// <param name="dataDir">Application data directory.</param>
/// <param name="distributionFolderPath">Full path to folder containing Tor installation files.</param>
public TorSettings(string dataDir, string distributionFolderPath, bool terminateOnExit, int? owningProcessId = null)
{
TorBinaryFilePath = GetTorBinaryFilePath();
TorBinaryDir = Path.Combine(MicroserviceHelpers.GetBinaryFolder(), "Tor");
TorDataDir = Path.Combine(dataDir, "tordata2");
CookieAuthFilePath = Path.Combine(dataDir, "control_auth_cookie");
LogFilePath = Path.Combine(dataDir, "TorLogs.txt");
IoHelpers.EnsureContainingDirectoryExists(LogFilePath);
DistributionFolder = distributionFolderPath;
TerminateOnExit = terminateOnExit;
OwningProcessId = owningProcessId;
GeoIpPath = Path.Combine(DistributionFolder, "Tor", "Geoip", "geoip");
GeoIp6Path = Path.Combine(DistributionFolder, "Tor", "Geoip", "geoip6");
}
/// <summary>Full directory path where Tor binaries are placed.</summary>
public string TorBinaryDir { get; }
/// <summary>Full directory path where Tor stores its data.</summary>
public string TorDataDir { get; }
/// <summary>Full path. Directory may not necessarily exist.</summary>
public string LogFilePath { get; }
/// <summary>Full Tor distribution folder where Tor installation files are located.</summary>
public string DistributionFolder { get; }
/// <summary>Whether Tor should be terminated when Wasabi Wallet terminates.</summary>
public bool TerminateOnExit { get; }
/// <summary>Owning process ID for Tor program.</summary>
public int? OwningProcessId { get; }
/// <summary>Full path to executable file that is used to start Tor process.</summary>
public string TorBinaryFilePath { get; }
/// <summary>Full path to Tor cookie file.</summary>
public string CookieAuthFilePath { get; }
/// <summary>Tor SOCKS5 endpoint.</summary>
public EndPoint SocksEndpoint { get; } = new IPEndPoint(IPAddress.Loopback, 37150);
/// <summary>Tor control endpoint.</summary>
public EndPoint ControlEndpoint { get; set; } = new IPEndPoint(IPAddress.Loopback, 37151);
private string GeoIpPath { get; }
private string GeoIp6Path { get; }
/// <returns>Full path to Tor binary for selected <paramref name="platform"/>.</returns>
public static string GetTorBinaryFilePath(OSPlatform? platform = null)
{
platform ??= MicroserviceHelpers.GetCurrentPlatform();
string binaryPath = MicroserviceHelpers.GetBinaryPath(Path.Combine("Tor", TorBinaryFileName), platform);
return platform == OSPlatform.OSX ? $"{binaryPath}.real" : binaryPath;
}
/// <returns>Tor binary file name for selected <paramref name="platform"/>.</returns>
public static string GetTorBinaryFileName(OSPlatform? platform = null)
{
platform ??= MicroserviceHelpers.GetCurrentPlatform();
return platform == OSPlatform.OSX ? $"{TorBinaryFileName}.real" : TorBinaryFileName;
}
/// <seealso href="https://github.com/torproject/tor/blob/7528524aee3ffe3c9b7c69fa18f659e1993f59a3/doc/man/tor.1.txt#L1505-L1509">For <c>KeepAliveIsolateSOCKSAuth</c> explanation.</seealso>
/// <seealso href="https://github.com/torproject/tor/blob/22cb4c23d0d23dfda2c91817bac74a01831f94af/doc/man/tor.1.txt#L1298-L1305">
/// Explains <c>MaxCircuitDirtiness</c> parameter which is affected by the <c>KeepAliveIsolateSOCKSAuth</c> flag.
/// </seealso>
public string GetCmdArguments()
{
if (!ControlEndpoint.TryGetPort(out int? port))
{
port = 9051; // Standard port for Tor control.
}
// `--SafeLogging 0` is useful for debugging to avoid "[scrubbed]" redactions in Tor log.
List<string> arguments = new()
{
$"--LogTimeGranularity 1",
$"--SOCKSPort \"{SocksEndpoint} ExtendedErrors KeepAliveIsolateSOCKSAuth\"",
$"--MaxCircuitDirtiness 1800", // 30 minutes. Default is 10 minutes.
$"--SocksTimeout 30", // Default is 2 minutes.
$"--CookieAuthentication 1",
$"--ControlPort {port}",
$"--CookieAuthFile \"{CookieAuthFilePath}\"",
$"--DataDirectory \"{TorDataDir}\"",
$"--GeoIPFile \"{GeoIpPath}\"",
$"--GeoIPv6File \"{GeoIp6Path}\"",
$"--Log \"notice file {LogFilePath}\""
};
if (TerminateOnExit && OwningProcessId is not null)
{
arguments.Add($"__OwningControllerProcess {OwningProcessId}");
}
return string.Join(" ", arguments);
}
}