mirror of
https://github.com/jellyfin/jellyfin.git
synced 2025-04-24 14:08:44 -04:00
Move subnet parser to NetworkExtensions
This commit is contained in:
parent
daf33143f6
commit
64ffd5fd95
2 changed files with 62 additions and 68 deletions
|
@ -326,8 +326,8 @@ namespace Jellyfin.Networking.Manager
|
||||||
// Get configuration options
|
// Get configuration options
|
||||||
string[] subnets = config.LocalNetworkSubnets;
|
string[] subnets = config.LocalNetworkSubnets;
|
||||||
|
|
||||||
_ = TryParseSubnets(subnets, out _lanSubnets, false);
|
_ = NetworkExtensions.TryParseSubnets(subnets, out _lanSubnets, false);
|
||||||
_ = TryParseSubnets(subnets, out _excludedSubnets, true);
|
_ = NetworkExtensions.TryParseSubnets(subnets, out _excludedSubnets, true);
|
||||||
|
|
||||||
if (_lanSubnets.Count == 0)
|
if (_lanSubnets.Count == 0)
|
||||||
{
|
{
|
||||||
|
@ -419,7 +419,7 @@ namespace Jellyfin.Networking.Manager
|
||||||
if (remoteIPFilter.Any() && !string.IsNullOrWhiteSpace(remoteIPFilter.First()))
|
if (remoteIPFilter.Any() && !string.IsNullOrWhiteSpace(remoteIPFilter.First()))
|
||||||
{
|
{
|
||||||
// Parse all IPs with netmask to a subnet
|
// Parse all IPs with netmask to a subnet
|
||||||
_ = TryParseSubnets(remoteIPFilter.Where(x => x.Contains('/', StringComparison.OrdinalIgnoreCase)).ToArray(), out _remoteAddressFilter, false);
|
_ = NetworkExtensions.TryParseSubnets(remoteIPFilter.Where(x => x.Contains('/', StringComparison.OrdinalIgnoreCase)).ToArray(), out _remoteAddressFilter, false);
|
||||||
|
|
||||||
// Parse everything else as an IP and construct subnet with a single IP
|
// Parse everything else as an IP and construct subnet with a single IP
|
||||||
var ips = remoteIPFilter.Where(x => !x.Contains('/', StringComparison.OrdinalIgnoreCase));
|
var ips = remoteIPFilter.Where(x => !x.Contains('/', StringComparison.OrdinalIgnoreCase));
|
||||||
|
@ -595,68 +595,6 @@ namespace Jellyfin.Networking.Manager
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Try parsing an array of strings into subnets, respecting exclusions.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="values">Input string 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 bool TryParseSubnets(string[] values, out Collection<IPNetwork> result, bool negated = false)
|
|
||||||
{
|
|
||||||
result = new Collection<IPNetwork>();
|
|
||||||
|
|
||||||
if (values == null || values.Length == 0)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int a = 0; a < values.Length; a++)
|
|
||||||
{
|
|
||||||
string[] v = values[a].Trim().Split("/");
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var address = IPAddress.None;
|
|
||||||
if (negated && v[0].StartsWith('!'))
|
|
||||||
{
|
|
||||||
_ = IPAddress.TryParse(v[0][1..], out address);
|
|
||||||
}
|
|
||||||
else if (!negated)
|
|
||||||
{
|
|
||||||
_ = IPAddress.TryParse(v[0][0..], out address);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (address != IPAddress.None && address != null)
|
|
||||||
{
|
|
||||||
if (int.TryParse(v[1], out var netmask))
|
|
||||||
{
|
|
||||||
result.Add(new IPNetwork(address, netmask));
|
|
||||||
}
|
|
||||||
else if (address.AddressFamily == AddressFamily.InterNetwork)
|
|
||||||
{
|
|
||||||
result.Add(new IPNetwork(address, 32));
|
|
||||||
}
|
|
||||||
else if (address.AddressFamily == AddressFamily.InterNetworkV6)
|
|
||||||
{
|
|
||||||
result.Add(new IPNetwork(address, 128));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (ArgumentException e)
|
|
||||||
{
|
|
||||||
_logger.LogWarning(e, "Ignoring LAN value {Value}.", v);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (result.Count > 0)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public bool HasRemoteAccess(IPAddress remoteIp)
|
public bool HasRemoteAccess(IPAddress remoteIp)
|
||||||
{
|
{
|
||||||
|
@ -802,12 +740,12 @@ namespace Jellyfin.Networking.Manager
|
||||||
|
|
||||||
if (source != null)
|
if (source != null)
|
||||||
{
|
{
|
||||||
if (IsIpv4Enabled && source.AddressFamily == AddressFamily.InterNetworkV6)
|
if (IsIpv4Enabled && !IsIpv6Enabled && source.AddressFamily == AddressFamily.InterNetworkV6)
|
||||||
{
|
{
|
||||||
_logger.LogWarning("IPv6 is disabled in Jellyfin, but enabled in the OS. This may affect how the interface is selected.");
|
_logger.LogWarning("IPv6 is disabled in Jellyfin, but enabled in the OS. This may affect how the interface is selected.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (IsIpv6Enabled && source.AddressFamily == AddressFamily.InterNetwork)
|
if (!IsIpv4Enabled && IsIpv6Enabled && source.AddressFamily == AddressFamily.InterNetwork)
|
||||||
{
|
{
|
||||||
_logger.LogWarning("IPv4 is disabled in Jellyfin, but enabled in the OS. This may affect how the interface is selected.");
|
_logger.LogWarning("IPv4 is disabled in Jellyfin, but enabled in the OS. This may affect how the interface is selected.");
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
|
using Microsoft.AspNetCore.HttpOverrides;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.ObjectModel;
|
||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
|
@ -136,6 +137,61 @@ namespace MediaBrowser.Common.Net
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Try parsing an array of strings into subnets, respecting exclusions.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="values">Input string 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 TryParseSubnets(string[] values, out Collection<IPNetwork> result, bool negated = false)
|
||||||
|
{
|
||||||
|
result = new Collection<IPNetwork>();
|
||||||
|
|
||||||
|
if (values == null || values.Length == 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int a = 0; a < values.Length; a++)
|
||||||
|
{
|
||||||
|
string[] v = values[a].Trim().Split("/");
|
||||||
|
|
||||||
|
var address = IPAddress.None;
|
||||||
|
if (negated && v[0].StartsWith('!'))
|
||||||
|
{
|
||||||
|
_ = IPAddress.TryParse(v[0][1..], out address);
|
||||||
|
}
|
||||||
|
else if (!negated)
|
||||||
|
{
|
||||||
|
_ = IPAddress.TryParse(v[0][0..], out address);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (address != IPAddress.None && address != null)
|
||||||
|
{
|
||||||
|
if (int.TryParse(v[1], out var netmask))
|
||||||
|
{
|
||||||
|
result.Add(new IPNetwork(address, netmask));
|
||||||
|
}
|
||||||
|
else if (address.AddressFamily == AddressFamily.InterNetwork)
|
||||||
|
{
|
||||||
|
result.Add(new IPNetwork(address, 32));
|
||||||
|
}
|
||||||
|
else if (address.AddressFamily == AddressFamily.InterNetworkV6)
|
||||||
|
{
|
||||||
|
result.Add(new IPNetwork(address, 128));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result.Count > 0)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Attempts to parse a host string.
|
/// Attempts to parse a host string.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue