jellyfin-server/MediaBrowser.Common/Net/NetworkExtensions.cs
BaronGreenback 124bd4c2c0
Networking: 1 - Network Manager (#4124)
* NetworkManager

* Config file with additional options.

* Update Jellyfin.Networking/Manager/INetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update Jellyfin.Networking/Manager/INetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update MediaBrowser.Model/Configuration/ServerConfiguration.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update MediaBrowser.Model/Configuration/ServerConfiguration.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update MediaBrowser.Model/Configuration/ServerConfiguration.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Split function.

* Update Jellyfin.Networking/Manager/INetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* fixed iterations

* Update Jellyfin.Networking.csproj

* Update NetworkManager.cs

* Updated to NetCollection 1.03.

* Update ServerConfiguration.cs

* Update StartupController.cs

* Update INetworkManager.cs

Removed public

* Update INetworkManager.cs

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Claus Vium <cvium@users.noreply.github.com>

* Updated comment

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Claus Vium <cvium@users.noreply.github.com>

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Claus Vium <cvium@users.noreply.github.com>

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Claus Vium <cvium@users.noreply.github.com>

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Claus Vium <cvium@users.noreply.github.com>

* Update Jellyfin.Networking/Manager/INetworkManager.cs

Co-authored-by: Claus Vium <cvium@users.noreply.github.com>

* Remove mono code.
Removed forced chromecast option

* Inverted if

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Moved config into a separate container

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Changed sortedList to dictionary.

* Update INetworkManager.cs

Changed UpdateSettings param type

* Update NetworkManager.cs

* Update NetworkManager.cs

* Update NetworkManager.cs

* Update NetworkConfiguration.cs

* Update INetworkManager.cs

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Claus Vium <cvium@users.noreply.github.com>

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Claus Vium <cvium@users.noreply.github.com>

* Update MediaBrowser.Model/Configuration/ServerConfiguration.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update MediaBrowser.Model/Configuration/ServerConfiguration.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Updated changes github didn't update.

* Fixed compilation.

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Removed read locking.

* Update NetworkManager.cs

Changed configuration event to NamedConfigurationUpdated

* updated comment

* removed whitespace

* Updated NetCollection to 1.0.6
Updated DXCopAnalyser to 3.3.1

* removed NetCollection

* Update NetworkManager.cs

* Update NetworkExtensions.cs

* Update NetworkExtensions.cs

Removed function.

* Update NetworkExtensions.cs

* Update NetworkManager.cs

Changed ToString() to AsString() as native collection formats incorrectly.

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update NetworkExtensions.cs

* Update Jellyfin.Networking/Configuration/NetworkConfiguration.cs

Co-authored-by: h1dden-da3m0n <33120068+h1dden-da3m0n@users.noreply.github.com>

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: h1dden-da3m0n <33120068+h1dden-da3m0n@users.noreply.github.com>

* Update Jellyfin.Networking/Configuration/NetworkConfiguration.cs

Co-authored-by: h1dden-da3m0n <33120068+h1dden-da3m0n@users.noreply.github.com>

* Update Jellyfin.Networking/Configuration/NetworkConfiguration.cs

Co-authored-by: h1dden-da3m0n <33120068+h1dden-da3m0n@users.noreply.github.com>

* Update MediaBrowser.Common/Net/IPObject.cs

Co-authored-by: h1dden-da3m0n <33120068+h1dden-da3m0n@users.noreply.github.com>

* updated

* Replaced NetCollection with Collection<IPObject>

* Update MediaBrowser.Common/Net/NetworkExtensions.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update MediaBrowser.Model/Configuration/PathSubstitution.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update MediaBrowser.Common/Net/NetworkExtensions.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update MediaBrowser.Common/Net/IPObject.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update MediaBrowser.Common/Net/IPObject.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update MediaBrowser.Common/Net/IPObject.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update MediaBrowser.Common/Net/IPHost.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update MediaBrowser.Common/Net/IPHost.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update MediaBrowser.Common/Net/IPHost.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update MediaBrowser.Common/Net/IPHost.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update MediaBrowser.Common/Net/IPHost.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update MediaBrowser.Common/Net/IPHost.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update MediaBrowser.Common/Net/IPHost.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update MediaBrowser.Common/Net/IPObject.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update MediaBrowser.Common/Net/IPObject.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* updated comments.

* Updated comments / changes as suggested by @crobibero.

* Split function as suggested

* Fixed null ref.

* Updated comment.

* Updated cs to .net5

* Fixed issue with PublishedServerUrl

* Fixes

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Claus Vium <cvium@users.noreply.github.com>

* Restored locking

* optimisation

* Added comment

* updates.

* updated.

* updates

* updated.

* Update IPHost.cs

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Claus Vium <cvium@users.noreply.github.com>

* Update NetworkManager.cs

* Removed whitespace.

* Added debug logging

* Added debug.

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Bond-009 <bond.009@outlook.com>

* Replaced GetAddressBytes

Co-authored-by: Cody Robibero <cody@robibe.ro>
Co-authored-by: Claus Vium <cvium@users.noreply.github.com>
Co-authored-by: h1dden-da3m0n <33120068+h1dden-da3m0n@users.noreply.github.com>
Co-authored-by: Bond-009 <bond.009@outlook.com>
2020-11-21 13:59:14 +01:00

263 lines
8.2 KiB
C#

#pragma warning disable CA1062 // Validate arguments of public methods
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Net;
using System.Runtime.CompilerServices;
using System.Text;
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="Collection{IPObject}"/>.</param>
/// <param name="ip">Item to add.</param>
public static void AddItem(this Collection<IPObject> source, IPAddress ip)
{
if (!source.ContainsAddress(ip))
{
source.Add(new IPNetAddress(ip, 32));
}
}
/// <summary>
/// Adds a network to the collection.
/// </summary>
/// <param name="source">The <see cref="Collection{IPObject}"/>.</param>
/// <param name="item">Item to add.</param>
public static void AddItem(this Collection<IPObject> source, IPObject item)
{
if (!source.ContainsAddress(item))
{
source.Add(item);
}
}
/// <summary>
/// Converts this object to a string.
/// </summary>
/// <param name="source">The <see cref="Collection{IPObject}"/>.</param>
/// <returns>Returns a string representation of this object.</returns>
public static string AsString(this Collection<IPObject> source)
{
return $"[{string.Join(',', source)}]";
}
/// <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="Collection{IPObject}"/>.</param>
/// <param name="item">The item to look for.</param>
/// <returns>True if the collection contains the item.</returns>
public static bool ContainsAddress(this Collection<IPObject> 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="Collection{IPObject}"/>.</param>
/// <param name="item">The item to look for.</param>
/// <returns>True if the collection contains the item.</returns>
public static bool ContainsAddress(this Collection<IPObject> 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>
/// Compares two Collection{IPObject} objects. The order is ignored.
/// </summary>
/// <param name="source">The <see cref="Collection{IPObject}"/>.</param>
/// <param name="dest">Item to compare to.</param>
/// <returns>True if both are equal.</returns>
public static bool Compare(this Collection<IPObject> source, Collection<IPObject> dest)
{
if (dest == null || source.Count != dest.Count)
{
return false;
}
foreach (var sourceItem in source)
{
bool found = false;
foreach (var destItem in dest)
{
if (sourceItem.Equals(destItem))
{
found = true;
break;
}
}
if (!found)
{
return false;
}
}
return true;
}
/// <summary>
/// Returns a collection containing the subnets of this collection given.
/// </summary>
/// <param name="source">The <see cref="Collection{IPObject}"/>.</param>
/// <returns>Collection{IPObject} object containing the subnets.</returns>
public static Collection<IPObject> AsNetworks(this Collection<IPObject> source)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
Collection<IPObject> res = new Collection<IPObject>();
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.AddItem(na);
}
else if (i is IPHost ipHost)
{
// Flatten out IPHost and add all its ip addresses.
foreach (var addr in ipHost.GetAddresses())
{
IPNetAddress host = new IPNetAddress(addr)
{
Tag = i.Tag
};
res.AddItem(host);
}
}
}
return res;
}
/// <summary>
/// Excludes all the items from this list that are found in excludeList.
/// </summary>
/// <param name="source">The <see cref="Collection{IPObject}"/>.</param>
/// <param name="excludeList">Items to exclude.</param>
/// <returns>A new collection, with the items excluded.</returns>
public static Collection<IPObject> Exclude(this Collection<IPObject> source, Collection<IPObject> excludeList)
{
if (source.Count == 0 || excludeList == null)
{
return new Collection<IPObject>(source);
}
Collection<IPObject> results = new Collection<IPObject>();
bool found;
foreach (var outer in source)
{
found = false;
foreach (var inner in excludeList)
{
if (outer.Equals(inner))
{
found = true;
break;
}
}
if (!found)
{
results.AddItem(outer);
}
}
return results;
}
/// <summary>
/// Returns all items that co-exist in this object and target.
/// </summary>
/// <param name="source">The <see cref="Collection{IPObject}"/>.</param>
/// <param name="target">Collection to compare with.</param>
/// <returns>A collection containing all the matches.</returns>
public static Collection<IPObject> Union(this Collection<IPObject> source, Collection<IPObject> target)
{
if (source.Count == 0)
{
return new Collection<IPObject>();
}
if (target == null)
{
throw new ArgumentNullException(nameof(target));
}
Collection<IPObject> nc = new Collection<IPObject>();
foreach (IPObject i in source)
{
if (target.ContainsAddress(i))
{
nc.AddItem(i);
}
}
return nc;
}
}
}