Updated with new NetManager

This commit is contained in:
Greenback 2020-10-31 18:21:46 +00:00
parent c25ec72864
commit 83af636c61
17 changed files with 1895 additions and 34 deletions

View File

@ -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)));

View File

@ -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;
}
/// <summary>
/// Returns an unused UDP port number in the range specified.
/// </summary>
/// <param name="range">Upper and Lower boundary of ports to select.</param>
/// <returns>System.Int32.</returns>
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));

View File

@ -23,10 +23,6 @@
<CodeAnalysisRuleSet>../jellyfin.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NetworkCollection" Version="1.0.6" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MediaBrowser.Common\MediaBrowser.Common.csproj" />
<ProjectReference Include="..\MediaBrowser.Controller\MediaBrowser.Controller.csproj" />

View File

@ -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<MediaBrowser.Common.Net.IPObject>;
namespace Jellyfin.Networking.Manager
{
@ -154,6 +153,22 @@ namespace Jellyfin.Networking.Manager
/// </summary>
public Dictionary<IPNetAddress, string> PublishedServerUrls => _publishedServerUrls;
/// <summary>
/// Creates a new network collection.
/// </summary>
/// <param name="source">Items to assign the collection, or null.</param>
/// <returns>The collection created.</returns>
public static NetCollection CreateCollection(IEnumerable<IPObject>? source)
{
var result = new NetCollection();
if (source != null)
{
return result.AddRange(source);
}
return result;
}
/// <inheritdoc/>
public void Dispose()
{
@ -162,10 +177,10 @@ namespace Jellyfin.Networking.Manager
}
/// <inheritdoc/>
public List<PhysicalAddress> GetMacAddresses()
public IReadOnlyCollection<PhysicalAddress> GetMacAddresses()
{
// Populated in construction - so always has values.
return _macAddresses.ToList();
return _macAddresses.AsReadOnly();
}
/// <inheritdoc/>
@ -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));

View File

@ -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))
{

View File

@ -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
{

View File

@ -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)

View File

@ -22,7 +22,6 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.9" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.Net.Http.Headers" Version="2.2.8" />
<PackageReference Include="NetworkCollection" Version="1.0.6" />
</ItemGroup>
<ItemGroup>

View File

@ -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<MediaBrowser.Common.Net.IPObject>;
namespace MediaBrowser.Common.Net
{
@ -130,7 +130,7 @@ namespace MediaBrowser.Common.Net
/// Get a list of all the MAC addresses associated with active interfaces.
/// </summary>
/// <returns>List of MAC addresses.</returns>
List<PhysicalAddress> GetMacAddresses();
IReadOnlyCollection<PhysicalAddress> GetMacAddresses();
/// <summary>
/// Checks to see if the IP Address provided matches an interface that has a gateway.

View File

@ -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
{
/// <summary>
/// Object that holds a host name.
/// </summary>
public class IPHost : IPObject
{
/// <summary>
/// Represents an IPHost that has no value.
/// </summary>
public static readonly IPHost None = new IPHost(string.Empty, IPAddress.None);
/// <summary>
/// Time when last resolved.
/// </summary>
private long _lastResolved;
/// <summary>
/// Gets the IP Addresses, attempting to resolve the name, if there are none.
/// </summary>
private IPAddress[] _addresses;
/// <summary>
/// Initializes a new instance of the <see cref="IPHost"/> class.
/// </summary>
/// <param name="name">Host name to assign.</param>
public IPHost(string name)
{
HostName = name ?? throw new ArgumentNullException(nameof(name));
_addresses = Array.Empty<IPAddress>();
Resolved = false;
}
/// <summary>
/// Initializes a new instance of the <see cref="IPHost"/> class.
/// </summary>
/// <param name="name">Host name to assign.</param>
/// <param name="address">Address to assign.</param>
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);
}
/// <summary>
/// Gets or sets the object's first IP address.
/// </summary>
public override IPAddress Address
{
get
{
return ResolveHost() ? this[0] : IPAddress.None;
}
set
{
// Not implemented.
}
}
/// <summary>
/// Gets or sets the object's first IP's subnet prefix.
/// The setter does nothing, but shouldn't raise an exception.
/// </summary>
public override byte PrefixLength
{
get
{
return (byte)(ResolveHost() ? 128 : 0);
}
set
{
// Not implemented.
}
}
/// <summary>
/// Gets or sets timeout value before resolve required, in minutes.
/// </summary>
public byte Timeout { get; set; } = 30;
/// <summary>
/// Gets a value indicating whether the address has a value.
/// </summary>
public bool HasAddress
{
get
{
return _addresses.Length > 0;
}
}
/// <summary>
/// Gets the host name of this object.
/// </summary>
public string HostName { get; }
/// <summary>
/// Gets a value indicating whether this host has attempted to be resolved.
/// </summary>
public bool Resolved { get; private set; }
/// <summary>
/// Gets or sets the IP Addresses associated with this object.
/// </summary>
/// <param name="index">Index of address.</param>
public IPAddress this[int index]
{
get
{
ResolveHost();
return index >= 0 && index < _addresses.Length ? _addresses[index] : IPAddress.None;
}
}
/// <summary>
/// Attempts to parse the host string.
/// </summary>
/// <param name="host">Host name to parse.</param>
/// <param name="hostObj">Object representing the string, if it has successfully been parsed.</param>
/// <returns>Success result of the parsing.</returns>
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;
}
/// <summary>
/// Attempts to parse the host string.
/// </summary>
/// <param name="host">Host name to parse.</param>
/// <returns>Object representing the string, if it has successfully been parsed.</returns>
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}");
}
/// <summary>
/// Attempts to parse the host string, ensuring that it resolves only to a specific IP type.
/// </summary>
/// <param name="host">Host name to parse.</param>
/// <param name="family">Addressfamily filter.</param>
/// <returns>Object representing the string, if it has successfully been parsed.</returns>
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}");
}
/// <summary>
/// Returns the Addresses that this item resolved to.
/// </summary>
/// <returns>IPAddress Array.</returns>
public IPAddress[] GetAddresses()
{
ResolveHost();
return _addresses;
}
/// <inheritdoc/>
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;
}
/// <inheritdoc/>
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;
}
/// <inheritdoc/>
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;
}
/// <inheritdoc/>
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;
}
/// <inheritdoc/>
public override void Remove(AddressFamily family)
{
if (ResolveHost())
{
_addresses = _addresses.Where(p => p.AddressFamily != family).ToArray();
}
}
/// <inheritdoc/>
public override bool Contains(IPObject address)
{
// An IPHost cannot contain another IPObject, it can only be equal.
return Equals(address);
}
/// <inheritdoc/>
protected override IPObject CalculateNetworkAddress()
{
var netAddr = NetworkAddressOf(this[0], PrefixLength);
return new IPNetAddress(netAddr.Address, netAddr.PrefixLength);
}
/// <summary>
/// Attempt to resolve the ip address of a host.
/// </summary>
/// <returns>The result of the comparison function.</returns>
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;
}
/// <summary>
/// Task that looks up a Host name and returns its IP addresses.
/// </summary>
/// <returns>Array of IPAddress objects.</returns>
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.
}
}
}
}
}
}

View File

@ -0,0 +1,277 @@
#nullable enable
using System;
using System.Net;
using System.Net.Sockets;
namespace MediaBrowser.Common.Net
{
/// <summary>
/// An object that holds and IP address and subnet mask.
/// </summary>
public class IPNetAddress : IPObject
{
/// <summary>
/// Represents an IPNetAddress that has no value.
/// </summary>
public static readonly IPNetAddress None = new IPNetAddress(IPAddress.None);
/// <summary>
/// IPv4 multicast address.
/// </summary>
public static readonly IPAddress MulticastIPv4 = IPAddress.Parse("239.255.255.250");
/// <summary>
/// IPv6 local link multicast address.
/// </summary>
public static readonly IPAddress MulticastIPv6LinkLocal = IPAddress.Parse("ff02::C");
/// <summary>
/// IPv6 site local multicast address.
/// </summary>
public static readonly IPAddress MulticastIPv6SiteLocal = IPAddress.Parse("ff05::C");
/// <summary>
/// IP4Loopback address host.
/// </summary>
public static readonly IPNetAddress IP4Loopback = IPNetAddress.Parse("127.0.0.1/32");
/// <summary>
/// IP6Loopback address host.
/// </summary>
public static readonly IPNetAddress IP6Loopback = IPNetAddress.Parse("::1");
/// <summary>
/// Object's IP address.
/// </summary>
private IPAddress _address;
/// <summary>
/// Initializes a new instance of the <see cref="IPNetAddress"/> class.
/// </summary>
/// <param name="address">Address to assign.</param>
public IPNetAddress(IPAddress address)
{
_address = address ?? throw new ArgumentNullException(nameof(address));
PrefixLength = (byte)(address.AddressFamily == AddressFamily.InterNetwork ? 32 : 128);
}
/// <summary>
/// Initializes a new instance of the <see cref="IPNetAddress"/> class.
/// </summary>
/// <param name="address">IP Address.</param>
/// <param name="prefixLength">Mask as a CIDR.</param>
public IPNetAddress(IPAddress address, byte prefixLength)
{
if (address?.IsIPv4MappedToIPv6 ?? throw new ArgumentNullException(nameof(address)))
{
_address = address.MapToIPv4();
}
else
{
_address = address;
}
PrefixLength = prefixLength;
}
/// <summary>
/// Gets or sets the object's IP address.
/// </summary>
public override IPAddress Address
{
get
{
return _address;
}
set
{
_address = value ?? IPAddress.None;
}
}
/// <inheritdoc/>
public override byte PrefixLength { get; set; }
/// <summary>
/// Try to parse the address and subnet strings into an IPNetAddress object.
/// </summary>
/// <param name="addr">IP address to parse. Can be CIDR or X.X.X.X notation.</param>
/// <param name="ip">Resultant object.</param>
/// <returns>True if the values parsed successfully. False if not, resulting in the IP being null.</returns>
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;
}
/// <summary>
/// Parses the string provided, throwing an exception if it is badly formed.
/// </summary>
/// <param name="addr">String to parse.</param>
/// <returns>IPNetAddress object.</returns>
public static IPNetAddress Parse(string addr)
{
if (TryParse(addr, out IPNetAddress o))
{
return o;
}
throw new ArgumentException("Unable to recognise object :" + addr);
}
/// <inheritdoc/>
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;
}
/// <inheritdoc/>
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;
}
/// <inheritdoc/>
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;
}
/// <inheritdoc/>
public override bool Equals(IPAddress address)
{
if (address != null && !address.Equals(IPAddress.None) && !Address.Equals(IPAddress.None))
{
return address.Equals(Address);
}
return false;
}
/// <inheritdoc/>
public override string ToString()
{
return ToString(false);
}
/// <summary>
/// Returns a textual representation of this object.
/// </summary>
/// <param name="shortVersion">Set to true, if the subnet is to be included as part of the address.</param>
/// <returns>String representation of this object.</returns>
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;
}
/// <inheritdoc/>
protected override IPObject CalculateNetworkAddress()
{
var value = NetworkAddressOf(_address, PrefixLength);
return new IPNetAddress(value.Address, value.PrefixLength);
}
}
}

View File

@ -0,0 +1,395 @@
#nullable enable
using System;
using System.Net;
using System.Net.Sockets;
namespace MediaBrowser.Common.Net
{
/// <summary>
/// Base network object class.
/// </summary>
public abstract class IPObject : IEquatable<IPObject>
{
/// <summary>
/// IPv6 Loopback address.
/// </summary>
protected static readonly byte[] Ipv6Loopback = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
/// <summary>
/// IPv4 Loopback address.
/// </summary>
protected static readonly byte[] Ipv4Loopback = { 127, 0, 0, 1 };
/// <summary>
/// The network address of this object.
/// </summary>
private IPObject? _networkAddress;
/// <summary>
/// Gets or sets the user defined functions that need storage in this object.
/// </summary>
public int Tag { get; set; }
/// <summary>
/// Gets or sets the object's IP address.
/// </summary>
public abstract IPAddress Address { get; set; }
/// <summary>
/// Gets the object's network address.
/// </summary>
public IPObject NetworkAddress
{
get
{
if (_networkAddress == null)
{
_networkAddress = CalculateNetworkAddress();
}
return _networkAddress;
}
}
/// <summary>
/// Gets or sets the object's IP address.
/// </summary>
public abstract byte PrefixLength { get; set; }
/// <summary>
/// Gets the AddressFamily of this object.
/// </summary>
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;
}
}
/// <summary>
/// Returns the network address of an object.
/// </summary>
/// <param name="address">IP Address to convert.</param>
/// <param name="prefixLength">Subnet prefix.</param>
/// <returns>IPAddress.</returns>
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);
}
/// <summary>
/// Tests to see if the ip address is a Loopback address.
/// </summary>
/// <param name="address">Value to test.</param>
/// <returns>True if it is.</returns>
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;
}
/// <summary>
/// Tests to see if the ip address is an IP6 address.
/// </summary>
/// <param name="address">Value to test.</param>
/// <returns>True if it is.</returns>
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);
}
/// <summary>
/// Tests to see if the address in the private address range.
/// </summary>
/// <param name="address">Object to test.</param>
/// <returns>True if it contains a private address.</returns>
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;
}
/// <summary>
/// Returns true if the IPAddress contains an IP6 Local link address.
/// </summary>
/// <param name="address">IPAddress object to check.</param>
/// <returns>True if it is a local link address.</returns>
/// <remarks>See https://stackoverflow.com/questions/6459928/explain-the-instance-properties-of-system-net-ipaddress
/// it appears that the IPAddress.IsIPv6LinkLocal is out of date.
/// </remarks>
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.
}
/// <summary>
/// Convert a subnet mask in CIDR notation to a dotted decimal string value. IPv4 only.
/// </summary>
/// <param name="cidr">Subnet mask in CIDR notation.</param>
/// <param name="family">IPv4 or IPv6 family.</param>
/// <returns>String value of the subnet mask in dotted decimal notation.</returns>
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);
}
/// <summary>
/// Convert a mask to a CIDR. IPv4 only.
/// https://stackoverflow.com/questions/36954345/get-cidr-from-netmask.
/// </summary>
/// <param name="mask">Subnet mask.</param>
/// <returns>Byte CIDR representing the mask.</returns>
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;
}
/// <summary>
/// Tests to see if this object is a Loopback address.
/// </summary>
/// <returns>True if it is.</returns>
public virtual bool IsLoopback()
{
return IsLoopback(Address);
}
/// <summary>
/// Removes all addresses of a specific type from this object.
/// </summary>
/// <param name="family">Type of address to remove.</param>
public virtual void Remove(AddressFamily family)
{
// This method only peforms a function in the IPHost implementation of IPObject.
}
/// <summary>
/// Tests to see if this object is an IPv6 address.
/// </summary>
/// <returns>True if it is.</returns>
public virtual bool IsIP6()
{
return IsIP6(Address);
}
/// <summary>
/// Returns true if this IP address is in the RFC private address range.
/// </summary>
/// <returns>True this object has a private address.</returns>
public virtual bool IsPrivateAddressRange()
{
return IsPrivateAddressRange(Address);
}
/// <summary>
/// Compares this to the object passed as a parameter.
/// </summary>
/// <param name="ip">Object to compare to.</param>
/// <returns>Equality result.</returns>
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;
}
/// <summary>
/// Compares this to the object passed as a parameter.
/// </summary>
/// <param name="other">Object to compare to.</param>
/// <returns>Equality result.</returns>
public virtual bool Equals(IPObject? other)
{
if (other != null && other is IPObject otherObj)
{
return !Address.Equals(IPAddress.None) && Address.Equals(otherObj.Address);
}
return false;
}
/// <summary>
/// Compares the address in this object and the address in the object passed as a parameter.
/// </summary>
/// <param name="address">Object's IP address to compare to.</param>
/// <returns>Comparison result.</returns>
public abstract bool Contains(IPObject address);
/// <summary>
/// Compares the address in this object and the address in the object passed as a parameter.
/// </summary>
/// <param name="address">Object's IP address to compare to.</param>
/// <returns>Comparison result.</returns>
public abstract bool Contains(IPAddress address);
/// <inheritdoc/>
public override int GetHashCode()
{
return Address.GetHashCode();
}
/// <inheritdoc/>
public override bool Equals(object obj)
{
return Equals(obj as IPObject);
}
/// <summary>
/// Calculates the network address of this object.
/// </summary>
/// <returns>Returns the network address of this object.</returns>
protected abstract IPObject CalculateNetworkAddress();
}
}

View File

@ -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<MediaBrowser.Common.Net.IPObject>;
namespace MediaBrowser.Common.Net
{
/// <summary>
/// Defines the <see cref="NetworkExtensions" />.
/// </summary>
public static class NetworkExtensions
{
/// <summary>
/// Add an address to the collection.
/// </summary>
/// <param name="source">The <see cref="NetCollection"/>.</param>
/// <param name="ip">Item to add.</param>
public static void AddItem(this NetCollection source, IPAddress ip)
{
if (!source.ContainsAddress(ip))
{
source.Add(new IPNetAddress(ip, 32));
}
}
/// <summary>
/// Add multiple items to the collection.
/// </summary>
/// <param name="destination">The <see cref="NetCollection"/>.</param>
/// <param name="source">Item to add.</param>
/// <returns>Return the collection.</returns>
public static NetCollection AddRange(this NetCollection destination, IEnumerable<IPObject> source)
{
foreach (var item in source)
{
destination.Add(item);
}
return destination;
}
/// <summary>
/// Adds a network to the collection.
/// </summary>
/// <param name="source">The <see cref="NetCollection"/>.</param>
/// <param name="item">Item to add.</param>
public static void AddItem(this NetCollection source, IPObject item)
{
if (!source.ContainsAddress(item))
{
source.Add(item);
}
}
/// <summary>
/// Converts this object to a string.
/// </summary>
/// <param name="source">The <see cref="NetCollection"/>.</param>
/// <returns>Returns a string representation of this object.</returns>
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}]";
}
/// <summary>
/// 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.
/// </summary>
/// <param name="source">The <see cref="NetCollection"/>.</param>
/// <param name="item">The item to look for.</param>
/// <returns>True if the collection contains the item.</returns>
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;
}
/// <summary>
/// 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.
/// </summary>
/// <param name="source">The <see cref="NetCollection"/>.</param>
/// <param name="item">The item to look for.</param>
/// <returns>True if the collection contains the item.</returns>
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;
}
/// <summary>
/// Returns a collection containing the subnets of this collection given.
/// </summary>
/// <param name="source">The <see cref="NetCollection"/>.</param>
/// <returns>NetCollection object containing the subnets.</returns>
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;
}
/// <summary>
/// Excludes all the items from this list that are found in excludeList.
/// </summary>
/// <param name="source">The <see cref="NetCollection"/>.</param>
/// <param name="excludeList">Items to exclude.</param>
/// <returns>A new collection, with the items excluded.</returns>
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;
}
/// <summary>
/// Returns all items that co-exist in this object and target.
/// </summary>
/// <param name="source">The <see cref="NetCollection"/>.</param>
/// <param name="target">Collection to compare with.</param>
/// <returns>A collection containing all the matches.</returns>
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;
}
}
}

View File

@ -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}

View File

@ -6,7 +6,6 @@ using System.Net;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.Net;
using NetworkCollection;
namespace Rssdp.Infrastructure
{

View File

@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">
<!-- ProjectGuid is only included as a requirement for SonarQube analysis -->
<PropertyGroup>
<ProjectGuid>{42816EA8-4511-4CBF-A9C7-7791D5DDDAE6}</ProjectGuid>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
<PackageReference Include="coverlet.collector" Version="1.2.1" />
<PackageReference Include="Moq" Version="4.14.5" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\Emby.Server.Implementations\Emby.Server.Implementations.csproj" />
<ProjectReference Include="..\..\..\MediaBrowser.Common\MediaBrowser.Common.csproj" />
</ItemGroup>
</Project>

View File

@ -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<MediaBrowser.Common.Net.IPObject>;
using XMLProperties = System.Collections.Generic.Dictionary<string, string>;
namespace NetworkTesting
{
public class NetTesting
{
/// <summary>
/// Trys to identify the string and return an object of that class.
/// </summary>
/// <param name="addr">String to parse.</param>
/// <param name="result">IPObject to return.</param>
/// <returns>True if the value parsed successfully.</returns>
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<IConfigurationManager>
{
CallBase = true
};
configManager.Setup(x => x.GetConfiguration(It.IsAny<string>())).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>());
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<NetworkManager>());
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<NetworkManager>());
// 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<NetworkManager>());
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<NetworkManager>());
// 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>());
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>());
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));
}
}
}