aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Common/Net/IPHost.cs
diff options
context:
space:
mode:
Diffstat (limited to 'MediaBrowser.Common/Net/IPHost.cs')
-rw-r--r--MediaBrowser.Common/Net/IPHost.cs97
1 files changed, 51 insertions, 46 deletions
diff --git a/MediaBrowser.Common/Net/IPHost.cs b/MediaBrowser.Common/Net/IPHost.cs
index 4cede9ab1..4a7c70190 100644
--- a/MediaBrowser.Common/Net/IPHost.cs
+++ b/MediaBrowser.Common/Net/IPHost.cs
@@ -128,62 +128,63 @@ namespace MediaBrowser.Common.Net
/// <returns><c>true</c> if the parsing is successful, <c>false</c> if not.</returns>
public static bool TryParse(string host, out IPHost hostObj)
{
- if (!string.IsNullOrEmpty(host))
+ if (string.IsNullOrWhiteSpace(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);
- }
+ hostObj = IPHost.None;
+ return false;
+ }
- // Is it a host or IPv4 with port?
- string[] hosts = host.Split(':');
+ // See if it's an IPv6 with port address e.g. [::1] or [::1]:120.
+ int i = host.IndexOf("]", StringComparison.OrdinalIgnoreCase);
+ if (i != -1)
+ {
+ return TryParse(host.Remove(i - 1).TrimStart(' ', '['), out hostObj);
+ }
- if (hosts.Length > 2)
- {
- hostObj = new IPHost(string.Empty, IPAddress.None);
- return false;
- }
+ if (IPNetAddress.TryParse(host, out var netAddress))
+ {
+ // Host name is an ip address, so fake resolve.
+ hostObj = new IPHost(host, netAddress.Address);
+ return true;
+ }
- // Remove port from IPv4 if it exists.
- host = hosts[0];
+ // Is it a host, IPv4/6 with/out port?
+ string[] hosts = host.Split(':');
- if (string.Equals("localhost", host, StringComparison.OrdinalIgnoreCase))
- {
- hostObj = new IPHost(host, new IPAddress(Ipv4Loopback));
- return true;
- }
+ if (hosts.Length <= 2)
+ {
+ // This is either a hostname: port, or an IP4:port.
+ host = hosts[0];
- if (IPNetAddress.TryParse(host, out IPNetAddress netIP))
- {
- // Host name is an ip address, so fake resolve.
- hostObj = new IPHost(host, netIP.Address);
- return true;
- }
+ if (string.Equals("localhost", host, StringComparison.OrdinalIgnoreCase))
+ {
+ hostObj = new IPHost(host);
+ return true;
}
- // Only thing left is to see if it's a host string.
- if (!string.IsNullOrEmpty(host))
+ if (IPAddress.TryParse(host, out var netIP))
{
- // 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;
- }
+ // 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;
@@ -344,10 +345,14 @@ namespace MediaBrowser.Common.Net
{
output += "Any Address,";
}
- else
+ else if (i.AddressFamily == AddressFamily.InterNetwork)
{
output += $"{i}/32,";
}
+ else
+ {
+ output += $"{i}/128,";
+ }
}
output = output[0..^1];