Esempio n. 1
0
        public override bool Equals(object obj)
        {
            if (obj is null)
            {
                return(false);
            }

            if (ReferenceEquals(this, obj))
            {
                return(true);
            }

            DomainEndPoint other = obj as DomainEndPoint;

            if (other == null)
            {
                return(false);
            }

            if (!_address.Equals(other._address, StringComparison.CurrentCultureIgnoreCase))
            {
                return(false);
            }

            if (_port != other._port)
            {
                return(false);
            }

            return(true);
        }
Esempio n. 2
0
        public static async Task <IPEndPoint> GetIPEndPointAsync(this EndPoint ep)
        {
            switch (ep.AddressFamily)
            {
            case AddressFamily.InterNetwork:
            case AddressFamily.InterNetworkV6:
                return(ep as IPEndPoint);

            case AddressFamily.Unspecified:
                DomainEndPoint dep = ep as DomainEndPoint;
                if (dep == null)
                {
                    throw new NotSupportedException("AddressFamily not supported.");
                }

                IPAddress[] ipAddresses = await System.Net.Dns.GetHostAddressesAsync(dep.Address);

                if (ipAddresses.Length == 0)
                {
                    throw new SocketException((int)SocketError.HostNotFound);
                }

                return(new IPEndPoint(ipAddresses[0], dep.Port));

            default:
                throw new NotSupportedException("AddressFamily not supported.");
            }
        }
Esempio n. 3
0
        protected override WebRequest GetWebRequest(Uri address)
        {
            HttpWebRequest request = null;

            if (_proxy == null)
            {
                switch (_networkType)
                {
                case WebClientExNetworkType.IPv4Only:
                    if (IPAddress.TryParse(address.Host, out IPAddress ipv4))
                    {
                        if (ipv4.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork)
                        {
                            throw new WebException("WebClientEx current network type does not allow url address family: " + ipv4.AddressFamily.ToString());
                        }

                        request = base.GetWebRequest(address) as HttpWebRequest;
                    }
                    else
                    {
                        try
                        {
                            DnsClient   dns         = new DnsClient();
                            IPAddress[] ipAddresses = dns.ResolveIP(address.Host);

                            if (ipAddresses.Length == 0)
                            {
                                throw new WebException("WebClientEx could not resolve IPv4 address for host: " + address.Host);
                            }

                            foreach (IPAddress ipAddress in ipAddresses)
                            {
                                if (ipAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                                {
                                    Uri newAddress = new Uri(address.Scheme + "://" + ipAddress.ToString() + ":" + address.Port + address.PathAndQuery);
                                    request      = base.GetWebRequest(newAddress) as HttpWebRequest;
                                    request.Host = address.Host;
                                    break;
                                }
                            }
                        }
                        catch (DnsClientException ex)
                        {
                            throw new WebException("WebClientEx could not resolve IPv4 address for host: " + address.Host, ex);
                        }

                        if (request == null)
                        {
                            throw new WebException("WebClientEx could not resolve IPv4 address for host: " + address.Host);
                        }
                    }
                    break;

                case WebClientExNetworkType.IPv6Only:
                    if (IPAddress.TryParse(address.Host, out IPAddress ipv6))
                    {
                        if (ipv6.AddressFamily != System.Net.Sockets.AddressFamily.InterNetworkV6)
                        {
                            throw new WebException("WebClientEx current network type does not allow url address family: " + ipv6.AddressFamily.ToString());
                        }

                        request = base.GetWebRequest(address) as HttpWebRequest;
                    }
                    else
                    {
                        try
                        {
                            DnsClient   dns         = new DnsClient(true);
                            IPAddress[] ipAddresses = dns.ResolveIP(address.Host, true);

                            if (ipAddresses.Length == 0)
                            {
                                throw new WebException("WebClientEx could not resolve IPv6 address for host: " + address.Host);
                            }

                            foreach (IPAddress ipAddress in ipAddresses)
                            {
                                if (ipAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
                                {
                                    Uri newAddress = new Uri(address.Scheme + "://[" + ipAddress.ToString() + "]:" + address.Port + address.PathAndQuery);
                                    request      = base.GetWebRequest(newAddress) as HttpWebRequest;
                                    request.Host = address.Host;
                                    break;
                                }
                            }
                        }
                        catch (DnsClientException ex)
                        {
                            throw new WebException("WebClientEx could not resolve IPv6 address for host: " + address.Host, ex);
                        }

                        if (request == null)
                        {
                            throw new WebException("WebClientEx could not resolve IPv6 address for host: " + address.Host);
                        }
                    }
                    break;

                default:
                    request = base.GetWebRequest(address) as HttpWebRequest;
                    break;
                }
            }
            else
            {
                if ((_proxy.ViaProxy == null) && (_proxy.Type == NetProxyType.Http))
                {
                    request       = base.GetWebRequest(address) as HttpWebRequest;
                    request.Proxy = _proxy.HttpProxy;
                }
                else
                {
                    EndPoint remoteEP;

                    if (IPAddress.TryParse(address.Host, out IPAddress ipAddr))
                    {
                        remoteEP = new IPEndPoint(ipAddr, address.Port);
                    }
                    else
                    {
                        remoteEP = new DomainEndPoint(address.Host, address.Port);
                    }

                    if ((_tunnelProxy != null) && !_tunnelProxy.RemoteEndPoint.Equals(remoteEP))
                    {
                        _tunnelProxy.Dispose();
                    }

                    if ((_tunnelProxy == null) || _tunnelProxy.Disposed)
                    {
                        _tunnelProxy = _proxy.CreateLocalTunnelProxy(remoteEP, _timeout);
                    }

                    if (address.Scheme == "https")
                    {
                        request       = base.GetWebRequest(address) as HttpWebRequest;
                        request.Proxy = _tunnelProxy.EmulateHttpProxy();
                    }
                    else
                    {
                        request      = base.GetWebRequest(new Uri("http://" + _tunnelProxy.TunnelEndPoint.Address.ToString() + ":" + _tunnelProxy.TunnelEndPoint.Port + address.PathAndQuery)) as HttpWebRequest;
                        request.Host = address.Host;
                    }
                }
            }

            if (_timeout > 0)
            {
                request.Timeout = _timeout;
            }

            request.CookieContainer = _cookie;

            if (_ifModifiedSince > (new DateTime()))
            {
                request.IfModifiedSince = _ifModifiedSince;
            }

            if (_userAgent != null)
            {
                request.UserAgent = _userAgent;
            }

            request.KeepAlive         = _keepAlive;
            request.AllowAutoRedirect = false;

            if (_enableAutomaticDecompression)
            {
                request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
            }

            foreach (KeyValuePair <string, string> header in _headers)
            {
                switch (header.Key.ToLower())
                {
                case "accept":
                    request.Accept = header.Value;
                    break;

                case "connection":
                    request.KeepAlive = (header.Value.ToLower() == "keep-alive");
                    break;

                case "content-type":
                    request.ContentType = header.Value;
                    break;

                case "user-agent":
                    request.UserAgent = header.Value;
                    break;

                case "host":
                    request.Host = header.Value;
                    break;

                default:
                    request.Headers.Add(header.Key, header.Value);
                    break;
                }
            }

            return(request);
        }