Merge pull request #8147 from Shadowghost/network-rewrite

This commit is contained in:
Joshua M. Boniface 2023-07-03 13:16:21 -04:00 committed by GitHub
commit 93b400343e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
62 changed files with 1951 additions and 3513 deletions

View File

@ -24,6 +24,7 @@
<PackageVersion Include="LrcParser" Version="2023.524.0" />
<PackageVersion Include="MetaBrainz.MusicBrainz" Version="5.0.0" />
<PackageVersion Include="Microsoft.AspNetCore.Authorization" Version="7.0.8" />
<PackageVersion Include="Microsoft.AspNetCore.HttpOverrides" Version="2.2.0" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.8" />
<PackageVersion Include="Microsoft.CodeAnalysis.BannedApiAnalyzers" Version="3.3.4" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.8" />

View File

@ -17,7 +17,7 @@ namespace Emby.Dlna.Configuration
BlastAliveMessages = true;
SendOnlyMatchedHost = true;
ClientDiscoveryIntervalSeconds = 60;
AliveMessageIntervalSeconds = 1800;
AliveMessageIntervalSeconds = 180;
}
/// <summary>

View File

@ -11,7 +11,6 @@ using System.Threading.Tasks;
using Emby.Dlna.PlayTo;
using Emby.Dlna.Ssdp;
using Jellyfin.Networking.Configuration;
using Jellyfin.Networking.Manager;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Common.Net;
@ -201,8 +200,7 @@ namespace Emby.Dlna.Main
{
if (_communicationsServer is null)
{
var enableMultiSocketBinding = OperatingSystem.IsWindows() ||
OperatingSystem.IsLinux();
var enableMultiSocketBinding = OperatingSystem.IsWindows() || OperatingSystem.IsLinux();
_communicationsServer = new SsdpCommunicationsServer(_socketFactory, _networkManager, _logger, enableMultiSocketBinding)
{
@ -248,11 +246,6 @@ namespace Emby.Dlna.Main
public void StartDevicePublisher(Configuration.DlnaOptions options)
{
if (!options.BlastAliveMessages)
{
return;
}
if (_publisher is not null)
{
return;
@ -263,7 +256,8 @@ namespace Emby.Dlna.Main
_publisher = new SsdpDevicePublisher(
_communicationsServer,
Environment.OSVersion.Platform.ToString(),
Environment.OSVersion.VersionString,
// Can not use VersionString here since that includes OS and version
Environment.OSVersion.Version.ToString(),
_config.GetDlnaConfiguration().SendOnlyMatchedHost)
{
LogFunction = (msg) => _logger.LogDebug("{Msg}", msg),
@ -272,7 +266,10 @@ namespace Emby.Dlna.Main
RegisterServerEndpoints();
_publisher.StartBroadcastingAliveMessages(TimeSpan.FromSeconds(options.BlastAliveMessageIntervalSeconds));
if (options.BlastAliveMessages)
{
_publisher.StartSendingAliveNotifications(TimeSpan.FromSeconds(options.BlastAliveMessageIntervalSeconds));
}
}
catch (Exception ex)
{
@ -285,42 +282,33 @@ namespace Emby.Dlna.Main
var udn = CreateUuid(_appHost.SystemId);
var descriptorUri = "/dlna/" + udn + "/description.xml";
var bindAddresses = NetworkManager.CreateCollection(
_networkManager.GetInternalBindAddresses()
.Where(i => i.AddressFamily == AddressFamily.InterNetwork || (i.AddressFamily == AddressFamily.InterNetworkV6 && i.Address.ScopeId != 0)));
// Only get bind addresses in LAN
// IPv6 is currently unsupported
var validInterfaces = _networkManager.GetInternalBindAddresses()
.Where(x => x.Address is not null)
.Where(x => x.AddressFamily != AddressFamily.InterNetworkV6)
.ToList();
if (bindAddresses.Count == 0)
if (validInterfaces.Count == 0)
{
// No interfaces returned, so use loopback.
bindAddresses = _networkManager.GetLoopbacks();
// No interfaces returned, fall back to loopback
validInterfaces = _networkManager.GetLoopbacks().ToList();
}
foreach (IPNetAddress address in bindAddresses)
foreach (var intf in validInterfaces)
{
if (address.AddressFamily == AddressFamily.InterNetworkV6)
{
// Not supporting IPv6 right now
continue;
}
// Limit to LAN addresses only
if (!_networkManager.IsInLocalNetwork(address))
{
continue;
}
var fullService = "urn:schemas-upnp-org:device:MediaServer:1";
_logger.LogInformation("Registering publisher for {ResourceName} on {DeviceAddress}", fullService, address);
_logger.LogInformation("Registering publisher for {ResourceName} on {DeviceAddress}", fullService, intf.Address);
var uri = new UriBuilder(_appHost.GetApiUrlForLocalAccess(address, false) + descriptorUri);
var uri = new UriBuilder(_appHost.GetApiUrlForLocalAccess(intf.Address, false) + descriptorUri);
var device = new SsdpRootDevice
{
CacheLifetime = TimeSpan.FromSeconds(1800), // How long SSDP clients can cache this info.
Location = uri.Uri, // Must point to the URL that serves your devices UPnP description document.
Address = address.Address,
PrefixLength = address.PrefixLength,
Address = intf.Address,
PrefixLength = NetworkExtensions.MaskToCidr(intf.Subnet.Prefix),
FriendlyName = "Jellyfin",
Manufacturer = "Jellyfin",
ModelName = "Jellyfin Server",

View File

@ -189,7 +189,7 @@ namespace Emby.Dlna.PlayTo
_sessionManager.UpdateDeviceName(sessionInfo.Id, deviceName);
string serverAddress = _appHost.GetSmartApiUrl(info.RemoteIpAddress);
string serverAddress = _appHost.GetSmartApiUrl(info.RemoteIPAddress);
controller = new PlayToController(
sessionInfo,

View File

@ -73,7 +73,11 @@ namespace Emby.Dlna.Ssdp
{
if (_listenerCount > 0 && _deviceLocator is null && _commsServer is not null)
{
_deviceLocator = new SsdpDeviceLocator(_commsServer);
_deviceLocator = new SsdpDeviceLocator(
_commsServer,
Environment.OSVersion.Platform.ToString(),
// Can not use VersionString here since that includes OS and version
Environment.OSVersion.Version.ToString());
// (Optional) Set the filter so we only see notifications for devices we care about
// (can be any search target value i.e device type, uuid value etc - any value that appears in the
@ -106,7 +110,7 @@ namespace Emby.Dlna.Ssdp
{
Location = e.DiscoveredDevice.DescriptionLocation,
Headers = headers,
RemoteIpAddress = e.RemoteIpAddress
RemoteIPAddress = e.RemoteIPAddress
});
DeviceDiscoveredInternal?.Invoke(this, args);

View File

@ -475,8 +475,8 @@ namespace Emby.Server.Implementations
}
var networkConfiguration = ConfigurationManager.GetNetworkConfiguration();
HttpPort = networkConfiguration.HttpServerPortNumber;
HttpsPort = networkConfiguration.HttpsPortNumber;
HttpPort = networkConfiguration.InternalHttpPort;
HttpsPort = networkConfiguration.InternalHttpsPort;
// Safeguard against invalid configuration
if (HttpPort == HttpsPort)
@ -785,8 +785,8 @@ namespace Emby.Server.Implementations
if (HttpPort != 0 && HttpsPort != 0)
{
// Need to restart if ports have changed
if (networkConfiguration.HttpServerPortNumber != HttpPort
|| networkConfiguration.HttpsPortNumber != HttpsPort)
if (networkConfiguration.InternalHttpPort != HttpPort
|| networkConfiguration.InternalHttpsPort != HttpsPort)
{
if (ConfigurationManager.Configuration.IsPortAuthorized)
{
@ -995,7 +995,7 @@ namespace Emby.Server.Implementations
return PublishedServerUrl.Trim('/');
}
string smart = NetManager.GetBindInterface(remoteAddr, out var port);
string smart = NetManager.GetBindAddress(remoteAddr, out var port);
return GetLocalApiUrl(smart.Trim('/'), null, port);
}
@ -1006,7 +1006,9 @@ namespace Emby.Server.Implementations
if (ConfigurationManager.GetNetworkConfiguration().EnablePublishedServerUriByRequest)
{
int? requestPort = request.Host.Port;
if ((requestPort == 80 && string.Equals(request.Scheme, "http", StringComparison.OrdinalIgnoreCase)) || (requestPort == 443 && string.Equals(request.Scheme, "https", StringComparison.OrdinalIgnoreCase)))
if (requestPort == null
|| (requestPort == 80 && string.Equals(request.Scheme, "http", StringComparison.OrdinalIgnoreCase))
|| (requestPort == 443 && string.Equals(request.Scheme, "https", StringComparison.OrdinalIgnoreCase)))
{
requestPort = -1;
}
@ -1027,15 +1029,15 @@ namespace Emby.Server.Implementations
return PublishedServerUrl.Trim('/');
}
string smart = NetManager.GetBindInterface(hostname, out var port);
string smart = NetManager.GetBindAddress(hostname, out var port);
return GetLocalApiUrl(smart.Trim('/'), null, port);
}
/// <inheritdoc/>
public string GetApiUrlForLocalAccess(IPObject hostname = null, bool allowHttps = true)
public string GetApiUrlForLocalAccess(IPAddress ipAddress = null, bool allowHttps = true)
{
// With an empty source, the port will be null
var smart = NetManager.GetBindInterface(hostname ?? IPHost.None, out _);
var smart = NetManager.GetBindAddress(ipAddress, out _, true);
var scheme = !allowHttps ? Uri.UriSchemeHttp : null;
int? port = !allowHttps ? HttpPort : null;
return GetLocalApiUrl(smart, scheme, port);

View File

@ -57,7 +57,7 @@ namespace Emby.Server.Implementations.EntryPoints
return new StringBuilder(32)
.Append(config.EnableUPnP).Append(Separator)
.Append(config.PublicPort).Append(Separator)
.Append(config.PublicHttpPort).Append(Separator)
.Append(config.PublicHttpsPort).Append(Separator)
.Append(_appHost.HttpPort).Append(Separator)
.Append(_appHost.HttpsPort).Append(Separator)
@ -146,7 +146,7 @@ namespace Emby.Server.Implementations.EntryPoints
private IEnumerable<Task> CreatePortMaps(INatDevice device)
{
var config = _config.GetNetworkConfiguration();
yield return CreatePortMap(device, _appHost.HttpPort, config.PublicPort);
yield return CreatePortMap(device, _appHost.HttpPort, config.PublicHttpPort);
if (_appHost.ListenWithHttps)
{

View File

@ -1,10 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using Emby.Server.Implementations.Udp;
using Jellyfin.Networking.Configuration;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Plugins;
using Microsoft.Extensions.Configuration;
@ -29,11 +33,13 @@ namespace Emby.Server.Implementations.EntryPoints
private readonly IServerApplicationHost _appHost;
private readonly IConfiguration _config;
private readonly IConfigurationManager _configurationManager;
private readonly INetworkManager _networkManager;
private readonly bool _enableMultiSocketBinding;
/// <summary>
/// The UDP server.
/// </summary>
private UdpServer? _udpServer;
private List<UdpServer> _udpServers;
private CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
private bool _disposed = false;
@ -44,16 +50,21 @@ namespace Emby.Server.Implementations.EntryPoints
/// <param name="appHost">Instance of the <see cref="IServerApplicationHost"/> interface.</param>
/// <param name="configuration">Instance of the <see cref="IConfiguration"/> interface.</param>
/// <param name="configurationManager">Instance of the <see cref="IConfigurationManager"/> interface.</param>
/// <param name="networkManager">Instance of the <see cref="INetworkManager"/> interface.</param>
public UdpServerEntryPoint(
ILogger<UdpServerEntryPoint> logger,
IServerApplicationHost appHost,
IConfiguration configuration,
IConfigurationManager configurationManager)
IConfigurationManager configurationManager,
INetworkManager networkManager)
{
_logger = logger;
_appHost = appHost;
_config = configuration;
_configurationManager = configurationManager;
_networkManager = networkManager;
_udpServers = new List<UdpServer>();
_enableMultiSocketBinding = OperatingSystem.IsWindows() || OperatingSystem.IsLinux();
}
/// <inheritdoc />
@ -68,8 +79,32 @@ namespace Emby.Server.Implementations.EntryPoints
try
{
_udpServer = new UdpServer(_logger, _appHost, _config, PortNumber);
_udpServer.Start(_cancellationTokenSource.Token);
if (_enableMultiSocketBinding)
{
// Add global broadcast socket
var server = new UdpServer(_logger, _appHost, _config, IPAddress.Broadcast, PortNumber);
server.Start(_cancellationTokenSource.Token);
_udpServers.Add(server);
// Add bind address specific broadcast sockets
// IPv6 is currently unsupported
var validInterfaces = _networkManager.GetInternalBindAddresses().Where(i => i.AddressFamily == AddressFamily.InterNetwork);
foreach (var intf in validInterfaces)
{
var broadcastAddress = NetworkExtensions.GetBroadcastAddress(intf.Subnet);
_logger.LogDebug("Binding UDP server to {Address} on port {PortNumber}", broadcastAddress, PortNumber);
server = new UdpServer(_logger, _appHost, _config, broadcastAddress, PortNumber);
server.Start(_cancellationTokenSource.Token);
_udpServers.Add(server);
}
}
else
{
var server = new UdpServer(_logger, _appHost, _config, IPAddress.Any, PortNumber);
server.Start(_cancellationTokenSource.Token);
_udpServers.Add(server);
}
}
catch (SocketException ex)
{
@ -97,9 +132,12 @@ namespace Emby.Server.Implementations.EntryPoints
_cancellationTokenSource.Cancel();
_cancellationTokenSource.Dispose();
_udpServer?.Dispose();
_udpServer = null;
foreach (var server in _udpServers)
{
server.Dispose();
}
_udpServers.Clear();
_disposed = true;
}
}

View File

@ -51,7 +51,7 @@ namespace Emby.Server.Implementations.HttpServer
using var connection = new WebSocketConnection(
_loggerFactory.CreateLogger<WebSocketConnection>(),
webSocket,
context.GetNormalizedRemoteIp())
context.GetNormalizedRemoteIP())
{
OnReceive = ProcessWebSocketMessageReceived
};

View File

@ -661,18 +661,18 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun
// Need a way to set the Receive timeout on the socket otherwise this might never timeout?
try
{
await udpClient.SendToAsync(discBytes, 0, discBytes.Length, new IPEndPoint(IPAddress.Parse("255.255.255.255"), 65001), cancellationToken).ConfigureAwait(false);
await udpClient.SendToAsync(discBytes, new IPEndPoint(IPAddress.Broadcast, 65001), cancellationToken).ConfigureAwait(false);
var receiveBuffer = new byte[8192];
while (!cancellationToken.IsCancellationRequested)
{
var response = await udpClient.ReceiveAsync(receiveBuffer, 0, receiveBuffer.Length, cancellationToken).ConfigureAwait(false);
var deviceIp = response.RemoteEndPoint.Address.ToString();
var response = await udpClient.ReceiveMessageFromAsync(receiveBuffer, new IPEndPoint(IPAddress.Any, 0), cancellationToken).ConfigureAwait(false);
var deviceIP = ((IPEndPoint)response.RemoteEndPoint).Address.ToString();
// check to make sure we have enough bytes received to be a valid message and make sure the 2nd byte is the discover reply byte
if (response.ReceivedBytes > 13 && response.Buffer[1] == 3)
// Check to make sure we have enough bytes received to be a valid message and make sure the 2nd byte is the discover reply byte
if (response.ReceivedBytes > 13 && receiveBuffer[1] == 3)
{
var deviceAddress = "http://" + deviceIp;
var deviceAddress = "http://" + deviceIP;
var info = await TryGetTunerHostInfo(deviceAddress, cancellationToken).ConfigureAwait(false);

View File

@ -48,10 +48,10 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun
GC.SuppressFinalize(this);
}
public async Task<bool> CheckTunerAvailability(IPAddress remoteIp, int tuner, CancellationToken cancellationToken)
public async Task<bool> CheckTunerAvailability(IPAddress remoteIP, int tuner, CancellationToken cancellationToken)
{
using var client = new TcpClient();
await client.ConnectAsync(remoteIp, HdHomeRunPort, cancellationToken).ConfigureAwait(false);
await client.ConnectAsync(remoteIP, HdHomeRunPort, cancellationToken).ConfigureAwait(false);
using var stream = client.GetStream();
return await CheckTunerAvailability(stream, tuner, cancellationToken).ConfigureAwait(false);
@ -75,9 +75,9 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun
}
}
public async Task StartStreaming(IPAddress remoteIp, IPAddress localIp, int localPort, IHdHomerunChannelCommands commands, int numTuners, CancellationToken cancellationToken)
public async Task StartStreaming(IPAddress remoteIP, IPAddress localIP, int localPort, IHdHomerunChannelCommands commands, int numTuners, CancellationToken cancellationToken)
{
_remoteEndPoint = new IPEndPoint(remoteIp, HdHomeRunPort);
_remoteEndPoint = new IPEndPoint(remoteIP, HdHomeRunPort);
_tcpClient = new TcpClient();
await _tcpClient.ConnectAsync(_remoteEndPoint, cancellationToken).ConfigureAwait(false);
@ -125,7 +125,7 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun
}
}
var targetValue = string.Format(CultureInfo.InvariantCulture, "rtp://{0}:{1}", localIp, localPort);
var targetValue = string.Format(CultureInfo.InvariantCulture, "rtp://{0}:{1}", localIP, localPort);
var targetMsgLen = WriteSetMessage(buffer, i, "target", targetValue, lockKeyValue);
await stream.WriteAsync(buffer.AsMemory(0, targetMsgLen), cancellationToken).ConfigureAwait(false);

View File

@ -10,60 +10,63 @@ namespace Emby.Server.Implementations.Net
public class SocketFactory : ISocketFactory
{
/// <inheritdoc />
public ISocket CreateUdpBroadcastSocket(int localPort)
public Socket CreateUdpBroadcastSocket(int localPort)
{
if (localPort < 0)
{
throw new ArgumentException("localPort cannot be less than zero.", nameof(localPort));
}
var retVal = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
try
{
retVal.EnableBroadcast = true;
retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
socket.EnableBroadcast = true;
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
socket.Bind(new IPEndPoint(IPAddress.Any, localPort));
return new UdpSocket(retVal, localPort, IPAddress.Any);
return socket;
}
catch
{
retVal?.Dispose();
socket?.Dispose();
throw;
}
}
/// <inheritdoc />
public ISocket CreateSsdpUdpSocket(IPAddress localIp, int localPort)
public Socket CreateSsdpUdpSocket(IPData bindInterface, int localPort)
{
ArgumentNullException.ThrowIfNull(bindInterface.Address);
if (localPort < 0)
{
throw new ArgumentException("localPort cannot be less than zero.", nameof(localPort));
}
var retVal = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
try
{
retVal.EnableBroadcast = true;
retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 4);
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
socket.Bind(new IPEndPoint(bindInterface.Address, localPort));
retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("239.255.255.250"), localIp));
return new UdpSocket(retVal, localPort, localIp);
return socket;
}
catch
{
retVal?.Dispose();
socket?.Dispose();
throw;
}
}
/// <inheritdoc />
public ISocket CreateUdpMulticastSocket(IPAddress ipAddress, int multicastTimeToLive, int localPort)
public Socket CreateUdpMulticastSocket(IPAddress multicastAddress, IPData bindInterface, int multicastTimeToLive, int localPort)
{
ArgumentNullException.ThrowIfNull(ipAddress);
var bindIPAddress = bindInterface.Address;
ArgumentNullException.ThrowIfNull(multicastAddress);
ArgumentNullException.ThrowIfNull(bindIPAddress);
if (multicastTimeToLive <= 0)
{
@ -75,36 +78,26 @@ namespace Emby.Server.Implementations.Net
throw new ArgumentException("localPort cannot be less than zero.", nameof(localPort));
}
var retVal = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
retVal.ExclusiveAddressUse = false;
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
try
{
// seeing occasional exceptions thrown on qnap
// System.Net.Sockets.SocketException (0x80004005): Protocol not available
retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
}
catch (SocketException)
{
}
var interfaceIndex = bindInterface.Index;
var interfaceIndexSwapped = (int)IPAddress.HostToNetworkOrder(interfaceIndex);
try
{
retVal.EnableBroadcast = true;
// retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, multicastTimeToLive);
socket.MulticastLoopback = false;
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.PacketInformation, true);
socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, multicastTimeToLive);
socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, interfaceIndexSwapped);
socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(multicastAddress, interfaceIndex));
socket.Bind(new IPEndPoint(multicastAddress, localPort));
var localIp = IPAddress.Any;
retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ipAddress, localIp));
retVal.MulticastLoopback = true;
return new UdpSocket(retVal, localPort, localIp);
return socket;
}
catch
{
retVal?.Dispose();
socket?.Dispose();
throw;
}

View File

@ -1,267 +0,0 @@
#nullable disable
#pragma warning disable CS1591
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Model.Net;
namespace Emby.Server.Implementations.Net
{
// THIS IS A LINKED FILE - SHARED AMONGST MULTIPLE PLATFORMS
// Be careful to check any changes compile and work for all platform projects it is shared in.
public sealed class UdpSocket : ISocket, IDisposable
{
private readonly int _localPort;
private readonly SocketAsyncEventArgs _receiveSocketAsyncEventArgs = new SocketAsyncEventArgs()
{
SocketFlags = SocketFlags.None
};
private readonly SocketAsyncEventArgs _sendSocketAsyncEventArgs = new SocketAsyncEventArgs()
{
SocketFlags = SocketFlags.None
};
private Socket _socket;
private bool _disposed = false;
private TaskCompletionSource<SocketReceiveResult> _currentReceiveTaskCompletionSource;
private TaskCompletionSource<int> _currentSendTaskCompletionSource;
public UdpSocket(Socket socket, int localPort, IPAddress ip)
{
ArgumentNullException.ThrowIfNull(socket);
_socket = socket;
_localPort = localPort;
LocalIPAddress = ip;
_socket.Bind(new IPEndPoint(ip, _localPort));
InitReceiveSocketAsyncEventArgs();
}
public UdpSocket(Socket socket, IPEndPoint endPoint)
{
ArgumentNullException.ThrowIfNull(socket);
_socket = socket;
_socket.Connect(endPoint);
InitReceiveSocketAsyncEventArgs();
}
public Socket Socket => _socket;
public IPAddress LocalIPAddress { get; }
private void InitReceiveSocketAsyncEventArgs()
{
var receiveBuffer = new byte[8192];
_receiveSocketAsyncEventArgs.SetBuffer(receiveBuffer, 0, receiveBuffer.Length);
_receiveSocketAsyncEventArgs.Completed += OnReceiveSocketAsyncEventArgsCompleted;
var sendBuffer = new byte[8192];
_sendSocketAsyncEventArgs.SetBuffer(sendBuffer, 0, sendBuffer.Length);
_sendSocketAsyncEventArgs.Completed += OnSendSocketAsyncEventArgsCompleted;
}
private void OnReceiveSocketAsyncEventArgsCompleted(object sender, SocketAsyncEventArgs e)
{
var tcs = _currentReceiveTaskCompletionSource;
if (tcs is not null)
{
_currentReceiveTaskCompletionSource = null;
if (e.SocketError == SocketError.Success)
{
tcs.TrySetResult(new SocketReceiveResult
{
Buffer = e.Buffer,
ReceivedBytes = e.BytesTransferred,
RemoteEndPoint = e.RemoteEndPoint as IPEndPoint,
LocalIPAddress = LocalIPAddress
});
}
else
{
tcs.TrySetException(new SocketException((int)e.SocketError));
}
}
}
private void OnSendSocketAsyncEventArgsCompleted(object sender, SocketAsyncEventArgs e)
{
var tcs = _currentSendTaskCompletionSource;
if (tcs is not null)
{
_currentSendTaskCompletionSource = null;
if (e.SocketError == SocketError.Success)
{
tcs.TrySetResult(e.BytesTransferred);
}
else
{
tcs.TrySetException(new SocketException((int)e.SocketError));
}
}
}
public IAsyncResult BeginReceive(byte[] buffer, int offset, int count, AsyncCallback callback)
{
ThrowIfDisposed();
EndPoint receivedFromEndPoint = new IPEndPoint(IPAddress.Any, 0);
return _socket.BeginReceiveFrom(buffer, offset, count, SocketFlags.None, ref receivedFromEndPoint, callback, buffer);
}
public int Receive(byte[] buffer, int offset, int count)
{
ThrowIfDisposed();
return _socket.Receive(buffer, 0, buffer.Length, SocketFlags.None);
}
public SocketReceiveResult EndReceive(IAsyncResult result)
{
ThrowIfDisposed();
var sender = new IPEndPoint(IPAddress.Any, 0);
var remoteEndPoint = (EndPoint)sender;
var receivedBytes = _socket.EndReceiveFrom(result, ref remoteEndPoint);
var buffer = (byte[])result.AsyncState;
return new SocketReceiveResult
{
ReceivedBytes = receivedBytes,
RemoteEndPoint = (IPEndPoint)remoteEndPoint,
Buffer = buffer,
LocalIPAddress = LocalIPAddress
};
}
public Task<SocketReceiveResult> ReceiveAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
ThrowIfDisposed();
var taskCompletion = new TaskCompletionSource<SocketReceiveResult>(TaskCreationOptions.RunContinuationsAsynchronously);
bool isResultSet = false;
Action<IAsyncResult> callback = callbackResult =>
{
try
{
if (!isResultSet)
{
isResultSet = true;
taskCompletion.TrySetResult(EndReceive(callbackResult));
}
}
catch (Exception ex)
{
taskCompletion.TrySetException(ex);
}
};
var result = BeginReceive(buffer, offset, count, new AsyncCallback(callback));
if (result.CompletedSynchronously)
{
callback(result);
return taskCompletion.Task;
}
cancellationToken.Register(() => taskCompletion.TrySetCanceled());
return taskCompletion.Task;
}
public Task SendToAsync(byte[] buffer, int offset, int bytes, IPEndPoint endPoint, CancellationToken cancellationToken)
{
ThrowIfDisposed();
var taskCompletion = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously);
bool isResultSet = false;
Action<IAsyncResult> callback = callbackResult =>
{
try
{
if (!isResultSet)
{
isResultSet = true;
taskCompletion.TrySetResult(EndSendTo(callbackResult));
}
}
catch (Exception ex)
{
taskCompletion.TrySetException(ex);
}
};
var result = BeginSendTo(buffer, offset, bytes, endPoint, new AsyncCallback(callback), null);
if (result.CompletedSynchronously)
{
callback(result);
return taskCompletion.Task;
}
cancellationToken.Register(() => taskCompletion.TrySetCanceled());
return taskCompletion.Task;
}
public IAsyncResult BeginSendTo(byte[] buffer, int offset, int size, IPEndPoint endPoint, AsyncCallback callback, object state)
{
ThrowIfDisposed();
return _socket.BeginSendTo(buffer, offset, size, SocketFlags.None, endPoint, callback, state);
}
public int EndSendTo(IAsyncResult result)
{
ThrowIfDisposed();
return _socket.EndSendTo(result);
}
private void ThrowIfDisposed()
{
if (_disposed)
{
throw new ObjectDisposedException(nameof(UdpSocket));
}
}
/// <inheritdoc />
public void Dispose()
{
if (_disposed)
{
return;
}
_socket?.Dispose();
_receiveSocketAsyncEventArgs.Dispose();
_sendSocketAsyncEventArgs.Dispose();
_currentReceiveTaskCompletionSource?.TrySetCanceled();
_currentSendTaskCompletionSource?.TrySetCanceled();
_socket = null;
_currentReceiveTaskCompletionSource = null;
_currentSendTaskCompletionSource = null;
_disposed = true;
}
}
}

View File

@ -37,18 +37,20 @@ namespace Emby.Server.Implementations.Udp
/// <param name="logger">The logger.</param>
/// <param name="appHost">The application host.</param>
/// <param name="configuration">The configuration manager.</param>
/// <param name="bindAddress"> The bind address.</param>
/// <param name="port">The port.</param>
public UdpServer(
ILogger logger,
IServerApplicationHost appHost,
IConfiguration configuration,
IPAddress bindAddress,
int port)
{
_logger = logger;
_appHost = appHost;
_config = configuration;
_endpoint = new IPEndPoint(IPAddress.Any, port);
_endpoint = new IPEndPoint(bindAddress, port);
_udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
_udpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

View File

@ -30,7 +30,7 @@ namespace Jellyfin.Api.Auth.AnonymousLanAccessPolicy
/// <inheritdoc />
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, AnonymousLanAccessRequirement requirement)
{
var ip = _httpContextAccessor.HttpContext?.GetNormalizedRemoteIp();
var ip = _httpContextAccessor.HttpContext?.GetNormalizedRemoteIP();
// Loopback will be on LAN, so we can accept null.
if (ip is null || _networkManager.IsInLocalNetwork(ip))

View File

@ -54,7 +54,7 @@ namespace Jellyfin.Api.Auth.DefaultAuthorizationPolicy
}
var isInLocalNetwork = _httpContextAccessor.HttpContext is not null
&& _networkManager.IsInLocalNetwork(_httpContextAccessor.HttpContext.GetNormalizedRemoteIp());
&& _networkManager.IsInLocalNetwork(_httpContextAccessor.HttpContext.GetNormalizedRemoteIP());
var user = _userManager.GetUserById(userId);
if (user is null)
{

View File

@ -31,7 +31,7 @@ namespace Jellyfin.Api.Auth.LocalAccessOrRequiresElevationPolicy
/// <inheritdoc />
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, LocalAccessOrRequiresElevationRequirement requirement)
{
var ip = _httpContextAccessor.HttpContext?.GetNormalizedRemoteIp();
var ip = _httpContextAccessor.HttpContext?.GetNormalizedRemoteIP();
// Loopback will be on LAN, so we can accept null.
if (ip is null || _networkManager.IsInLocalNetwork(ip))

View File

@ -184,7 +184,7 @@ public class MediaInfoController : BaseJellyfinApiController
enableTranscoding.Value,
allowVideoStreamCopy.Value,
allowAudioStreamCopy.Value,
Request.HttpContext.GetNormalizedRemoteIp());
Request.HttpContext.GetNormalizedRemoteIP());
}
_mediaInfoHelper.SortMediaSources(info, maxStreamingBitrate);

View File

@ -189,7 +189,7 @@ public class SystemController : BaseJellyfinApiController
return new EndPointInfo
{
IsLocal = HttpContext.IsLocal(),
IsInNetwork = _network.IsInLocalNetwork(HttpContext.GetNormalizedRemoteIp())
IsInNetwork = _network.IsInLocalNetwork(HttpContext.GetNormalizedRemoteIP())
};
}

View File

@ -138,7 +138,7 @@ public class UniversalAudioController : BaseJellyfinApiController
true,
true,
true,
Request.HttpContext.GetNormalizedRemoteIp());
Request.HttpContext.GetNormalizedRemoteIP());
}
_mediaInfoHelper.SortMediaSources(info, maxStreamingBitrate);

View File

@ -134,7 +134,7 @@ public class UserController : BaseJellyfinApiController
return NotFound("User not found");
}
var result = _userManager.GetUserDto(user, HttpContext.GetNormalizedRemoteIp().ToString());
var result = _userManager.GetUserDto(user, HttpContext.GetNormalizedRemoteIP().ToString());
return result;
}
@ -217,7 +217,7 @@ public class UserController : BaseJellyfinApiController
DeviceId = auth.DeviceId,
DeviceName = auth.Device,
Password = request.Pw,
RemoteEndPoint = HttpContext.GetNormalizedRemoteIp().ToString(),
RemoteEndPoint = HttpContext.GetNormalizedRemoteIP().ToString(),
Username = request.Username
}).ConfigureAwait(false);
@ -226,7 +226,7 @@ public class UserController : BaseJellyfinApiController
catch (SecurityException e)
{
// rethrow adding IP address to message
throw new SecurityException($"[{HttpContext.GetNormalizedRemoteIp()}] {e.Message}", e);
throw new SecurityException($"[{HttpContext.GetNormalizedRemoteIP()}] {e.Message}", e);
}
}
@ -248,7 +248,7 @@ public class UserController : BaseJellyfinApiController
catch (SecurityException e)
{
// rethrow adding IP address to message
throw new SecurityException($"[{HttpContext.GetNormalizedRemoteIp()}] {e.Message}", e);
throw new SecurityException($"[{HttpContext.GetNormalizedRemoteIP()}] {e.Message}", e);
}
}
@ -294,7 +294,7 @@ public class UserController : BaseJellyfinApiController
user.Username,
request.CurrentPw ?? string.Empty,
request.CurrentPw ?? string.Empty,
HttpContext.GetNormalizedRemoteIp().ToString(),
HttpContext.GetNormalizedRemoteIP().ToString(),
false).ConfigureAwait(false);
if (success is null)
@ -475,7 +475,7 @@ public class UserController : BaseJellyfinApiController
await _userManager.ChangePassword(newUser, request.Password).ConfigureAwait(false);
}
var result = _userManager.GetUserDto(newUser, HttpContext.GetNormalizedRemoteIp().ToString());
var result = _userManager.GetUserDto(newUser, HttpContext.GetNormalizedRemoteIP().ToString());
return result;
}
@ -490,7 +490,7 @@ public class UserController : BaseJellyfinApiController
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<ActionResult<ForgotPasswordResult>> ForgotPassword([FromBody, Required] ForgotPasswordDto forgotPasswordRequest)
{
var ip = HttpContext.GetNormalizedRemoteIp();
var ip = HttpContext.GetNormalizedRemoteIP();
var isLocal = HttpContext.IsLocal()
|| _networkManager.IsInLocalNetwork(ip);
@ -571,7 +571,7 @@ public class UserController : BaseJellyfinApiController
if (filterByNetwork)
{
if (!_networkManager.IsInLocalNetwork(HttpContext.GetNormalizedRemoteIp()))
if (!_networkManager.IsInLocalNetwork(HttpContext.GetNormalizedRemoteIP()))
{
users = users.Where(i => i.HasPermission(PermissionKind.EnableRemoteAccess));
}
@ -579,7 +579,7 @@ public class UserController : BaseJellyfinApiController
var result = users
.OrderBy(u => u.Username)
.Select(i => _userManager.GetUserDto(i, HttpContext.GetNormalizedRemoteIp().ToString()));
.Select(i => _userManager.GetUserDto(i, HttpContext.GetNormalizedRemoteIP().ToString()));
return result;
}

View File

@ -284,7 +284,7 @@ public class DynamicHlsHelper
}
}
if (EnableAdaptiveBitrateStreaming(state, isLiveStream, enableAdaptiveBitrateStreaming, _httpContextAccessor.HttpContext.GetNormalizedRemoteIp()))
if (EnableAdaptiveBitrateStreaming(state, isLiveStream, enableAdaptiveBitrateStreaming, _httpContextAccessor.HttpContext.GetNormalizedRemoteIP()))
{
var requestedVideoBitrate = state.VideoRequest is null ? 0 : state.VideoRequest.VideoBitRate ?? 0;

View File

@ -421,7 +421,7 @@ public class MediaInfoHelper
true,
true,
true,
httpContext.GetNormalizedRemoteIp());
httpContext.GetNormalizedRemoteIP());
}
else
{
@ -487,7 +487,7 @@ public class MediaInfoHelper
{
var isInLocalNetwork = _networkManager.IsInLocalNetwork(ipAddress);
_logger.LogInformation("RemoteClientBitrateLimit: {0}, RemoteIp: {1}, IsInLocalNetwork: {2}", remoteClientMaxBitrate, ipAddress, isInLocalNetwork);
_logger.LogInformation("RemoteClientBitrateLimit: {0}, RemoteIP: {1}, IsInLocalNetwork: {2}", remoteClientMaxBitrate, ipAddress, isInLocalNetwork);
if (!isInLocalNetwork)
{
maxBitrate = Math.Min(maxBitrate ?? remoteClientMaxBitrate, remoteClientMaxBitrate);

View File

@ -125,7 +125,7 @@ public static class RequestHelpers
httpContext.User.GetVersion(),
httpContext.User.GetDeviceId(),
httpContext.User.GetDevice(),
httpContext.GetNormalizedRemoteIp().ToString(),
httpContext.GetNormalizedRemoteIP().ToString(),
user).ConfigureAwait(false);
if (session is null)

View File

@ -9,15 +9,15 @@ namespace Jellyfin.Api.Middleware;
/// <summary>
/// Validates the IP of requests coming from local networks wrt. remote access.
/// </summary>
public class IpBasedAccessValidationMiddleware
public class IPBasedAccessValidationMiddleware
{
private readonly RequestDelegate _next;
/// <summary>
/// Initializes a new instance of the <see cref="IpBasedAccessValidationMiddleware"/> class.
/// Initializes a new instance of the <see cref="IPBasedAccessValidationMiddleware"/> class.
/// </summary>
/// <param name="next">The next delegate in the pipeline.</param>
public IpBasedAccessValidationMiddleware(RequestDelegate next)
public IPBasedAccessValidationMiddleware(RequestDelegate next)
{
_next = next;
}
@ -37,9 +37,9 @@ public class IpBasedAccessValidationMiddleware
return;
}
var remoteIp = httpContext.Connection.RemoteIpAddress ?? IPAddress.Loopback;
var remoteIP = httpContext.Connection.RemoteIpAddress ?? IPAddress.Loopback;
if (!networkManager.HasRemoteAccess(remoteIp))
if (!networkManager.HasRemoteAccess(remoteIP))
{
return;
}

View File

@ -38,7 +38,7 @@ public class LanFilteringMiddleware
return;
}
var host = httpContext.GetNormalizedRemoteIp();
var host = httpContext.GetNormalizedRemoteIP();
if (!networkManager.IsInLocalNetwork(host))
{
return;

View File

@ -51,9 +51,9 @@ public class ResponseTimeMiddleware
if (enableWarning && responseTimeMs > warningThreshold && _logger.IsEnabled(LogLevel.Debug))
{
_logger.LogDebug(
"Slow HTTP Response from {Url} to {RemoteIp} in {Elapsed:g} with Status Code {StatusCode}",
"Slow HTTP Response from {Url} to {RemoteIP} in {Elapsed:g} with Status Code {StatusCode}",
context.Request.GetDisplayUrl(),
context.GetNormalizedRemoteIp(),
context.GetNormalizedRemoteIP(),
responseTime,
context.Response.StatusCode);
}

View File

@ -10,32 +10,17 @@ namespace Jellyfin.Networking.Configuration
public class NetworkConfiguration
{
/// <summary>
/// The default value for <see cref="HttpServerPortNumber"/>.
/// The default value for <see cref="InternalHttpPort"/>.
/// </summary>
public const int DefaultHttpPort = 8096;
/// <summary>
/// The default value for <see cref="PublicHttpsPort"/> and <see cref="HttpsPortNumber"/>.
/// The default value for <see cref="PublicHttpsPort"/> and <see cref="InternalHttpsPort"/>.
/// </summary>
public const int DefaultHttpsPort = 8920;
private string _baseUrl = string.Empty;
/// <summary>
/// Gets or sets a value indicating whether the server should force connections over HTTPS.
/// </summary>
public bool RequireHttps { get; set; }
/// <summary>
/// Gets or sets the filesystem path of an X.509 certificate to use for SSL.
/// </summary>
public string CertificatePath { get; set; } = string.Empty;
/// <summary>
/// Gets or sets the password required to access the X.509 certificate data in the file specified by <see cref="CertificatePath"/>.
/// </summary>
public string CertificatePassword { get; set; } = string.Empty;
/// <summary>
/// Gets or sets a value used to specify the URL prefix that your Jellyfin instance can be accessed at.
/// </summary>
@ -69,24 +54,6 @@ namespace Jellyfin.Networking.Configuration
}
}
/// <summary>
/// Gets or sets the public HTTPS port.
/// </summary>
/// <value>The public HTTPS port.</value>
public int PublicHttpsPort { get; set; } = DefaultHttpsPort;
/// <summary>
/// Gets or sets the HTTP server port number.
/// </summary>
/// <value>The HTTP server port number.</value>
public int HttpServerPortNumber { get; set; } = DefaultHttpPort;
/// <summary>
/// Gets or sets the HTTPS server port number.
/// </summary>
/// <value>The HTTPS server port number.</value>
public int HttpsPortNumber { get; set; } = DefaultHttpsPort;
/// <summary>
/// Gets or sets a value indicating whether to use HTTPS.
/// </summary>
@ -97,118 +64,66 @@ namespace Jellyfin.Networking.Configuration
public bool EnableHttps { get; set; }
/// <summary>
/// Gets or sets the public mapped port.
/// Gets or sets a value indicating whether the server should force connections over HTTPS.
/// </summary>
/// <value>The public mapped port.</value>
public int PublicPort { get; set; } = DefaultHttpPort;
public bool RequireHttps { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the http port should be mapped as part of UPnP automatic port forwarding.
/// Gets or sets the filesystem path of an X.509 certificate to use for SSL.
/// </summary>
public bool UPnPCreateHttpPortMap { get; set; }
public string CertificatePath { get; set; } = string.Empty;
/// <summary>
/// Gets or sets the UDPPortRange.
/// Gets or sets the password required to access the X.509 certificate data in the file specified by <see cref="CertificatePath"/>.
/// </summary>
public string UDPPortRange { get; set; } = string.Empty;
public string CertificatePassword { get; set; } = string.Empty;
/// <summary>
/// Gets or sets a value indicating whether gets or sets IPV6 capability.
/// Gets or sets the internal HTTP server port.
/// </summary>
public bool EnableIPV6 { get; set; }
/// <value>The HTTP server port.</value>
public int InternalHttpPort { get; set; } = DefaultHttpPort;
/// <summary>
/// Gets or sets a value indicating whether gets or sets IPV4 capability.
/// Gets or sets the internal HTTPS server port.
/// </summary>
public bool EnableIPV4 { get; set; } = true;
/// <value>The HTTPS server port.</value>
public int InternalHttpsPort { get; set; } = DefaultHttpsPort;
/// <summary>
/// Gets or sets a value indicating whether detailed SSDP logs are sent to the console/log.
/// "Emby.Dlna": "Debug" must be set in logging.default.json for this property to have any effect.
/// Gets or sets the public HTTP port.
/// </summary>
public bool EnableSSDPTracing { get; set; }
/// <value>The public HTTP port.</value>
public int PublicHttpPort { get; set; } = DefaultHttpPort;
/// <summary>
/// Gets or sets the SSDPTracingFilter
/// Gets or sets a value indicating whether an IP address is to be used to filter the detailed ssdp logs that are being sent to the console/log.
/// If the setting "Emby.Dlna": "Debug" msut be set in logging.default.json for this property to work.
/// Gets or sets the public HTTPS port.
/// </summary>
public string SSDPTracingFilter { get; set; } = string.Empty;
/// <summary>
/// Gets or sets the number of times SSDP UDP messages are sent.
/// </summary>
public int UDPSendCount { get; set; } = 2;
/// <summary>
/// Gets or sets the delay between each groups of SSDP messages (in ms).
/// </summary>
public int UDPSendDelay { get; set; } = 100;
/// <summary>
/// Gets or sets a value indicating whether address names that match <see cref="VirtualInterfaceNames"/> should be Ignore for the purposes of binding.
/// </summary>
public bool IgnoreVirtualInterfaces { get; set; } = true;
/// <summary>
/// Gets or sets a value indicating the interfaces that should be ignored. The list can be comma separated. <seealso cref="IgnoreVirtualInterfaces"/>.
/// </summary>
public string VirtualInterfaceNames { get; set; } = "vEthernet*";
/// <summary>
/// Gets or sets the time (in seconds) between the pings of SSDP gateway monitor.
/// </summary>
public int GatewayMonitorPeriod { get; set; } = 60;
/// <summary>
/// Gets a value indicating whether multi-socket binding is available.
/// </summary>
public bool EnableMultiSocketBinding { get; } = true;
/// <summary>
/// Gets or sets a value indicating whether all IPv6 interfaces should be treated as on the internal network.
/// Depending on the address range implemented ULA ranges might not be used.
/// </summary>
public bool TrustAllIP6Interfaces { get; set; }
/// <summary>
/// Gets or sets the ports that HDHomerun uses.
/// </summary>
public string HDHomerunPortRange { get; set; } = string.Empty;
/// <summary>
/// Gets or sets the PublishedServerUriBySubnet
/// Gets or sets PublishedServerUri to advertise for specific subnets.
/// </summary>
public string[] PublishedServerUriBySubnet { get; set; } = Array.Empty<string>();
/// <summary>
/// Gets or sets a value indicating whether Autodiscovery tracing is enabled.
/// </summary>
public bool AutoDiscoveryTracing { get; set; }
/// <value>The public HTTPS port.</value>
public int PublicHttpsPort { get; set; } = DefaultHttpsPort;
/// <summary>
/// Gets or sets a value indicating whether Autodiscovery is enabled.
/// </summary>
public bool AutoDiscovery { get; set; } = true;
/// <summary>
/// Gets or sets the filter for remote IP connectivity. Used in conjunction with <seealso cref="IsRemoteIPFilterBlacklist"/>.
/// </summary>
public string[] RemoteIPFilter { get; set; } = Array.Empty<string>();
/// <summary>
/// Gets or sets a value indicating whether <seealso cref="RemoteIPFilter"/> contains a blacklist or a whitelist. Default is a whitelist.
/// </summary>
public bool IsRemoteIPFilterBlacklist { get; set; }
/// <summary>
/// Gets or sets a value indicating whether to enable automatic port forwarding.
/// </summary>
public bool EnableUPnP { get; set; }
/// <summary>
/// Gets or sets a value indicating whether access outside of the LAN is permitted.
/// Gets or sets a value indicating whether IPv6 is enabled.
/// </summary>
public bool EnableIPv4 { get; set; } = true;
/// <summary>
/// Gets or sets a value indicating whether IPv6 is enabled.
/// </summary>
public bool EnableIPv6 { get; set; }
/// <summary>
/// Gets or sets a value indicating whether access from outside of the LAN is permitted.
/// </summary>
public bool EnableRemoteAccess { get; set; } = true;
@ -223,13 +138,39 @@ namespace Jellyfin.Networking.Configuration
public string[] LocalNetworkAddresses { get; set; } = Array.Empty<string>();
/// <summary>
/// Gets or sets the known proxies. If the proxy is a network, it's added to the KnownNetworks.
/// Gets or sets the known proxies.
/// </summary>
public string[] KnownProxies { get; set; } = Array.Empty<string>();
/// <summary>
/// Gets or sets a value indicating whether address names that match <see cref="VirtualInterfaceNames"/> should be ignored for the purposes of binding.
/// </summary>
public bool IgnoreVirtualInterfaces { get; set; } = true;
/// <summary>
/// Gets or sets a value indicating the interface name prefixes that should be ignored. The list can be comma separated and values are case-insensitive. <seealso cref="IgnoreVirtualInterfaces"/>.
/// </summary>
public string[] VirtualInterfaceNames { get; set; } = new string[] { "veth" };
/// <summary>
/// Gets or sets a value indicating whether the published server uri is based on information in HTTP requests.
/// </summary>
public bool EnablePublishedServerUriByRequest { get; set; } = false;
/// <summary>
/// Gets or sets the PublishedServerUriBySubnet
/// Gets or sets PublishedServerUri to advertise for specific subnets.
/// </summary>
public string[] PublishedServerUriBySubnet { get; set; } = Array.Empty<string>();
/// <summary>
/// Gets or sets the filter for remote IP connectivity. Used in conjuntion with <seealso cref="IsRemoteIPFilterBlacklist"/>.
/// </summary>
public string[] RemoteIPFilter { get; set; } = Array.Empty<string>();
/// <summary>
/// Gets or sets a value indicating whether <seealso cref="RemoteIPFilter"/> contains a blacklist or a whitelist. Default is a whitelist.
/// </summary>
public bool IsRemoteIPFilterBlacklist { get; set; }
}
}

View File

@ -14,7 +14,7 @@ namespace Jellyfin.Networking.Configuration
/// <returns>The <see cref="NetworkConfiguration"/>.</returns>
public static NetworkConfiguration GetNetworkConfiguration(this IConfigurationManager config)
{
return config.GetConfiguration<NetworkConfiguration>("network");
return config.GetConfiguration<NetworkConfiguration>(NetworkConfigurationStore.StoreKey);
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -63,9 +63,9 @@ namespace Jellyfin.Server.Extensions
/// </summary>
/// <param name="appBuilder">The application builder.</param>
/// <returns>The updated application builder.</returns>
public static IApplicationBuilder UseIpBasedAccessValidation(this IApplicationBuilder appBuilder)
public static IApplicationBuilder UseIPBasedAccessValidation(this IApplicationBuilder appBuilder)
{
return appBuilder.UseMiddleware<IpBasedAccessValidationMiddleware>();
return appBuilder.UseMiddleware<IPBasedAccessValidationMiddleware>();
}
/// <summary>

View File

@ -99,7 +99,7 @@ namespace Jellyfin.Server.Extensions
}
/// <summary>
/// Extension method for adding the jellyfin API to the service collection.
/// Extension method for adding the Jellyfin API to the service collection.
/// </summary>
/// <param name="serviceCollection">The service collection.</param>
/// <param name="pluginAssemblies">An IEnumerable containing all plugin assemblies with API controllers.</param>
@ -260,7 +260,7 @@ namespace Jellyfin.Server.Extensions
}
/// <summary>
/// Sets up the proxy configuration based on the addresses in <paramref name="allowedProxies"/>.
/// Sets up the proxy configuration based on the addresses/subnets in <paramref name="allowedProxies"/>.
/// </summary>
/// <param name="config">The <see cref="NetworkConfiguration"/> containing the config settings.</param>
/// <param name="allowedProxies">The string array to parse.</param>
@ -269,33 +269,37 @@ namespace Jellyfin.Server.Extensions
{
for (var i = 0; i < allowedProxies.Length; i++)
{
if (IPNetAddress.TryParse(allowedProxies[i], out var addr))
if (IPAddress.TryParse(allowedProxies[i], out var addr))
{
AddIpAddress(config, options, addr.Address, addr.PrefixLength);
AddIPAddress(config, options, addr, addr.AddressFamily == AddressFamily.InterNetwork ? 32 : 128);
}
else if (IPHost.TryParse(allowedProxies[i], out var host))
else if (NetworkExtensions.TryParseToSubnet(allowedProxies[i], out var subnet))
{
foreach (var address in host.GetAddresses())
if (subnet != null)
{
AddIpAddress(config, options, address, address.AddressFamily == AddressFamily.InterNetwork ? 32 : 128);
AddIPAddress(config, options, subnet.Prefix, subnet.PrefixLength);
}
}
else if (NetworkExtensions.TryParseHost(allowedProxies[i], out var addresses))
{
foreach (var address in addresses)
{
AddIPAddress(config, options, address, address.AddressFamily == AddressFamily.InterNetwork ? 32 : 128);
}
}
}
}
private static void AddIpAddress(NetworkConfiguration config, ForwardedHeadersOptions options, IPAddress addr, int prefixLength)
private static void AddIPAddress(NetworkConfiguration config, ForwardedHeadersOptions options, IPAddress addr, int prefixLength)
{
if ((!config.EnableIPV4 && addr.AddressFamily == AddressFamily.InterNetwork) || (!config.EnableIPV6 && addr.AddressFamily == AddressFamily.InterNetworkV6))
if ((!config.EnableIPv4 && addr.AddressFamily == AddressFamily.InterNetwork) || (!config.EnableIPv6 && addr.AddressFamily == AddressFamily.InterNetworkV6))
{
return;
}
// In order for dual-mode sockets to be used, IP6 has to be enabled in JF and an interface has to have an IP6 address.
if (addr.AddressFamily == AddressFamily.InterNetwork && config.EnableIPV6)
if (addr.IsIPv4MappedToIPv6)
{
// If the server is using dual-mode sockets, IPv4 addresses are supplied in an IPv6 format.
// https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/proxy-load-balancer?view=aspnetcore-5.0 .
addr = addr.MapToIPv6();
addr = addr.MapToIPv4();
}
if (prefixLength == 32)

View File

@ -39,9 +39,9 @@ public static class WebHostBuilderExtensions
var addresses = appHost.NetManager.GetAllBindInterfaces();
bool flagged = false;
foreach (IPObject netAdd in addresses)
foreach (var netAdd in addresses)
{
logger.LogInformation("Kestrel listening on {Address}", IPAddress.IPv6Any.Equals(netAdd.Address) ? "All Addresses" : netAdd);
logger.LogInformation("Kestrel is listening on {Address}", IPAddress.IPv6Any.Equals(netAdd.Address) ? "All IPv6 addresses" : netAdd.Address);
options.Listen(netAdd.Address, appHost.HttpPort);
if (appHost.ListenWithHttps)
{

View File

@ -22,7 +22,8 @@ namespace Jellyfin.Server.Migrations
private static readonly Type[] _preStartupMigrationTypes =
{
typeof(PreStartupRoutines.CreateNetworkConfiguration),
typeof(PreStartupRoutines.MigrateMusicBrainzTimeout)
typeof(PreStartupRoutines.MigrateMusicBrainzTimeout),
typeof(PreStartupRoutines.MigrateNetworkConfiguration)
};
/// <summary>

View File

@ -114,9 +114,7 @@ public class CreateNetworkConfiguration : IMigrationRoutine
public bool IgnoreVirtualInterfaces { get; set; } = true;
public string VirtualInterfaceNames { get; set; } = "vEthernet*";
public bool TrustAllIP6Interfaces { get; set; }
public string[] VirtualInterfaceNames { get; set; } = new string[] { "veth" };
public string[] PublishedServerUriBySubnet { get; set; } = Array.Empty<string>();

View File

@ -0,0 +1,195 @@
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using Emby.Server.Implementations;
using Jellyfin.Networking.Configuration;
using Microsoft.Extensions.Logging;
namespace Jellyfin.Server.Migrations.PreStartupRoutines;
/// <inheritdoc />
public class MigrateNetworkConfiguration : IMigrationRoutine
{
private readonly ServerApplicationPaths _applicationPaths;
private readonly ILogger<MigrateNetworkConfiguration> _logger;
/// <summary>
/// Initializes a new instance of the <see cref="MigrateNetworkConfiguration"/> class.
/// </summary>
/// <param name="applicationPaths">An instance of <see cref="ServerApplicationPaths"/>.</param>
/// <param name="loggerFactory">An instance of the <see cref="ILoggerFactory"/> interface.</param>
public MigrateNetworkConfiguration(ServerApplicationPaths applicationPaths, ILoggerFactory loggerFactory)
{
_applicationPaths = applicationPaths;
_logger = loggerFactory.CreateLogger<MigrateNetworkConfiguration>();
}
/// <inheritdoc />
public Guid Id => Guid.Parse("4FB5C950-1991-11EE-9B4B-0800200C9A66");
/// <inheritdoc />
public string Name => nameof(MigrateNetworkConfiguration);
/// <inheritdoc />
public bool PerformOnNewInstall => false;
/// <inheritdoc />
public void Perform()
{
string path = Path.Combine(_applicationPaths.ConfigurationDirectoryPath, "network.xml");
var oldNetworkConfigSerializer = new XmlSerializer(typeof(OldNetworkConfiguration), new XmlRootAttribute("NetworkConfiguration"));
using var xmlReader = XmlReader.Create(path);
var oldNetworkConfiguration = (OldNetworkConfiguration?)oldNetworkConfigSerializer.Deserialize(xmlReader);
if (oldNetworkConfiguration is not null)
{
// Migrate network config values to new config schema
var networkConfiguration = new NetworkConfiguration();
networkConfiguration.AutoDiscovery = oldNetworkConfiguration.AutoDiscovery;
networkConfiguration.BaseUrl = oldNetworkConfiguration.BaseUrl;
networkConfiguration.CertificatePassword = oldNetworkConfiguration.CertificatePassword;
networkConfiguration.CertificatePath = oldNetworkConfiguration.CertificatePath;
networkConfiguration.EnableHttps = oldNetworkConfiguration.EnableHttps;
networkConfiguration.EnableIPv4 = oldNetworkConfiguration.EnableIPV4;
networkConfiguration.EnableIPv6 = oldNetworkConfiguration.EnableIPV6;
networkConfiguration.EnablePublishedServerUriByRequest = oldNetworkConfiguration.EnablePublishedServerUriByRequest;
networkConfiguration.EnableRemoteAccess = oldNetworkConfiguration.EnableRemoteAccess;
networkConfiguration.EnableUPnP = oldNetworkConfiguration.EnableUPnP;
networkConfiguration.IgnoreVirtualInterfaces = oldNetworkConfiguration.IgnoreVirtualInterfaces;
networkConfiguration.InternalHttpPort = oldNetworkConfiguration.HttpServerPortNumber;
networkConfiguration.InternalHttpsPort = oldNetworkConfiguration.HttpsPortNumber;
networkConfiguration.IsRemoteIPFilterBlacklist = oldNetworkConfiguration.IsRemoteIPFilterBlacklist;
networkConfiguration.KnownProxies = oldNetworkConfiguration.KnownProxies;
networkConfiguration.LocalNetworkAddresses = oldNetworkConfiguration.LocalNetworkAddresses;
networkConfiguration.LocalNetworkSubnets = oldNetworkConfiguration.LocalNetworkSubnets;
networkConfiguration.PublicHttpPort = oldNetworkConfiguration.PublicPort;
networkConfiguration.PublicHttpsPort = oldNetworkConfiguration.PublicHttpsPort;
networkConfiguration.PublishedServerUriBySubnet = oldNetworkConfiguration.PublishedServerUriBySubnet;
networkConfiguration.RemoteIPFilter = oldNetworkConfiguration.RemoteIPFilter;
networkConfiguration.RequireHttps = oldNetworkConfiguration.RequireHttps;
// Migrate old virtual interface name schema
var oldVirtualInterfaceNames = oldNetworkConfiguration.VirtualInterfaceNames;
if (oldVirtualInterfaceNames.Equals("vEthernet*", StringComparison.OrdinalIgnoreCase))
{
networkConfiguration.VirtualInterfaceNames = new string[] { "veth" };
}
else
{
networkConfiguration.VirtualInterfaceNames = oldVirtualInterfaceNames.Replace("*", string.Empty, StringComparison.OrdinalIgnoreCase).Split(',');
}
var networkConfigSerializer = new XmlSerializer(typeof(NetworkConfiguration));
var xmlWriterSettings = new XmlWriterSettings { Indent = true };
using var xmlWriter = XmlWriter.Create(path, xmlWriterSettings);
networkConfigSerializer.Serialize(xmlWriter, networkConfiguration);
}
}
#pragma warning disable
public sealed class OldNetworkConfiguration
{
public const int DefaultHttpPort = 8096;
public const int DefaultHttpsPort = 8920;
private string _baseUrl = string.Empty;
public bool RequireHttps { get; set; }
public string CertificatePath { get; set; } = string.Empty;
public string CertificatePassword { get; set; } = string.Empty;
public string BaseUrl
{
get => _baseUrl;
set
{
// Normalize the start of the string
if (string.IsNullOrWhiteSpace(value))
{
// If baseUrl is empty, set an empty prefix string
_baseUrl = string.Empty;
return;
}
if (value[0] != '/')
{
// If baseUrl was not configured with a leading slash, append one for consistency
value = "/" + value;
}
// Normalize the end of the string
if (value[^1] == '/')
{
// If baseUrl was configured with a trailing slash, remove it for consistency
value = value.Remove(value.Length - 1);
}
_baseUrl = value;
}
}
public int PublicHttpsPort { get; set; } = DefaultHttpsPort;
public int HttpServerPortNumber { get; set; } = DefaultHttpPort;
public int HttpsPortNumber { get; set; } = DefaultHttpsPort;
public bool EnableHttps { get; set; }
public int PublicPort { get; set; } = DefaultHttpPort;
public bool UPnPCreateHttpPortMap { get; set; }
public string UDPPortRange { get; set; } = string.Empty;
public bool EnableIPV6 { get; set; }
public bool EnableIPV4 { get; set; } = true;
public bool EnableSSDPTracing { get; set; }
public string SSDPTracingFilter { get; set; } = string.Empty;
public int UDPSendCount { get; set; } = 2;
public int UDPSendDelay { get; set; } = 100;
public bool IgnoreVirtualInterfaces { get; set; } = true;
public string VirtualInterfaceNames { get; set; } = "vEthernet*";
public int GatewayMonitorPeriod { get; set; } = 60;
public bool EnableMultiSocketBinding { get; } = true;
public bool TrustAllIP6Interfaces { get; set; }
public string HDHomerunPortRange { get; set; } = string.Empty;
public string[] PublishedServerUriBySubnet { get; set; } = Array.Empty<string>();
public bool AutoDiscoveryTracing { get; set; }
public bool AutoDiscovery { get; set; } = true;
public string[] RemoteIPFilter { get; set; } = Array.Empty<string>();
public bool IsRemoteIPFilterBlacklist { get; set; }
public bool EnableUPnP { get; set; }
public bool EnableRemoteAccess { get; set; } = true;
public string[] LocalNetworkSubnets { get; set; } = Array.Empty<string>();
public string[] LocalNetworkAddresses { get; set; } = Array.Empty<string>();
public string[] KnownProxies { get; set; } = Array.Empty<string>();
public bool EnablePublishedServerUriByRequest { get; set; } = false;
}
#pragma warning restore
}

View File

@ -213,7 +213,7 @@ namespace Jellyfin.Server
mainApp.UseAuthorization();
mainApp.UseLanFiltering();
mainApp.UseIpBasedAccessValidation();
mainApp.UseIPBasedAccessValidation();
mainApp.UseWebSocketHandler();
mainApp.UseServerStartupMessage();

View File

@ -25,7 +25,7 @@ namespace MediaBrowser.Common.Extensions
/// </summary>
/// <param name="context">The HTTP context.</param>
/// <returns>The remote caller IP address.</returns>
public static IPAddress GetNormalizedRemoteIp(this HttpContext context)
public static IPAddress GetNormalizedRemoteIP(this HttpContext context)
{
// Default to the loopback address if no RemoteIpAddress is specified (i.e. during integration tests)
var ip = context.Connection.RemoteIpAddress ?? IPAddress.Loopback;

View File

@ -1,8 +1,9 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Net.NetworkInformation;
using MediaBrowser.Model.Net;
using Microsoft.AspNetCore.Http;
namespace MediaBrowser.Common.Net
@ -18,47 +19,32 @@ namespace MediaBrowser.Common.Net
event EventHandler NetworkChanged;
/// <summary>
/// Gets the published server urls list.
/// Gets a value indicating whether IPv4 is enabled.
/// </summary>
Dictionary<IPNetAddress, string> PublishedServerUrls { get; }
bool IsIPv4Enabled { get; }
/// <summary>
/// Gets a value indicating whether is all IPv6 interfaces are trusted as internal.
/// Gets a value indicating whether IPv6 is enabled.
/// </summary>
bool TrustAllIP6Interfaces { get; }
/// <summary>
/// Gets the remote address filter.
/// </summary>
Collection<IPObject> RemoteAddressFilter { get; }
/// <summary>
/// Gets or sets a value indicating whether iP6 is enabled.
/// </summary>
bool IsIP6Enabled { get; set; }
/// <summary>
/// Gets or sets a value indicating whether iP4 is enabled.
/// </summary>
bool IsIP4Enabled { get; set; }
bool IsIPv6Enabled { get; }
/// <summary>
/// Calculates the list of interfaces to use for Kestrel.
/// </summary>
/// <returns>A Collection{IPObject} object containing all the interfaces to bind.
/// <returns>A IReadOnlyList{IPData} object containing all the interfaces to bind.
/// If all the interfaces are specified, and none are excluded, it returns zero items
/// to represent any address.</returns>
/// <param name="individualInterfaces">When false, return <see cref="IPAddress.Any"/> or <see cref="IPAddress.IPv6Any"/> for all interfaces.</param>
Collection<IPObject> GetAllBindInterfaces(bool individualInterfaces = false);
IReadOnlyList<IPData> GetAllBindInterfaces(bool individualInterfaces = false);
/// <summary>
/// Returns a collection containing the loopback interfaces.
/// Returns a list containing the loopback interfaces.
/// </summary>
/// <returns>Collection{IPObject}.</returns>
Collection<IPObject> GetLoopbacks();
/// <returns>IReadOnlyList{IPData}.</returns>
IReadOnlyList<IPData> GetLoopbacks();
/// <summary>
/// Retrieves the bind address to use in system url's. (Server Discovery, PlayTo, LiveTV, SystemInfo)
/// Retrieves the bind address to use in system URLs. (Server Discovery, PlayTo, LiveTV, SystemInfo)
/// If no bind addresses are specified, an internal interface address is selected.
/// The priority of selection is as follows:-
///
@ -72,90 +58,50 @@ namespace MediaBrowser.Common.Net
///
/// If the source is from a public subnet address range and the user hasn't specified any bind addresses:-
/// The first public interface that isn't a loopback and contains the source subnet.
/// The first public interface that isn't a loopback. Priority is given to interfaces with gateways.
/// An internal interface if there are no public ip addresses.
/// The first public interface that isn't a loopback.
/// The first internal interface that isn't a loopback.
///
/// If the source is from a private subnet address range and the user hasn't specified any bind addresses:-
/// The first private interface that contains the source subnet.
/// The first private interface that isn't a loopback. Priority is given to interfaces with gateways.
/// The first private interface that isn't a loopback.
///
/// If no interfaces meet any of these criteria, then a loopback address is returned.
///
/// Interface that have been specifically excluded from binding are not used in any of the calculations.
/// Interfaces that have been specifically excluded from binding are not used in any of the calculations.
/// </summary>
/// <param name="source">Source of the request.</param>
/// <param name="port">Optional port returned, if it's part of an override.</param>
/// <returns>IP Address to use, or loopback address if all else fails.</returns>
string GetBindInterface(IPObject source, out int? port);
/// <returns>IP address to use, or loopback address if all else fails.</returns>
string GetBindAddress(HttpRequest source, out int? port);
/// <summary>
/// Retrieves the bind address to use in system url's. (Server Discovery, PlayTo, LiveTV, SystemInfo)
/// Retrieves the bind address to use in system URLs. (Server Discovery, PlayTo, LiveTV, SystemInfo)
/// If no bind addresses are specified, an internal interface address is selected.
/// (See <see cref="GetBindInterface(IPObject, out int?)"/>.
/// </summary>
/// <param name="source">Source of the request.</param>
/// <param name="port">Optional port returned, if it's part of an override.</param>
/// <returns>IP Address to use, or loopback address if all else fails.</returns>
string GetBindInterface(HttpRequest source, out int? port);
/// <summary>
/// Retrieves the bind address to use in system url's. (Server Discovery, PlayTo, LiveTV, SystemInfo)
/// If no bind addresses are specified, an internal interface address is selected.
/// (See <see cref="GetBindInterface(IPObject, out int?)"/>.
/// </summary>
/// <param name="source">IP address of the request.</param>
/// <param name="port">Optional port returned, if it's part of an override.</param>
/// <returns>IP Address to use, or loopback address if all else fails.</returns>
string GetBindInterface(IPAddress source, out int? port);
/// <param name="skipOverrides">Optional boolean denoting if published server overrides should be ignored. Defaults to false.</param>
/// <returns>IP address to use, or loopback address if all else fails.</returns>
string GetBindAddress(IPAddress source, out int? port, bool skipOverrides = false);
/// <summary>
/// Retrieves the bind address to use in system url's. (Server Discovery, PlayTo, LiveTV, SystemInfo)
/// Retrieves the bind address to use in system URLs. (Server Discovery, PlayTo, LiveTV, SystemInfo)
/// If no bind addresses are specified, an internal interface address is selected.
/// (See <see cref="GetBindInterface(IPObject, out int?)"/>.
/// (See <see cref="GetBindAddress(IPAddress, out int?, bool)"/>.
/// </summary>
/// <param name="source">Source of the request.</param>
/// <param name="port">Optional port returned, if it's part of an override.</param>
/// <returns>IP Address to use, or loopback address if all else fails.</returns>
string GetBindInterface(string source, out int? port);
/// <summary>
/// Checks to see if the ip address is specifically excluded in LocalNetworkAddresses.
/// </summary>
/// <param name="address">IP address to check.</param>
/// <returns>True if it is.</returns>
bool IsExcludedInterface(IPAddress address);
/// <returns>IP address to use, or loopback address if all else fails.</returns>
string GetBindAddress(string source, out int? port);
/// <summary>
/// Get a list of all the MAC addresses associated with active interfaces.
/// </summary>
/// <returns>List of MAC addresses.</returns>
IReadOnlyCollection<PhysicalAddress> GetMacAddresses();
/// <summary>
/// Checks to see if the IP Address provided matches an interface that has a gateway.
/// </summary>
/// <param name="addressObj">IP to check. Can be an IPAddress or an IPObject.</param>
/// <returns>Result of the check.</returns>
bool IsGatewayInterface(IPObject? addressObj);
/// <summary>
/// Checks to see if the IP Address provided matches an interface that has a gateway.
/// </summary>
/// <param name="addressObj">IP to check. Can be an IPAddress or an IPObject.</param>
/// <returns>Result of the check.</returns>
bool IsGatewayInterface(IPAddress? addressObj);
/// <summary>
/// Returns true if the address is a private address.
/// The configuration option TrustIP6Interfaces overrides this functions behaviour.
/// </summary>
/// <param name="address">Address to check.</param>
/// <returns>True or False.</returns>
bool IsPrivateAddressRange(IPObject address);
IReadOnlyList<PhysicalAddress> GetMacAddresses();
/// <summary>
/// Returns true if the address is part of the user defined LAN.
/// The configuration option TrustIP6Interfaces overrides this functions behaviour.
/// </summary>
/// <param name="address">IP to check.</param>
/// <returns>True if endpoint is within the LAN range.</returns>
@ -163,76 +109,31 @@ namespace MediaBrowser.Common.Net
/// <summary>
/// Returns true if the address is part of the user defined LAN.
/// The configuration option TrustIP6Interfaces overrides this functions behaviour.
/// </summary>
/// <param name="address">IP to check.</param>
/// <returns>True if endpoint is within the LAN range.</returns>
bool IsInLocalNetwork(IPObject address);
/// <summary>
/// Returns true if the address is part of the user defined LAN.
/// The configuration option TrustIP6Interfaces overrides this functions behaviour.
/// </summary>
/// <param name="address">IP to check.</param>
/// <returns>True if endpoint is within the LAN range.</returns>
bool IsInLocalNetwork(IPAddress address);
/// <summary>
/// Attempts to convert the token to an IP address, permitting for interface descriptions and indexes.
/// eg. "eth1", or "TP-LINK Wireless USB Adapter".
/// Attempts to convert the interface name to an IP address.
/// eg. "eth1", or "enp3s5".
/// </summary>
/// <param name="token">Token to parse.</param>
/// <param name="result">Resultant object's ip addresses, if successful.</param>
/// <param name="intf">Interface name.</param>
/// <param name="result">Resulting object's IP addresses, if successful.</param>
/// <returns>Success of the operation.</returns>
bool TryParseInterface(string token, out Collection<IPObject>? result);
bool TryParseInterface(string intf, [NotNullWhen(true)] out IReadOnlyList<IPData>? result);
/// <summary>
/// Parses an array of strings into a Collection{IPObject}.
/// Returns all internal (LAN) bind interface addresses.
/// </summary>
/// <param name="values">Values to parse.</param>
/// <param name="negated">When true, only include values beginning with !. When false, ignore ! values.</param>
/// <returns>IPCollection object containing the value strings.</returns>
Collection<IPObject> CreateIPCollection(string[] values, bool negated = false);
/// <returns>An list of internal (LAN) interfaces addresses.</returns>
IReadOnlyList<IPData> GetInternalBindAddresses();
/// <summary>
/// Returns all the internal Bind interface addresses.
/// Checks if <paramref name="remoteIP"/> has access to the server.
/// </summary>
/// <returns>An internal list of interfaces addresses.</returns>
Collection<IPObject> GetInternalBindAddresses();
/// <summary>
/// Checks to see if an IP address is still a valid interface address.
/// </summary>
/// <param name="address">IP address to check.</param>
/// <returns>True if it is.</returns>
bool IsValidInterfaceAddress(IPAddress address);
/// <summary>
/// Returns true if the IP address is in the excluded list.
/// </summary>
/// <param name="ip">IP to check.</param>
/// <returns>True if excluded.</returns>
bool IsExcluded(IPAddress ip);
/// <summary>
/// Returns true if the IP address is in the excluded list.
/// </summary>
/// <param name="ip">IP to check.</param>
/// <returns>True if excluded.</returns>
bool IsExcluded(EndPoint ip);
/// <summary>
/// Gets the filtered LAN ip addresses.
/// </summary>
/// <param name="filter">Optional filter for the list.</param>
/// <returns>Returns a filtered list of LAN addresses.</returns>
Collection<IPObject> GetFilteredLANSubnets(Collection<IPObject>? filter = null);
/// <summary>
/// Checks to see if <paramref name="remoteIp"/> has access.
/// </summary>
/// <param name="remoteIp">IP Address of client.</param>
/// <returns><b>True</b> if has access, otherwise <b>false</b>.</returns>
bool HasRemoteAccess(IPAddress remoteIp);
/// <param name="remoteIP">IP address of the client.</param>
/// <returns><b>True</b> if it has access, otherwise <b>false</b>.</returns>
bool HasRemoteAccess(IPAddress remoteIP);
}
}

View File

@ -1,441 +0,0 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;
namespace MediaBrowser.Common.Net
{
/// <summary>
/// Object that holds a host name.
/// </summary>
public class IPHost : IPObject
{
/// <summary>
/// Gets or sets timeout value before resolve required, in minutes.
/// </summary>
public const int Timeout = 30;
/// <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 in ticks.
/// </summary>
private DateTime? _lastResolved = null;
/// <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, as a host's address is determined by DNS.
throw new NotImplementedException("The address of a host is determined by DNS.");
}
}
/// <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 => (byte)(ResolveHost() ? 128 : 32);
// Not implemented, as a host object can only have a prefix length of 128 (IPv6) or 32 (IPv4) prefix length,
// which is automatically determined by it's IP type. Anything else is meaningless.
set => throw new NotImplementedException();
}
/// <summary>
/// Gets a value indicating whether the address has a value.
/// </summary>
public bool HasAddress => _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><c>true</c> if the parsing is successful, <c>false</c> if not.</returns>
public static bool TryParse(string host, out IPHost hostObj)
{
if (string.IsNullOrWhiteSpace(host))
{
hostObj = IPHost.None;
return false;
}
// See if it's an IPv6 with port address e.g. [::1] or [::1]:120.
int i = host.IndexOf(']', StringComparison.Ordinal);
if (i != -1)
{
return TryParse(host.Remove(i - 1).TrimStart(' ', '['), out hostObj);
}
if (IPNetAddress.TryParse(host, out var netAddress))
{
// Host name is an ip address, so fake resolve.
hostObj = new IPHost(host, netAddress.Address);
return true;
}
// Is it a host, IPv4/6 with/out port?
string[] hosts = host.Split(':');
if (hosts.Length <= 2)
{
// This is either a hostname: port, or an IP4:port.
host = hosts[0];
if (string.Equals("localhost", host, StringComparison.OrdinalIgnoreCase))
{
hostObj = new IPHost(host);
return true;
}
if (IPAddress.TryParse(host, out var netIP))
{
// Host name is an ip address, so fake resolve.
hostObj = new IPHost(host, netIP);
return true;
}
}
else
{
// Invalid host name, as it cannot contain :
hostObj = new IPHost(string.Empty, IPAddress.None);
return false;
}
// Use regular expression as CheckHostName isn't RFC5892 compliant.
// Modified from gSkinner's expression at https://stackoverflow.com/questions/11809631/fully-qualified-domain-name-validation
string pattern = @"(?im)^(?!:\/\/)(?=.{1,255}$)((.{1,63}\.){0,127}(?![0-9]*$)[a-z0-9-]+\.?)$";
if (Regex.IsMatch(host, pattern))
{
hostObj = new IPHost(host);
return true;
}
hostObj = IPHost.None;
return false;
}
/// <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 (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 (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 is not 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 if (i.AddressFamily == AddressFamily.InterNetwork)
{
output += $"{i}/32,";
}
else
{
output += $"{i}/128,";
}
}
output = output[..^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 (address, prefixLength) = NetworkAddressOf(this[0], PrefixLength);
return new IPNetAddress(address, prefixLength);
}
/// <summary>
/// Attempt to resolve the ip address of a host.
/// </summary>
/// <returns><c>true</c> if any addresses have been resolved, otherwise <c>false</c>.</returns>
private bool ResolveHost()
{
// When was the last time we resolved?
_lastResolved ??= DateTime.UtcNow;
// If we haven't resolved before, or our timer has run out...
if ((_addresses.Length == 0 && !Resolved) || (DateTime.UtcNow > _lastResolved.Value.AddMinutes(Timeout)))
{
_lastResolved = DateTime.UtcNow;
ResolveHostInternal();
Resolved = true;
}
return _addresses.Length > 0;
}
/// <summary>
/// Task that looks up a Host name and returns its IP addresses.
/// </summary>
private void ResolveHostInternal()
{
var hostName = HostName;
if (string.IsNullOrEmpty(hostName))
{
return;
}
// Resolves the host name - so save a DNS lookup.
if (string.Equals(hostName, "localhost", StringComparison.OrdinalIgnoreCase))
{
_addresses = new IPAddress[] { IPAddress.Loopback, IPAddress.IPv6Loopback };
return;
}
if (Uri.CheckHostName(hostName) == UriHostNameType.Dns)
{
try
{
_addresses = Dns.GetHostEntry(hostName).AddressList;
}
catch (SocketException ex)
{
// Log and then ignore socket errors, as the result value will just be an empty array.
Debug.WriteLine("GetHostAddresses failed with {Message}.", ex.Message);
}
}
}
}
}

View File

@ -1,278 +0,0 @@
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 SSDPMulticastIPv4 = IPAddress.Parse("239.255.255.250");
/// <summary>
/// IPv6 local link multicast address.
/// </summary>
public static readonly IPAddress SSDPMulticastIPv6LinkLocal = IPAddress.Parse("ff02::C");
/// <summary>
/// IPv6 site local multicast address.
/// </summary>
public static readonly IPAddress SSDPMulticastIPv6SiteLocal = IPAddress.Parse("ff05::C");
/// <summary>
/// IP4Loopback address host.
/// </summary>
public static readonly IPNetAddress IP4Loopback = IPNetAddress.Parse("127.0.0.1/8");
/// <summary>
/// IP6Loopback address host.
/// </summary>
public static readonly IPNetAddress IP6Loopback = new IPNetAddress(IPAddress.IPv6Loopback);
/// <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)
{
ArgumentNullException.ThrowIfNull(address);
if (address.IsIPv4MappedToIPv6)
{
address = address.MapToIPv4();
}
if (address.AddressFamily != AddressFamily)
{
return false;
}
var (altAddress, altPrefix) = NetworkAddressOf(address, PrefixLength);
return NetworkAddress.Address.Equals(altAddress) && NetworkAddress.PrefixLength >= altPrefix;
}
/// <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).Address;
return NetworkAddress.Address.Equals(altAddress);
}
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 ip)
{
if (ip is not null && !ip.Equals(IPAddress.None) && !Address.Equals(IPAddress.None))
{
return ip.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 excluded 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 (address, prefixLength) = NetworkAddressOf(_address, PrefixLength);
return new IPNetAddress(address, prefixLength);
}
}
}

View File

@ -1,355 +0,0 @@
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>
/// The network address of this object.
/// </summary>
private IPObject? _networkAddress;
/// <summary>
/// Gets or sets a user defined value that is associated with 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 => _networkAddress ??= CalculateNetworkAddress();
/// <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)
{
ArgumentNullException.ThrowIfNull(address);
if (address.IsIPv4MappedToIPv6)
{
address = address.MapToIPv4();
}
if (IPAddress.IsLoopback(address))
{
return (address, prefixLength);
}
// An ip address is just a list of bytes, each one representing a segment on the network.
// This separates the IP address into octets and calculates how many octets will need to be altered or set to zero dependant upon the
// prefix length value. eg. /16 on a 4 octet ip4 address (192.168.2.240) will result in the 2 and the 240 being zeroed out.
// Where there is not an exact boundary (eg /23), mod is used to calculate how many bits of this value are to be kept.
// GetAddressBytes
Span<byte> addressBytes = stackalloc byte[address.AddressFamily == AddressFamily.InterNetwork ? 4 : 16];
address.TryWriteBytes(addressBytes, out _);
int div = prefixLength / 8;
int mod = prefixLength % 8;
if (mod != 0)
{
// Prefix length is counted right to left, so subtract 8 so we know how many bits to clear.
mod = 8 - mod;
// Shift out the bits from the octet that we don't want, by moving right then back left.
addressBytes[div] = (byte)((int)addressBytes[div] >> mod << mod);
// Move on the next byte.
div++;
}
// Blank out the remaining octets from mod + 1 to the end of the byte array. (192.168.2.240/16 becomes 192.168.0.0)
for (int octet = div; octet < addressBytes.Length; octet++)
{
addressBytes[octet] = 0;
}
// Return the network address for the prefix.
return (new IPAddress(addressBytes), prefixLength);
}
/// <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)
{
ArgumentNullException.ThrowIfNull(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)
{
ArgumentNullException.ThrowIfNull(address);
if (!address.Equals(IPAddress.None))
{
if (address.IsIPv4MappedToIPv6)
{
address = address.MapToIPv4();
}
if (address.AddressFamily == AddressFamily.InterNetwork)
{
// GetAddressBytes
Span<byte> octet = stackalloc byte[4];
address.TryWriteBytes(octet, out _);
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
{
// GetAddressBytes
Span<byte> octet = stackalloc byte[16];
address.TryWriteBytes(octet, out _);
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)
{
ArgumentNullException.ThrowIfNull(address);
if (address.IsIPv4MappedToIPv6)
{
address = address.MapToIPv4();
}
if (address.AddressFamily != AddressFamily.InterNetworkV6)
{
return false;
}
// GetAddressBytes
Span<byte> octet = stackalloc byte[16];
address.TryWriteBytes(octet, out _);
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)
{
ArgumentNullException.ThrowIfNull(mask);
byte cidrnet = 0;
if (!mask.Equals(IPAddress.Any))
{
// GetAddressBytes
Span<byte> bytes = stackalloc byte[mask.AddressFamily == AddressFamily.InterNetwork ? 4 : 16];
mask.TryWriteBytes(bytes, out _);
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 IPAddress.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 performs 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 is not 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 is not null)
{
return !Address.Equals(IPAddress.None) && Address.Equals(other.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

@ -1,6 +1,12 @@
using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using Jellyfin.Extensions;
using Microsoft.AspNetCore.HttpOverrides;
namespace MediaBrowser.Common.Net
{
@ -9,240 +15,336 @@ namespace MediaBrowser.Common.Net
/// </summary>
public static class NetworkExtensions
{
// 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
private static readonly Regex _fqdnRegex = new Regex(@"(?im)^(?!:\/\/)(?=.{1,255}$)((.{1,63}\.){0,127}(?![0-9]*$)[a-z0-9-]+\.?)(:(\d){1,5}){0,1}$");
/// <summary>
/// Add an address to the collection.
/// Returns true if the IPAddress contains an IP6 Local link address.
/// </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)
/// <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 (!source.ContainsAddress(ip))
ArgumentNullException.ThrowIfNull(address);
if (address.IsIPv4MappedToIPv6)
{
source.Add(new IPNetAddress(ip, 32));
address = address.MapToIPv4();
}
}
/// <summary>
/// Adds a network to the collection.
/// </summary>
/// <param name="source">The <see cref="Collection{IPObject}"/>.</param>
/// <param name="item">Item to add.</param>
/// <param name="itemsAreNetworks">If <c>true</c> the values are treated as subnets.
/// If <b>false</b> items are addresses.</param>
public static void AddItem(this Collection<IPObject> source, IPObject item, bool itemsAreNetworks = true)
{
if (!source.ContainsAddress(item) || !itemsAreNetworks)
{
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)
if (address.AddressFamily != AddressFamily.InterNetworkV6)
{
return false;
}
ArgumentNullException.ThrowIfNull(item);
// GetAddressBytes
Span<byte> octet = stackalloc byte[16];
address.TryWriteBytes(octet, out _);
uint word = (uint)(octet[0] << 8) + octet[1];
if (item.IsIPv4MappedToIPv6)
{
item = item.MapToIPv4();
}
foreach (var i in source)
{
if (i.Contains(item))
{
return true;
}
}
return false;
return word >= 0xfe80 && word <= 0xfebf; // fe80::/10 :Local link.
}
/// <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.
/// Convert a subnet mask in CIDR notation to a dotted decimal string value. IPv4 only.
/// </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)
/// <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)
{
if (source.Count == 0)
{
return false;
}
ArgumentNullException.ThrowIfNull(item);
foreach (var i in source)
{
if (i.Contains(item))
{
return true;
}
}
return false;
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>
/// Compares two Collection{IPObject} objects. The order is ignored.
/// Convert a subnet mask in CIDR notation to a dotted decimal string value. IPv4 only.
/// </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)
/// <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(int cidr, AddressFamily family)
{
if (dest is null || source.Count != dest.Count)
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 subnet 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)
{
ArgumentNullException.ThrowIfNull(mask);
byte cidrnet = 0;
if (mask.Equals(IPAddress.Any))
{
return false;
return cidrnet;
}
foreach (var sourceItem in source)
// GetAddressBytes
Span<byte> bytes = stackalloc byte[mask.AddressFamily == AddressFamily.InterNetwork ? 4 : 16];
if (!mask.TryWriteBytes(bytes, out var bytesWritten))
{
bool found = false;
foreach (var destItem in dest)
Console.WriteLine("Unable to write address bytes, only {Bytes} bytes written.", bytesWritten);
}
var zeroed = false;
for (var i = 0; i < bytes.Length; i++)
{
for (int v = bytes[i]; (v & 0xFF) != 0; v <<= 1)
{
if (sourceItem.Equals(destItem))
if (zeroed)
{
found = true;
break;
// Invalid netmask.
return (byte)~cidrnet;
}
if ((v & 0x80) == 0)
{
zeroed = true;
}
else
{
cidrnet++;
}
}
if (!found)
{
return false;
}
}
return true;
return cidrnet;
}
/// <summary>
/// Returns a collection containing the subnets of this collection given.
/// Converts an IPAddress into a string.
/// IPv6 addresses are returned in [ ], with their scope removed.
/// </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)
/// <param name="address">Address to convert.</param>
/// <returns>URI safe conversion of the address.</returns>
public static string FormatIPString(IPAddress? address)
{
ArgumentNullException.ThrowIfNull(source);
Collection<IPObject> res = new Collection<IPObject>();
foreach (IPObject i in source)
if (address is null)
{
if (i is IPNetAddress nw)
return string.Empty;
}
var str = address.ToString();
if (address.AddressFamily == AddressFamily.InterNetworkV6)
{
int i = str.IndexOf('%', StringComparison.Ordinal);
if (i != -1)
{
// Add the subnet calculated from the interface address/mask.
var na = nw.NetworkAddress;
na.Tag = i.Tag;
res.AddItem(na);
str = str.Substring(0, i);
}
else if (i is IPHost ipHost)
return $"[{str}]";
}
return str;
}
/// <summary>
/// Try parsing an array of strings into <see cref="IPNetwork"/> objects, respecting exclusions.
/// Elements without a subnet mask will be represented as <see cref="IPNetwork"/> with a single IP.
/// </summary>
/// <param name="values">Input string array to be parsed.</param>
/// <param name="result">Collection of <see cref="IPNetwork"/>.</param>
/// <param name="negated">Boolean signaling if negated or not negated values should be parsed.</param>
/// <returns><c>True</c> if parsing was successful.</returns>
public static bool TryParseToSubnets(string[] values, [NotNullWhen(true)] out IReadOnlyList<IPNetwork>? result, bool negated = false)
{
if (values is null || values.Length == 0)
{
result = null;
return false;
}
var tmpResult = new List<IPNetwork>();
for (int a = 0; a < values.Length; a++)
{
if (TryParseToSubnet(values[a], out var innerResult, negated))
{
// Flatten out IPHost and add all its ip addresses.
foreach (var addr in ipHost.GetAddresses())
tmpResult.Add(innerResult);
}
}
result = tmpResult;
return tmpResult.Count > 0;
}
/// <summary>
/// Try parsing a string into an <see cref="IPNetwork"/>, respecting exclusions.
/// Inputs without a subnet mask will be represented as <see cref="IPNetwork"/> with a single IP.
/// </summary>
/// <param name="value">Input string to be parsed.</param>
/// <param name="result">An <see cref="IPNetwork"/>.</param>
/// <param name="negated">Boolean signaling if negated or not negated values should be parsed.</param>
/// <returns><c>True</c> if parsing was successful.</returns>
public static bool TryParseToSubnet(ReadOnlySpan<char> value, [NotNullWhen(true)] out IPNetwork? result, bool negated = false)
{
var splitString = value.Trim().Split('/');
if (splitString.MoveNext())
{
var ipBlock = splitString.Current;
var address = IPAddress.None;
if (negated && ipBlock.StartsWith("!") && IPAddress.TryParse(ipBlock[1..], out var tmpAddress))
{
address = tmpAddress;
}
else if (!negated && IPAddress.TryParse(ipBlock, out tmpAddress))
{
address = tmpAddress;
}
if (address != IPAddress.None)
{
if (splitString.MoveNext())
{
IPNetAddress host = new IPNetAddress(addr)
var subnetBlock = splitString.Current;
if (int.TryParse(subnetBlock, out var netmask))
{
Tag = i.Tag
};
res.AddItem(host);
result = new IPNetwork(address, netmask);
return true;
}
else if (IPAddress.TryParse(subnetBlock, out var netmaskAddress))
{
result = new IPNetwork(address, NetworkExtensions.MaskToCidr(netmaskAddress));
return true;
}
}
}
}
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>
/// <param name="isNetwork">Collection is a network collection.</param>
/// <returns>A new collection, with the items excluded.</returns>
public static Collection<IPObject> Exclude(this Collection<IPObject> source, Collection<IPObject> excludeList, bool isNetwork)
{
if (source.Count == 0 || excludeList is 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))
else if (address.AddressFamily == AddressFamily.InterNetwork)
{
found = true;
break;
result = new IPNetwork(address, 32);
return true;
}
else if (address.AddressFamily == AddressFamily.InterNetworkV6)
{
result = new IPNetwork(address, 128);
return true;
}
}
if (!found)
{
results.AddItem(outer, isNetwork);
}
}
return results;
result = null;
return false;
}
/// <summary>
/// Returns all items that co-exist in this object and target.
/// Attempts to parse a host span.
/// </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> ThatAreContainedInNetworks(this Collection<IPObject> source, Collection<IPObject> target)
/// <param name="host">Host name to parse.</param>
/// <param name="addresses">Object representing the span, if it has successfully been parsed.</param>
/// <param name="isIPv4Enabled"><c>true</c> if IPv4 is enabled.</param>
/// <param name="isIPv6Enabled"><c>true</c> if IPv6 is enabled.</param>
/// <returns><c>true</c> if the parsing is successful, <c>false</c> if not.</returns>
public static bool TryParseHost(ReadOnlySpan<char> host, [NotNullWhen(true)] out IPAddress[]? addresses, bool isIPv4Enabled = true, bool isIPv6Enabled = false)
{
if (source.Count == 0)
if (host.IsEmpty)
{
return new Collection<IPObject>();
addresses = null;
return false;
}
ArgumentNullException.ThrowIfNull(target);
host = host.Trim();
Collection<IPObject> nc = new Collection<IPObject>();
foreach (IPObject i in source)
// See if it's an IPv6 with port address e.g. [::1] or [::1]:120.
if (host[0] == '[')
{
if (target.ContainsAddress(i))
int i = host.IndexOf("]", StringComparison.Ordinal);
if (i != -1)
{
nc.AddItem(i);
return TryParseHost(host[1..(i - 1)], out addresses);
}
addresses = Array.Empty<IPAddress>();
return false;
}
var hosts = new List<string>();
foreach (var splitSpan in host.Split(':'))
{
hosts.Add(splitSpan.ToString());
}
if (hosts.Count <= 2)
{
// Is hostname or hostname:port
if (_fqdnRegex.IsMatch(hosts[0]))
{
try
{
addresses = Dns.GetHostAddresses(hosts[0]);
return true;
}
catch (SocketException)
{
// Log and then ignore socket errors, as the result value will just be an empty array.
Console.WriteLine("GetHostAddresses failed.");
}
}
// Is an IP4 or IP4:port
if (IPAddress.TryParse(hosts[0].AsSpan().LeftPart('/'), out var address))
{
if (((address.AddressFamily == AddressFamily.InterNetwork) && (!isIPv4Enabled && isIPv6Enabled))
|| ((address.AddressFamily == AddressFamily.InterNetworkV6) && (isIPv4Enabled && !isIPv6Enabled)))
{
addresses = Array.Empty<IPAddress>();
return false;
}
addresses = new[] { address };
// Host name is an ip4 address, so fake resolve.
return true;
}
}
else if (hosts.Count > 0 && hosts.Count <= 9) // 8 octets + port
{
if (IPAddress.TryParse(host.LeftPart('/'), out var address))
{
addresses = new[] { address };
return true;
}
}
return nc;
addresses = Array.Empty<IPAddress>();
return false;
}
/// <summary>
/// Gets the broadcast address for a <see cref="IPNetwork"/>.
/// </summary>
/// <param name="network">The <see cref="IPNetwork"/>.</param>
/// <returns>The broadcast address.</returns>
public static IPAddress GetBroadcastAddress(IPNetwork network)
{
var addressBytes = network.Prefix.GetAddressBytes();
if (BitConverter.IsLittleEndian)
{
addressBytes.Reverse();
}
uint iPAddress = BitConverter.ToUInt32(addressBytes, 0);
uint ipMaskV4 = BitConverter.ToUInt32(CidrToMask(network.PrefixLength, AddressFamily.InterNetwork).GetAddressBytes(), 0);
uint broadCastIPAddress = iPAddress | ~ipMaskV4;
return new IPAddress(BitConverter.GetBytes(broadCastIPAddress));
}
}
}

View File

@ -4,7 +4,6 @@
using System.Net;
using MediaBrowser.Common;
using MediaBrowser.Common.Net;
using MediaBrowser.Model.System;
using Microsoft.AspNetCore.Http;
@ -75,10 +74,10 @@ namespace MediaBrowser.Controller
/// <summary>
/// Gets an URL that can be used to access the API over LAN.
/// </summary>
/// <param name="hostname">An optional hostname to use.</param>
/// <param name="ipAddress">An optional IP address to use.</param>
/// <param name="allowHttps">A value indicating whether to allow HTTPS.</param>
/// <returns>The API URL.</returns>
string GetApiUrlForLocalAccess(IPObject hostname = null, bool allowHttps = true);
string GetApiUrlForLocalAccess(IPAddress ipAddress = null, bool allowHttps = true);
/// <summary>
/// Gets a local (LAN) URL that can be used to access the API.

View File

@ -13,10 +13,10 @@ namespace MediaBrowser.Model.Dlna
public Dictionary<string, string> Headers { get; set; }
public IPAddress LocalIpAddress { get; set; }
public IPAddress LocalIPAddress { get; set; }
public int LocalPort { get; set; }
public IPAddress RemoteIpAddress { get; set; }
public IPAddress RemoteIPAddress { get; set; }
}
}

View File

@ -33,6 +33,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.HttpOverrides" />
<PackageReference Include="Microsoft.SourceLink.GitHub" PrivateAssets="All" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
<PackageReference Include="MimeTypes">

View File

@ -0,0 +1,74 @@
using System.Net;
using System.Net.Sockets;
using Microsoft.AspNetCore.HttpOverrides;
namespace MediaBrowser.Model.Net;
/// <summary>
/// Base network object class.
/// </summary>
public class IPData
{
/// <summary>
/// Initializes a new instance of the <see cref="IPData"/> class.
/// </summary>
/// <param name="address">The <see cref="IPAddress"/>.</param>
/// <param name="subnet">The <see cref="IPNetwork"/>.</param>
/// <param name="name">The interface name.</param>
public IPData(IPAddress address, IPNetwork? subnet, string name)
{
Address = address;
Subnet = subnet ?? (address.AddressFamily == AddressFamily.InterNetwork ? new IPNetwork(address, 32) : new IPNetwork(address, 128));
Name = name;
}
/// <summary>
/// Initializes a new instance of the <see cref="IPData"/> class.
/// </summary>
/// <param name="address">The <see cref="IPAddress"/>.</param>
/// <param name="subnet">The <see cref="IPNetwork"/>.</param>
public IPData(IPAddress address, IPNetwork? subnet)
: this(address, subnet, string.Empty)
{
}
/// <summary>
/// Gets or sets the object's IP address.
/// </summary>
public IPAddress Address { get; set; }
/// <summary>
/// Gets or sets the object's IP address.
/// </summary>
public IPNetwork Subnet { get; set; }
/// <summary>
/// Gets or sets the interface index.
/// </summary>
public int Index { get; set; }
/// <summary>
/// Gets or sets the interface name.
/// </summary>
public string Name { get; set; }
/// <summary>
/// Gets the AddressFamily of the object.
/// </summary>
public AddressFamily AddressFamily
{
get
{
if (Address.Equals(IPAddress.None))
{
return Subnet.Prefix.AddressFamily.Equals(IPAddress.None)
? AddressFamily.Unspecified
: Subnet.Prefix.AddressFamily;
}
else
{
return Address.AddressFamily;
}
}
}
}

View File

@ -1,34 +0,0 @@
#pragma warning disable CS1591
using System;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Model.Net
{
/// <summary>
/// Provides a common interface across platforms for UDP sockets used by this SSDP implementation.
/// </summary>
public interface ISocket : IDisposable
{
IPAddress LocalIPAddress { get; }
Task<SocketReceiveResult> ReceiveAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken);
IAsyncResult BeginReceive(byte[] buffer, int offset, int count, AsyncCallback callback);
SocketReceiveResult EndReceive(IAsyncResult result);
/// <summary>
/// Sends a UDP message to a particular end point (uni or multicast).
/// </summary>
/// <param name="buffer">An array of type <see cref="byte" /> that contains the data to send.</param>
/// <param name="offset">The zero-based position in buffer at which to begin sending data.</param>
/// <param name="bytes">The number of bytes to send.</param>
/// <param name="endPoint">An <see cref="IPEndPoint" /> that represents the remote device.</param>
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
/// <returns>The task object representing the asynchronous operation.</returns>
Task SendToAsync(byte[] buffer, int offset, int bytes, IPEndPoint endPoint, CancellationToken cancellationToken);
}
}

View File

@ -1,31 +1,35 @@
#pragma warning disable CS1591
using System.Net;
using System.Net.Sockets;
namespace MediaBrowser.Model.Net
namespace MediaBrowser.Model.Net;
/// <summary>
/// Implemented by components that can create specific socket configurations.
/// </summary>
public interface ISocketFactory
{
/// <summary>
/// Implemented by components that can create a platform specific UDP socket implementation, and wrap it in the cross platform <see cref="ISocket"/> interface.
/// Creates a new unicast socket using the specified local port number.
/// </summary>
public interface ISocketFactory
{
ISocket CreateUdpBroadcastSocket(int localPort);
/// <param name="localPort">The local port to bind to.</param>
/// <returns>A new unicast socket using the specified local port number.</returns>
Socket CreateUdpBroadcastSocket(int localPort);
/// <summary>
/// Creates a new unicast socket using the specified local port number.
/// </summary>
/// <param name="localIp">The local IP address to bind to.</param>
/// <param name="localPort">The local port to bind to.</param>
/// <returns>A new unicast socket using the specified local port number.</returns>
ISocket CreateSsdpUdpSocket(IPAddress localIp, int localPort);
/// <summary>
/// Creates a new unicast socket using the specified local port number.
/// </summary>
/// <param name="bindInterface">The bind interface.</param>
/// <param name="localPort">The local port to bind to.</param>
/// <returns>A new unicast socket using the specified local port number.</returns>
Socket CreateSsdpUdpSocket(IPData bindInterface, int localPort);
/// <summary>
/// Creates a new multicast socket using the specified multicast IP address, multicast time to live and local port.
/// </summary>
/// <param name="ipAddress">The multicast IP address to bind to.</param>
/// <param name="multicastTimeToLive">The multicast time to live value. Actually a maximum number of network hops for UDP packets.</param>
/// <param name="localPort">The local port to bind to.</param>
/// <returns>A <see cref="ISocket"/> implementation.</returns>
ISocket CreateUdpMulticastSocket(IPAddress ipAddress, int multicastTimeToLive, int localPort);
}
/// <summary>
/// Creates a new multicast socket using the specified multicast IP address, multicast time to live and local port.
/// </summary>
/// <param name="multicastAddress">The multicast IP address to bind to.</param>
/// <param name="bindInterface">The bind interface.</param>
/// <param name="multicastTimeToLive">The multicast time to live value. Actually a maximum number of network hops for UDP packets.</param>
/// <param name="localPort">The local port to bind to.</param>
/// <returns>A new multicast socket using the specfied bind interface, multicast address, multicast time to live and port.</returns>
Socket CreateUdpMulticastSocket(IPAddress multicastAddress, IPData bindInterface, int multicastTimeToLive, int localPort);
}

View File

@ -8,7 +8,7 @@ namespace Rssdp
/// </summary>
public sealed class DeviceAvailableEventArgs : EventArgs
{
public IPAddress RemoteIpAddress { get; set; }
public IPAddress RemoteIPAddress { get; set; }
private readonly DiscoveredSsdpDevice _DiscoveredDevice;

View File

@ -23,23 +23,23 @@ namespace Rssdp.Infrastructure
/// <summary>
/// Causes the server to begin listening for multicast messages, being SSDP search requests and notifications.
/// </summary>
void BeginListeningForBroadcasts();
void BeginListeningForMulticast();
/// <summary>
/// Causes the server to stop listening for multicast messages, being SSDP search requests and notifications.
/// </summary>
void StopListeningForBroadcasts();
void StopListeningForMulticast();
/// <summary>
/// Sends a message to a particular address (uni or multicast) and port.
/// </summary>
Task SendMessage(byte[] messageData, IPEndPoint destination, IPAddress fromLocalIpAddress, CancellationToken cancellationToken);
Task SendMessage(byte[] messageData, IPEndPoint destination, IPAddress fromLocalIPAddress, CancellationToken cancellationToken);
/// <summary>
/// Sends a message to the SSDP multicast address and port.
/// </summary>
Task SendMulticastMessage(string message, IPAddress fromLocalIpAddress, CancellationToken cancellationToken);
Task SendMulticastMessage(string message, int sendCount, IPAddress fromLocalIpAddress, CancellationToken cancellationToken);
Task SendMulticastMessage(string message, IPAddress fromLocalIPAddress, CancellationToken cancellationToken);
Task SendMulticastMessage(string message, int sendCount, IPAddress fromLocalIPAddress, CancellationToken cancellationToken);
/// <summary>
/// Gets or sets a boolean value indicating whether or not this instance is shared amongst multiple <see cref="SsdpDeviceLocator"/> and/or <see cref="ISsdpDevicePublisher"/> instances.

View File

@ -13,16 +13,16 @@ namespace Rssdp.Infrastructure
private readonly IPEndPoint _ReceivedFrom;
public IPAddress LocalIpAddress { get; private set; }
public IPAddress LocalIPAddress { get; private set; }
/// <summary>
/// Full constructor.
/// </summary>
public RequestReceivedEventArgs(HttpRequestMessage message, IPEndPoint receivedFrom, IPAddress localIpAddress)
public RequestReceivedEventArgs(HttpRequestMessage message, IPEndPoint receivedFrom, IPAddress localIPAddress)
{
_Message = message;
_ReceivedFrom = receivedFrom;
LocalIpAddress = localIpAddress;
LocalIPAddress = localIPAddress;
}
/// <summary>

View File

@ -9,7 +9,7 @@ namespace Rssdp.Infrastructure
/// </summary>
public sealed class ResponseReceivedEventArgs : EventArgs
{
public IPAddress LocalIpAddress { get; set; }
public IPAddress LocalIPAddress { get; set; }
private readonly HttpResponseMessage _Message;

View File

@ -25,18 +25,18 @@ namespace Rssdp.Infrastructure
* Since stopping the service would be a bad idea (might not be allowed security wise and might
* break other apps running on the system) the only other work around is to use two sockets.
*
* We use one socket to listen for/receive notifications and search requests (_BroadcastListenSocket).
* We use a second socket, bound to a different local port, to send search requests and listen for
* responses (_SendSocket). The responses are sent to the local port this socket is bound to,
* which isn't port 1900 so the MS service doesn't steal them. While the caller can specify a local
* We use one group of sockets to listen for/receive notifications and search requests (_MulticastListenSockets).
* We use a second group, bound to a different local port, to send search requests and listen for
* responses (_SendSockets). The responses are sent to the local ports these sockets are bound to,
* which aren't port 1900 so the MS service doesn't steal them. While the caller can specify a local
* port to use, we will default to 0 which allows the underlying system to auto-assign a free port.
*/
private object _BroadcastListenSocketSynchroniser = new object();
private ISocket _BroadcastListenSocket;
private List<Socket> _MulticastListenSockets;
private object _SendSocketSynchroniser = new object();
private List<ISocket> _sendSockets;
private List<Socket> _sendSockets;
private HttpRequestParser _RequestParser;
private HttpResponseParser _ResponseParser;
@ -78,7 +78,7 @@ namespace Rssdp.Infrastructure
/// <exception cref="ArgumentOutOfRangeException">The <paramref name="multicastTimeToLive"/> argument is less than or equal to zero.</exception>
public SsdpCommunicationsServer(ISocketFactory socketFactory, int localPort, int multicastTimeToLive, INetworkManager networkManager, ILogger logger, bool enableMultiSocketBinding)
{
if (socketFactory == null)
if (socketFactory is null)
{
throw new ArgumentNullException(nameof(socketFactory));
}
@ -107,28 +107,25 @@ namespace Rssdp.Infrastructure
/// Causes the server to begin listening for multicast messages, being SSDP search requests and notifications.
/// </summary>
/// <exception cref="ObjectDisposedException">Thrown if the <see cref="DisposableManagedObjectBase.IsDisposed"/> property is true (because <seealso cref="DisposableManagedObjectBase.Dispose()" /> has been called previously).</exception>
public void BeginListeningForBroadcasts()
public void BeginListeningForMulticast()
{
ThrowIfDisposed();
if (_BroadcastListenSocket == null)
lock (_BroadcastListenSocketSynchroniser)
{
lock (_BroadcastListenSocketSynchroniser)
if (_MulticastListenSockets is null)
{
if (_BroadcastListenSocket == null)
try
{
try
{
_BroadcastListenSocket = ListenForBroadcastsAsync();
}
catch (SocketException ex)
{
_logger.LogError("Failed to bind to port 1900: {Message}. DLNA will be unavailable", ex.Message);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error in BeginListeningForBroadcasts");
}
_MulticastListenSockets = CreateMulticastSocketsAndListen();
}
catch (SocketException ex)
{
_logger.LogError("Failed to bind to multicast address: {Message}. DLNA will be unavailable", ex.Message);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error in BeginListeningForMulticast");
}
}
}
@ -138,15 +135,19 @@ namespace Rssdp.Infrastructure
/// Causes the server to stop listening for multicast messages, being SSDP search requests and notifications.
/// </summary>
/// <exception cref="ObjectDisposedException">Thrown if the <see cref="DisposableManagedObjectBase.IsDisposed"/> property is true (because <seealso cref="DisposableManagedObjectBase.Dispose()" /> has been called previously).</exception>
public void StopListeningForBroadcasts()
public void StopListeningForMulticast()
{
lock (_BroadcastListenSocketSynchroniser)
{
if (_BroadcastListenSocket != null)
if (_MulticastListenSockets is not null)
{
_logger.LogInformation("{0} disposing _BroadcastListenSocket", GetType().Name);
_BroadcastListenSocket.Dispose();
_BroadcastListenSocket = null;
foreach (var socket in _MulticastListenSockets)
{
socket.Dispose();
}
_MulticastListenSockets = null;
}
}
}
@ -154,16 +155,16 @@ namespace Rssdp.Infrastructure
/// <summary>
/// Sends a message to a particular address (uni or multicast) and port.
/// </summary>
public async Task SendMessage(byte[] messageData, IPEndPoint destination, IPAddress fromLocalIpAddress, CancellationToken cancellationToken)
public async Task SendMessage(byte[] messageData, IPEndPoint destination, IPAddress fromlocalIPAddress, CancellationToken cancellationToken)
{
if (messageData == null)
if (messageData is null)
{
throw new ArgumentNullException(nameof(messageData));
}
ThrowIfDisposed();
var sockets = GetSendSockets(fromLocalIpAddress, destination);
var sockets = GetSendSockets(fromlocalIPAddress, destination);
if (sockets.Count == 0)
{
@ -180,11 +181,11 @@ namespace Rssdp.Infrastructure
}
}
private async Task SendFromSocket(ISocket socket, byte[] messageData, IPEndPoint destination, CancellationToken cancellationToken)
private async Task SendFromSocket(Socket socket, byte[] messageData, IPEndPoint destination, CancellationToken cancellationToken)
{
try
{
await socket.SendToAsync(messageData, 0, messageData.Length, destination, cancellationToken).ConfigureAwait(false);
await socket.SendToAsync(messageData, destination, cancellationToken).ConfigureAwait(false);
}
catch (ObjectDisposedException)
{
@ -194,37 +195,42 @@ namespace Rssdp.Infrastructure
}
catch (Exception ex)
{
_logger.LogError(ex, "Error sending socket message from {0} to {1}", socket.LocalIPAddress.ToString(), destination.ToString());
var localIP = ((IPEndPoint)socket.LocalEndPoint).Address;
_logger.LogError(ex, "Error sending socket message from {0} to {1}", localIP.ToString(), destination.ToString());
}
}
private List<ISocket> GetSendSockets(IPAddress fromLocalIpAddress, IPEndPoint destination)
private List<Socket> GetSendSockets(IPAddress fromlocalIPAddress, IPEndPoint destination)
{
EnsureSendSocketCreated();
lock (_SendSocketSynchroniser)
{
var sockets = _sendSockets.Where(i => i.LocalIPAddress.AddressFamily == fromLocalIpAddress.AddressFamily);
var sockets = _sendSockets.Where(s => s.AddressFamily == fromlocalIPAddress.AddressFamily);
// Send from the Any socket and the socket with the matching address
if (fromLocalIpAddress.AddressFamily == AddressFamily.InterNetwork)
if (fromlocalIPAddress.AddressFamily == AddressFamily.InterNetwork)
{
sockets = sockets.Where(i => i.LocalIPAddress.Equals(IPAddress.Any) || fromLocalIpAddress.Equals(i.LocalIPAddress));
sockets = sockets.Where(s => ((IPEndPoint)s.LocalEndPoint).Address.Equals(IPAddress.Any)
|| ((IPEndPoint)s.LocalEndPoint).Address.Equals(fromlocalIPAddress));
// If sending to the loopback address, filter the socket list as well
if (destination.Address.Equals(IPAddress.Loopback))
{
sockets = sockets.Where(i => i.LocalIPAddress.Equals(IPAddress.Any) || i.LocalIPAddress.Equals(IPAddress.Loopback));
sockets = sockets.Where(s => ((IPEndPoint)s.LocalEndPoint).Address.Equals(IPAddress.Any)
|| ((IPEndPoint)s.LocalEndPoint).Address.Equals(IPAddress.Loopback));
}
}
else if (fromLocalIpAddress.AddressFamily == AddressFamily.InterNetworkV6)
else if (fromlocalIPAddress.AddressFamily == AddressFamily.InterNetworkV6)
{
sockets = sockets.Where(i => i.LocalIPAddress.Equals(IPAddress.IPv6Any) || fromLocalIpAddress.Equals(i.LocalIPAddress));
sockets = sockets.Where(s => ((IPEndPoint)s.LocalEndPoint).Address.Equals(IPAddress.IPv6Any)
|| ((IPEndPoint)s.LocalEndPoint).Address.Equals(fromlocalIPAddress));
// If sending to the loopback address, filter the socket list as well
if (destination.Address.Equals(IPAddress.IPv6Loopback))
{
sockets = sockets.Where(i => i.LocalIPAddress.Equals(IPAddress.IPv6Any) || i.LocalIPAddress.Equals(IPAddress.IPv6Loopback));
sockets = sockets.Where(s => ((IPEndPoint)s.LocalEndPoint).Address.Equals(IPAddress.IPv6Any)
|| ((IPEndPoint)s.LocalEndPoint).Address.Equals(IPAddress.IPv6Loopback));
}
}
@ -232,17 +238,17 @@ namespace Rssdp.Infrastructure
}
}
public Task SendMulticastMessage(string message, IPAddress fromLocalIpAddress, CancellationToken cancellationToken)
public Task SendMulticastMessage(string message, IPAddress fromlocalIPAddress, CancellationToken cancellationToken)
{
return SendMulticastMessage(message, SsdpConstants.UdpResendCount, fromLocalIpAddress, cancellationToken);
return SendMulticastMessage(message, SsdpConstants.UdpResendCount, fromlocalIPAddress, cancellationToken);
}
/// <summary>
/// Sends a message to the SSDP multicast address and port.
/// </summary>
public async Task SendMulticastMessage(string message, int sendCount, IPAddress fromLocalIpAddress, CancellationToken cancellationToken)
public async Task SendMulticastMessage(string message, int sendCount, IPAddress fromlocalIPAddress, CancellationToken cancellationToken)
{
if (message == null)
if (message is null)
{
throw new ArgumentNullException(nameof(message));
}
@ -263,7 +269,7 @@ namespace Rssdp.Infrastructure
new IPEndPoint(
IPAddress.Parse(SsdpConstants.MulticastLocalAdminAddress),
SsdpConstants.MulticastPort),
fromLocalIpAddress,
fromlocalIPAddress,
cancellationToken).ConfigureAwait(false);
await Task.Delay(100, cancellationToken).ConfigureAwait(false);
@ -278,7 +284,7 @@ namespace Rssdp.Infrastructure
{
lock (_SendSocketSynchroniser)
{
if (_sendSockets != null)
if (_sendSockets is not null)
{
var sockets = _sendSockets.ToList();
_sendSockets = null;
@ -287,7 +293,8 @@ namespace Rssdp.Infrastructure
foreach (var socket in sockets)
{
_logger.LogInformation("{0} disposing sendSocket from {1}", GetType().Name, socket.LocalIPAddress);
var socketAddress = ((IPEndPoint)socket.LocalEndPoint).Address;
_logger.LogInformation("{0} disposing sendSocket from {1}", GetType().Name, socketAddress);
socket.Dispose();
}
}
@ -315,20 +322,20 @@ namespace Rssdp.Infrastructure
{
if (disposing)
{
StopListeningForBroadcasts();
StopListeningForMulticast();
StopListeningForResponses();
}
}
private Task SendMessageIfSocketNotDisposed(byte[] messageData, IPEndPoint destination, IPAddress fromLocalIpAddress, CancellationToken cancellationToken)
private Task SendMessageIfSocketNotDisposed(byte[] messageData, IPEndPoint destination, IPAddress fromlocalIPAddress, CancellationToken cancellationToken)
{
var sockets = _sendSockets;
if (sockets != null)
if (sockets is not null)
{
sockets = sockets.ToList();
var tasks = sockets.Where(s => (fromLocalIpAddress == null || fromLocalIpAddress.Equals(s.LocalIPAddress)))
var tasks = sockets.Where(s => (fromlocalIPAddress is null || fromlocalIPAddress.Equals(((IPEndPoint)s.LocalEndPoint).Address)))
.Select(s => SendFromSocket(s, messageData, destination, cancellationToken));
return Task.WhenAll(tasks);
}
@ -336,50 +343,78 @@ namespace Rssdp.Infrastructure
return Task.CompletedTask;
}
private ISocket ListenForBroadcastsAsync()
private List<Socket> CreateMulticastSocketsAndListen()
{
var socket = _SocketFactory.CreateUdpMulticastSocket(IPAddress.Parse(SsdpConstants.MulticastLocalAdminAddress), _MulticastTtl, SsdpConstants.MulticastPort);
_ = ListenToSocketInternal(socket);
return socket;
}
private List<ISocket> CreateSocketAndListenForResponsesAsync()
{
var sockets = new List<ISocket>();
sockets.Add(_SocketFactory.CreateSsdpUdpSocket(IPAddress.Any, _LocalPort));
var sockets = new List<Socket>();
var multicastGroupAddress = IPAddress.Parse(SsdpConstants.MulticastLocalAdminAddress);
if (_enableMultiSocketBinding)
{
foreach (var address in _networkManager.GetInternalBindAddresses())
{
if (address.AddressFamily == AddressFamily.InterNetworkV6)
{
// Not support IPv6 right now
continue;
}
// IPv6 is currently unsupported
var validInterfaces = _networkManager.GetInternalBindAddresses()
.Where(x => x.Address is not null)
.Where(x => x.AddressFamily == AddressFamily.InterNetwork)
.DistinctBy(x => x.Index);
foreach (var intf in validInterfaces)
{
try
{
sockets.Add(_SocketFactory.CreateSsdpUdpSocket(address.Address, _LocalPort));
var socket = _SocketFactory.CreateUdpMulticastSocket(multicastGroupAddress, intf, _MulticastTtl, SsdpConstants.MulticastPort);
_ = ListenToSocketInternal(socket);
sockets.Add(socket);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error in CreateSsdpUdpSocket. IPAddress: {0}", address);
_logger.LogError(ex, "Error in CreateMulticastSocketsAndListen. IP address: {0}", intf.Address);
}
}
}
foreach (var socket in sockets)
else
{
var socket = _SocketFactory.CreateUdpMulticastSocket(multicastGroupAddress, new IPData(IPAddress.Any, null), _MulticastTtl, SsdpConstants.MulticastPort);
_ = ListenToSocketInternal(socket);
sockets.Add(socket);
}
return sockets;
}
private async Task ListenToSocketInternal(ISocket socket)
private List<Socket> CreateSendSockets()
{
var sockets = new List<Socket>();
if (_enableMultiSocketBinding)
{
// IPv6 is currently unsupported
var validInterfaces = _networkManager.GetInternalBindAddresses()
.Where(x => x.Address is not null)
.Where(x => x.AddressFamily == AddressFamily.InterNetwork);
foreach (var intf in validInterfaces)
{
try
{
var socket = _SocketFactory.CreateSsdpUdpSocket(intf, _LocalPort);
_ = ListenToSocketInternal(socket);
sockets.Add(socket);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error in CreateSsdpUdpSocket. IPAddress: {0}", intf.Address);
}
}
}
else
{
var socket = _SocketFactory.CreateSsdpUdpSocket(new IPData(IPAddress.Any, null), _LocalPort);
_ = ListenToSocketInternal(socket);
sockets.Add(socket);
}
return sockets;
}
private async Task ListenToSocketInternal(Socket socket)
{
var cancelled = false;
var receiveBuffer = new byte[8192];
@ -388,14 +423,17 @@ namespace Rssdp.Infrastructure
{
try
{
var result = await socket.ReceiveAsync(receiveBuffer, 0, receiveBuffer.Length, CancellationToken.None).ConfigureAwait(false);
var result = await socket.ReceiveMessageFromAsync(receiveBuffer, SocketFlags.None, new IPEndPoint(IPAddress.Any, 0), CancellationToken.None).ConfigureAwait(false);;
if (result.ReceivedBytes > 0)
{
// Strange cannot convert compiler error here if I don't explicitly
// assign or cast to Action first. Assignment is easier to read,
// so went with that.
ProcessMessage(UTF8Encoding.UTF8.GetString(result.Buffer, 0, result.ReceivedBytes), result.RemoteEndPoint, result.LocalIPAddress);
var remoteEndpoint = (IPEndPoint)result.RemoteEndPoint;
var localEndpointAdapter = _networkManager.GetAllBindInterfaces().First(a => a.Index == result.PacketInformation.Interface);
ProcessMessage(
UTF8Encoding.UTF8.GetString(receiveBuffer, 0, result.ReceivedBytes),
remoteEndpoint,
localEndpointAdapter.Address);
}
}
catch (ObjectDisposedException)
@ -411,21 +449,22 @@ namespace Rssdp.Infrastructure
private void EnsureSendSocketCreated()
{
if (_sendSockets == null)
if (_sendSockets is null)
{
lock (_SendSocketSynchroniser)
{
_sendSockets ??= CreateSocketAndListenForResponsesAsync();
_sendSockets ??= CreateSendSockets();
}
}
}
private void ProcessMessage(string data, IPEndPoint endPoint, IPAddress receivedOnLocalIpAddress)
private void ProcessMessage(string data, IPEndPoint endPoint, IPAddress receivedOnlocalIPAddress)
{
// Responses start with the HTTP version, prefixed with HTTP/ while
// requests start with a method which can vary and might be one we haven't
// seen/don't know. We'll check if this message is a request or a response
// by checking for the HTTP/ prefix on the start of the message.
_logger.LogDebug("Received data from {From} on {Port} at {Address}:\n{Data}", endPoint.Address, endPoint.Port, receivedOnlocalIPAddress, data);
if (data.StartsWith("HTTP/", StringComparison.OrdinalIgnoreCase))
{
HttpResponseMessage responseMessage = null;
@ -438,9 +477,9 @@ namespace Rssdp.Infrastructure
// Ignore invalid packets.
}
if (responseMessage != null)
if (responseMessage is not null)
{
OnResponseReceived(responseMessage, endPoint, receivedOnLocalIpAddress);
OnResponseReceived(responseMessage, endPoint, receivedOnlocalIPAddress);
}
}
else
@ -455,14 +494,14 @@ namespace Rssdp.Infrastructure
// Ignore invalid packets.
}
if (requestMessage != null)
if (requestMessage is not null)
{
OnRequestReceived(requestMessage, endPoint, receivedOnLocalIpAddress);
OnRequestReceived(requestMessage, endPoint, receivedOnlocalIPAddress);
}
}
}
private void OnRequestReceived(HttpRequestMessage data, IPEndPoint remoteEndPoint, IPAddress receivedOnLocalIpAddress)
private void OnRequestReceived(HttpRequestMessage data, IPEndPoint remoteEndPoint, IPAddress receivedOnlocalIPAddress)
{
// SSDP specification says only * is currently used but other uri's might
// be implemented in the future and should be ignored unless understood.
@ -473,20 +512,20 @@ namespace Rssdp.Infrastructure
}
var handlers = this.RequestReceived;
if (handlers != null)
if (handlers is not null)
{
handlers(this, new RequestReceivedEventArgs(data, remoteEndPoint, receivedOnLocalIpAddress));
handlers(this, new RequestReceivedEventArgs(data, remoteEndPoint, receivedOnlocalIPAddress));
}
}
private void OnResponseReceived(HttpResponseMessage data, IPEndPoint endPoint, IPAddress localIpAddress)
private void OnResponseReceived(HttpResponseMessage data, IPEndPoint endPoint, IPAddress localIPAddress)
{
var handlers = this.ResponseReceived;
if (handlers != null)
if (handlers is not null)
{
handlers(this, new ResponseReceivedEventArgs(data, endPoint)
{
LocalIpAddress = localIpAddress
LocalIPAddress = localIPAddress
});
}
}

View File

@ -26,6 +26,8 @@ namespace Rssdp.Infrastructure
internal const string SsdpDeviceDescriptionXmlNamespace = "urn:schemas-upnp-org:device-1-0";
internal const string ServerVersion = "1.0";
/// <summary>
/// Default buffer size for receiving SSDP broadcasts. Value is 8192 (bytes).
/// </summary>

View File

@ -1,11 +1,11 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace Rssdp.Infrastructure
{
/// <summary>
@ -19,19 +19,27 @@ namespace Rssdp.Infrastructure
private Timer _BroadcastTimer;
private object _timerLock = new object();
private string _OSName;
private string _OSVersion;
private readonly TimeSpan DefaultSearchWaitTime = TimeSpan.FromSeconds(4);
private readonly TimeSpan OneSecond = TimeSpan.FromSeconds(1);
/// <summary>
/// Default constructor.
/// </summary>
public SsdpDeviceLocator(ISsdpCommunicationsServer communicationsServer)
public SsdpDeviceLocator(
ISsdpCommunicationsServer communicationsServer,
string osName,
string osVersion)
{
if (communicationsServer == null)
{
throw new ArgumentNullException(nameof(communicationsServer));
}
ArgumentNullException.ThrowIfNull(communicationsServer);
ArgumentNullException.ThrowIfNullOrEmpty(osName);
ArgumentNullException.ThrowIfNullOrEmpty(osVersion);
_OSName = osName;
_OSVersion = osVersion;
_CommunicationsServer = communicationsServer;
_CommunicationsServer.ResponseReceived += CommsServer_ResponseReceived;
@ -72,7 +80,7 @@ namespace Rssdp.Infrastructure
{
lock (_timerLock)
{
if (_BroadcastTimer == null)
if (_BroadcastTimer is null)
{
_BroadcastTimer = new Timer(OnBroadcastTimerCallback, null, dueTime, period);
}
@ -87,7 +95,7 @@ namespace Rssdp.Infrastructure
{
lock (_timerLock)
{
if (_BroadcastTimer != null)
if (_BroadcastTimer is not null)
{
_BroadcastTimer.Dispose();
_BroadcastTimer = null;
@ -148,7 +156,7 @@ namespace Rssdp.Infrastructure
private Task SearchAsync(string searchTarget, TimeSpan searchWaitTime, CancellationToken cancellationToken)
{
if (searchTarget == null)
if (searchTarget is null)
{
throw new ArgumentNullException(nameof(searchTarget));
}
@ -187,7 +195,7 @@ namespace Rssdp.Infrastructure
{
_CommunicationsServer.RequestReceived -= CommsServer_RequestReceived;
_CommunicationsServer.RequestReceived += CommsServer_RequestReceived;
_CommunicationsServer.BeginListeningForBroadcasts();
_CommunicationsServer.BeginListeningForMulticast();
}
/// <summary>
@ -211,7 +219,7 @@ namespace Rssdp.Infrastructure
/// Raises the <see cref="DeviceAvailable"/> event.
/// </summary>
/// <seealso cref="DeviceAvailable"/>
protected virtual void OnDeviceAvailable(DiscoveredSsdpDevice device, bool isNewDevice, IPAddress IpAddress)
protected virtual void OnDeviceAvailable(DiscoveredSsdpDevice device, bool isNewDevice, IPAddress IPAddress)
{
if (this.IsDisposed)
{
@ -219,11 +227,11 @@ namespace Rssdp.Infrastructure
}
var handlers = this.DeviceAvailable;
if (handlers != null)
if (handlers is not null)
{
handlers(this, new DeviceAvailableEventArgs(device, isNewDevice)
{
RemoteIpAddress = IpAddress
RemoteIPAddress = IPAddress
});
}
}
@ -242,7 +250,7 @@ namespace Rssdp.Infrastructure
}
var handlers = this.DeviceUnavailable;
if (handlers != null)
if (handlers is not null)
{
handlers(this, new DeviceUnavailableEventArgs(device, expired));
}
@ -281,7 +289,7 @@ namespace Rssdp.Infrastructure
var commsServer = _CommunicationsServer;
_CommunicationsServer = null;
if (commsServer != null)
if (commsServer is not null)
{
commsServer.ResponseReceived -= this.CommsServer_ResponseReceived;
commsServer.RequestReceived -= this.CommsServer_RequestReceived;
@ -289,13 +297,13 @@ namespace Rssdp.Infrastructure
}
}
private void AddOrUpdateDiscoveredDevice(DiscoveredSsdpDevice device, IPAddress IpAddress)
private void AddOrUpdateDiscoveredDevice(DiscoveredSsdpDevice device, IPAddress IPAddress)
{
bool isNewDevice = false;
lock (_Devices)
{
var existingDevice = FindExistingDeviceNotification(_Devices, device.NotificationType, device.Usn);
if (existingDevice == null)
if (existingDevice is null)
{
_Devices.Add(device);
isNewDevice = true;
@ -307,17 +315,17 @@ namespace Rssdp.Infrastructure
}
}
DeviceFound(device, isNewDevice, IpAddress);
DeviceFound(device, isNewDevice, IPAddress);
}
private void DeviceFound(DiscoveredSsdpDevice device, bool isNewDevice, IPAddress IpAddress)
private void DeviceFound(DiscoveredSsdpDevice device, bool isNewDevice, IPAddress IPAddress)
{
if (!NotificationTypeMatchesFilter(device))
{
return;
}
OnDeviceAvailable(device, isNewDevice, IpAddress);
OnDeviceAvailable(device, isNewDevice, IPAddress);
}
private bool NotificationTypeMatchesFilter(DiscoveredSsdpDevice device)
@ -329,12 +337,12 @@ namespace Rssdp.Infrastructure
private Task BroadcastDiscoverMessage(string serviceType, TimeSpan mxValue, CancellationToken cancellationToken)
{
const string header = "M-SEARCH * HTTP/1.1";
var values = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
values["HOST"] = "239.255.255.250:1900";
values["USER-AGENT"] = "UPnP/1.0 DLNADOC/1.50 Platinum/1.0.4.2";
// values["X-EMBY-SERVERID"] = _appHost.SystemId;
values["USER-AGENT"] = string.Format(CultureInfo.InvariantCulture, "{0}/{1} UPnP/1.0 RSSDP/{2}", _OSName, _OSVersion, SsdpConstants.ServerVersion);
values["MAN"] = "\"ssdp:discover\"";
// Search target
@ -343,14 +351,12 @@ namespace Rssdp.Infrastructure
// Seconds to delay response
values["MX"] = "3";
var header = "M-SEARCH * HTTP/1.1";
var message = BuildMessage(header, values);
return _CommunicationsServer.SendMulticastMessage(message, null, cancellationToken);
}
private void ProcessSearchResponseMessage(HttpResponseMessage message, IPAddress IpAddress)
private void ProcessSearchResponseMessage(HttpResponseMessage message, IPAddress IPAddress)
{
if (!message.IsSuccessStatusCode)
{
@ -358,7 +364,7 @@ namespace Rssdp.Infrastructure
}
var location = GetFirstHeaderUriValue("Location", message);
if (location != null)
if (location is not null)
{
var device = new DiscoveredSsdpDevice()
{
@ -370,11 +376,11 @@ namespace Rssdp.Infrastructure
ResponseHeaders = message.Headers
};
AddOrUpdateDiscoveredDevice(device, IpAddress);
AddOrUpdateDiscoveredDevice(device, IPAddress);
}
}
private void ProcessNotificationMessage(HttpRequestMessage message, IPAddress IpAddress)
private void ProcessNotificationMessage(HttpRequestMessage message, IPAddress IPAddress)
{
if (String.Compare(message.Method.Method, "Notify", StringComparison.OrdinalIgnoreCase) != 0)
{
@ -384,7 +390,7 @@ namespace Rssdp.Infrastructure
var notificationType = GetFirstHeaderStringValue("NTS", message);
if (String.Compare(notificationType, SsdpConstants.SsdpKeepAliveNotification, StringComparison.OrdinalIgnoreCase) == 0)
{
ProcessAliveNotification(message, IpAddress);
ProcessAliveNotification(message, IPAddress);
}
else if (String.Compare(notificationType, SsdpConstants.SsdpByeByeNotification, StringComparison.OrdinalIgnoreCase) == 0)
{
@ -392,10 +398,10 @@ namespace Rssdp.Infrastructure
}
}
private void ProcessAliveNotification(HttpRequestMessage message, IPAddress IpAddress)
private void ProcessAliveNotification(HttpRequestMessage message, IPAddress IPAddress)
{
var location = GetFirstHeaderUriValue("Location", message);
if (location != null)
if (location is not null)
{
var device = new DiscoveredSsdpDevice()
{
@ -407,7 +413,7 @@ namespace Rssdp.Infrastructure
ResponseHeaders = message.Headers
};
AddOrUpdateDiscoveredDevice(device, IpAddress);
AddOrUpdateDiscoveredDevice(device, IPAddress);
}
}
@ -445,7 +451,7 @@ namespace Rssdp.Infrastructure
if (message.Headers.Contains(headerName))
{
message.Headers.TryGetValues(headerName, out values);
if (values != null)
if (values is not null)
{
retVal = values.FirstOrDefault();
}
@ -461,7 +467,7 @@ namespace Rssdp.Infrastructure
if (message.Headers.Contains(headerName))
{
message.Headers.TryGetValues(headerName, out values);
if (values != null)
if (values is not null)
{
retVal = values.FirstOrDefault();
}
@ -477,7 +483,7 @@ namespace Rssdp.Infrastructure
if (request.Headers.Contains(headerName))
{
request.Headers.TryGetValues(headerName, out values);
if (values != null)
if (values is not null)
{
value = values.FirstOrDefault();
}
@ -494,7 +500,7 @@ namespace Rssdp.Infrastructure
if (response.Headers.Contains(headerName))
{
response.Headers.TryGetValues(headerName, out values);
if (values != null)
if (values is not null)
{
value = values.FirstOrDefault();
}
@ -506,7 +512,7 @@ namespace Rssdp.Infrastructure
private TimeSpan CacheAgeFromHeader(System.Net.Http.Headers.CacheControlHeaderValue headerValue)
{
if (headerValue == null)
if (headerValue is null)
{
return TimeSpan.Zero;
}
@ -563,7 +569,7 @@ namespace Rssdp.Infrastructure
}
}
if (existingDevices != null && existingDevices.Count > 0)
if (existingDevices is not null && existingDevices.Count > 0)
{
foreach (var removedDevice in existingDevices)
{
@ -619,7 +625,7 @@ namespace Rssdp.Infrastructure
private void CommsServer_ResponseReceived(object sender, ResponseReceivedEventArgs e)
{
ProcessSearchResponseMessage(e.Message, e.LocalIpAddress);
ProcessSearchResponseMessage(e.Message, e.LocalIPAddress);
}
private void CommsServer_RequestReceived(object sender, RequestReceivedEventArgs e)

View File

@ -4,9 +4,9 @@ using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.Net;
namespace Rssdp.Infrastructure
{
@ -31,8 +31,6 @@ namespace Rssdp.Infrastructure
private Random _Random;
private const string ServerVersion = "1.0";
/// <summary>
/// Default constructor.
/// </summary>
@ -42,30 +40,9 @@ namespace Rssdp.Infrastructure
string osVersion,
bool sendOnlyMatchedHost)
{
if (communicationsServer == null)
{
throw new ArgumentNullException(nameof(communicationsServer));
}
if (osName == null)
{
throw new ArgumentNullException(nameof(osName));
}
if (osName.Length == 0)
{
throw new ArgumentException("osName cannot be an empty string.", nameof(osName));
}
if (osVersion == null)
{
throw new ArgumentNullException(nameof(osVersion));
}
if (osVersion.Length == 0)
{
throw new ArgumentException("osVersion cannot be an empty string.", nameof(osName));
}
ArgumentNullException.ThrowIfNull(communicationsServer);
ArgumentNullException.ThrowIfNullOrEmpty(osName);
ArgumentNullException.ThrowIfNullOrEmpty(osVersion);
_SupportPnpRootDevice = true;
_Devices = new List<SsdpRootDevice>();
@ -79,10 +56,13 @@ namespace Rssdp.Infrastructure
_OSVersion = osVersion;
_sendOnlyMatchedHost = sendOnlyMatchedHost;
_CommsServer.BeginListeningForBroadcasts();
_CommsServer.BeginListeningForMulticast();
// Send alive notification once on creation
SendAllAliveNotifications(null);
}
public void StartBroadcastingAliveMessages(TimeSpan interval)
public void StartSendingAliveNotifications(TimeSpan interval)
{
_RebroadcastAliveNotificationsTimer = new Timer(SendAllAliveNotifications, null, TimeSpan.FromSeconds(5), interval);
}
@ -98,10 +78,9 @@ namespace Rssdp.Infrastructure
/// <param name="device">The <see cref="SsdpDevice"/> instance to add.</param>
/// <exception cref="ArgumentNullException">Thrown if the <paramref name="device"/> argument is null.</exception>
/// <exception cref="InvalidOperationException">Thrown if the <paramref name="device"/> contains property values that are not acceptable to the UPnP 1.0 specification.</exception>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1804:RemoveUnusedLocals", MessageId = "t", Justification = "Capture task to local variable suppresses compiler warning, but task is not really needed.")]
public void AddDevice(SsdpRootDevice device)
{
if (device == null)
if (device is null)
{
throw new ArgumentNullException(nameof(device));
}
@ -137,7 +116,7 @@ namespace Rssdp.Infrastructure
/// <exception cref="ArgumentNullException">Thrown if the <paramref name="device"/> argument is null.</exception>
public async Task RemoveDevice(SsdpRootDevice device)
{
if (device == null)
if (device is null)
{
throw new ArgumentNullException(nameof(device));
}
@ -199,7 +178,7 @@ namespace Rssdp.Infrastructure
DisposeRebroadcastTimer();
var commsServer = _CommsServer;
if (commsServer != null)
if (commsServer is not null)
{
commsServer.RequestReceived -= this.CommsServer_RequestReceived;
}
@ -208,7 +187,7 @@ namespace Rssdp.Infrastructure
Task.WaitAll(tasks);
_CommsServer = null;
if (commsServer != null)
if (commsServer is not null)
{
if (!commsServer.IsShared)
{
@ -224,7 +203,7 @@ namespace Rssdp.Infrastructure
string mx,
string searchTarget,
IPEndPoint remoteEndPoint,
IPAddress receivedOnlocalIpAddress,
IPAddress receivedOnlocalIPAddress,
CancellationToken cancellationToken)
{
if (String.IsNullOrEmpty(searchTarget))
@ -280,27 +259,25 @@ namespace Rssdp.Infrastructure
}
else if (searchTarget.Trim().StartsWith("uuid:", StringComparison.OrdinalIgnoreCase))
{
devices = (from device in GetAllDevicesAsFlatEnumerable() where String.Compare(device.Uuid, searchTarget.Substring(5), StringComparison.OrdinalIgnoreCase) == 0 select device).ToArray();
devices = GetAllDevicesAsFlatEnumerable().Where(d => String.Compare(d.Uuid, searchTarget.Substring(5), StringComparison.OrdinalIgnoreCase) == 0).ToArray();
}
else if (searchTarget.StartsWith("urn:", StringComparison.OrdinalIgnoreCase))
{
devices = (from device in GetAllDevicesAsFlatEnumerable() where String.Compare(device.FullDeviceType, searchTarget, StringComparison.OrdinalIgnoreCase) == 0 select device).ToArray();
devices = GetAllDevicesAsFlatEnumerable().Where(d => String.Compare(d.FullDeviceType, searchTarget, StringComparison.OrdinalIgnoreCase) == 0).ToArray();
}
}
if (devices != null)
if (devices is not null)
{
var deviceList = devices.ToList();
// WriteTrace(String.Format("Sending {0} search responses", deviceList.Count));
foreach (var device in deviceList)
foreach (var device in devices)
{
var root = device.ToRootDevice();
var source = new IPNetAddress(root.Address, root.PrefixLength);
var destination = new IPNetAddress(remoteEndPoint.Address, root.PrefixLength);
if (!_sendOnlyMatchedHost || source.NetworkAddress.Equals(destination.NetworkAddress))
if (!_sendOnlyMatchedHost || root.Address.Equals(receivedOnlocalIPAddress))
{
SendDeviceSearchResponses(device, remoteEndPoint, receivedOnlocalIpAddress, cancellationToken);
SendDeviceSearchResponses(device, remoteEndPoint, receivedOnlocalIPAddress, cancellationToken);
}
}
}
@ -315,22 +292,22 @@ namespace Rssdp.Infrastructure
private void SendDeviceSearchResponses(
SsdpDevice device,
IPEndPoint endPoint,
IPAddress receivedOnlocalIpAddress,
IPAddress receivedOnlocalIPAddress,
CancellationToken cancellationToken)
{
bool isRootDevice = (device as SsdpRootDevice) != null;
bool isRootDevice = (device as SsdpRootDevice) is not null;
if (isRootDevice)
{
SendSearchResponse(SsdpConstants.UpnpDeviceTypeRootDevice, device, GetUsn(device.Udn, SsdpConstants.UpnpDeviceTypeRootDevice), endPoint, receivedOnlocalIpAddress, cancellationToken);
SendSearchResponse(SsdpConstants.UpnpDeviceTypeRootDevice, device, GetUsn(device.Udn, SsdpConstants.UpnpDeviceTypeRootDevice), endPoint, receivedOnlocalIPAddress, cancellationToken);
if (this.SupportPnpRootDevice)
{
SendSearchResponse(SsdpConstants.PnpDeviceTypeRootDevice, device, GetUsn(device.Udn, SsdpConstants.PnpDeviceTypeRootDevice), endPoint, receivedOnlocalIpAddress, cancellationToken);
SendSearchResponse(SsdpConstants.PnpDeviceTypeRootDevice, device, GetUsn(device.Udn, SsdpConstants.PnpDeviceTypeRootDevice), endPoint, receivedOnlocalIPAddress, cancellationToken);
}
}
SendSearchResponse(device.Udn, device, device.Udn, endPoint, receivedOnlocalIpAddress, cancellationToken);
SendSearchResponse(device.Udn, device, device.Udn, endPoint, receivedOnlocalIPAddress, cancellationToken);
SendSearchResponse(device.FullDeviceType, device, GetUsn(device.Udn, device.FullDeviceType), endPoint, receivedOnlocalIpAddress, cancellationToken);
SendSearchResponse(device.FullDeviceType, device, GetUsn(device.Udn, device.FullDeviceType), endPoint, receivedOnlocalIPAddress, cancellationToken);
}
private string GetUsn(string udn, string fullDeviceType)
@ -343,22 +320,20 @@ namespace Rssdp.Infrastructure
SsdpDevice device,
string uniqueServiceName,
IPEndPoint endPoint,
IPAddress receivedOnlocalIpAddress,
IPAddress receivedOnlocalIPAddress,
CancellationToken cancellationToken)
{
var rootDevice = device.ToRootDevice();
// var additionalheaders = FormatCustomHeadersForResponse(device);
const string header = "HTTP/1.1 200 OK";
var rootDevice = device.ToRootDevice();
var values = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
values["EXT"] = "";
values["DATE"] = DateTime.UtcNow.ToString("r");
values["HOST"] = "239.255.255.250:1900";
values["CACHE-CONTROL"] = "max-age = " + rootDevice.CacheLifetime.TotalSeconds;
values["ST"] = searchTarget;
values["SERVER"] = string.Format(CultureInfo.InvariantCulture, "{0}/{1} UPnP/1.0 RSSDP/{2}", _OSName, _OSVersion, ServerVersion);
values["SERVER"] = string.Format(CultureInfo.InvariantCulture, "{0}/{1} UPnP/1.0 RSSDP/{2}", _OSName, _OSVersion, SsdpConstants.ServerVersion);
values["USN"] = uniqueServiceName;
values["LOCATION"] = rootDevice.Location.ToString();
@ -367,9 +342,9 @@ namespace Rssdp.Infrastructure
try
{
await _CommsServer.SendMessage(
System.Text.Encoding.UTF8.GetBytes(message),
Encoding.UTF8.GetBytes(message),
endPoint,
receivedOnlocalIpAddress,
receivedOnlocalIPAddress,
cancellationToken)
.ConfigureAwait(false);
}
@ -492,7 +467,7 @@ namespace Rssdp.Infrastructure
values["DATE"] = DateTime.UtcNow.ToString("r");
values["CACHE-CONTROL"] = "max-age = " + rootDevice.CacheLifetime.TotalSeconds;
values["LOCATION"] = rootDevice.Location.ToString();
values["SERVER"] = string.Format(CultureInfo.InvariantCulture, "{0}/{1} UPnP/1.0 RSSDP/{2}", _OSName, _OSVersion, ServerVersion);
values["SERVER"] = string.Format(CultureInfo.InvariantCulture, "{0}/{1} UPnP/1.0 RSSDP/{2}", _OSName, _OSVersion, SsdpConstants.ServerVersion);
values["NTS"] = "ssdp:alive";
values["NT"] = notificationType;
values["USN"] = uniqueServiceName;
@ -527,7 +502,6 @@ namespace Rssdp.Infrastructure
return Task.WhenAll(tasks);
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "byebye", Justification = "Correct value for this type of notification in SSDP.")]
private Task SendByeByeNotification(SsdpDevice device, string notificationType, string uniqueServiceName, CancellationToken cancellationToken)
{
const string header = "NOTIFY * HTTP/1.1";
@ -537,7 +511,7 @@ namespace Rssdp.Infrastructure
// If needed later for non-server devices, these headers will need to be dynamic
values["HOST"] = "239.255.255.250:1900";
values["DATE"] = DateTime.UtcNow.ToString("r");
values["SERVER"] = string.Format(CultureInfo.InvariantCulture, "{0}/{1} UPnP/1.0 RSSDP/{2}", _OSName, _OSVersion, ServerVersion);
values["SERVER"] = string.Format(CultureInfo.InvariantCulture, "{0}/{1} UPnP/1.0 RSSDP/{2}", _OSName, _OSVersion, SsdpConstants.ServerVersion);
values["NTS"] = "ssdp:byebye";
values["NT"] = notificationType;
values["USN"] = uniqueServiceName;
@ -553,7 +527,7 @@ namespace Rssdp.Infrastructure
{
var timer = _RebroadcastAliveNotificationsTimer;
_RebroadcastAliveNotificationsTimer = null;
if (timer != null)
if (timer is not null)
{
timer.Dispose();
}
@ -578,7 +552,7 @@ namespace Rssdp.Infrastructure
private string GetFirstHeaderValue(System.Net.Http.Headers.HttpRequestHeaders httpRequestHeaders, string headerName)
{
string retVal = null;
if (httpRequestHeaders.TryGetValues(headerName, out var values) && values != null)
if (httpRequestHeaders.TryGetValues(headerName, out var values) && values is not null)
{
retVal = values.FirstOrDefault();
}
@ -590,7 +564,7 @@ namespace Rssdp.Infrastructure
private void WriteTrace(string text)
{
if (LogFunction != null)
if (LogFunction is not null)
{
LogFunction(text);
}
@ -600,7 +574,7 @@ namespace Rssdp.Infrastructure
private void WriteTrace(string text, SsdpDevice device)
{
var rootDevice = device as SsdpRootDevice;
if (rootDevice != null)
if (rootDevice is not null)
{
WriteTrace(text + " " + device.DeviceType + " - " + device.Uuid + " - " + rootDevice.Location);
}
@ -626,7 +600,7 @@ namespace Rssdp.Infrastructure
// else if (!e.Message.Headers.Contains("MAN"))
// WriteTrace("Ignoring search request - missing MAN header.");
// else
ProcessSearchRequest(GetFirstHeaderValue(e.Message.Headers, "MX"), GetFirstHeaderValue(e.Message.Headers, "ST"), e.ReceivedFrom, e.LocalIpAddress, CancellationToken.None);
ProcessSearchRequest(GetFirstHeaderValue(e.Message.Headers, "MX"), GetFirstHeaderValue(e.Message.Headers, "ST"), e.ReceivedFrom, e.LocalIPAddress, CancellationToken.None);
}
}

View File

@ -1,49 +0,0 @@
using FsCheck;
using FsCheck.Xunit;
using MediaBrowser.Common.Net;
using Xunit;
namespace Jellyfin.Networking.Tests
{
public static class IPNetAddressTests
{
/// <summary>
/// Checks IP address formats.
/// </summary>
/// <param name="address">IP Address.</param>
[Theory]
[InlineData("127.0.0.1")]
[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]")]
[InlineData("fe80::7add:12ff:febb:c67b%16")]
[InlineData("[fe80::7add:12ff:febb:c67b%16]:123")]
[InlineData("fe80::7add:12ff:febb:c67b%16:123")]
[InlineData("[fe80::7add:12ff:febb:c67b%16]")]
[InlineData("192.168.1.2/255.255.255.0")]
[InlineData("192.168.1.2/24")]
public static void TryParse_ValidIPStrings_True(string address)
=> Assert.True(IPNetAddress.TryParse(address, out _));
[Property]
public static Property TryParse_IPv4Address_True(IPv4Address address)
=> IPNetAddress.TryParse(address.Item.ToString(), out _).ToProperty();
[Property]
public static Property TryParse_IPv6Address_True(IPv6Address address)
=> IPNetAddress.TryParse(address.Item.ToString(), out _).ToProperty();
/// <summary>
/// All should be invalid address strings.
/// </summary>
/// <param name="address">Invalid address strings.</param>
[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")]
[InlineData("[fd23:184f:2029:0:3139:7386:67d7:d517:1231]")]
public static void TryParse_InvalidAddressString_False(string address)
=> Assert.False(IPNetAddress.TryParse(address, out _));
}
}

View File

@ -5,7 +5,7 @@ using Xunit;
namespace Jellyfin.Networking.Tests
{
public static class IPHostTests
public static class NetworkExtensionsTests
{
/// <summary>
/// Checks IP address formats.
@ -27,15 +27,15 @@ namespace Jellyfin.Networking.Tests
[InlineData("192.168.1.2/255.255.255.0")]
[InlineData("192.168.1.2/24")]
public static void TryParse_ValidHostStrings_True(string address)
=> Assert.True(IPHost.TryParse(address, out _));
=> Assert.True(NetworkExtensions.TryParseHost(address, out _, true, true));
[Property]
public static Property TryParse_IPv4Address_True(IPv4Address address)
=> IPHost.TryParse(address.Item.ToString(), out _).ToProperty();
=> NetworkExtensions.TryParseHost(address.Item.ToString(), out _, true, true).ToProperty();
[Property]
public static Property TryParse_IPv6Address_True(IPv6Address address)
=> IPHost.TryParse(address.Item.ToString(), out _).ToProperty();
=> NetworkExtensions.TryParseHost(address.Item.ToString(), out _, true, true).ToProperty();
/// <summary>
/// All should be invalid address strings.
@ -48,6 +48,6 @@ namespace Jellyfin.Networking.Tests
[InlineData("fd23:184f:2029:0:3139:7386:67d7:d517:1231")]
[InlineData("[fd23:184f:2029:0:3139:7386:67d7:d517:1231]")]
public static void TryParse_InvalidAddressString_False(string address)
=> Assert.False(IPHost.TryParse(address, out _));
=> Assert.False(NetworkExtensions.TryParseHost(address, out _, true, true));
}
}

View File

@ -23,8 +23,8 @@ namespace Jellyfin.Networking.Tests
var ip = IPAddress.Parse(value);
var conf = new NetworkConfiguration()
{
EnableIPV6 = true,
EnableIPV4 = true,
EnableIPv6 = true,
EnableIPv4 = true,
LocalNetworkSubnets = network.Split(',')
};
@ -51,8 +51,8 @@ namespace Jellyfin.Networking.Tests
var ip = IPAddress.Parse(value);
var conf = new NetworkConfiguration()
{
EnableIPV6 = true,
EnableIPV4 = true,
EnableIPv6 = true,
EnableIPv4 = true,
LocalNetworkSubnets = network.Split(',')
};

View File

@ -1,10 +1,12 @@
using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Jellyfin.Networking.Configuration;
using Jellyfin.Networking.Manager;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Net;
using MediaBrowser.Model.Net;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using Xunit;
@ -34,6 +36,8 @@ namespace Jellyfin.Networking.Tests
[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]")]
// eth16 only
[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]")]
// eth16 only without mask
[InlineData("192.168.1.208,-16,eth16|200.200.200.200,11,eth11", "192.168.1.0/24", "[192.168.1.208/32]")]
// All interfaces excluded. (including loopbacks)
[InlineData("192.168.1.208/24,-16,vEthernet1|192.168.2.208/24,-16,vEthernet212|200.200.200.200/24,11,eth11", "192.168.1.0/24", "[]")]
// vEthernet1 and vEthernet212 should be excluded.
@ -44,8 +48,8 @@ namespace Jellyfin.Networking.Tests
{
var conf = new NetworkConfiguration()
{
EnableIPV6 = true,
EnableIPV4 = true,
EnableIPv6 = true,
EnableIPv4 = true,
LocalNetworkSubnets = lan?.Split(';') ?? throw new ArgumentNullException(nameof(lan))
};
@ -53,162 +57,97 @@ namespace Jellyfin.Networking.Tests
using var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>());
NetworkManager.MockNetworkSettings = string.Empty;
Assert.Equal(nm.GetInternalBindAddresses().AsString(), value);
Assert.Equal(value, "[" + string.Join(",", nm.GetInternalBindAddresses().Select(x => x.Address + "/" + x.Subnet.PrefixLength)) + "]");
}
/// <summary>
/// Test collection parsing.
/// Checks valid IP address formats.
/// </summary>
/// <param name="settings">Collection to parse.</param>
/// <param name="result1">Included addresses from the collection.</param>
/// <param name="result2">Included IP4 addresses from the collection.</param>
/// <param name="result3">Excluded addresses from the collection.</param>
/// <param name="result4">Excluded IP4 addresses from the collection.</param>
/// <param name="result5">Network addresses of the collection.</param>
/// <param name="address">IP Address.</param>
[Theory]
[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/16, localhost, fd23:184f:2029:0:3139:7386:67d7:d517, !10.10.10.10",
"[192.158.1.2/16,[127.0.0.1/32,::1/128],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,::1/128,fd23:184f:2029:0:3139:7386:67d7:d517/128]")]
[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]")]
public void TestCollections(string settings, string result1, string result2, string result3, string result4, string result5)
{
ArgumentNullException.ThrowIfNull(settings);
var conf = new NetworkConfiguration()
{
EnableIPV6 = true,
EnableIPV4 = true,
};
using var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>());
// Test included.
Collection<IPObject> nc = nm.CreateIPCollection(settings.Split(','), false);
Assert.Equal(nc.AsString(), result1);
// Test excluded.
nc = nm.CreateIPCollection(settings.Split(','), true);
Assert.Equal(nc.AsString(), result3);
conf.EnableIPV6 = false;
nm.UpdateSettings(conf);
// Test IP4 included.
nc = nm.CreateIPCollection(settings.Split(','), false);
Assert.Equal(nc.AsString(), result2);
// Test IP4 excluded.
nc = nm.CreateIPCollection(settings.Split(','), true);
Assert.Equal(nc.AsString(), result4);
conf.EnableIPV6 = true;
nm.UpdateSettings(conf);
// Test network addresses of collection.
nc = nm.CreateIPCollection(settings.Split(','), false);
nc = nc.AsNetworks();
Assert.Equal(nc.AsString(), result5);
}
[InlineData("127.0.0.1")]
[InlineData("127.0.0.1/8")]
[InlineData("192.168.1.2")]
[InlineData("192.168.1.2/24")]
[InlineData("192.168.1.2/255.255.255.0")]
[InlineData("fd23:184f:2029:0:3139:7386:67d7:d517")]
[InlineData("[fd23:184f:2029:0:3139:7386:67d7:d517]")]
[InlineData("fe80::7add:12ff:febb:c67b%16")]
[InlineData("[fe80::7add:12ff:febb:c67b%16]:123")]
[InlineData("fe80::7add:12ff:febb:c67b%16:123")]
[InlineData("[fe80::7add:12ff:febb:c67b%16]")]
[InlineData("fd23:184f:2029:0:3139:7386:67d7:d517/56")]
public static void TryParseValidIPStringsTrue(string address)
=> Assert.True(NetworkExtensions.TryParseToSubnet(address, out _));
/// <summary>
/// Union two collections.
/// Checks invalid IP address formats.
/// </summary>
/// <param name="settings">Source.</param>
/// <param name="compare">Destination.</param>
/// <param name="result">Result.</param>
/// <param name="address">IP Address.</param>
[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)
{
ArgumentNullException.ThrowIfNull(settings);
ArgumentNullException.ThrowIfNull(compare);
ArgumentNullException.ThrowIfNull(result);
var conf = new NetworkConfiguration()
{
EnableIPV6 = true,
EnableIPV4 = true,
};
using var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>());
Collection<IPObject> nc1 = nm.CreateIPCollection(settings.Split(','), false);
Collection<IPObject> nc2 = nm.CreateIPCollection(compare.Split(','), false);
Assert.Equal(nc1.ThatAreContainedInNetworks(nc2).AsString(), result);
}
[InlineData("127.0.0.1#")]
[InlineData("localhost!")]
[InlineData("256.128.0.0.0.1")]
[InlineData("fd23:184f:2029:0:3139:7386:67d7:d517:1231")]
[InlineData("[fd23:184f:2029:0:3139:7386:67d7:d517:1231]")]
public static void TryParseInvalidIPStringsFalse(string address)
=> Assert.False(NetworkExtensions.TryParseToSubnet(address, out _));
/// <summary>
/// Checks if IPv4 address is within a defined subnet.
/// </summary>
/// <param name="netMask">Network mask.</param>
/// <param name="IPAddress">IP Address.</param>
[Theory]
[InlineData("192.168.5.85/24", "192.168.5.1")]
[InlineData("192.168.5.85/24", "192.168.5.254")]
[InlineData("192.168.5.85/255.255.255.0", "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("10.128.240.50/255.255.255.252", "10.128.240.51")]
[InlineData("127.0.0.1/8", "127.0.0.1")]
public void IpV4SubnetMaskMatchesValidIpAddress(string netMask, string ipAddress)
public void IPv4SubnetMaskMatchesValidIPAddress(string netMask, string ipAddress)
{
var ipAddressObj = IPNetAddress.Parse(netMask);
Assert.True(ipAddressObj.Contains(IPAddress.Parse(ipAddress)));
var ipa = IPAddress.Parse(ipAddress);
Assert.True(NetworkExtensions.TryParseToSubnet(netMask, out var subnet) && subnet.Contains(IPAddress.Parse(ipAddress)));
}
/// <summary>
/// Checks if IPv4 address is not within a defined subnet.
/// </summary>
/// <param name="netMask">Network mask.</param>
/// <param name="ipAddress">IP Address.</param>
[Theory]
[InlineData("192.168.5.85/24", "192.168.4.254")]
[InlineData("192.168.5.85/24", "191.168.5.254")]
[InlineData("192.168.5.85/255.255.255.252", "192.168.4.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)
[InlineData("10.128.240.50/255.255.255.252", "10.127.240.51")]
public void IPv4SubnetMaskDoesNotMatchInvalidIPAddress(string netMask, string ipAddress)
{
var ipAddressObj = IPNetAddress.Parse(netMask);
Assert.False(ipAddressObj.Contains(IPAddress.Parse(ipAddress)));
var ipa = IPAddress.Parse(ipAddress);
Assert.False(NetworkExtensions.TryParseToSubnet(netMask, out var subnet) && subnet.Contains(IPAddress.Parse(ipAddress)));
}
/// <summary>
/// Checks if IPv6 address is within a defined subnet.
/// </summary>
/// <param name="netMask">Network mask.</param>
/// <param name="ipAddress">IP Address.</param>
[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)
public void IPv6SubnetMaskMatchesValidIPAddress(string netMask, string ipAddress)
{
var ipAddressObj = IPNetAddress.Parse(netMask);
Assert.True(ipAddressObj.Contains(IPAddress.Parse(ipAddress)));
Assert.True(NetworkExtensions.TryParseToSubnet(netMask, out var subnet) && subnet.Contains(IPAddress.Parse(ipAddress)));
}
[Theory]
@ -217,79 +156,16 @@ namespace Jellyfin.Networking.Tests
[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)
public void IPv6SubnetMaskDoesNotMatchInvalidIPAddress(string netMask, string ipAddress)
{
var ipAddressObj = IPNetAddress.Parse(netMask);
Assert.False(ipAddressObj.Contains(IPAddress.Parse(ipAddress)));
Assert.False(NetworkExtensions.TryParseToSubnet(netMask, out var subnet) && subnet.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 TestSubnetContains(string network, string ip)
{
Assert.True(IPNetAddress.TryParse(network, out var networkObj));
Assert.True(IPNetAddress.TryParse(ip, out var ipObj));
Assert.True(networkObj.Contains(ipObj));
}
[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 TestCollectionEquality(string source, string dest, string result)
{
ArgumentNullException.ThrowIfNull(source);
ArgumentNullException.ThrowIfNull(dest);
ArgumentNullException.ThrowIfNull(result);
var conf = new NetworkConfiguration()
{
EnableIPV6 = true,
EnableIPV4 = true
};
using var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>());
// Test included, IP6.
Collection<IPObject> ncSource = nm.CreateIPCollection(source.Split(','));
Collection<IPObject> ncDest = nm.CreateIPCollection(dest.Split(','));
Collection<IPObject> ncResult = ncSource.ThatAreContainedInNetworks(ncDest);
Collection<IPObject> resultCollection = nm.CreateIPCollection(result.Split(','));
Assert.True(ncResult.Compare(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.
// On my system eth16 is internal, eth11 external (Windows defines the indexes).
//
// This test is to replicate how DNLA requests work throughout the system.
// This test is to replicate how DLNA 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")]
@ -319,23 +195,24 @@ namespace Jellyfin.Networking.Tests
var conf = new NetworkConfiguration()
{
LocalNetworkAddresses = bindAddresses.Split(','),
EnableIPV6 = ipv6enabled,
EnableIPV4 = true
EnableIPv6 = ipv6enabled,
EnableIPv4 = true
};
NetworkManager.MockNetworkSettings = "192.168.1.208/24,-16,eth16|200.200.200.200/24,11,eth11";
using var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>());
NetworkManager.MockNetworkSettings = string.Empty;
_ = nm.TryParseInterface(result, out Collection<IPObject>? resultObj);
// Check to see if dns resolution is working. If not, skip test.
_ = IPHost.TryParse(source, out var host);
if (resultObj is not null && host?.HasAddress == true)
// Check to see if DNS resolution is working. If not, skip test.
if (!NetworkExtensions.TryParseHost(source, out var host))
{
result = ((IPNetAddress)resultObj[0]).ToString(true);
var intf = nm.GetBindInterface(source, out _);
return;
}
if (nm.TryParseInterface(result, out var resultObj))
{
result = resultObj.First().Address.ToString();
var intf = nm.GetBindAddress(source, out _);
Assert.Equal(intf, result);
}
@ -363,8 +240,8 @@ namespace Jellyfin.Networking.Tests
// User on external network, internal binding only - so assumption 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 overridden.
[InlineData("jellyfin.org", "192.168.1.0/24", "", 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")]
@ -381,8 +258,8 @@ namespace Jellyfin.Networking.Tests
{
LocalNetworkSubnets = lan.Split(','),
LocalNetworkAddresses = bindAddresses.Split(','),
EnableIPV6 = ipv6enabled,
EnableIPV4 = true,
EnableIPv6 = ipv6enabled,
EnableIPv4 = true,
PublishedServerUriBySubnet = new string[] { publishedServers }
};
@ -390,15 +267,15 @@ namespace Jellyfin.Networking.Tests
using var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>());
NetworkManager.MockNetworkSettings = string.Empty;
if (nm.TryParseInterface(result, out Collection<IPObject>? resultObj) && resultObj is not null)
if (nm.TryParseInterface(result, out IReadOnlyList<IPData>? resultObj) && resultObj is not null)
{
// Parse out IPAddresses so we can do a string comparison. (Ignore subnet masks).
result = ((IPNetAddress)resultObj[0]).ToString(true);
// Parse out IPAddresses so we can do a string comparison (ignore subnet masks).
result = resultObj.First().Address.ToString();
}
var intf = nm.GetBindInterface(source, out int? _);
var intf = nm.GetBindAddress(source, out int? _);
Assert.Equal(intf, result);
Assert.Equal(result, intf);
}
[Theory]
@ -406,39 +283,40 @@ namespace Jellyfin.Networking.Tests
[InlineData("185.10.10.10", "185.10.10.10", false)]
[InlineData("", "100.100.100.100", false)]
public void HasRemoteAccess_GivenWhitelist_AllowsOnlyIpsInWhitelist(string addresses, string remoteIp, bool denied)
public void HasRemoteAccess_GivenWhitelist_AllowsOnlyIPsInWhitelist(string addresses, string remoteIP, bool denied)
{
// 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.
var conf = new NetworkConfiguration()
{
EnableIPV4 = true,
EnableIPv4 = true,
RemoteIPFilter = addresses.Split(','),
IsRemoteIPFilterBlacklist = false
};
using var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>());
Assert.NotEqual(nm.HasRemoteAccess(IPAddress.Parse(remoteIp)), denied);
Assert.NotEqual(nm.HasRemoteAccess(IPAddress.Parse(remoteIP)), denied);
}
[Theory]
[InlineData("185.10.10.10", "79.2.3.4", false)]
[InlineData("185.10.10.10", "185.10.10.10", true)]
[InlineData("", "100.100.100.100", false)]
public void HasRemoteAccess_GivenBlacklist_BlacklistTheIps(string addresses, string remoteIp, bool denied)
public void HasRemoteAccess_GivenBlacklist_BlacklistTheIPs(string addresses, string remoteIP, bool denied)
{
// 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.
var conf = new NetworkConfiguration()
{
EnableIPV4 = true,
EnableIPv4 = true,
RemoteIPFilter = addresses.Split(','),
IsRemoteIPFilterBlacklist = true
};
using var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>());
Assert.NotEqual(nm.HasRemoteAccess(IPAddress.Parse(remoteIp)), denied);
Assert.NotEqual(nm.HasRemoteAccess(IPAddress.Parse(remoteIP)), denied);
}
[Theory]
@ -450,7 +328,7 @@ namespace Jellyfin.Networking.Tests
{
var conf = new NetworkConfiguration
{
EnableIPV4 = true,
EnableIPv4 = true,
LocalNetworkSubnets = lan.Split(','),
LocalNetworkAddresses = bind.Split(',')
};
@ -458,7 +336,7 @@ namespace Jellyfin.Networking.Tests
NetworkManager.MockNetworkSettings = interfaces;
using var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>());
var interfaceToUse = nm.GetBindInterface(string.Empty, out _);
var interfaceToUse = nm.GetBindAddress(string.Empty, out _);
Assert.Equal(result, interfaceToUse);
}
@ -474,7 +352,7 @@ namespace Jellyfin.Networking.Tests
{
var conf = new NetworkConfiguration
{
EnableIPV4 = true,
EnableIPv4 = true,
LocalNetworkSubnets = lan.Split(','),
LocalNetworkAddresses = bind.Split(',')
};
@ -482,7 +360,7 @@ namespace Jellyfin.Networking.Tests
NetworkManager.MockNetworkSettings = interfaces;
using var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>());
var interfaceToUse = nm.GetBindInterface(source, out _);
var interfaceToUse = nm.GetBindAddress(source, out _);
Assert.Equal(result, interfaceToUse);
}

View File

@ -21,9 +21,9 @@ namespace Jellyfin.Server.Tests
data.Add(
true,
true,
new string[] { "192.168.t", "127.0.0.1", "1234.1232.12.1234" },
new IPAddress[] { IPAddress.Loopback.MapToIPv6() },
Array.Empty<IPNetwork>());
new string[] { "192.168.t", "127.0.0.1", "::1", "1234.1232.12.1234" },
new IPAddress[] { IPAddress.Loopback },
new IPNetwork[] { new IPNetwork(IPAddress.IPv6Loopback, 128) });
data.Add(
true,
@ -64,7 +64,7 @@ namespace Jellyfin.Server.Tests
true,
true,
new string[] { "localhost" },
new IPAddress[] { IPAddress.Loopback.MapToIPv6() },
new IPAddress[] { IPAddress.Loopback },
new IPNetwork[] { new IPNetwork(IPAddress.IPv6Loopback, 128) });
return data;
}
@ -77,8 +77,8 @@ namespace Jellyfin.Server.Tests
var settings = new NetworkConfiguration
{
EnableIPV4 = ip4,
EnableIPV6 = ip6
EnableIPv4 = ip4,
EnableIPv6 = ip6
};
ForwardedHeadersOptions options = new ForwardedHeadersOptions();
@ -116,8 +116,8 @@ namespace Jellyfin.Server.Tests
{
var conf = new NetworkConfiguration()
{
EnableIPV6 = true,
EnableIPV4 = true,
EnableIPv6 = true,
EnableIPv4 = true,
};
return new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>());