diff --git a/Emby.Dlna/Main/DlnaEntryPoint.cs b/Emby.Dlna/Main/DlnaEntryPoint.cs
index 504bde996b..be618be2b3 100644
--- a/Emby.Dlna/Main/DlnaEntryPoint.cs
+++ b/Emby.Dlna/Main/DlnaEntryPoint.cs
@@ -9,6 +9,7 @@ using System.Threading;
using System.Threading.Tasks;
using Emby.Dlna.PlayTo;
using Emby.Dlna.Ssdp;
+using Jellyfin.Networking.Manager;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Common.Net;
@@ -26,7 +27,6 @@ using MediaBrowser.Model.Globalization;
using MediaBrowser.Model.Net;
using MediaBrowser.Model.System;
using Microsoft.Extensions.Logging;
-using NetworkCollection;
using Rssdp;
using Rssdp.Infrastructure;
using OperatingSystem = MediaBrowser.Common.System.OperatingSystem;
@@ -261,7 +261,7 @@ namespace Emby.Dlna.Main
{
var udn = CreateUuid(_appHost.SystemId);
- var bindAddresses = new NetCollection(
+ var bindAddresses = NetworkManager.CreateCollection(
_networkManager.GetInternalBindAddresses()
.Where(i => i.AddressFamily == AddressFamily.InterNetwork || (i.AddressFamily == AddressFamily.InterNetworkV6 && i.Address.ScopeId != 0)));
diff --git a/Emby.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunUdpStream.cs b/Emby.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunUdpStream.cs
index cfc5278ece..c4176eb7ba 100644
--- a/Emby.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunUdpStream.cs
+++ b/Emby.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunUdpStream.cs
@@ -3,7 +3,9 @@
using System;
using System.Collections.Generic;
using System.IO;
+using System.Linq;
using System.Net;
+using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
@@ -16,7 +18,6 @@ using MediaBrowser.Model.IO;
using MediaBrowser.Model.LiveTv;
using MediaBrowser.Model.MediaInfo;
using Microsoft.Extensions.Logging;
-using NetworkCollection.Udp;
namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun
{
@@ -51,6 +52,25 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun
EnableStreamSharing = true;
}
+ ///
+ /// Returns an unused UDP port number in the range specified.
+ ///
+ /// Upper and Lower boundary of ports to select.
+ /// System.Int32.
+ private static int GetUdpPortFromRange((int Min, int Max) range)
+ {
+ var properties = IPGlobalProperties.GetIPGlobalProperties();
+
+ // Get active udp listeners.
+ var udpListenerPorts = properties.GetActiveUdpListeners()
+ .Where(n => n.Port >= range.Min && n.Port <= range.Max)
+ .Select(n => n.Port);
+
+ return Enumerable.Range(range.Min, range.Max)
+ .Where(i => !udpListenerPorts.Contains(i))
+ .FirstOrDefault();
+ }
+
public override async Task Open(CancellationToken openCancellationToken)
{
LiveStreamCancellationTokenSource.Token.ThrowIfCancellationRequested();
@@ -58,7 +78,8 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun
var mediaSource = OriginalMediaSource;
var uri = new Uri(mediaSource.Path);
- var localPort = UdpHelper.GetRandomUnusedUdpPort();
+ // Temporary Code to reduce PR size.
+ var localPort = GetUdpPortFromRange((49152, 65535));
Directory.CreateDirectory(Path.GetDirectoryName(TempFilePath));
diff --git a/Jellyfin.Networking/Jellyfin.Networking.csproj b/Jellyfin.Networking/Jellyfin.Networking.csproj
index 330d36a80d..1747a1dc7a 100644
--- a/Jellyfin.Networking/Jellyfin.Networking.csproj
+++ b/Jellyfin.Networking/Jellyfin.Networking.csproj
@@ -23,10 +23,6 @@
../jellyfin.ruleset
-
-
-
-
diff --git a/Jellyfin.Networking/Manager/NetworkManager.cs b/Jellyfin.Networking/Manager/NetworkManager.cs
index 616774d043..76ac02d791 100644
--- a/Jellyfin.Networking/Manager/NetworkManager.cs
+++ b/Jellyfin.Networking/Manager/NetworkManager.cs
@@ -13,8 +13,7 @@ using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Net;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
-using NetworkCollection;
-using NetworkCollection.Udp;
+using NetCollection = System.Collections.ObjectModel.Collection;
namespace Jellyfin.Networking.Manager
{
@@ -154,6 +153,22 @@ namespace Jellyfin.Networking.Manager
///
public Dictionary PublishedServerUrls => _publishedServerUrls;
+ ///
+ /// Creates a new network collection.
+ ///
+ /// Items to assign the collection, or null.
+ /// The collection created.
+ public static NetCollection CreateCollection(IEnumerable? source)
+ {
+ var result = new NetCollection();
+ if (source != null)
+ {
+ return result.AddRange(source);
+ }
+
+ return result;
+ }
+
///
public void Dispose()
{
@@ -162,10 +177,10 @@ namespace Jellyfin.Networking.Manager
}
///
- public List GetMacAddresses()
+ public IReadOnlyCollection GetMacAddresses()
{
// Populated in construction - so always has values.
- return _macAddresses.ToList();
+ return _macAddresses.AsReadOnly();
}
///
@@ -187,12 +202,12 @@ namespace Jellyfin.Networking.Manager
NetCollection nc = new NetCollection();
if (IsIP4Enabled)
{
- nc.Add(IPAddress.Loopback);
+ nc.AddItem(IPAddress.Loopback);
}
if (IsIP6Enabled)
{
- nc.Add(IPAddress.IPv6Loopback);
+ nc.AddItem(IPAddress.IPv6Loopback);
}
return nc;
@@ -276,12 +291,12 @@ namespace Jellyfin.Networking.Manager
if (IsIP4Enabled)
{
- result.Add(IPAddress.Any);
+ result.AddItem(IPAddress.Any);
}
if (IsIP6Enabled)
{
- result.Add(IPAddress.IPv6Any);
+ result.AddItem(IPAddress.IPv6Any);
}
return result;
@@ -375,7 +390,7 @@ namespace Jellyfin.Networking.Manager
}
// Get the first LAN interface address that isn't a loopback.
- var interfaces = new NetCollection(_interfaceAddresses
+ var interfaces = CreateCollection(_interfaceAddresses
.Exclude(_bindExclusions)
.Where(p => IsInLocalNetwork(p))
.OrderBy(p => p.Tag));
@@ -418,11 +433,11 @@ namespace Jellyfin.Networking.Manager
if (_bindExclusions.Count > 0)
{
// Return all the internal interfaces except the ones excluded.
- return new NetCollection(_internalInterfaces.Where(p => !_bindExclusions.Contains(p)));
+ return CreateCollection(_internalInterfaces.Where(p => !_bindExclusions.Contains(p)));
}
// No bind address, so return all internal interfaces.
- return new NetCollection(_internalInterfaces.Where(p => !p.IsLoopback()));
+ return CreateCollection(_internalInterfaces.Where(p => !p.IsLoopback()));
}
return new NetCollection(_bindAddresses);
@@ -572,7 +587,7 @@ namespace Jellyfin.Networking.Manager
}
TrustAllIP6Interfaces = config.TrustAllIP6Interfaces;
- UdpHelper.EnableMultiSocketBinding = config.EnableMultiSocketBinding;
+ // UdpHelper.EnableMultiSocketBinding = config.EnableMultiSocketBinding;
if (string.IsNullOrEmpty(MockNetworkSettings))
{
@@ -941,7 +956,7 @@ namespace Jellyfin.Networking.Manager
{
_logger.LogDebug("Using LAN interface addresses as user provided no LAN details.");
// Internal interfaces must be private and not excluded.
- _internalInterfaces = new NetCollection(_interfaceAddresses.Where(i => IsPrivateAddressRange(i) && !_excludedSubnets.Contains(i)));
+ _internalInterfaces = CreateCollection(_interfaceAddresses.Where(i => IsPrivateAddressRange(i) && !_excludedSubnets.Contains(i)));
// Subnets are the same as the calculated internal interface.
_lanSubnets = new NetCollection();
@@ -976,7 +991,7 @@ namespace Jellyfin.Networking.Manager
}
// Internal interfaces must be private, not excluded and part of the LocalNetworkSubnet.
- _internalInterfaces = new NetCollection(_interfaceAddresses.Where(i => IsInLocalNetwork(i) && !_excludedSubnets.Contains(i) && _lanSubnets.Contains(i)));
+ _internalInterfaces = CreateCollection(_interfaceAddresses.Where(i => IsInLocalNetwork(i) && !_excludedSubnets.Contains(i) && _lanSubnets.Contains(i)));
}
_logger.LogInformation("Defined LAN addresses : {0}", _lanSubnets);
@@ -1082,7 +1097,7 @@ namespace Jellyfin.Networking.Manager
IPHost host = new IPHost(Dns.GetHostName());
foreach (var a in host.GetAddresses())
{
- _interfaceAddresses.Add(a);
+ _interfaceAddresses.AddItem(a);
}
if (_interfaceAddresses.Count == 0)
@@ -1189,7 +1204,7 @@ namespace Jellyfin.Networking.Manager
if (isExternal)
{
// Find all external bind addresses. Store the default gateway, but check to see if there is a better match first.
- bindResult = new NetCollection(nc
+ bindResult = CreateCollection(nc
.Where(p => !IsInLocalNetwork(p))
.OrderBy(p => p.Tag));
defaultGateway = bindResult.FirstOrDefault()?.Address;
@@ -1246,7 +1261,7 @@ namespace Jellyfin.Networking.Manager
{
result = string.Empty;
// Get the first WAN interface address that isn't a loopback.
- var extResult = new NetCollection(_interfaceAddresses
+ var extResult = CreateCollection(_interfaceAddresses
.Exclude(_bindExclusions)
.Where(p => !IsInLocalNetwork(p))
.OrderBy(p => p.Tag));
diff --git a/Jellyfin.Server/Middleware/IpBasedAccessValidationMiddleware.cs b/Jellyfin.Server/Middleware/IpBasedAccessValidationMiddleware.cs
index c3533d7956..525cd9ffe2 100644
--- a/Jellyfin.Server/Middleware/IpBasedAccessValidationMiddleware.cs
+++ b/Jellyfin.Server/Middleware/IpBasedAccessValidationMiddleware.cs
@@ -5,7 +5,6 @@ using MediaBrowser.Common.Extensions;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration;
using Microsoft.AspNetCore.Http;
-using NetworkCollection;
namespace Jellyfin.Server.Middleware
{
@@ -47,7 +46,7 @@ namespace Jellyfin.Server.Middleware
{
// Comma separated list of IP addresses or IP/netmask entries for networks that will be allowed to connect remotely.
// If left blank, all remote addresses will be allowed.
- NetCollection remoteAddressFilter = networkManager.RemoteAddressFilter;
+ var remoteAddressFilter = networkManager.RemoteAddressFilter;
if (remoteAddressFilter.Count > 0 && !networkManager.IsInLocalNetwork(remoteIp))
{
diff --git a/Jellyfin.Server/Middleware/LanFilteringMiddleware.cs b/Jellyfin.Server/Middleware/LanFilteringMiddleware.cs
index 1f4e80053d..8065054a1e 100644
--- a/Jellyfin.Server/Middleware/LanFilteringMiddleware.cs
+++ b/Jellyfin.Server/Middleware/LanFilteringMiddleware.cs
@@ -7,7 +7,6 @@ using MediaBrowser.Common.Extensions;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration;
using Microsoft.AspNetCore.Http;
-using NetworkCollection;
namespace Jellyfin.Server.Middleware
{
diff --git a/Jellyfin.Server/Program.cs b/Jellyfin.Server/Program.cs
index fd300da7f1..61f7da16a6 100644
--- a/Jellyfin.Server/Program.cs
+++ b/Jellyfin.Server/Program.cs
@@ -14,6 +14,7 @@ using Emby.Server.Implementations;
using Emby.Server.Implementations.IO;
using Jellyfin.Api.Controllers;
using MediaBrowser.Common.Configuration;
+using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Extensions;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Server.Kestrel.Core;
@@ -23,7 +24,6 @@ using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
-using NetworkCollection;
using Serilog;
using Serilog.Extensions.Logging;
using SQLitePCL;
@@ -271,7 +271,7 @@ namespace Jellyfin.Server
return builder
.UseKestrel((builderContext, options) =>
{
- NetCollection addresses = appHost.NetManager.GetAllBindInterfaces();
+ var addresses = appHost.NetManager.GetAllBindInterfaces();
bool flagged = false;
foreach (IPObject netAdd in addresses)
diff --git a/MediaBrowser.Common/MediaBrowser.Common.csproj b/MediaBrowser.Common/MediaBrowser.Common.csproj
index 0b8f431c04..777136f8bf 100644
--- a/MediaBrowser.Common/MediaBrowser.Common.csproj
+++ b/MediaBrowser.Common/MediaBrowser.Common.csproj
@@ -22,7 +22,6 @@
-
diff --git a/MediaBrowser.Common/Net/INetworkManager.cs b/MediaBrowser.Common/Net/INetworkManager.cs
index f60f369d6a..a7beabbdcb 100644
--- a/MediaBrowser.Common/Net/INetworkManager.cs
+++ b/MediaBrowser.Common/Net/INetworkManager.cs
@@ -4,7 +4,7 @@ using System.Collections.Generic;
using System.Net;
using System.Net.NetworkInformation;
using Microsoft.AspNetCore.Http;
-using NetworkCollection;
+using NetCollection = System.Collections.ObjectModel.Collection;
namespace MediaBrowser.Common.Net
{
@@ -130,7 +130,7 @@ namespace MediaBrowser.Common.Net
/// Get a list of all the MAC addresses associated with active interfaces.
///
/// List of MAC addresses.
- List GetMacAddresses();
+ IReadOnlyCollection GetMacAddresses();
///
/// Checks to see if the IP Address provided matches an interface that has a gateway.
diff --git a/MediaBrowser.Common/Net/IPHost.cs b/MediaBrowser.Common/Net/IPHost.cs
new file mode 100644
index 0000000000..80052727af
--- /dev/null
+++ b/MediaBrowser.Common/Net/IPHost.cs
@@ -0,0 +1,447 @@
+#nullable enable
+using System;
+using System.Linq;
+using System.Net;
+using System.Net.Sockets;
+using System.Text.RegularExpressions;
+using System.Threading.Tasks;
+
+namespace MediaBrowser.Common.Net
+{
+ ///
+ /// Object that holds a host name.
+ ///
+ public class IPHost : IPObject
+ {
+ ///
+ /// Represents an IPHost that has no value.
+ ///
+ public static readonly IPHost None = new IPHost(string.Empty, IPAddress.None);
+
+ ///
+ /// Time when last resolved.
+ ///
+ private long _lastResolved;
+
+ ///
+ /// Gets the IP Addresses, attempting to resolve the name, if there are none.
+ ///
+ private IPAddress[] _addresses;
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// Host name to assign.
+ public IPHost(string name)
+ {
+ HostName = name ?? throw new ArgumentNullException(nameof(name));
+ _addresses = Array.Empty();
+ Resolved = false;
+ }
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// Host name to assign.
+ /// Address to assign.
+ private IPHost(string name, IPAddress address)
+ {
+ HostName = name ?? throw new ArgumentNullException(nameof(name));
+ _addresses = new IPAddress[] { address ?? throw new ArgumentNullException(nameof(address)) };
+ Resolved = !address.Equals(IPAddress.None);
+ }
+
+ ///
+ /// Gets or sets the object's first IP address.
+ ///
+ public override IPAddress Address
+ {
+ get
+ {
+ return ResolveHost() ? this[0] : IPAddress.None;
+ }
+
+ set
+ {
+ // Not implemented.
+ }
+ }
+
+ ///
+ /// Gets or sets the object's first IP's subnet prefix.
+ /// The setter does nothing, but shouldn't raise an exception.
+ ///
+ public override byte PrefixLength
+ {
+ get
+ {
+ return (byte)(ResolveHost() ? 128 : 0);
+ }
+
+ set
+ {
+ // Not implemented.
+ }
+ }
+
+ ///
+ /// Gets or sets timeout value before resolve required, in minutes.
+ ///
+ public byte Timeout { get; set; } = 30;
+
+ ///
+ /// Gets a value indicating whether the address has a value.
+ ///
+ public bool HasAddress
+ {
+ get
+ {
+ return _addresses.Length > 0;
+ }
+ }
+
+ ///
+ /// Gets the host name of this object.
+ ///
+ public string HostName { get; }
+
+ ///
+ /// Gets a value indicating whether this host has attempted to be resolved.
+ ///
+ public bool Resolved { get; private set; }
+
+ ///
+ /// Gets or sets the IP Addresses associated with this object.
+ ///
+ /// Index of address.
+ public IPAddress this[int index]
+ {
+ get
+ {
+ ResolveHost();
+ return index >= 0 && index < _addresses.Length ? _addresses[index] : IPAddress.None;
+ }
+ }
+
+ ///
+ /// Attempts to parse the host string.
+ ///
+ /// Host name to parse.
+ /// Object representing the string, if it has successfully been parsed.
+ /// Success result of the parsing.
+ 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;
+ }
+
+ ///
+ /// Attempts to parse the host string.
+ ///
+ /// Host name to parse.
+ /// Object representing the string, if it has successfully been parsed.
+ public static IPHost Parse(string host)
+ {
+ if (!string.IsNullOrEmpty(host) && IPHost.TryParse(host, out IPHost res))
+ {
+ return res;
+ }
+
+ throw new InvalidCastException("Host does not contain a valid value. {host}");
+ }
+
+ ///
+ /// Attempts to parse the host string, ensuring that it resolves only to a specific IP type.
+ ///
+ /// Host name to parse.
+ /// Addressfamily filter.
+ /// Object representing the string, if it has successfully been parsed.
+ public static IPHost Parse(string host, AddressFamily family)
+ {
+ if (!string.IsNullOrEmpty(host) && IPHost.TryParse(host, out IPHost res))
+ {
+ if (family == AddressFamily.InterNetwork)
+ {
+ res.Remove(AddressFamily.InterNetworkV6);
+ }
+ else
+ {
+ res.Remove(AddressFamily.InterNetwork);
+ }
+
+ return res;
+ }
+
+ throw new InvalidCastException("Host does not contain a valid value. {host}");
+ }
+
+ ///
+ /// Returns the Addresses that this item resolved to.
+ ///
+ /// IPAddress Array.
+ public IPAddress[] GetAddresses()
+ {
+ ResolveHost();
+ return _addresses;
+ }
+
+ ///
+ public override bool Contains(IPAddress address)
+ {
+ if (address != null && !Address.Equals(IPAddress.None))
+ {
+ if (address.IsIPv4MappedToIPv6)
+ {
+ address = address.MapToIPv4();
+ }
+
+ foreach (var addr in GetAddresses())
+ {
+ if (address.Equals(addr))
+ {
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+
+ ///
+ public override bool Equals(IPObject? other)
+ {
+ if (other is IPHost otherObj)
+ {
+ // Do we have the name Hostname?
+ if (string.Equals(otherObj.HostName, HostName, StringComparison.OrdinalIgnoreCase))
+ {
+ return true;
+ }
+
+ if (!ResolveHost() || !otherObj.ResolveHost())
+ {
+ return false;
+ }
+
+ // Do any of our IP addresses match?
+ foreach (IPAddress addr in _addresses)
+ {
+ foreach (IPAddress otherAddress in otherObj._addresses)
+ {
+ if (addr.Equals(otherAddress))
+ {
+ return true;
+ }
+ }
+ }
+ }
+
+ return false;
+ }
+
+ ///
+ public override bool IsIP6()
+ {
+ // Returns true if interfaces are only IP6.
+ if (ResolveHost())
+ {
+ foreach (IPAddress i in _addresses)
+ {
+ if (i.AddressFamily != AddressFamily.InterNetworkV6)
+ {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ return false;
+ }
+
+ ///
+ public override string ToString()
+ {
+ // StringBuilder not optimum here.
+ string output = string.Empty;
+ if (_addresses.Length > 0)
+ {
+ bool moreThanOne = _addresses.Length > 1;
+ if (moreThanOne)
+ {
+ output = "[";
+ }
+
+ foreach (var i in _addresses)
+ {
+ if (Address.Equals(IPAddress.None) && Address.AddressFamily == AddressFamily.Unspecified)
+ {
+ output += HostName + ",";
+ }
+ else if (i.Equals(IPAddress.Any))
+ {
+ output += "Any IP4 Address,";
+ }
+ else if (Address.Equals(IPAddress.IPv6Any))
+ {
+ output += "Any IP6 Address,";
+ }
+ else if (i.Equals(IPAddress.Broadcast))
+ {
+ output += "Any Address,";
+ }
+ else
+ {
+ output += $"{i}/32,";
+ }
+ }
+
+ output = output[0..^1];
+
+ if (moreThanOne)
+ {
+ output += "]";
+ }
+ }
+ else
+ {
+ output = HostName;
+ }
+
+ return output;
+ }
+
+ ///
+ public override void Remove(AddressFamily family)
+ {
+ if (ResolveHost())
+ {
+ _addresses = _addresses.Where(p => p.AddressFamily != family).ToArray();
+ }
+ }
+
+ ///
+ public override bool Contains(IPObject address)
+ {
+ // An IPHost cannot contain another IPObject, it can only be equal.
+ return Equals(address);
+ }
+
+ ///
+ protected override IPObject CalculateNetworkAddress()
+ {
+ var netAddr = NetworkAddressOf(this[0], PrefixLength);
+ return new IPNetAddress(netAddr.Address, netAddr.PrefixLength);
+ }
+
+ ///
+ /// Attempt to resolve the ip address of a host.
+ ///
+ /// The result of the comparison function.
+ private bool ResolveHost()
+ {
+ // When was the last time we resolved?
+ if (_lastResolved == 0)
+ {
+ _lastResolved = DateTime.Now.Ticks;
+ }
+
+ // If we haven't resolved before, or out timer has run out...
+ if ((_addresses.Length == 0 && !Resolved) || (TimeSpan.FromTicks(DateTime.Now.Ticks - _lastResolved).TotalMinutes > Timeout))
+ {
+ _lastResolved = DateTime.Now.Ticks;
+ ResolveHostInternal().GetAwaiter().GetResult();
+ Resolved = true;
+ }
+
+ return _addresses.Length > 0;
+ }
+
+ ///
+ /// Task that looks up a Host name and returns its IP addresses.
+ ///
+ /// Array of IPAddress objects.
+ private async Task ResolveHostInternal()
+ {
+ if (!string.IsNullOrEmpty(HostName))
+ {
+ // Resolves the host name - so save a DNS lookup.
+ if (string.Equals(HostName, "localhost", StringComparison.OrdinalIgnoreCase))
+ {
+ _addresses = new IPAddress[] { new IPAddress(Ipv4Loopback), new IPAddress(Ipv6Loopback) };
+ return;
+ }
+
+ if (Uri.CheckHostName(HostName).Equals(UriHostNameType.Dns))
+ {
+ try
+ {
+ IPHostEntry ip = await Dns.GetHostEntryAsync(HostName).ConfigureAwait(false);
+ _addresses = ip.AddressList;
+ }
+ catch (SocketException)
+ {
+ // Ignore socket errors, as the result value will just be an empty array.
+ }
+ }
+ }
+ }
+ }
+}
diff --git a/MediaBrowser.Common/Net/IPNetAddress.cs b/MediaBrowser.Common/Net/IPNetAddress.cs
new file mode 100644
index 0000000000..bcd049f3d5
--- /dev/null
+++ b/MediaBrowser.Common/Net/IPNetAddress.cs
@@ -0,0 +1,277 @@
+#nullable enable
+using System;
+using System.Net;
+using System.Net.Sockets;
+
+namespace MediaBrowser.Common.Net
+{
+ ///
+ /// An object that holds and IP address and subnet mask.
+ ///
+ public class IPNetAddress : IPObject
+ {
+ ///
+ /// Represents an IPNetAddress that has no value.
+ ///
+ public static readonly IPNetAddress None = new IPNetAddress(IPAddress.None);
+
+ ///
+ /// IPv4 multicast address.
+ ///
+ public static readonly IPAddress MulticastIPv4 = IPAddress.Parse("239.255.255.250");
+
+ ///
+ /// IPv6 local link multicast address.
+ ///
+ public static readonly IPAddress MulticastIPv6LinkLocal = IPAddress.Parse("ff02::C");
+
+ ///
+ /// IPv6 site local multicast address.
+ ///
+ public static readonly IPAddress MulticastIPv6SiteLocal = IPAddress.Parse("ff05::C");
+
+ ///
+ /// IP4Loopback address host.
+ ///
+ public static readonly IPNetAddress IP4Loopback = IPNetAddress.Parse("127.0.0.1/32");
+
+ ///
+ /// IP6Loopback address host.
+ ///
+ public static readonly IPNetAddress IP6Loopback = IPNetAddress.Parse("::1");
+
+ ///
+ /// Object's IP address.
+ ///
+ private IPAddress _address;
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// Address to assign.
+ public IPNetAddress(IPAddress address)
+ {
+ _address = address ?? throw new ArgumentNullException(nameof(address));
+ PrefixLength = (byte)(address.AddressFamily == AddressFamily.InterNetwork ? 32 : 128);
+ }
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// IP Address.
+ /// Mask as a CIDR.
+ public IPNetAddress(IPAddress address, byte prefixLength)
+ {
+ if (address?.IsIPv4MappedToIPv6 ?? throw new ArgumentNullException(nameof(address)))
+ {
+ _address = address.MapToIPv4();
+ }
+ else
+ {
+ _address = address;
+ }
+
+ PrefixLength = prefixLength;
+ }
+
+ ///
+ /// Gets or sets the object's IP address.
+ ///
+ public override IPAddress Address
+ {
+ get
+ {
+ return _address;
+ }
+
+ set
+ {
+ _address = value ?? IPAddress.None;
+ }
+ }
+
+ ///
+ public override byte PrefixLength { get; set; }
+
+ ///
+ /// Try to parse the address and subnet strings into an IPNetAddress object.
+ ///
+ /// IP address to parse. Can be CIDR or X.X.X.X notation.
+ /// Resultant object.
+ /// True if the values parsed successfully. False if not, resulting in the IP being null.
+ public static bool TryParse(string addr, out IPNetAddress ip)
+ {
+ if (!string.IsNullOrEmpty(addr))
+ {
+ addr = addr.Trim();
+
+ // Try to parse it as is.
+ if (IPAddress.TryParse(addr, out IPAddress res))
+ {
+ ip = new IPNetAddress(res);
+ return true;
+ }
+
+ // Is it a network?
+ string[] tokens = addr.Split("/");
+
+ if (tokens.Length == 2)
+ {
+ tokens[0] = tokens[0].TrimEnd();
+ tokens[1] = tokens[1].TrimStart();
+
+ if (IPAddress.TryParse(tokens[0], out res))
+ {
+ // Is the subnet part a cidr?
+ if (byte.TryParse(tokens[1], out byte cidr))
+ {
+ ip = new IPNetAddress(res, cidr);
+ return true;
+ }
+
+ // Is the subnet in x.y.a.b form?
+ if (IPAddress.TryParse(tokens[1], out IPAddress mask))
+ {
+ ip = new IPNetAddress(res, MaskToCidr(mask));
+ return true;
+ }
+ }
+ }
+ }
+
+ ip = None;
+ return false;
+ }
+
+ ///
+ /// Parses the string provided, throwing an exception if it is badly formed.
+ ///
+ /// String to parse.
+ /// IPNetAddress object.
+ public static IPNetAddress Parse(string addr)
+ {
+ if (TryParse(addr, out IPNetAddress o))
+ {
+ return o;
+ }
+
+ throw new ArgumentException("Unable to recognise object :" + addr);
+ }
+
+ ///
+ public override bool Contains(IPAddress address)
+ {
+ if (address == null)
+ {
+ throw new ArgumentNullException(nameof(address));
+ }
+
+ if (address.IsIPv4MappedToIPv6)
+ {
+ address = address.MapToIPv4();
+ }
+
+ var altAddress = NetworkAddressOf(address, PrefixLength);
+ return NetworkAddress.Address.Equals(altAddress.Address) && NetworkAddress.PrefixLength >= altAddress.PrefixLength;
+ }
+
+ ///
+ public override bool Contains(IPObject address)
+ {
+ if (address is IPHost addressObj && addressObj.HasAddress)
+ {
+ foreach (IPAddress addr in addressObj.GetAddresses())
+ {
+ if (Contains(addr))
+ {
+ return true;
+ }
+ }
+ }
+ else if (address is IPNetAddress netaddrObj)
+ {
+ // Have the same network address, but different subnets?
+ if (NetworkAddress.Address.Equals(netaddrObj.NetworkAddress.Address))
+ {
+ return NetworkAddress.PrefixLength <= netaddrObj.PrefixLength;
+ }
+
+ var altAddress = NetworkAddressOf(netaddrObj.Address, PrefixLength);
+ return NetworkAddress.Address.Equals(altAddress.Address);
+ }
+
+ return false;
+ }
+
+ ///
+ public override bool Equals(IPObject? other)
+ {
+ if (other is IPNetAddress otherObj && !Address.Equals(IPAddress.None) && !otherObj.Address.Equals(IPAddress.None))
+ {
+ return Address.Equals(otherObj.Address) &&
+ PrefixLength == otherObj.PrefixLength;
+ }
+
+ return false;
+ }
+
+ ///
+ public override bool Equals(IPAddress address)
+ {
+ if (address != null && !address.Equals(IPAddress.None) && !Address.Equals(IPAddress.None))
+ {
+ return address.Equals(Address);
+ }
+
+ return false;
+ }
+
+ ///
+ public override string ToString()
+ {
+ return ToString(false);
+ }
+
+ ///
+ /// Returns a textual representation of this object.
+ ///
+ /// Set to true, if the subnet is to be included as part of the address.
+ /// String representation of this object.
+ public string ToString(bool shortVersion)
+ {
+ if (!Address.Equals(IPAddress.None))
+ {
+ if (Address.Equals(IPAddress.Any))
+ {
+ return "Any IP4 Address";
+ }
+
+ if (Address.Equals(IPAddress.IPv6Any))
+ {
+ return "Any IP6 Address";
+ }
+
+ if (Address.Equals(IPAddress.Broadcast))
+ {
+ return "Any Address";
+ }
+
+ if (shortVersion)
+ {
+ return Address.ToString();
+ }
+
+ return $"{Address}/{PrefixLength}";
+ }
+
+ return string.Empty;
+ }
+
+ ///
+ protected override IPObject CalculateNetworkAddress()
+ {
+ var value = NetworkAddressOf(_address, PrefixLength);
+ return new IPNetAddress(value.Address, value.PrefixLength);
+ }
+ }
+}
diff --git a/MediaBrowser.Common/Net/IPObject.cs b/MediaBrowser.Common/Net/IPObject.cs
new file mode 100644
index 0000000000..a08694c266
--- /dev/null
+++ b/MediaBrowser.Common/Net/IPObject.cs
@@ -0,0 +1,395 @@
+#nullable enable
+using System;
+using System.Net;
+using System.Net.Sockets;
+
+namespace MediaBrowser.Common.Net
+{
+ ///
+ /// Base network object class.
+ ///
+ public abstract class IPObject : IEquatable
+ {
+ ///
+ /// IPv6 Loopback address.
+ ///
+ protected static readonly byte[] Ipv6Loopback = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
+
+ ///
+ /// IPv4 Loopback address.
+ ///
+ protected static readonly byte[] Ipv4Loopback = { 127, 0, 0, 1 };
+
+ ///
+ /// The network address of this object.
+ ///
+ private IPObject? _networkAddress;
+
+ ///
+ /// Gets or sets the user defined functions that need storage in this object.
+ ///
+ public int Tag { get; set; }
+
+ ///
+ /// Gets or sets the object's IP address.
+ ///
+ public abstract IPAddress Address { get; set; }
+
+ ///
+ /// Gets the object's network address.
+ ///
+ public IPObject NetworkAddress
+ {
+ get
+ {
+ if (_networkAddress == null)
+ {
+ _networkAddress = CalculateNetworkAddress();
+ }
+
+ return _networkAddress;
+ }
+ }
+
+ ///
+ /// Gets or sets the object's IP address.
+ ///
+ public abstract byte PrefixLength { get; set; }
+
+ ///
+ /// Gets the AddressFamily of this object.
+ ///
+ public AddressFamily AddressFamily
+ {
+ get
+ {
+ // Keep terms separate as Address performs other functions in inherited objects.
+ IPAddress address = Address;
+ return address.Equals(IPAddress.None) ? AddressFamily.Unspecified : address.AddressFamily;
+ }
+ }
+
+ ///
+ /// Returns the network address of an object.
+ ///
+ /// IP Address to convert.
+ /// Subnet prefix.
+ /// IPAddress.
+ public static (IPAddress Address, byte PrefixLength) NetworkAddressOf(IPAddress address, byte prefixLength)
+ {
+ if (address == null)
+ {
+ throw new ArgumentNullException(nameof(address));
+ }
+
+ if (address.IsIPv4MappedToIPv6)
+ {
+ address = address.MapToIPv4();
+ }
+
+ if (IsLoopback(address))
+ {
+ return (Address: address, PrefixLength: prefixLength);
+ }
+
+ byte[] addressBytes = address.GetAddressBytes();
+
+ int div = prefixLength / 8;
+ int mod = prefixLength % 8;
+ if (mod != 0)
+ {
+ mod = 8 - mod;
+ addressBytes[div] = (byte)((int)addressBytes[div] >> mod << mod);
+ div++;
+ }
+
+ for (int octet = div; octet < addressBytes.Length; octet++)
+ {
+ addressBytes[octet] = 0;
+ }
+
+ return (Address: new IPAddress(addressBytes), PrefixLength: prefixLength);
+ }
+
+ ///
+ /// Tests to see if the ip address is a Loopback address.
+ ///
+ /// Value to test.
+ /// True if it is.
+ public static bool IsLoopback(IPAddress address)
+ {
+ if (address == null)
+ {
+ throw new ArgumentNullException(nameof(address));
+ }
+
+ if (!address.Equals(IPAddress.None))
+ {
+ if (address.IsIPv4MappedToIPv6)
+ {
+ address = address.MapToIPv4();
+ }
+
+ return address.Equals(IPAddress.Loopback) || address.Equals(IPAddress.IPv6Loopback);
+ }
+
+ return false;
+ }
+
+ ///
+ /// Tests to see if the ip address is an IP6 address.
+ ///
+ /// Value to test.
+ /// True if it is.
+ public static bool IsIP6(IPAddress address)
+ {
+ if (address == null)
+ {
+ throw new ArgumentNullException(nameof(address));
+ }
+
+ if (address.IsIPv4MappedToIPv6)
+ {
+ address = address.MapToIPv4();
+ }
+
+ return !address.Equals(IPAddress.None) && (address.AddressFamily == AddressFamily.InterNetworkV6);
+ }
+
+ ///
+ /// Tests to see if the address in the private address range.
+ ///
+ /// Object to test.
+ /// True if it contains a private address.
+ public static bool IsPrivateAddressRange(IPAddress address)
+ {
+ if (address == null)
+ {
+ throw new ArgumentNullException(nameof(address));
+ }
+
+ if (!address.Equals(IPAddress.None))
+ {
+ if (address.AddressFamily == AddressFamily.InterNetwork)
+ {
+ if (address.IsIPv4MappedToIPv6)
+ {
+ address = address.MapToIPv4();
+ }
+
+ byte[] octet = address.GetAddressBytes();
+
+ return (octet[0] == 10) ||
+ (octet[0] == 172 && octet[1] >= 16 && octet[1] <= 31) || // RFC1918
+ (octet[0] == 192 && octet[1] == 168) || // RFC1918
+ (octet[0] == 127); // RFC1122
+ }
+ else
+ {
+ byte[] octet = address.GetAddressBytes();
+ uint word = (uint)(octet[0] << 8) + octet[1];
+
+ return (word >= 0xfe80 && word <= 0xfebf) || // fe80::/10 :Local link.
+ (word >= 0xfc00 && word <= 0xfdff); // fc00::/7 :Unique local address.
+ }
+ }
+
+ return false;
+ }
+
+ ///
+ /// Returns true if the IPAddress contains an IP6 Local link address.
+ ///
+ /// IPAddress object to check.
+ /// True if it is a local link address.
+ /// See https://stackoverflow.com/questions/6459928/explain-the-instance-properties-of-system-net-ipaddress
+ /// it appears that the IPAddress.IsIPv6LinkLocal is out of date.
+ ///
+ public static bool IsIPv6LinkLocal(IPAddress address)
+ {
+ if (address == null)
+ {
+ throw new ArgumentNullException(nameof(address));
+ }
+
+ if (address.IsIPv4MappedToIPv6)
+ {
+ address = address.MapToIPv4();
+ }
+
+ if (address.AddressFamily != AddressFamily.InterNetworkV6)
+ {
+ return false;
+ }
+
+ byte[] octet = address.GetAddressBytes();
+ uint word = (uint)(octet[0] << 8) + octet[1];
+
+ return word >= 0xfe80 && word <= 0xfebf; // fe80::/10 :Local link.
+ }
+
+ ///
+ /// Convert a subnet mask in CIDR notation to a dotted decimal string value. IPv4 only.
+ ///
+ /// Subnet mask in CIDR notation.
+ /// IPv4 or IPv6 family.
+ /// String value of the subnet mask in dotted decimal notation.
+ public static IPAddress CidrToMask(byte cidr, AddressFamily family)
+ {
+ uint addr = 0xFFFFFFFF << (family == AddressFamily.InterNetwork ? 32 : 128 - cidr);
+ addr =
+ ((addr & 0xff000000) >> 24) |
+ ((addr & 0x00ff0000) >> 8) |
+ ((addr & 0x0000ff00) << 8) |
+ ((addr & 0x000000ff) << 24);
+ return new IPAddress(addr);
+ }
+
+ ///
+ /// Convert a mask to a CIDR. IPv4 only.
+ /// https://stackoverflow.com/questions/36954345/get-cidr-from-netmask.
+ ///
+ /// Subnet mask.
+ /// Byte CIDR representing the mask.
+ public static byte MaskToCidr(IPAddress mask)
+ {
+ if (mask == null)
+ {
+ throw new ArgumentNullException(nameof(mask));
+ }
+
+ byte cidrnet = 0;
+ if (!mask.Equals(IPAddress.Any))
+ {
+ byte[] bytes = mask.GetAddressBytes();
+
+ var zeroed = false;
+ for (var i = 0; i < bytes.Length; i++)
+ {
+ for (int v = bytes[i]; (v & 0xFF) != 0; v <<= 1)
+ {
+ if (zeroed)
+ {
+ // Invalid netmask.
+ return (byte)~cidrnet;
+ }
+
+ if ((v & 0x80) == 0)
+ {
+ zeroed = true;
+ }
+ else
+ {
+ cidrnet++;
+ }
+ }
+ }
+ }
+
+ return cidrnet;
+ }
+
+ ///
+ /// Tests to see if this object is a Loopback address.
+ ///
+ /// True if it is.
+ public virtual bool IsLoopback()
+ {
+ return IsLoopback(Address);
+ }
+
+ ///
+ /// Removes all addresses of a specific type from this object.
+ ///
+ /// Type of address to remove.
+ public virtual void Remove(AddressFamily family)
+ {
+ // This method only peforms a function in the IPHost implementation of IPObject.
+ }
+
+ ///
+ /// Tests to see if this object is an IPv6 address.
+ ///
+ /// True if it is.
+ public virtual bool IsIP6()
+ {
+ return IsIP6(Address);
+ }
+
+ ///
+ /// Returns true if this IP address is in the RFC private address range.
+ ///
+ /// True this object has a private address.
+ public virtual bool IsPrivateAddressRange()
+ {
+ return IsPrivateAddressRange(Address);
+ }
+
+ ///
+ /// Compares this to the object passed as a parameter.
+ ///
+ /// Object to compare to.
+ /// Equality result.
+ public virtual bool Equals(IPAddress ip)
+ {
+ if (ip != null)
+ {
+ if (ip.IsIPv4MappedToIPv6)
+ {
+ ip = ip.MapToIPv4();
+ }
+
+ return !Address.Equals(IPAddress.None) && Address.Equals(ip);
+ }
+
+ return false;
+ }
+
+ ///
+ /// Compares this to the object passed as a parameter.
+ ///
+ /// Object to compare to.
+ /// Equality result.
+ public virtual bool Equals(IPObject? other)
+ {
+ if (other != null && other is IPObject otherObj)
+ {
+ return !Address.Equals(IPAddress.None) && Address.Equals(otherObj.Address);
+ }
+
+ return false;
+ }
+
+ ///
+ /// Compares the address in this object and the address in the object passed as a parameter.
+ ///
+ /// Object's IP address to compare to.
+ /// Comparison result.
+ public abstract bool Contains(IPObject address);
+
+ ///
+ /// Compares the address in this object and the address in the object passed as a parameter.
+ ///
+ /// Object's IP address to compare to.
+ /// Comparison result.
+ public abstract bool Contains(IPAddress address);
+
+ ///
+ public override int GetHashCode()
+ {
+ return Address.GetHashCode();
+ }
+
+ ///
+ public override bool Equals(object obj)
+ {
+ return Equals(obj as IPObject);
+ }
+
+ ///
+ /// Calculates the network address of this object.
+ ///
+ /// Returns the network address of this object.
+ protected abstract IPObject CalculateNetworkAddress();
+ }
+}
diff --git a/MediaBrowser.Common/Net/NetworkExtensions.cs b/MediaBrowser.Common/Net/NetworkExtensions.cs
new file mode 100644
index 0000000000..6e9cb46dc1
--- /dev/null
+++ b/MediaBrowser.Common/Net/NetworkExtensions.cs
@@ -0,0 +1,254 @@
+#pragma warning disable CA1062 // Validate arguments of public methods
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Net;
+using System.Runtime.CompilerServices;
+using NetCollection = System.Collections.ObjectModel.Collection;
+
+namespace MediaBrowser.Common.Net
+{
+ ///
+ /// Defines the .
+ ///
+ public static class NetworkExtensions
+ {
+ ///
+ /// Add an address to the collection.
+ ///
+ /// The .
+ /// Item to add.
+ public static void AddItem(this NetCollection source, IPAddress ip)
+ {
+ if (!source.ContainsAddress(ip))
+ {
+ source.Add(new IPNetAddress(ip, 32));
+ }
+ }
+
+ ///
+ /// Add multiple items to the collection.
+ ///
+ /// The .
+ /// Item to add.
+ /// Return the collection.
+ public static NetCollection AddRange(this NetCollection destination, IEnumerable source)
+ {
+ foreach (var item in source)
+ {
+ destination.Add(item);
+ }
+
+ return destination;
+ }
+
+ ///
+ /// Adds a network to the collection.
+ ///
+ /// The .
+ /// Item to add.
+ public static void AddItem(this NetCollection source, IPObject item)
+ {
+ if (!source.ContainsAddress(item))
+ {
+ source.Add(item);
+ }
+ }
+
+ ///
+ /// Converts this object to a string.
+ ///
+ /// The .
+ /// Returns a string representation of this object.
+ public static string Readable(this NetCollection source)
+ {
+ string output = "[";
+ if (source.Count > 0)
+ {
+ foreach (var i in source)
+ {
+ output += $"{i},";
+ }
+
+ output = output[0..^1];
+ }
+
+ return $"{output}]";
+ }
+
+ ///
+ /// Returns true if the collection contains an item with the ip address,
+ /// or the ip address falls within any of the collection's network ranges.
+ ///
+ /// The .
+ /// The item to look for.
+ /// True if the collection contains the item.
+ public static bool ContainsAddress(this NetCollection source, IPAddress item)
+ {
+ if (source.Count == 0)
+ {
+ return false;
+ }
+
+ if (item == null)
+ {
+ throw new ArgumentNullException(nameof(item));
+ }
+
+ if (item.IsIPv4MappedToIPv6)
+ {
+ item = item.MapToIPv4();
+ }
+
+ foreach (var i in source)
+ {
+ if (i.Contains(item))
+ {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ ///
+ /// Returns true if the collection contains an item with the ip address,
+ /// or the ip address falls within any of the collection's network ranges.
+ ///
+ /// The .
+ /// The item to look for.
+ /// True if the collection contains the item.
+ public static bool ContainsAddress(this NetCollection source, IPObject item)
+ {
+ if (source.Count == 0)
+ {
+ return false;
+ }
+
+ if (item == null)
+ {
+ throw new ArgumentNullException(nameof(item));
+ }
+
+ foreach (var i in source)
+ {
+ if (i.Contains(item))
+ {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ ///
+ /// Returns a collection containing the subnets of this collection given.
+ ///
+ /// The .
+ /// NetCollection object containing the subnets.
+ public static NetCollection AsNetworks(this NetCollection source)
+ {
+ if (source == null)
+ {
+ throw new ArgumentNullException(nameof(source));
+ }
+
+ NetCollection res = new NetCollection();
+
+ foreach (IPObject i in source)
+ {
+ if (i is IPNetAddress nw)
+ {
+ // Add the subnet calculated from the interface address/mask.
+ var na = nw.NetworkAddress;
+ na.Tag = i.Tag;
+ res.Add(na);
+ }
+ else
+ {
+ // Flatten out IPHost and add all its ip addresses.
+ foreach (var addr in ((IPHost)i).GetAddresses())
+ {
+ IPNetAddress host = new IPNetAddress(addr)
+ {
+ Tag = i.Tag
+ };
+
+ res.Add(host);
+ }
+ }
+ }
+
+ return res;
+ }
+
+ ///
+ /// Excludes all the items from this list that are found in excludeList.
+ ///
+ /// The .
+ /// Items to exclude.
+ /// A new collection, with the items excluded.
+ public static NetCollection Exclude(this NetCollection source, NetCollection excludeList)
+ {
+ if (source.Count == 0 || excludeList == null)
+ {
+ return new NetCollection(source);
+ }
+
+ NetCollection results = new NetCollection();
+
+ bool found;
+ foreach (var outer in source)
+ {
+ found = false;
+
+ foreach (var inner in excludeList)
+ {
+ if (outer.Equals(inner))
+ {
+ found = true;
+ break;
+ }
+ }
+
+ if (!found)
+ {
+ results.Add(outer);
+ }
+ }
+
+ return results;
+ }
+
+ ///
+ /// Returns all items that co-exist in this object and target.
+ ///
+ /// The .
+ /// Collection to compare with.
+ /// A collection containing all the matches.
+ public static NetCollection Union(this NetCollection source, NetCollection target)
+ {
+ if (source.Count == 0)
+ {
+ return new NetCollection();
+ }
+
+ if (target == null)
+ {
+ throw new ArgumentNullException(nameof(target));
+ }
+
+ NetCollection nc = new NetCollection();
+
+ foreach (IPObject i in source)
+ {
+ if (target.ContainsAddress(i))
+ {
+ nc.Add(i);
+ }
+ }
+
+ return nc;
+ }
+ }
+}
diff --git a/MediaBrowser.sln b/MediaBrowser.sln
index d460c0ab0c..cb204137bd 100644
--- a/MediaBrowser.sln
+++ b/MediaBrowser.sln
@@ -68,6 +68,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Jellyfin.Server.Implementat
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Jellyfin.Networking", "Jellyfin.Networking\Jellyfin.Networking.csproj", "{0A3FCC4D-C714-4072-B90F-E374A15F9FF9}"
EndProject
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Jellyfin.Networking.Tests", "tests\Jellyfin.Networking.Tests\NetworkTesting\Jellyfin.Networking.Tests.csproj", "{42816EA8-4511-4CBF-A9C7-7791D5DDDAE6}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -182,6 +184,10 @@ Global
{0A3FCC4D-C714-4072-B90F-E374A15F9FF9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0A3FCC4D-C714-4072-B90F-E374A15F9FF9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0A3FCC4D-C714-4072-B90F-E374A15F9FF9}.Release|Any CPU.Build.0 = Release|Any CPU
+ {42816EA8-4511-4CBF-A9C7-7791D5DDDAE6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {42816EA8-4511-4CBF-A9C7-7791D5DDDAE6}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {42816EA8-4511-4CBF-A9C7-7791D5DDDAE6}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {42816EA8-4511-4CBF-A9C7-7791D5DDDAE6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -193,6 +199,7 @@ Global
{A2FD0A10-8F62-4F9D-B171-FFDF9F0AFA9D} = {FBBB5129-006E-4AD7-BAD5-8B7CA1D10ED6}
{2E3A1B4B-4225-4AAA-8B29-0181A84E7AEE} = {FBBB5129-006E-4AD7-BAD5-8B7CA1D10ED6}
{462584F7-5023-4019-9EAC-B98CA458C0A0} = {FBBB5129-006E-4AD7-BAD5-8B7CA1D10ED6}
+ {42816EA8-4511-4CBF-A9C7-7791D5DDDAE6} = {FBBB5129-006E-4AD7-BAD5-8B7CA1D10ED6}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3448830C-EBDC-426C-85CD-7BBB9651A7FE}
diff --git a/RSSDP/SsdpDevicePublisher.cs b/RSSDP/SsdpDevicePublisher.cs
index ae175d8c9d..3e89d7f602 100644
--- a/RSSDP/SsdpDevicePublisher.cs
+++ b/RSSDP/SsdpDevicePublisher.cs
@@ -6,7 +6,6 @@ using System.Net;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.Net;
-using NetworkCollection;
namespace Rssdp.Infrastructure
{
diff --git a/tests/Jellyfin.Networking.Tests/NetworkTesting/Jellyfin.Networking.Tests.csproj b/tests/Jellyfin.Networking.Tests/NetworkTesting/Jellyfin.Networking.Tests.csproj
new file mode 100644
index 0000000000..fa18316dff
--- /dev/null
+++ b/tests/Jellyfin.Networking.Tests/NetworkTesting/Jellyfin.Networking.Tests.csproj
@@ -0,0 +1,28 @@
+
+
+
+
+ {42816EA8-4511-4CBF-A9C7-7791D5DDDAE6}
+
+
+
+ netcoreapp3.1
+ false
+ true
+ enable
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/tests/Jellyfin.Networking.Tests/NetworkTesting/UnitTesting.cs b/tests/Jellyfin.Networking.Tests/NetworkTesting/UnitTesting.cs
new file mode 100644
index 0000000000..9e7e8d3ac3
--- /dev/null
+++ b/tests/Jellyfin.Networking.Tests/NetworkTesting/UnitTesting.cs
@@ -0,0 +1,425 @@
+using System;
+using System.Net;
+using Emby.Dlna.PlayTo;
+using Jellyfin.Networking.Configuration;
+using Jellyfin.Networking.Manager;
+using MediaBrowser.Common.Configuration;
+using MediaBrowser.Common.Net;
+using Moq;
+using Microsoft.Extensions.Logging.Abstractions;
+using Xunit;
+using NetCollection = System.Collections.ObjectModel.Collection;
+using XMLProperties = System.Collections.Generic.Dictionary;
+
+namespace NetworkTesting
+{
+ public class NetTesting
+ {
+ ///
+ /// Trys to identify the string and return an object of that class.
+ ///
+ /// String to parse.
+ /// IPObject to return.
+ /// True if the value parsed successfully.
+ private static bool TryParse(string addr, out IPObject result)
+ {
+ if (!string.IsNullOrEmpty(addr))
+ {
+ // Is it an IP address
+ if (IPNetAddress.TryParse(addr, out IPNetAddress nw))
+ {
+ result = nw;
+ return true;
+ }
+
+ if (IPHost.TryParse(addr, out IPHost h))
+ {
+ result = h;
+ return true;
+ }
+ }
+
+ result = IPNetAddress.None;
+ return false;
+ }
+
+
+ private IConfigurationManager GetMockConfig(NetworkConfiguration conf)
+ {
+ var configManager = new Mock
+ {
+ CallBase = true
+ };
+ configManager.Setup(x => x.GetConfiguration(It.IsAny())).Returns(conf);
+ return (IConfigurationManager)configManager.Object;
+ }
+
+ [Theory]
+ [InlineData("192.168.1.208/24,-16,eth16:200.200.200.200/24,11,eth11", "192.168.1.0/24;200.200.200.0/24", "[192.168.1.208/24,200.200.200.200/24]")]
+ [InlineData("192.168.1.208/24,-16,eth16:200.200.200.200/24,11,eth11", "192.168.1.0/24", "[192.168.1.208/24]")]
+ [InlineData("192.168.1.208/24,-16,vEthernet1:192.168.1.208/24,-16,vEthernet212;200.200.200.200/24,11,eth11", "192.168.1.0/24", "[192.168.1.208/24]")]
+ public void IgnoreVirtualInterfaces(string interfaces, string lan, string value)
+ {
+ var conf = new NetworkConfiguration()
+ {
+ EnableIPV6 = true,
+ EnableIPV4 = true,
+ LocalNetworkSubnets = lan.Split(';')
+ };
+
+ NetworkManager.MockNetworkSettings = interfaces;
+ var nm = new NetworkManager(GetMockConfig(conf), new NullLogger());
+ NetworkManager.MockNetworkSettings = string.Empty;
+
+ Assert.True(string.Equals(nm.GetInternalBindAddresses().ToString(), value, StringComparison.Ordinal));
+ }
+
+ [Theory]
+ [InlineData("192.168.10.0/24, !192.168.10.60/32", "192.168.10.60")]
+ public void TextIsInNetwork(string network, string value)
+ {
+ var conf = new NetworkConfiguration()
+ {
+ EnableIPV6 = true,
+ EnableIPV4 = true,
+ LocalNetworkSubnets = network.Split(',')
+ };
+
+ var nm = new NetworkManager(GetMockConfig(conf), new NullLogger());
+
+ Assert.True(!nm.IsInLocalNetwork(value));
+ }
+
+ [Theory]
+ [InlineData("127.0.0.1")]
+ [InlineData("127.0.0.1:123")]
+ [InlineData("localhost")]
+ [InlineData("localhost:1345")]
+ [InlineData("www.google.co.uk")]
+ [InlineData("fd23:184f:2029:0:3139:7386:67d7:d517")]
+ [InlineData("fd23:184f:2029:0:3139:7386:67d7:d517/56")]
+ [InlineData("[fd23:184f:2029:0:3139:7386:67d7:d517]:124")]
+ [InlineData("fe80::7add:12ff:febb:c67b%16")]
+ [InlineData("[fe80::7add:12ff:febb:c67b%16]:123")]
+ [InlineData("192.168.1.2/255.255.255.0")]
+ [InlineData("192.168.1.2/24")]
+
+ public void TestCollectionCreation(string address)
+ {
+ Assert.True(TryParse(address, out _));
+ }
+
+ [Theory]
+ [InlineData("256.128.0.0.0.1")]
+ [InlineData("127.0.0.1#")]
+ [InlineData("localhost!")]
+ [InlineData("fd23:184f:2029:0:3139:7386:67d7:d517:1231")]
+ public void TestInvalidCollectionCreation(string address)
+ {
+ Assert.False(TryParse(address, out _));
+ }
+
+ [Theory]
+ // Src, IncIP6, incIP4, exIP6, ecIP4, net
+ [InlineData("127.0.0.1#",
+ "[]",
+ "[]",
+ "[]",
+ "[]",
+ "[]")]
+ [InlineData("[127.0.0.1]",
+ "[]",
+ "[]",
+ "[127.0.0.1/32]",
+ "[127.0.0.1/32]",
+ "[]")]
+ [InlineData("",
+ "[]",
+ "[]",
+ "[]",
+ "[]",
+ "[]")]
+ [InlineData("192.158.1.2/255.255.0.0,192.169.1.2/8",
+ "[192.158.1.2/16,192.169.1.2/8]",
+ "[192.158.1.2/16,192.169.1.2/8]",
+ "[]",
+ "[]",
+ "[192.158.0.0/16,192.0.0.0/8]")]
+ [InlineData("192.158.1.2/16, localhost, fd23:184f:2029:0:3139:7386:67d7:d517, [10.10.10.10]",
+ "[192.158.1.2/16,127.0.0.1/32,fd23:184f:2029:0:3139:7386:67d7:d517/128]",
+ "[192.158.1.2/16,127.0.0.1/32]",
+ "[10.10.10.10/32]",
+ "[10.10.10.10/32]",
+ "[192.158.0.0/16,127.0.0.1/32,fd23:184f:2029:0:3139:7386:67d7:d517/128]")]
+ public void TestCollections(string settings, string result1, string result2, string result3, string result4, string result5)
+ {
+ var conf = new NetworkConfiguration()
+ {
+ EnableIPV6 = true,
+ EnableIPV4 = true,
+ };
+
+ var nm = new NetworkManager(GetMockConfig(conf), new NullLogger());
+
+ // Test included, IP6.
+ NetCollection nc = nm.CreateIPCollection(settings.Split(","), false);
+ Assert.True(string.Equals(nc.ToString(), result1, System.StringComparison.OrdinalIgnoreCase));
+
+ // Text excluded, non IP6.
+ nc = nm.CreateIPCollection(settings.Split(","), true);
+ Assert.True(string.Equals(nc?.ToString(), result3, System.StringComparison.OrdinalIgnoreCase));
+
+ conf.EnableIPV6 = false;
+ nm.UpdateSettings(conf);
+
+ // Test included, non IP6.
+ nc = nm.CreateIPCollection(settings.Split(","), false);
+ Assert.True(string.Equals(nc.ToString(), result2, System.StringComparison.OrdinalIgnoreCase));
+
+ // Test excluded, including IPv6.
+ nc = nm.CreateIPCollection(settings.Split(","), true);
+ Assert.True(string.Equals(nc.ToString(), result4, System.StringComparison.OrdinalIgnoreCase));
+
+ conf.EnableIPV6 = true;
+ nm.UpdateSettings(conf);
+
+ // Test network addresses of collection.
+ nc = nm.CreateIPCollection(settings.Split(","), false);
+ nc = nc.AsNetworks();
+ Assert.True(string.Equals(nc.ToString(), result5, System.StringComparison.OrdinalIgnoreCase));
+ }
+
+ [Theory]
+ [InlineData("127.0.0.1", "fd23:184f:2029:0:3139:7386:67d7:d517/64,fd23:184f:2029:0:c0f0:8a8a:7605:fffa/128,fe80::3139:7386:67d7:d517%16/64,192.168.1.208/24,::1/128,127.0.0.1/8", "[127.0.0.1/32]")]
+ [InlineData("127.0.0.1", "127.0.0.1/8", "[127.0.0.1/32]")]
+ public void UnionCheck(string settings, string compare, string result)
+ {
+ var conf = new NetworkConfiguration()
+ {
+ EnableIPV6 = true,
+ EnableIPV4 = true,
+ };
+
+ var nm = new NetworkManager(GetMockConfig(conf), new NullLogger());
+
+ NetCollection nc1 = nm.CreateIPCollection(settings.Split(","), false);
+ NetCollection nc2 = nm.CreateIPCollection(compare.Split(","), false);
+
+ Assert.True(nc1.Union(nc2).ToString() == result);
+ }
+
+ [Theory]
+ [InlineData("192.168.5.85/24", "192.168.5.1")]
+ [InlineData("192.168.5.85/24", "192.168.5.254")]
+ [InlineData("10.128.240.50/30", "10.128.240.48")]
+ [InlineData("10.128.240.50/30", "10.128.240.49")]
+ [InlineData("10.128.240.50/30", "10.128.240.50")]
+ [InlineData("10.128.240.50/30", "10.128.240.51")]
+ [InlineData("127.0.0.1/8", "127.0.0.1")]
+ public void IpV4SubnetMaskMatchesValidIpAddress(string netMask, string ipAddress)
+ {
+ var ipAddressObj = IPNetAddress.Parse(netMask);
+ Assert.True(ipAddressObj.Contains(IPAddress.Parse(ipAddress)));
+ }
+
+ [Theory]
+ [InlineData("192.168.5.85/24", "192.168.4.254")]
+ [InlineData("192.168.5.85/24", "191.168.5.254")]
+ [InlineData("10.128.240.50/30", "10.128.240.47")]
+ [InlineData("10.128.240.50/30", "10.128.240.52")]
+ [InlineData("10.128.240.50/30", "10.128.239.50")]
+ [InlineData("10.128.240.50/30", "10.127.240.51")]
+ public void IpV4SubnetMaskDoesNotMatchInvalidIpAddress(string netMask, string ipAddress)
+ {
+ var ipAddressObj = IPNetAddress.Parse(netMask);
+ Assert.False(ipAddressObj.Contains(IPAddress.Parse(ipAddress)));
+ }
+
+ [Theory]
+ [InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0012:0000:0000:0000:0000")]
+ [InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0012:FFFF:FFFF:FFFF:FFFF")]
+ [InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0012:0001:0000:0000:0000")]
+ [InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0012:FFFF:FFFF:FFFF:FFF0")]
+ [InlineData("2001:db8:abcd:0012::0/128", "2001:0DB8:ABCD:0012:0000:0000:0000:0000")]
+ public void IpV6SubnetMaskMatchesValidIpAddress(string netMask, string ipAddress)
+ {
+ var ipAddressObj = IPNetAddress.Parse(netMask);
+ Assert.True(ipAddressObj.Contains(IPAddress.Parse(ipAddress)));
+ }
+
+ [Theory]
+ [InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0011:FFFF:FFFF:FFFF:FFFF")]
+ [InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0013:0000:0000:0000:0000")]
+ [InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0013:0001:0000:0000:0000")]
+ [InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0011:FFFF:FFFF:FFFF:FFF0")]
+ [InlineData("2001:db8:abcd:0012::0/128", "2001:0DB8:ABCD:0012:0000:0000:0000:0001")]
+ public void IpV6SubnetMaskDoesNotMatchInvalidIpAddress(string netMask, string ipAddress)
+ {
+ var ipAddressObj = IPNetAddress.Parse(netMask);
+ Assert.False(ipAddressObj.Contains(IPAddress.Parse(ipAddress)));
+ }
+
+ [Theory]
+ [InlineData("10.0.0.0/255.0.0.0", "10.10.10.1/32")]
+ [InlineData("10.0.0.0/8", "10.10.10.1/32")]
+ [InlineData("10.0.0.0/255.0.0.0", "10.10.10.1")]
+
+ [InlineData("10.10.0.0/255.255.0.0", "10.10.10.1/32")]
+ [InlineData("10.10.0.0/16", "10.10.10.1/32")]
+ [InlineData("10.10.0.0/255.255.0.0", "10.10.10.1")]
+
+ [InlineData("10.10.10.0/255.255.255.0", "10.10.10.1/32")]
+ [InlineData("10.10.10.0/24", "10.10.10.1/32")]
+ [InlineData("10.10.10.0/255.255.255.0", "10.10.10.1")]
+
+ public void TestSubnets(string network, string ip)
+ {
+ Assert.True(TryParse(network, out IPObject? networkObj));
+ Assert.True(TryParse(ip, out IPObject? ipObj));
+
+#pragma warning disable CS8602 // Dereference of a possibly null reference.
+#pragma warning disable CS8604 // Possible null reference argument.
+ Assert.True(networkObj.Contains(ipObj));
+#pragma warning restore CS8604 // Possible null reference argument.
+#pragma warning restore CS8602 // Dereference of a possibly null reference.
+ }
+
+ [Theory]
+ [InlineData("192.168.1.2/24,10.10.10.1/24,172.168.1.2/24", "172.168.1.2/24", "172.168.1.2/24")]
+ [InlineData("192.168.1.2/24,10.10.10.1/24,172.168.1.2/24", "172.168.1.2/24, 10.10.10.1", "172.168.1.2/24,10.10.10.1/24")]
+ [InlineData("192.168.1.2/24,10.10.10.1/24,172.168.1.2/24", "192.168.1.2/255.255.255.0, 10.10.10.1", "192.168.1.2/24,10.10.10.1/24")]
+ [InlineData("192.168.1.2/24,10.10.10.1/24,172.168.1.2/24", "192.168.1.2/24, 100.10.10.1", "192.168.1.2/24")]
+ [InlineData("192.168.1.2/24,10.10.10.1/24,172.168.1.2/24", "194.168.1.2/24, 100.10.10.1", "")]
+
+ public void TestMatches(string source, string dest, string result)
+ {
+ var conf = new NetworkConfiguration()
+ {
+ EnableIPV6 = true,
+ EnableIPV4 = true
+ };
+
+ var nm = new NetworkManager(GetMockConfig(conf), new NullLogger());
+
+ // Test included, IP6.
+ NetCollection ncSource = nm.CreateIPCollection(source.Split(","));
+ NetCollection ncDest = nm.CreateIPCollection(dest.Split(","));
+ NetCollection ncResult = ncSource.Union(ncDest);
+ NetCollection resultCollection = nm.CreateIPCollection(result.Split(","));
+ Assert.True(ncResult.Equals(resultCollection));
+ }
+
+
+ [Theory]
+ [InlineData("10.1.1.1/32", "10.1.1.1")]
+ [InlineData("192.168.1.254/32", "192.168.1.254/255.255.255.255")]
+
+ public void TestEquals(string source, string dest)
+ {
+ Assert.True(IPNetAddress.Parse(source).Equals(IPNetAddress.Parse(dest)));
+ Assert.True(IPNetAddress.Parse(dest).Equals(IPNetAddress.Parse(source)));
+ }
+
+ [Theory]
+
+ // Testing bind interfaces. These are set for my system so won't work elsewhere.
+ // On my system eth16 is internal, eth11 external (Windows defines the indexes).
+ //
+ // This test is to replicate how DNLA requests work throughout the system.
+
+ // User on internal network, we're bound internal and external - so result is internal.
+ [InlineData("192.168.1.1", "eth16,eth11", false, "eth16")]
+ // User on external network, we're bound internal and external - so result is external.
+ [InlineData("8.8.8.8", "eth16,eth11", false, "eth11")]
+ // User on internal network, we're bound internal only - so result is internal.
+ [InlineData("10.10.10.10", "eth16", false, "eth16")]
+ // User on internal network, no binding specified - so result is the 1st internal.
+ [InlineData("192.168.1.1", "", false, "eth16")]
+ // User on external network, internal binding only - so result is the 1st internal.
+ [InlineData("jellyfin.org", "eth16", false, "eth16")]
+ // User on external network, no binding - so result is the 1st external.
+ [InlineData("jellyfin.org", "", false, "eth11")]
+ // User assumed to be internal, no binding - so result is the 1st internal.
+ [InlineData("", "", false, "eth16")]
+ public void TestBindInterfaces(string source, string bindAddresses, bool ipv6enabled, string result)
+ {
+ var conf = new NetworkConfiguration()
+ {
+ LocalNetworkAddresses = bindAddresses.Split(','),
+ EnableIPV6 = ipv6enabled,
+ EnableIPV4 = true
+ };
+
+ NetworkManager.MockNetworkSettings = "192.168.1.208/24,-16,eth16:200.200.200.200/24,11,eth11";
+ var nm = new NetworkManager(GetMockConfig(conf), new NullLogger());
+ NetworkManager.MockNetworkSettings = string.Empty;
+
+ _ = nm.TryParseInterface(result, out NetCollection? resultObj);
+
+ if (resultObj != null)
+ {
+ result = ((IPNetAddress)resultObj[0]).ToString(true);
+ var intf = nm.GetBindInterface(source, out int? _);
+
+ Assert.True(string.Equals(intf, result, System.StringComparison.OrdinalIgnoreCase));
+ }
+ }
+
+ [Theory]
+
+ // Testing bind interfaces. These are set for my system so won't work elsewhere.
+ // On my system eth16 is internal, eth11 external (Windows defines the indexes).
+ //
+ // This test is to replicate how subnet bound ServerPublisherUri work throughout the system.
+
+ // User on internal network, we're bound internal and external - so result is internal override.
+ [InlineData("192.168.1.1", "192.168.1.0/24", "eth16,eth11", false, "192.168.1.0/24=internal.jellyfin", "internal.jellyfin")]
+
+ // User on external network, we're bound internal and external - so result is override.
+ [InlineData("8.8.8.8", "192.168.1.0/24", "eth16,eth11", false, "0.0.0.0=http://helloworld.com", "http://helloworld.com")]
+
+ // User on internal network, we're bound internal only, but the address isn't in the LAN - so return the override.
+ [InlineData("10.10.10.10", "192.168.1.0/24", "eth16", false, "0.0.0.0=http://internalButNotDefinedAsLan.com", "http://internalButNotDefinedAsLan.com")]
+
+ // User on internal network, no binding specified - so result is the 1st internal.
+ [InlineData("192.168.1.1", "192.168.1.0/24", "", false, "0.0.0.0=http://helloworld.com", "eth16")]
+
+ // User on external network, internal binding only - so asumption is a proxy forward, return external override.
+ [InlineData("jellyfin.org", "192.168.1.0/24", "eth16", false, "0.0.0.0=http://helloworld.com", "http://helloworld.com")]
+
+ // User on external network, no binding - so result is the 1st external which is overriden.
+ [InlineData("jellyfin.org", "192.168.1.0/24", "", false, "0.0.0.0 = http://helloworld.com", "http://helloworld.com")]
+
+ // User assumed to be internal, no binding - so result is the 1st internal.
+ [InlineData("", "192.168.1.0/24", "", false, "0.0.0.0=http://helloworld.com", "eth16")]
+
+ // User is internal, no binding - so result is the 1st internal, which is then overridden.
+ [InlineData("192.168.1.1", "192.168.1.0/24", "", false, "eth16=http://helloworld.com", "http://helloworld.com")]
+
+ public void TestBindInterfaceOverrides(string source, string lan, string bindAddresses, bool ipv6enabled, string publishedServers, string result)
+ {
+ var conf = new NetworkConfiguration()
+ {
+ LocalNetworkSubnets = lan.Split(','),
+ LocalNetworkAddresses = bindAddresses.Split(','),
+ EnableIPV6 = ipv6enabled,
+ EnableIPV4 = true,
+ PublishedServerUriBySubnet = new string[] { publishedServers }
+ };
+
+ NetworkManager.MockNetworkSettings = "192.168.1.208/24,-16,eth16:200.200.200.200/24,11,eth11";
+ var nm = new NetworkManager(GetMockConfig(conf), new NullLogger());
+ NetworkManager.MockNetworkSettings = string.Empty;
+
+ if (nm.TryParseInterface(result, out NetCollection? resultObj) && resultObj != null)
+ {
+ // Parse out IPAddresses so we can do a string comparison. (Ignore subnet masks).
+ result = ((IPNetAddress)resultObj[0]).ToString(true);
+ }
+
+ var intf = nm.GetBindInterface(source, out int? _);
+
+ Assert.True(string.Equals(intf, result, System.StringComparison.OrdinalIgnoreCase));
+ }
+ }
+}