예제 #1
0
        public Socket Connect(EndPoint remoteEP, int timeout = 10000)
        {
            Socket viaProxySocket = null;

            if (_viaProxy != null)
            {
                viaProxySocket = _viaProxy.Connect(this.ProxyEndPoint, timeout);
            }

            switch (_type)
            {
            case NetProxyType.Http:
                if (viaProxySocket == null)
                {
                    return(_httpProxy.Connect(remoteEP, timeout));
                }
                else
                {
                    return(_httpProxy.Connect(remoteEP, viaProxySocket));
                }

            case NetProxyType.Socks5:
                if (viaProxySocket == null)
                {
                    return(_socksProxy.Connect(remoteEP, timeout));
                }
                else
                {
                    return(_socksProxy.Connect(remoteEP, viaProxySocket));
                }

            default:
                throw new NotSupportedException("Proxy type not supported.");
            }
        }
예제 #2
0
        public Socket Connect(EndPoint remoteEP, int timeout = 10000)
        {
            if (IsProxyBypassed(remoteEP))
            {
                IPEndPoint hostEP = remoteEP.GetIPEndPoint();
                Socket     socket = new Socket(hostEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

                IAsyncResult result = socket.BeginConnect(hostEP, null, null);
                if (!result.AsyncWaitHandle.WaitOne(timeout))
                {
                    throw new SocketException((int)SocketError.TimedOut);
                }

                if (!socket.Connected)
                {
                    throw new SocketException((int)SocketError.ConnectionRefused);
                }

                return(socket);
            }

            Socket viaProxySocket = null;

            if (_viaProxy != null)
            {
                viaProxySocket = _viaProxy.Connect(this.ProxyEndPoint, timeout);
            }

            switch (_type)
            {
            case NetProxyType.Http:
                if (viaProxySocket == null)
                {
                    return(_httpProxy.Connect(remoteEP, timeout));
                }
                else
                {
                    return(_httpProxy.Connect(remoteEP, viaProxySocket));
                }

            case NetProxyType.Socks5:
                if (viaProxySocket == null)
                {
                    return(_socksProxy.Connect(remoteEP, timeout));
                }
                else
                {
                    return(_socksProxy.Connect(remoteEP, viaProxySocket));
                }

            default:
                throw new NotSupportedException("Proxy type not supported.");
            }
        }
예제 #3
0
        public Socket Connect(IPEndPoint remoteEP, int timeout = 10000)
        {
            switch (_type)
            {
            case NetProxyType.Http:
                return(_httpProxy.Connect(remoteEP, timeout));

            case NetProxyType.Socks5:
                using (SocksConnectRequestHandler requestHandler = _socksProxy.Connect(remoteEP, timeout))
                {
                    return(requestHandler.GetSocket());
                }

            default:
                throw new NotSupportedException("Proxy type not supported.");
            }
        }