public static string GetBinaryPath(string binaryNameWithoutExtension)
        {
            var fullBaseDirectory = EnvironmentHelpers.GetFullBaseDirectory();

            string commonPartialPath = Path.Combine(fullBaseDirectory, "Microservices", "Binaries");
            string path;

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                path = Path.Combine(commonPartialPath, $"win64", $"{binaryNameWithoutExtension}.exe");
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                path = Path.Combine(commonPartialPath, $"lin64", binaryNameWithoutExtension);
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                path = Path.Combine(commonPartialPath, $"osx64", binaryNameWithoutExtension);
            }
            else
            {
                throw new NotSupportedException("Operating system is not supported.");
            }

            return(path);
        }
Esempio n. 2
0
        private string GetHwiPath()
        {
            var fullBaseDirectory = EnvironmentHelpers.GetFullBaseDirectory();

            string commonPartialPath = Path.Combine(fullBaseDirectory, "Hwi", "Binaries");
            string path;

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                path = Path.Combine(commonPartialPath, "hwi-win64", "hwi.exe");
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                path = Path.Combine(commonPartialPath, "hwi-lin64", "hwi");
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                path = Path.Combine(commonPartialPath, "hwi-osx64", "hwi");
            }
            else
            {
                throw new NotSupportedException("Operating system is not supported.");
            }

            return(path);
        }
Esempio n. 3
0
    public static string GetBinaryFolder(OSPlatform?platform = null)
    {
        platform ??= GetCurrentPlatform();

        string fullBaseDirectory = EnvironmentHelpers.GetFullBaseDirectory();
        string commonPartialPath = Path.Combine(fullBaseDirectory, "Microservices", "Binaries");

        string path;

        if (platform == OSPlatform.Windows)
        {
            path = Path.Combine(commonPartialPath, "win64");
        }
        else if (platform == OSPlatform.Linux)
        {
            path = Path.Combine(commonPartialPath, "lin64");
        }
        else if (platform == OSPlatform.OSX)
        {
            path = Path.Combine(commonPartialPath, "osx64");
        }
        else
        {
            throw new NotSupportedException("Operating system is not supported.");
        }

        return(path);
    }
Esempio n. 4
0
        public Global()
        {
            TorSocks5Endpoint = new IPEndPoint(IPAddress.Loopback, 9050);

            DataDir = EnvironmentHelpers.GetDataDir(Path.Combine("WalletWasabi", "Tests"));

            string torLogsFile           = Path.Combine(DataDir, "TorLogs.txt");
            string torDistributionFolder = Path.Combine(EnvironmentHelpers.GetFullBaseDirectory(), "TorDaemons");

            TorSettings = new TorSettings(DataDir, torLogsFile, torDistributionFolder);

            Logger.SetFilePath(Path.Combine(DataDir, "Logs.txt"));
            Logger.SetMinimumLevel(LogLevel.Info);
            Logger.SetModes(LogMode.Debug, LogMode.Console, LogMode.File);
        }
Esempio n. 5
0
    public Global(string dataDir, Config config, UiConfig uiConfig, WalletManager walletManager)
    {
        using (BenchmarkLogger.Measure())
        {
            DataDir     = dataDir;
            Config      = config;
            UiConfig    = uiConfig;
            TorSettings = new TorSettings(DataDir, distributionFolderPath: EnvironmentHelpers.GetFullBaseDirectory(), Config.TerminateTorOnExit, Environment.ProcessId);

            HostedServices = new HostedServices();
            WalletManager  = walletManager;

            var networkWorkFolderPath = Path.Combine(DataDir, "BitcoinStore", Network.ToString());
            AllTransactionStore = new AllTransactionStore(networkWorkFolderPath, Network);
            SmartHeaderChain smartHeaderChain = new(maxChainSize : 20_000);
            IndexStore = new IndexStore(Path.Combine(networkWorkFolderPath, "IndexStore"), Network, smartHeaderChain);
            var mempoolService = new MempoolService();
            var blocks         = new FileSystemBlockRepository(Path.Combine(networkWorkFolderPath, "Blocks"), Network);

            BitcoinStore = new BitcoinStore(IndexStore, AllTransactionStore, mempoolService, blocks);

            if (Config.UseTor)
            {
                BackendHttpClientFactory  = new HttpClientFactory(TorSettings.SocksEndpoint, backendUriGetter: () => Config.GetCurrentBackendUri());
                ExternalHttpClientFactory = new HttpClientFactory(TorSettings.SocksEndpoint, backendUriGetter: null);
            }
            else
            {
                BackendHttpClientFactory  = new HttpClientFactory(torEndPoint: null, backendUriGetter: () => Config.GetFallbackBackendUri());
                ExternalHttpClientFactory = new HttpClientFactory(torEndPoint: null, backendUriGetter: null);
            }

            Synchronizer           = new WasabiSynchronizer(BitcoinStore, BackendHttpClientFactory);
            LegalChecker           = new(DataDir);
            TransactionBroadcaster = new TransactionBroadcaster(Network, BitcoinStore, BackendHttpClientFactory, WalletManager);

            RoundStateUpdaterCircuit = new PersonCircuit();

            Cache = new MemoryCache(new MemoryCacheOptions
            {
                SizeLimit = 1_000,
                ExpirationScanFrequency = TimeSpan.FromSeconds(30)
            });
Esempio n. 6
0
        public Global(string dataDir, Config config, UiConfig uiConfig, WalletManager walletManager)
        {
            using (BenchmarkLogger.Measure())
            {
                StoppingCts = new CancellationTokenSource();
                DataDir     = dataDir;
                Config      = config;
                UiConfig    = uiConfig;
                TorSettings = new TorSettings(DataDir, distributionFolderPath: EnvironmentHelpers.GetFullBaseDirectory(), Config.TerminateTorOnExit, Environment.ProcessId);

                HostedServices = new HostedServices();
                WalletManager  = walletManager;

                WalletManager.OnDequeue += WalletManager_OnDequeue;
                WalletManager.WalletRelevantTransactionProcessed += WalletManager_WalletRelevantTransactionProcessed;

                var networkWorkFolderPath = Path.Combine(DataDir, "BitcoinStore", Network.ToString());
                var transactionStore      = new AllTransactionStore(networkWorkFolderPath, Network);
                var indexStore            = new IndexStore(Path.Combine(networkWorkFolderPath, "IndexStore"), Network, new SmartHeaderChain());
                var mempoolService        = new MempoolService();
                var blocks = new FileSystemBlockRepository(Path.Combine(networkWorkFolderPath, "Blocks"), Network);

                BitcoinStore = new BitcoinStore(indexStore, transactionStore, mempoolService, blocks);

                if (Config.UseTor)
                {
                    BackendHttpClientFactory  = new HttpClientFactory(TorSettings.SocksEndpoint, backendUriGetter: () => Config.GetCurrentBackendUri());
                    ExternalHttpClientFactory = new HttpClientFactory(TorSettings.SocksEndpoint, backendUriGetter: null);
                }
                else
                {
                    BackendHttpClientFactory  = new HttpClientFactory(torEndPoint: null, backendUriGetter: () => Config.GetFallbackBackendUri());
                    ExternalHttpClientFactory = new HttpClientFactory(torEndPoint: null, backendUriGetter: null);
                }

                Synchronizer           = new WasabiSynchronizer(BitcoinStore, BackendHttpClientFactory);
                LegalChecker           = new(DataDir);
                TransactionBroadcaster = new TransactionBroadcaster(Network, BitcoinStore, BackendHttpClientFactory, WalletManager);
            }
        }
Esempio n. 7
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. 8
0
        private async Task AddKnownBitcoinFullNodeAsHiddenServiceAsync(AddressManager addressManager)
        {
            if (Network == Network.RegTest)
            {
                return;
            }

            // curl -s https://bitnodes.21.co/api/v1/snapshots/latest/ | egrep -o '[a-z0-9]{16}\.onion:?[0-9]*' | sort -ru
            // Then filtered to include only /Satoshi:0.17.x
            var fullBaseDirectory = EnvironmentHelpers.GetFullBaseDirectory();

            var onions = await File.ReadAllLinesAsync(Path.Combine(fullBaseDirectory, "OnionSeeds", $"{Network}OnionSeeds.txt"));

            onions.Shuffle();
            foreach (var onion in onions.Take(60))
            {
                if (Utils.TryParseEndpoint(onion, Network.DefaultPort, out var endpoint))
                {
                    await addressManager.AddAsync(endpoint);
                }
            }
        }
Esempio n. 9
0
        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-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. 10
0
        public Global(string dataDir, string torLogsFile, Config config, UiConfig uiConfig, WalletManager walletManager)
        {
            using (BenchmarkLogger.Measure())
            {
                CrashReporter = new CrashReporter();
                StoppingCts   = new CancellationTokenSource();
                DataDir       = dataDir;
                Config        = config;
                UiConfig      = uiConfig;
                TorSettings   = new TorSettings(DataDir, torLogsFile, distributionFolderPath: EnvironmentHelpers.GetFullBaseDirectory());

                Logger.InitializeDefaults(Path.Combine(DataDir, "Logs.txt"));

                HostedServices = new HostedServices();
                WalletManager  = walletManager;

                LegalDocuments = LegalDocuments.TryLoadAgreed(DataDir);

                WalletManager.OnDequeue += WalletManager_OnDequeue;
                WalletManager.WalletRelevantTransactionProcessed += WalletManager_WalletRelevantTransactionProcessed;

                var networkWorkFolderPath = Path.Combine(DataDir, "BitcoinStore", Network.ToString());
                var transactionStore      = new AllTransactionStore(networkWorkFolderPath, Network);
                var indexStore            = new IndexStore(Path.Combine(networkWorkFolderPath, "IndexStore"), Network, new SmartHeaderChain());
                var mempoolService        = new MempoolService();
                var blocks = new FileSystemBlockRepository(Path.Combine(networkWorkFolderPath, "Blocks"), Network);

                BitcoinStore = new BitcoinStore(indexStore, transactionStore, mempoolService, blocks);

                SingleInstanceChecker = new SingleInstanceChecker(Network);

                if (Config.UseTor)
                {
                    Synchronizer = new WasabiSynchronizer(Network, BitcoinStore, () => Config.GetCurrentBackendUri(), Config.TorSocks5EndPoint);
                }
                else
                {
                    Synchronizer = new WasabiSynchronizer(Network, BitcoinStore, Config.GetFallbackBackendUri(), null);
                }
            }
        }
        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 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";
                        }

                        if (!File.Exists(torPath))
                        {
                            Logger.LogInfo($"Tor instance NOT found at '{torPath}'. Attempting to acquire it ...");
                            InstallTor(torDir);
                        }
                        else if (!IoHelpers.CheckExpectedHash(hashSourcePath, 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(torDir);
                        }
                        else
                        {
                            Logger.LogInfo($"Tor instance found at '{torPath}'.");
                        }

                        string torArguments = $"--SOCKSPort {TorSocks5EndPoint} --DataDirectory \"{torDataDir}\" --GeoIPFile \"{geoIpPath}\" GeoIPv6File \"{geoIp6Path}\"";
                        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();
        }