mirror of
https://github.com/jellyfin/jellyfin.git
synced 2024-11-16 18:42:52 -07:00
commit
3a5df3d060
@ -35,8 +35,6 @@ namespace Emby.Dlna.Main
|
|||||||
private readonly IServerConfigurationManager _config;
|
private readonly IServerConfigurationManager _config;
|
||||||
private readonly ILogger<DlnaEntryPoint> _logger;
|
private readonly ILogger<DlnaEntryPoint> _logger;
|
||||||
private readonly IServerApplicationHost _appHost;
|
private readonly IServerApplicationHost _appHost;
|
||||||
|
|
||||||
private PlayToManager _manager;
|
|
||||||
private readonly ISessionManager _sessionManager;
|
private readonly ISessionManager _sessionManager;
|
||||||
private readonly IHttpClient _httpClient;
|
private readonly IHttpClient _httpClient;
|
||||||
private readonly ILibraryManager _libraryManager;
|
private readonly ILibraryManager _libraryManager;
|
||||||
@ -47,14 +45,13 @@ namespace Emby.Dlna.Main
|
|||||||
private readonly ILocalizationManager _localization;
|
private readonly ILocalizationManager _localization;
|
||||||
private readonly IMediaSourceManager _mediaSourceManager;
|
private readonly IMediaSourceManager _mediaSourceManager;
|
||||||
private readonly IMediaEncoder _mediaEncoder;
|
private readonly IMediaEncoder _mediaEncoder;
|
||||||
|
|
||||||
private readonly IDeviceDiscovery _deviceDiscovery;
|
private readonly IDeviceDiscovery _deviceDiscovery;
|
||||||
|
|
||||||
private SsdpDevicePublisher _Publisher;
|
|
||||||
|
|
||||||
private readonly ISocketFactory _socketFactory;
|
private readonly ISocketFactory _socketFactory;
|
||||||
private readonly INetworkManager _networkManager;
|
private readonly INetworkManager _networkManager;
|
||||||
|
private readonly object _syncLock = new object();
|
||||||
|
|
||||||
|
private PlayToManager _manager;
|
||||||
|
private SsdpDevicePublisher _publisher;
|
||||||
private ISsdpCommunicationsServer _communicationsServer;
|
private ISsdpCommunicationsServer _communicationsServer;
|
||||||
|
|
||||||
internal IContentDirectory ContentDirectory { get; private set; }
|
internal IContentDirectory ContentDirectory { get; private set; }
|
||||||
@ -181,7 +178,7 @@ namespace Emby.Dlna.Main
|
|||||||
var enableMultiSocketBinding = OperatingSystem.Id == OperatingSystemId.Windows ||
|
var enableMultiSocketBinding = OperatingSystem.Id == OperatingSystemId.Windows ||
|
||||||
OperatingSystem.Id == OperatingSystemId.Linux;
|
OperatingSystem.Id == OperatingSystemId.Linux;
|
||||||
|
|
||||||
_communicationsServer = new SsdpCommunicationsServer(_config, _socketFactory, _networkManager, _logger, enableMultiSocketBinding)
|
_communicationsServer = new SsdpCommunicationsServer(_socketFactory, _networkManager, _logger, enableMultiSocketBinding)
|
||||||
{
|
{
|
||||||
IsShared = true
|
IsShared = true
|
||||||
};
|
};
|
||||||
@ -232,20 +229,22 @@ namespace Emby.Dlna.Main
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_Publisher != null)
|
if (_publisher != null)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_Publisher = new SsdpDevicePublisher(_communicationsServer, _networkManager, OperatingSystem.Name, Environment.OSVersion.VersionString, _config.GetDlnaConfiguration().SendOnlyMatchedHost);
|
_publisher = new SsdpDevicePublisher(_communicationsServer, _networkManager, OperatingSystem.Name, Environment.OSVersion.VersionString, _config.GetDlnaConfiguration().SendOnlyMatchedHost)
|
||||||
_Publisher.LogFunction = LogMessage;
|
{
|
||||||
_Publisher.SupportPnpRootDevice = false;
|
LogFunction = LogMessage,
|
||||||
|
SupportPnpRootDevice = false
|
||||||
|
};
|
||||||
|
|
||||||
await RegisterServerEndpoints().ConfigureAwait(false);
|
await RegisterServerEndpoints().ConfigureAwait(false);
|
||||||
|
|
||||||
_Publisher.StartBroadcastingAliveMessages(TimeSpan.FromSeconds(options.BlastAliveMessageIntervalSeconds));
|
_publisher.StartBroadcastingAliveMessages(TimeSpan.FromSeconds(options.BlastAliveMessageIntervalSeconds));
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@ -267,6 +266,12 @@ namespace Emby.Dlna.Main
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Limit to LAN addresses only
|
||||||
|
if (!_networkManager.IsAddressInSubnets(address, true, true))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
var fullService = "urn:schemas-upnp-org:device:MediaServer:1";
|
var fullService = "urn:schemas-upnp-org:device:MediaServer:1";
|
||||||
|
|
||||||
_logger.LogInformation("Registering publisher for {0} on {1}", fullService, address);
|
_logger.LogInformation("Registering publisher for {0} on {1}", fullService, address);
|
||||||
@ -288,13 +293,13 @@ namespace Emby.Dlna.Main
|
|||||||
};
|
};
|
||||||
|
|
||||||
SetProperies(device, fullService);
|
SetProperies(device, fullService);
|
||||||
_Publisher.AddDevice(device);
|
_publisher.AddDevice(device);
|
||||||
|
|
||||||
var embeddedDevices = new[]
|
var embeddedDevices = new[]
|
||||||
{
|
{
|
||||||
"urn:schemas-upnp-org:service:ContentDirectory:1",
|
"urn:schemas-upnp-org:service:ContentDirectory:1",
|
||||||
"urn:schemas-upnp-org:service:ConnectionManager:1",
|
"urn:schemas-upnp-org:service:ConnectionManager:1",
|
||||||
//"urn:microsoft.com:service:X_MS_MediaReceiverRegistrar:1"
|
// "urn:microsoft.com:service:X_MS_MediaReceiverRegistrar:1"
|
||||||
};
|
};
|
||||||
|
|
||||||
foreach (var subDevice in embeddedDevices)
|
foreach (var subDevice in embeddedDevices)
|
||||||
@ -326,7 +331,7 @@ namespace Emby.Dlna.Main
|
|||||||
|
|
||||||
private void SetProperies(SsdpDevice device, string fullDeviceType)
|
private void SetProperies(SsdpDevice device, string fullDeviceType)
|
||||||
{
|
{
|
||||||
var service = fullDeviceType.Replace("urn:", string.Empty).Replace(":1", string.Empty);
|
var service = fullDeviceType.Replace("urn:", string.Empty, StringComparison.OrdinalIgnoreCase).Replace(":1", string.Empty, StringComparison.OrdinalIgnoreCase);
|
||||||
|
|
||||||
var serviceParts = service.Split(':');
|
var serviceParts = service.Split(':');
|
||||||
|
|
||||||
@ -337,7 +342,6 @@ namespace Emby.Dlna.Main
|
|||||||
device.DeviceType = serviceParts[2];
|
device.DeviceType = serviceParts[2];
|
||||||
}
|
}
|
||||||
|
|
||||||
private readonly object _syncLock = new object();
|
|
||||||
private void StartPlayToManager()
|
private void StartPlayToManager()
|
||||||
{
|
{
|
||||||
lock (_syncLock)
|
lock (_syncLock)
|
||||||
@ -416,11 +420,11 @@ namespace Emby.Dlna.Main
|
|||||||
|
|
||||||
public void DisposeDevicePublisher()
|
public void DisposeDevicePublisher()
|
||||||
{
|
{
|
||||||
if (_Publisher != null)
|
if (_publisher != null)
|
||||||
{
|
{
|
||||||
_logger.LogInformation("Disposing SsdpDevicePublisher");
|
_logger.LogInformation("Disposing SsdpDevicePublisher");
|
||||||
_Publisher.Dispose();
|
_publisher.Dispose();
|
||||||
_Publisher = null;
|
_publisher = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1234,7 +1234,7 @@ namespace Emby.Server.Implementations
|
|||||||
|
|
||||||
if (addresses.Count == 0)
|
if (addresses.Count == 0)
|
||||||
{
|
{
|
||||||
addresses.AddRange(_networkManager.GetLocalIpAddresses(ServerConfigurationManager.Configuration.IgnoreVirtualInterfaces));
|
addresses.AddRange(_networkManager.GetLocalIpAddresses());
|
||||||
}
|
}
|
||||||
|
|
||||||
var resultList = new List<IPAddress>();
|
var resultList = new List<IPAddress>();
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Globalization;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.NetworkInformation;
|
using System.Net.NetworkInformation;
|
||||||
@ -13,6 +12,9 @@ using Microsoft.Extensions.Logging;
|
|||||||
|
|
||||||
namespace Emby.Server.Implementations.Networking
|
namespace Emby.Server.Implementations.Networking
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Class to take care of network interface management.
|
||||||
|
/// </summary>
|
||||||
public class NetworkManager : INetworkManager
|
public class NetworkManager : INetworkManager
|
||||||
{
|
{
|
||||||
private readonly ILogger<NetworkManager> _logger;
|
private readonly ILogger<NetworkManager> _logger;
|
||||||
@ -21,8 +23,14 @@ namespace Emby.Server.Implementations.Networking
|
|||||||
private readonly object _localIpAddressSyncLock = new object();
|
private readonly object _localIpAddressSyncLock = new object();
|
||||||
|
|
||||||
private readonly object _subnetLookupLock = new object();
|
private readonly object _subnetLookupLock = new object();
|
||||||
private Dictionary<string, List<string>> _subnetLookup = new Dictionary<string, List<string>>(StringComparer.Ordinal);
|
private readonly Dictionary<string, List<string>> _subnetLookup = new Dictionary<string, List<string>>(StringComparer.Ordinal);
|
||||||
|
|
||||||
|
private List<PhysicalAddress> _macAddresses;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="NetworkManager"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="logger">Logger to use for messages.</param>
|
||||||
public NetworkManager(ILogger<NetworkManager> logger)
|
public NetworkManager(ILogger<NetworkManager> logger)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
@ -31,8 +39,10 @@ namespace Emby.Server.Implementations.Networking
|
|||||||
NetworkChange.NetworkAvailabilityChanged += OnNetworkAvailabilityChanged;
|
NetworkChange.NetworkAvailabilityChanged += OnNetworkAvailabilityChanged;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
public event EventHandler NetworkChanged;
|
public event EventHandler NetworkChanged;
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
public Func<string[]> LocalSubnetsFn { get; set; }
|
public Func<string[]> LocalSubnetsFn { get; set; }
|
||||||
|
|
||||||
private void OnNetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
|
private void OnNetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
|
||||||
@ -58,13 +68,14 @@ namespace Emby.Server.Implementations.Networking
|
|||||||
NetworkChanged?.Invoke(this, EventArgs.Empty);
|
NetworkChanged?.Invoke(this, EventArgs.Empty);
|
||||||
}
|
}
|
||||||
|
|
||||||
public IPAddress[] GetLocalIpAddresses(bool ignoreVirtualInterface = true)
|
/// <inheritdoc/>
|
||||||
|
public IPAddress[] GetLocalIpAddresses()
|
||||||
{
|
{
|
||||||
lock (_localIpAddressSyncLock)
|
lock (_localIpAddressSyncLock)
|
||||||
{
|
{
|
||||||
if (_localIpAddresses == null)
|
if (_localIpAddresses == null)
|
||||||
{
|
{
|
||||||
var addresses = GetLocalIpAddressesInternal(ignoreVirtualInterface).ToArray();
|
var addresses = GetLocalIpAddressesInternal().ToArray();
|
||||||
|
|
||||||
_localIpAddresses = addresses;
|
_localIpAddresses = addresses;
|
||||||
}
|
}
|
||||||
@ -73,42 +84,47 @@ namespace Emby.Server.Implementations.Networking
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<IPAddress> GetLocalIpAddressesInternal(bool ignoreVirtualInterface)
|
private List<IPAddress> GetLocalIpAddressesInternal()
|
||||||
{
|
{
|
||||||
var list = GetIPsDefault(ignoreVirtualInterface).ToList();
|
var list = GetIPsDefault().ToList();
|
||||||
|
|
||||||
if (list.Count == 0)
|
if (list.Count == 0)
|
||||||
{
|
{
|
||||||
list = GetLocalIpAddressesFallback().GetAwaiter().GetResult().ToList();
|
list = GetLocalIpAddressesFallback().GetAwaiter().GetResult().ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
var listClone = list.ToList();
|
var listClone = new List<IPAddress>();
|
||||||
|
|
||||||
return list
|
var subnets = LocalSubnetsFn();
|
||||||
|
|
||||||
|
foreach (var i in list)
|
||||||
|
{
|
||||||
|
if (i.IsIPv6LinkLocal || i.ToString().StartsWith("169.254.", StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Array.IndexOf(subnets, $"[{i}]") == -1)
|
||||||
|
{
|
||||||
|
listClone.Add(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return listClone
|
||||||
.OrderBy(i => i.AddressFamily == AddressFamily.InterNetwork ? 0 : 1)
|
.OrderBy(i => i.AddressFamily == AddressFamily.InterNetwork ? 0 : 1)
|
||||||
.ThenBy(i => listClone.IndexOf(i))
|
// .ThenBy(i => listClone.IndexOf(i))
|
||||||
.Where(FilterIpAddress)
|
|
||||||
.GroupBy(i => i.ToString())
|
.GroupBy(i => i.ToString())
|
||||||
.Select(x => x.First())
|
.Select(x => x.First())
|
||||||
.ToList();
|
.ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool FilterIpAddress(IPAddress address)
|
/// <inheritdoc/>
|
||||||
{
|
|
||||||
if (address.IsIPv6LinkLocal
|
|
||||||
|| address.ToString().StartsWith("169.", StringComparison.OrdinalIgnoreCase))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool IsInPrivateAddressSpace(string endpoint)
|
public bool IsInPrivateAddressSpace(string endpoint)
|
||||||
{
|
{
|
||||||
return IsInPrivateAddressSpace(endpoint, true);
|
return IsInPrivateAddressSpace(endpoint, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Checks if the address in endpoint is an RFC1918, RFC1122, or RFC3927 address
|
||||||
private bool IsInPrivateAddressSpace(string endpoint, bool checkSubnets)
|
private bool IsInPrivateAddressSpace(string endpoint, bool checkSubnets)
|
||||||
{
|
{
|
||||||
if (string.Equals(endpoint, "::1", StringComparison.OrdinalIgnoreCase))
|
if (string.Equals(endpoint, "::1", StringComparison.OrdinalIgnoreCase))
|
||||||
@ -116,12 +132,12 @@ namespace Emby.Server.Implementations.Networking
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ipv6
|
// IPV6
|
||||||
if (endpoint.Split('.').Length > 4)
|
if (endpoint.Split('.').Length > 4)
|
||||||
{
|
{
|
||||||
// Handle ipv4 mapped to ipv6
|
// Handle ipv4 mapped to ipv6
|
||||||
var originalEndpoint = endpoint;
|
var originalEndpoint = endpoint;
|
||||||
endpoint = endpoint.Replace("::ffff:", string.Empty);
|
endpoint = endpoint.Replace("::ffff:", string.Empty, StringComparison.OrdinalIgnoreCase);
|
||||||
|
|
||||||
if (string.Equals(endpoint, originalEndpoint, StringComparison.OrdinalIgnoreCase))
|
if (string.Equals(endpoint, originalEndpoint, StringComparison.OrdinalIgnoreCase))
|
||||||
{
|
{
|
||||||
@ -130,23 +146,21 @@ namespace Emby.Server.Implementations.Networking
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Private address space:
|
// Private address space:
|
||||||
// http://en.wikipedia.org/wiki/Private_network
|
|
||||||
|
|
||||||
if (endpoint.StartsWith("172.", StringComparison.OrdinalIgnoreCase))
|
if (string.Equals(endpoint, "localhost", StringComparison.OrdinalIgnoreCase))
|
||||||
{
|
|
||||||
return Is172AddressPrivate(endpoint);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (endpoint.StartsWith("localhost", StringComparison.OrdinalIgnoreCase) ||
|
|
||||||
endpoint.StartsWith("127.", StringComparison.OrdinalIgnoreCase) ||
|
|
||||||
endpoint.StartsWith("169.", StringComparison.OrdinalIgnoreCase))
|
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (checkSubnets && endpoint.StartsWith("192.168", StringComparison.OrdinalIgnoreCase))
|
byte[] octet = IPAddress.Parse(endpoint).GetAddressBytes();
|
||||||
|
|
||||||
|
if ((octet[0] == 10) ||
|
||||||
|
(octet[0] == 172 && (octet[1] >= 16 && octet[1] <= 31)) || // RFC1918
|
||||||
|
(octet[0] == 192 && octet[1] == 168) || // RFC1918
|
||||||
|
(octet[0] == 127) || // RFC1122
|
||||||
|
(octet[0] == 169 && octet[1] == 254)) // RFC3927
|
||||||
{
|
{
|
||||||
return true;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (checkSubnets && IsInPrivateAddressSpaceAndLocalSubnet(endpoint))
|
if (checkSubnets && IsInPrivateAddressSpaceAndLocalSubnet(endpoint))
|
||||||
@ -157,6 +171,7 @@ namespace Emby.Server.Implementations.Networking
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
public bool IsInPrivateAddressSpaceAndLocalSubnet(string endpoint)
|
public bool IsInPrivateAddressSpaceAndLocalSubnet(string endpoint)
|
||||||
{
|
{
|
||||||
if (endpoint.StartsWith("10.", StringComparison.OrdinalIgnoreCase))
|
if (endpoint.StartsWith("10.", StringComparison.OrdinalIgnoreCase))
|
||||||
@ -179,6 +194,7 @@ namespace Emby.Server.Implementations.Networking
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Gives a list of possible subnets from the system whose interface ip starts with endpointFirstPart
|
||||||
private List<string> GetSubnets(string endpointFirstPart)
|
private List<string> GetSubnets(string endpointFirstPart)
|
||||||
{
|
{
|
||||||
lock (_subnetLookupLock)
|
lock (_subnetLookupLock)
|
||||||
@ -224,46 +240,75 @@ namespace Emby.Server.Implementations.Networking
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool Is172AddressPrivate(string endpoint)
|
/// <inheritdoc/>
|
||||||
{
|
|
||||||
for (var i = 16; i <= 31; i++)
|
|
||||||
{
|
|
||||||
if (endpoint.StartsWith("172." + i.ToString(CultureInfo.InvariantCulture) + ".", StringComparison.OrdinalIgnoreCase))
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool IsInLocalNetwork(string endpoint)
|
public bool IsInLocalNetwork(string endpoint)
|
||||||
{
|
{
|
||||||
return IsInLocalNetworkInternal(endpoint, true);
|
return IsInLocalNetworkInternal(endpoint, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
public bool IsAddressInSubnets(string addressString, string[] subnets)
|
public bool IsAddressInSubnets(string addressString, string[] subnets)
|
||||||
{
|
{
|
||||||
return IsAddressInSubnets(IPAddress.Parse(addressString), addressString, subnets);
|
return IsAddressInSubnets(IPAddress.Parse(addressString), addressString, subnets);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public bool IsAddressInSubnets(IPAddress address, bool excludeInterfaces, bool excludeRFC)
|
||||||
|
{
|
||||||
|
byte[] octet = address.GetAddressBytes();
|
||||||
|
|
||||||
|
if ((octet[0] == 127) || // RFC1122
|
||||||
|
(octet[0] == 169 && octet[1] == 254)) // RFC3927
|
||||||
|
{
|
||||||
|
// don't use on loopback or 169 interfaces
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
string addressString = address.ToString();
|
||||||
|
string excludeAddress = "[" + addressString + "]";
|
||||||
|
var subnets = LocalSubnetsFn();
|
||||||
|
|
||||||
|
// Exclude any addresses if they appear in the LAN list in [ ]
|
||||||
|
if (Array.IndexOf(subnets, excludeAddress) != -1)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return IsAddressInSubnets(address, addressString, subnets);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Checks if the give address falls within the ranges given in [subnets]. The addresses in subnets can be hosts or subnets in the CIDR format.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="address">IPAddress version of the address.</param>
|
||||||
|
/// <param name="addressString">The address to check.</param>
|
||||||
|
/// <param name="subnets">If true, check against addresses in the LAN settings which have [] arroud and return true if it matches the address give in address.</param>
|
||||||
|
/// <returns><c>false</c>if the address isn't in the subnets, <c>true</c> otherwise.</returns>
|
||||||
private static bool IsAddressInSubnets(IPAddress address, string addressString, string[] subnets)
|
private static bool IsAddressInSubnets(IPAddress address, string addressString, string[] subnets)
|
||||||
{
|
{
|
||||||
foreach (var subnet in subnets)
|
foreach (var subnet in subnets)
|
||||||
{
|
{
|
||||||
var normalizedSubnet = subnet.Trim();
|
var normalizedSubnet = subnet.Trim();
|
||||||
|
// Is the subnet a host address and does it match the address being passes?
|
||||||
if (string.Equals(normalizedSubnet, addressString, StringComparison.OrdinalIgnoreCase))
|
if (string.Equals(normalizedSubnet, addressString, StringComparison.OrdinalIgnoreCase))
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Parse CIDR subnets and see if address falls within it.
|
||||||
if (normalizedSubnet.Contains('/', StringComparison.Ordinal))
|
if (normalizedSubnet.Contains('/', StringComparison.Ordinal))
|
||||||
{
|
{
|
||||||
var ipNetwork = IPNetwork.Parse(normalizedSubnet);
|
try
|
||||||
if (ipNetwork.Contains(address))
|
|
||||||
{
|
{
|
||||||
return true;
|
var ipNetwork = IPNetwork.Parse(normalizedSubnet);
|
||||||
|
if (ipNetwork.Contains(address))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
// Ignoring - invalid subnet passed encountered.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -288,7 +333,7 @@ namespace Emby.Server.Implementations.Networking
|
|||||||
var localSubnets = localSubnetsFn();
|
var localSubnets = localSubnetsFn();
|
||||||
foreach (var subnet in localSubnets)
|
foreach (var subnet in localSubnets)
|
||||||
{
|
{
|
||||||
// only validate if there's at least one valid entry
|
// Only validate if there's at least one valid entry.
|
||||||
if (!string.IsNullOrWhiteSpace(subnet))
|
if (!string.IsNullOrWhiteSpace(subnet))
|
||||||
{
|
{
|
||||||
return IsAddressInSubnets(address, addressString, localSubnets) || IsInPrivateAddressSpace(addressString, false);
|
return IsAddressInSubnets(address, addressString, localSubnets) || IsInPrivateAddressSpace(addressString, false);
|
||||||
@ -345,7 +390,7 @@ namespace Emby.Server.Implementations.Networking
|
|||||||
}
|
}
|
||||||
catch (InvalidOperationException)
|
catch (InvalidOperationException)
|
||||||
{
|
{
|
||||||
// Can happen with reverse proxy or IIS url rewriting
|
// Can happen with reverse proxy or IIS url rewriting?
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@ -362,7 +407,7 @@ namespace Emby.Server.Implementations.Networking
|
|||||||
return Dns.GetHostAddressesAsync(hostName);
|
return Dns.GetHostAddressesAsync(hostName);
|
||||||
}
|
}
|
||||||
|
|
||||||
private IEnumerable<IPAddress> GetIPsDefault(bool ignoreVirtualInterface)
|
private IEnumerable<IPAddress> GetIPsDefault()
|
||||||
{
|
{
|
||||||
IEnumerable<NetworkInterface> interfaces;
|
IEnumerable<NetworkInterface> interfaces;
|
||||||
|
|
||||||
@ -382,15 +427,7 @@ namespace Emby.Server.Implementations.Networking
|
|||||||
{
|
{
|
||||||
var ipProperties = network.GetIPProperties();
|
var ipProperties = network.GetIPProperties();
|
||||||
|
|
||||||
// Try to exclude virtual adapters
|
// Exclude any addresses if they appear in the LAN list in [ ]
|
||||||
// http://stackoverflow.com/questions/8089685/c-sharp-finding-my-machines-local-ip-address-and-not-the-vms
|
|
||||||
var addr = ipProperties.GatewayAddresses.FirstOrDefault();
|
|
||||||
if (addr == null
|
|
||||||
|| (ignoreVirtualInterface
|
|
||||||
&& (addr.Address.Equals(IPAddress.Any) || addr.Address.Equals(IPAddress.IPv6Any))))
|
|
||||||
{
|
|
||||||
return Enumerable.Empty<IPAddress>();
|
|
||||||
}
|
|
||||||
|
|
||||||
return ipProperties.UnicastAddresses
|
return ipProperties.UnicastAddresses
|
||||||
.Select(i => i.Address)
|
.Select(i => i.Address)
|
||||||
@ -423,33 +460,29 @@ namespace Emby.Server.Implementations.Networking
|
|||||||
return port;
|
return port;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
public int GetRandomUnusedUdpPort()
|
public int GetRandomUnusedUdpPort()
|
||||||
{
|
{
|
||||||
var localEndPoint = new IPEndPoint(IPAddress.Any, 0);
|
var localEndPoint = new IPEndPoint(IPAddress.Any, 0);
|
||||||
using (var udpClient = new UdpClient(localEndPoint))
|
using (var udpClient = new UdpClient(localEndPoint))
|
||||||
{
|
{
|
||||||
var port = ((IPEndPoint)udpClient.Client.LocalEndPoint).Port;
|
return ((IPEndPoint)udpClient.Client.LocalEndPoint).Port;
|
||||||
return port;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<PhysicalAddress> _macAddresses;
|
/// <inheritdoc/>
|
||||||
public List<PhysicalAddress> GetMacAddresses()
|
public List<PhysicalAddress> GetMacAddresses()
|
||||||
{
|
{
|
||||||
if (_macAddresses == null)
|
return _macAddresses ??= GetMacAddressesInternal().ToList();
|
||||||
{
|
|
||||||
_macAddresses = GetMacAddressesInternal().ToList();
|
|
||||||
}
|
|
||||||
|
|
||||||
return _macAddresses;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static IEnumerable<PhysicalAddress> GetMacAddressesInternal()
|
private static IEnumerable<PhysicalAddress> GetMacAddressesInternal()
|
||||||
=> NetworkInterface.GetAllNetworkInterfaces()
|
=> NetworkInterface.GetAllNetworkInterfaces()
|
||||||
.Where(i => i.NetworkInterfaceType != NetworkInterfaceType.Loopback)
|
.Where(i => i.NetworkInterfaceType != NetworkInterfaceType.Loopback)
|
||||||
.Select(x => x.GetPhysicalAddress())
|
.Select(x => x.GetPhysicalAddress())
|
||||||
.Where(x => x != null && x != PhysicalAddress.None);
|
.Where(x => !x.Equals(PhysicalAddress.None));
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
public bool IsInSameSubnet(IPAddress address1, IPAddress address2, IPAddress subnetMask)
|
public bool IsInSameSubnet(IPAddress address1, IPAddress address2, IPAddress subnetMask)
|
||||||
{
|
{
|
||||||
IPAddress network1 = GetNetworkAddress(address1, subnetMask);
|
IPAddress network1 = GetNetworkAddress(address1, subnetMask);
|
||||||
@ -476,6 +509,7 @@ namespace Emby.Server.Implementations.Networking
|
|||||||
return new IPAddress(broadcastAddress);
|
return new IPAddress(broadcastAddress);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
public IPAddress GetLocalIpSubnetMask(IPAddress address)
|
public IPAddress GetLocalIpSubnetMask(IPAddress address)
|
||||||
{
|
{
|
||||||
NetworkInterface[] interfaces;
|
NetworkInterface[] interfaces;
|
||||||
@ -496,14 +530,11 @@ namespace Emby.Server.Implementations.Networking
|
|||||||
|
|
||||||
foreach (NetworkInterface ni in interfaces)
|
foreach (NetworkInterface ni in interfaces)
|
||||||
{
|
{
|
||||||
if (ni.GetIPProperties().GatewayAddresses.FirstOrDefault() != null)
|
foreach (UnicastIPAddressInformation ip in ni.GetIPProperties().UnicastAddresses)
|
||||||
{
|
{
|
||||||
foreach (UnicastIPAddressInformation ip in ni.GetIPProperties().UnicastAddresses)
|
if (ip.Address.Equals(address) && ip.IPv4Mask != null)
|
||||||
{
|
{
|
||||||
if (ip.Address.Equals(address) && ip.IPv4Mask != null)
|
return ip.IPv4Mask;
|
||||||
{
|
|
||||||
return ip.IPv4Mask;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,14 +11,21 @@ namespace MediaBrowser.Common.Net
|
|||||||
{
|
{
|
||||||
event EventHandler NetworkChanged;
|
event EventHandler NetworkChanged;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets a function to return the list of user defined LAN addresses.
|
||||||
|
/// </summary>
|
||||||
Func<string[]> LocalSubnetsFn { get; set; }
|
Func<string[]> LocalSubnetsFn { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets a random port number that is currently available.
|
/// Gets a random port TCP number that is currently available.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>System.Int32.</returns>
|
/// <returns>System.Int32.</returns>
|
||||||
int GetRandomUnusedTcpPort();
|
int GetRandomUnusedTcpPort();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a random port UDP number that is currently available.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>System.Int32.</returns>
|
||||||
int GetRandomUnusedUdpPort();
|
int GetRandomUnusedUdpPort();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -34,6 +41,13 @@ namespace MediaBrowser.Common.Net
|
|||||||
/// <returns><c>true</c> if [is in private address space] [the specified endpoint]; otherwise, <c>false</c>.</returns>
|
/// <returns><c>true</c> if [is in private address space] [the specified endpoint]; otherwise, <c>false</c>.</returns>
|
||||||
bool IsInPrivateAddressSpace(string endpoint);
|
bool IsInPrivateAddressSpace(string endpoint);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Determines whether [is in private address space 10.x.x.x] [the specified endpoint] and exists in the subnets returned by GetSubnets().
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="endpoint">The endpoint.</param>
|
||||||
|
/// <returns><c>true</c> if [is in private address space 10.x.x.x] [the specified endpoint]; otherwise, <c>false</c>.</returns>
|
||||||
|
bool IsInPrivateAddressSpaceAndLocalSubnet(string endpoint);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Determines whether [is in local network] [the specified endpoint].
|
/// Determines whether [is in local network] [the specified endpoint].
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -41,12 +55,43 @@ namespace MediaBrowser.Common.Net
|
|||||||
/// <returns><c>true</c> if [is in local network] [the specified endpoint]; otherwise, <c>false</c>.</returns>
|
/// <returns><c>true</c> if [is in local network] [the specified endpoint]; otherwise, <c>false</c>.</returns>
|
||||||
bool IsInLocalNetwork(string endpoint);
|
bool IsInLocalNetwork(string endpoint);
|
||||||
|
|
||||||
IPAddress[] GetLocalIpAddresses(bool ignoreVirtualInterface);
|
/// <summary>
|
||||||
|
/// Investigates an caches a list of interface addresses, excluding local link and LAN excluded addresses.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>The list of ipaddresses.</returns>
|
||||||
|
IPAddress[] GetLocalIpAddresses();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Checks if the given address falls within the ranges given in [subnets]. The addresses in subnets can be hosts or subnets in the CIDR format.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="addressString">The address to check.</param>
|
||||||
|
/// <param name="subnets">If true, check against addresses in the LAN settings surrounded by brackets ([]).</param>
|
||||||
|
/// <returns><c>true</c>if the address is in at least one of the given subnets, <c>false</c> otherwise.</returns>
|
||||||
bool IsAddressInSubnets(string addressString, string[] subnets);
|
bool IsAddressInSubnets(string addressString, string[] subnets);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns true if address is in the LAN list in the config file.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="address">The address to check.</param>
|
||||||
|
/// <param name="excludeInterfaces">If true, check against addresses in the LAN settings which have [] arroud and return true if it matches the address give in address.</param>
|
||||||
|
/// <param name="excludeRFC">If true, returns false if address is in the 127.x.x.x or 169.128.x.x range.</param>
|
||||||
|
/// <returns><c>false</c>if the address isn't in the LAN list, <c>true</c> if the address has been defined as a LAN address.</returns>
|
||||||
|
bool IsAddressInSubnets(IPAddress address, bool excludeInterfaces, bool excludeRFC);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Checks if address is in the LAN list in the config file.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="address1">Source address to check.</param>
|
||||||
|
/// <param name="address2">Destination address to check against.</param>
|
||||||
|
/// <param name="subnetMask">Destination subnet to check against.</param>
|
||||||
|
/// <returns><c>true/false</c>depending on whether address1 is in the same subnet as IPAddress2 with subnetMask.</returns>
|
||||||
bool IsInSameSubnet(IPAddress address1, IPAddress address2, IPAddress subnetMask);
|
bool IsInSameSubnet(IPAddress address1, IPAddress address2, IPAddress subnetMask);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns the subnet mask of an interface with the given address.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="address">The address to check.</param>
|
||||||
|
/// <returns>Returns the subnet mask of an interface with the given address, or null if an interface match cannot be found.</returns>
|
||||||
IPAddress GetLocalIpSubnetMask(IPAddress address);
|
IPAddress GetLocalIpSubnetMask(IPAddress address);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,6 @@ using System.Text;
|
|||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using MediaBrowser.Common.Net;
|
using MediaBrowser.Common.Net;
|
||||||
using MediaBrowser.Controller.Configuration;
|
|
||||||
using MediaBrowser.Model.Net;
|
using MediaBrowser.Model.Net;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
@ -44,7 +43,6 @@ namespace Rssdp.Infrastructure
|
|||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
private ISocketFactory _SocketFactory;
|
private ISocketFactory _SocketFactory;
|
||||||
private readonly INetworkManager _networkManager;
|
private readonly INetworkManager _networkManager;
|
||||||
private readonly IServerConfigurationManager _config;
|
|
||||||
|
|
||||||
private int _LocalPort;
|
private int _LocalPort;
|
||||||
private int _MulticastTtl;
|
private int _MulticastTtl;
|
||||||
@ -66,11 +64,11 @@ namespace Rssdp.Infrastructure
|
|||||||
/// Minimum constructor.
|
/// Minimum constructor.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <exception cref="ArgumentNullException">The <paramref name="socketFactory"/> argument is null.</exception>
|
/// <exception cref="ArgumentNullException">The <paramref name="socketFactory"/> argument is null.</exception>
|
||||||
public SsdpCommunicationsServer(IServerConfigurationManager config, ISocketFactory socketFactory,
|
public SsdpCommunicationsServer(ISocketFactory socketFactory,
|
||||||
INetworkManager networkManager, ILogger logger, bool enableMultiSocketBinding)
|
INetworkManager networkManager, ILogger logger, bool enableMultiSocketBinding)
|
||||||
: this(socketFactory, 0, SsdpConstants.SsdpDefaultMulticastTimeToLive, networkManager, logger, enableMultiSocketBinding)
|
: this(socketFactory, 0, SsdpConstants.SsdpDefaultMulticastTimeToLive, networkManager, logger, enableMultiSocketBinding)
|
||||||
{
|
{
|
||||||
_config = config;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -355,7 +353,7 @@ namespace Rssdp.Infrastructure
|
|||||||
|
|
||||||
if (_enableMultiSocketBinding)
|
if (_enableMultiSocketBinding)
|
||||||
{
|
{
|
||||||
foreach (var address in _networkManager.GetLocalIpAddresses(_config.Configuration.IgnoreVirtualInterfaces))
|
foreach (var address in _networkManager.GetLocalIpAddresses())
|
||||||
{
|
{
|
||||||
if (address.AddressFamily == AddressFamily.InterNetworkV6)
|
if (address.AddressFamily == AddressFamily.InterNetworkV6)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user