コード例 #1
0
ファイル: FeeJob.cs プロジェクト: nopara73/HBitcoin
        public async Task StartAsync(CancellationToken ctsToken)
        {
            while (true)
            {
                try
                {
                    if (ctsToken.IsCancellationRequested)
                    {
                        return;
                    }

                    await _controlPortClient.ChangeCircuitAsync(ctsToken).ConfigureAwait(false);

                    HttpResponseMessage response =
                        await _torHttpClient.GetAsync(@"http://api.blockcypher.com/v1/btc/main", HttpCompletionOption.ResponseContentRead, ctsToken)
                        .ConfigureAwait(false);

                    var json = JObject.Parse(await response.Content.ReadAsStringAsync().ConfigureAwait(false));
                    _lowFeePerBytes    = new Money((int)(json.Value <decimal>("low_fee_per_kb") / 1024), MoneyUnit.Satoshi);
                    _mediumFeePerBytes = new Money((int)(json.Value <decimal>("medium_fee_per_kb") / 1024), MoneyUnit.Satoshi);
                    _highFeePerBytes   = new Money((int)(json.Value <decimal>("high_fee_per_kb") / 1024), MoneyUnit.Satoshi);

                    if (ctsToken.IsCancellationRequested)
                    {
                        return;
                    }
                    _firstRun = false;
                }
                catch (OperationCanceledException)
                {
                    if (ctsToken.IsCancellationRequested)
                    {
                        return;
                    }
                    continue;
                }
                catch (Exception ex)
                {
                    if (_firstRun)
                    {
                        throw;
                    }

                    Debug.WriteLine($"Ignoring {nameof(FeeJob)} exception:");
                    Debug.WriteLine(ex);
                }

                var waitMinutes = new Random().Next(3, 10);
                await Task.Delay(TimeSpan.FromMinutes(waitMinutes), ctsToken).ContinueWith(t => { }).ConfigureAwait(false);
            }
        }
コード例 #2
0
        public void ChangeCircuit(SecureString password)
        {
            if (password == null)
            {
                throw new ArgumentNullException(nameof(password));
            }

            try
            {
                using (var insecurePassword = password.Insecure())
                {
                    var controlPortClient = new DotNetTor.ControlPort.Client(controlHost, ControlPort, insecurePassword.Value);
                    controlPortClient.ChangeCircuitAsync().Wait();
                }
            }
            catch (DotNetTor.TorException ex)
            {
                console.WriteLine(ex.Message);
            }
        }
コード例 #3
0
        public TorConnectResult Connect()
        {
            var requestUri = "http://icanhazip.com/";

            try
            {
                _HttpClient = new HttpClient(new SocksPortHandler(Options.TorProxy.Address, socksPort: Options.TorProxy.SocksPort));
                var message = _HttpClient.GetAsync(requestUri).Result;
                this.TorIP = message.Content.ReadAsStringAsync().Result.Trim('\n').Trim('\r');
                Logger.LogInformation("Your Tor IP: {0}", this.TorIP);

                // 3. Change Tor IP
                var controlPortClient = new DotNetTor.ControlPort.Client(Options.TorProxy.Address, controlPort: Options.TorProxy.ControlPort, password: Options.TorProxy.ControlPassword);
                controlPortClient.ChangeCircuitAsync().Wait();

                // 4. Get changed Tor IP
                message           = _HttpClient.GetAsync(requestUri).Result;
                this.CurrentTorIP = message.Content.ReadAsStringAsync().Result.Trim('\n').Trim('\r');
                Logger.LogInformation("Your other Tor IP: {0}", this.CurrentTorIP);

                this.Connected       = true;
                this.ConnectionError = string.Empty;

                return(new TorConnectResult(this.CurrentTorIP, null));
            }
            catch (Exception ex)
            {
                this.ConnectionError = ex.Message;
                if (ex.InnerException != null)
                {
                    AppendInnerException(ex.InnerException);
                }
                Logger.LogError(this.ConnectionError, $"Error while connecting to TOR at {this.Address}:{this.SocksPort}");

                return(new TorConnectResult(string.Empty, ex));
            }
        }