示例#1
0
文件: IPHost.cs 项目: ypid/jellyfin
        public static bool TryParse(string host, out IPHost hostObj)
        {
            if (string.IsNullOrWhiteSpace(host))
            {
                hostObj = IPHost.None;
                return(false);
            }

            // See if it's an IPv6 with port address e.g. [::1] or [::1]:120.
            int i = host.IndexOf(']', StringComparison.Ordinal);

            if (i != -1)
            {
                return(TryParse(host.Remove(i - 1).TrimStart(' ', '['), out hostObj));
            }

            if (IPNetAddress.TryParse(host, out var netAddress))
            {
                // Host name is an ip address, so fake resolve.
                hostObj = new IPHost(host, netAddress.Address);
                return(true);
            }

            // Is it a host, IPv4/6 with/out port?
            string[] hosts = host.Split(':');

            if (hosts.Length <= 2)
            {
                // This is either a hostname: port, or an IP4:port.
                host = hosts[0];

                if (string.Equals("localhost", host, StringComparison.OrdinalIgnoreCase))
                {
                    hostObj = new IPHost(host);
                    return(true);
                }

                if (IPAddress.TryParse(host, out var netIP))
                {
                    // Host name is an ip address, so fake resolve.
                    hostObj = new IPHost(host, netIP);
                    return(true);
                }
            }
            else
            {
                // Invalid host name, as it cannot contain :
                hostObj = new IPHost(string.Empty, IPAddress.None);
                return(false);
            }

            // Use regular expression as CheckHostName isn't RFC5892 compliant.
            // Modified from gSkinner's expression at https://stackoverflow.com/questions/11809631/fully-qualified-domain-name-validation
            string pattern = @"(?im)^(?!:\/\/)(?=.{1,255}$)((.{1,63}\.){0,127}(?![0-9]*$)[a-z0-9-]+\.?)$";

            if (Regex.IsMatch(host, pattern))
            {
                hostObj = new IPHost(host);
                return(true);
            }

            hostObj = IPHost.None;
            return(false);
        }
示例#2
0
        public static bool TryParse(string host, out IPHost hostObj)
        {
            if (!string.IsNullOrEmpty(host))
            {
                // See if it's an IPv6 with port address e.g. [::1]:120.
                int i = host.IndexOf("]:", StringComparison.OrdinalIgnoreCase);
                if (i != -1)
                {
                    return(TryParse(host.Remove(i - 1).TrimStart(' ', '['), out hostObj));
                }
                else
                {
                    // See if it's an IPv6 in [] with no port.
                    i = host.IndexOf(']', StringComparison.OrdinalIgnoreCase);
                    if (i != -1)
                    {
                        return(TryParse(host.Remove(i - 1).TrimStart(' ', '['), out hostObj));
                    }

                    // Is it a host or IPv4 with port?
                    string[] hosts = host.Split(':');

                    if (hosts.Length > 2)
                    {
                        hostObj = new IPHost(string.Empty, IPAddress.None);
                        return(false);
                    }

                    // Remove port from IPv4 if it exists.
                    host = hosts[0];

                    if (string.Equals("localhost", host, StringComparison.OrdinalIgnoreCase))
                    {
                        hostObj = new IPHost(host, new IPAddress(Ipv4Loopback));
                        return(true);
                    }

                    if (IPNetAddress.TryParse(host, out IPNetAddress netIP))
                    {
                        // Host name is an ip address, so fake resolve.
                        hostObj = new IPHost(host, netIP.Address);
                        return(true);
                    }
                }

                // Only thing left is to see if it's a host string.
                if (!string.IsNullOrEmpty(host))
                {
                    // Use regular expression as CheckHostName isn't RFC5892 compliant.
                    // Modified from gSkinner's expression at https://stackoverflow.com/questions/11809631/fully-qualified-domain-name-validation
                    Regex re = new Regex(@"^(?!:\/\/)(?=.{1,255}$)((.{1,63}\.){0,127}(?![0-9]*$)[a-z0-9-]+\.?)$", RegexOptions.IgnoreCase | RegexOptions.Multiline);
                    if (re.Match(host).Success)
                    {
                        hostObj = new IPHost(host);
                        return(true);
                    }
                }
            }

            hostObj = IPHost.None;
            return(false);
        }