Ejemplo n.º 1
0
 public Client(ITcpClientFactory tcpClientFactory, SocksAddress address, SocksAddress destinationAddress, bool useSsl)
 {
     _destinationAddress = destinationAddress;
     _useSsl             = useSsl;
     _tcpClientFactory   = tcpClientFactory;
     _address            = address;
 }
        protected override ITcpClient CreateClient(HttpRequestMessage request, SocksAddress destinationAddress, bool useSsl, CancellationToken cancellationToken, bool forceRecreate)
        {
            _connection = _pool.Get(destinationAddress, useSsl, forceRecreate);
            var client = _connection.Client;

            return(client);
        }
 public OpenConnection(SocksAddress address, ITcpClient client)
 {
     Address       = address;
     Client        = client;
     Timeout       = InfiniteTimespan;
     MaxUsageCount = -1;
 }
        internal OpenConnection GetOrCreateClient(SocksAddress address, bool useSsl, DateTime now)
        {
            lock (_connectionPool)
            {
                var            key = new OpenConnectionKey(address, useSsl);
                OpenConnection info;
                if (_connectionPool.TryGetValue(key, out info))
                {
                    if (!info.DisposeIfInvalid(now))
                    {
#if SUPPORTS_NLOG
                        _logger.Debug("Pool: Reuse client");
#endif
                        return(info);
                    }
#if SUPPORTS_NLOG
                    _logger.Debug("Dispose client");
#endif
                    _connectionPool.Remove(key);
                    info.Client.Dispose();
                }
#if SUPPORTS_NLOG
                _logger.Debug("Pool: Create client (2): {0}", address);
#endif
                info = new OpenConnection(address, Factory.Create(address, useSsl));
                _connectionPool.Add(key, info);
                return(info);
            }
        }
        public IPooledConnection Get(SocksAddress address, bool useSsl, bool forceCreate)
        {
            var conn = forceCreate
                ? Create(address, useSsl)
                : GetOrCreateClient(address, useSsl);

            return(conn);
        }
Ejemplo n.º 6
0
        private async Task Connect(SocksAddress destinationAddress, CancellationToken ct)
        {
            var response = await Execute <ConnectResponse>(new ConnectRequest(destinationAddress, GetUserId()), ct);

            if (response.Status != Socks4.ConnectStatus.Granted)
            {
                throw new Socks4.Socks4ConnectException(response.Status);
            }
        }
Ejemplo n.º 7
0
        private async Task <SocksAddress> Connect(SocksAddress destinationAddress, CancellationToken ct)
        {
            var response = await Execute <ConnectResponse>(new ConnectRequest(destinationAddress), ct);

            if (response.Status != ConnectStatus.Succeeded)
            {
                throw new Socks5ConnectException(response.Status);
            }
            return(response.BoundAddress);
        }
Ejemplo n.º 8
0
        protected override void ReadPayloadFrom(BinaryReader reader)
        {
            var reply = reader.ReadByte();

            if (reply < 90 || reply > 93)
            {
                throw new InvalidDataException();
            }
            Status         = (ConnectStatus)reply;
            IgnoredAddress = new SocksAddress(reader, SocksVersion.SocksV4);
        }
Ejemplo n.º 9
0
        protected override void ReadPayloadFrom(BinaryReader reader)
        {
            var reply = reader.ReadByte();

            if (reply >= 9)
            {
                throw new InvalidDataException();
            }
            Status = (ConnectStatus)reply;
            reader.ReadByte();  // Reserved
            BoundAddress = new SocksAddress(reader, SocksVersion.SocksV5);
        }
Ejemplo n.º 10
0
        protected override ITcpClient CreateClient(HttpRequestMessage request, SocksAddress destinationAddress, bool useSsl, CancellationToken cancellationToken, bool forceRecreate)
        {
            if (Proxy == null)
            {
                throw new InvalidOperationException("Proxy property cannot be null.");
            }

            var proxyUri = Proxy.GetProxy(request.RequestUri);

            _connection = forceRecreate
                ? _pool.Create(destinationAddress, useSsl)
                : _pool.GetOrCreateClient(destinationAddress, useSsl);

            var client = new Client(new Pooling.TcpClientPoolFactory(_pool), new SocksAddress(proxyUri), destinationAddress, useSsl)
            {
                Credentials = Proxy.Credentials,
            };

            return(client);
        }
        internal OpenConnection Create(SocksAddress address, bool useSsl)
        {
            var key    = new OpenConnectionKey(address, useSsl);
            var client = Factory.Create(address, useSsl);

            lock (_connectionPool)
            {
                OpenConnection oldInfo;
                if (_connectionPool.TryGetValue(key, out oldInfo))
                {
                    _connectionPool.Remove(key);
                    oldInfo.Client.Dispose();
                }
                var result = new OpenConnection(address, client);
                _connectionPool.Add(key, result);
#if SUPPORTS_NLOG
                _logger.Debug("Pool: Create client (1): {0}", address);
#endif
                return(result);
            }
        }
 public SocksProxyAvailableTheoryAttribute()
 {
     if (_socksProxyFound == null)
     {
         try
         {
             var socksAddress = new SocksAddress(SocksUri);
             var tcpClient    = new TcpClient {
                 ReceiveTimeout = 5000
             };
             var result  = tcpClient.BeginConnect(socksAddress.Host, socksAddress.Port, null, null);
             var success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(1));
             if (!success)
             {
                 throw new Exception("Connect failed");
             }
             try
             {
                 tcpClient.EndConnect(result);
                 _socksProxyFound = true;
             }
             finally
             {
                 tcpClient.Close();
             }
         }
         catch (Exception ex)
         {
             _socksProxyDetectionError = ex;
             _socksProxyFound          = false;
         }
     }
     if (!_socksProxyFound.Value)
     {
         Skip = string.Format("SOCKS proxy not found ({0})", _socksProxyDetectionError.Message);
     }
 }
        public virtual ITcpClient Create(SocksAddress destinationAddress, bool useSsl)
        {
            var conn = Pool.GetOrCreateClient(destinationAddress, useSsl);

            return(conn.Client);
        }
 internal OpenConnection GetOrCreateClient(SocksAddress address, bool useSsl)
 {
     return(GetOrCreateClient(address, useSsl, DateTime.UtcNow));
 }
 public TcpClientWrapper(SocksAddress destinationAddress, bool useSsl, ITcpClientFactory factory)
 {
     _destinationAddress = destinationAddress;
     _useSsl             = useSsl;
     _factory            = factory;
 }
Ejemplo n.º 16
0
 public OpenConnectionKey(SocksAddress address, bool useSsl)
 {
     Address = address;
     UseSsl  = useSsl;
 }
Ejemplo n.º 17
0
 public ITcpClient Create(SocksAddress destinationAddress, bool useSsl)
 {
     return(new TcpClientWrapper(destinationAddress, useSsl, this));
 }
 public ConnectRequest(SocksAddress address)
 {
     Address = address;
 }
 public ConnectRequest(SocksAddress address, string userId)
 {
     UserId  = userId;
     Address = address;
 }
Ejemplo n.º 20
0
 public Socks4WebProxy(SocksAddress socksAddress)
 {
     _socksAddress = socksAddress;
 }