Updating to latest network code.

pull/4125/head
Greenback 4 years ago
parent c54eb2d2df
commit face3f8c65

@ -10,8 +10,6 @@ namespace Jellyfin.Networking.Configuration
/// </summary> /// </summary>
public class NetworkConfiguration public class NetworkConfiguration
{ {
private string _baseUrl = string.Empty;
/// <summary> /// <summary>
/// Gets the default http port. /// Gets the default http port.
/// </summary> /// </summary>
@ -22,10 +20,12 @@ namespace Jellyfin.Networking.Configuration
/// </summary> /// </summary>
public const int DefaultHttpsPort = 8920; public const int DefaultHttpsPort = 8920;
private string _baseUrl = string.Empty;
/// <summary> /// <summary>
/// Gets or sets a value indicating whether the server should force connections over HTTPS. /// Gets or sets a value indicating whether the server should force connections over HTTPS.
/// </summary> /// </summary>
public bool RequireHttps { get; set; } = false; public bool RequireHttps { get; set; }
/// <summary> /// <summary>
/// Gets or sets a value used to specify the URL prefix that your Jellyfin instance can be accessed at. /// Gets or sets a value used to specify the URL prefix that your Jellyfin instance can be accessed at.
@ -50,7 +50,7 @@ namespace Jellyfin.Networking.Configuration
} }
// Normalize the end of the string // Normalize the end of the string
if (value[value.Length - 1] == '/') if (value[^1] == '/')
{ {
// If baseUrl was configured with a trailing slash, remove it for consistency // If baseUrl was configured with a trailing slash, remove it for consistency
value = value.Remove(value.Length - 1); value = value.Remove(value.Length - 1);
@ -85,7 +85,7 @@ namespace Jellyfin.Networking.Configuration
/// In order for HTTPS to be used, in addition to setting this to true, valid values must also be /// In order for HTTPS to be used, in addition to setting this to true, valid values must also be
/// provided for <see cref="ServerConfiguration.CertificatePath"/> and <see cref="ServerConfiguration.CertificatePassword"/>. /// provided for <see cref="ServerConfiguration.CertificatePath"/> and <see cref="ServerConfiguration.CertificatePassword"/>.
/// </remarks> /// </remarks>
public bool EnableHttps { get; set; } = false; public bool EnableHttps { get; set; }
/// <summary> /// <summary>
/// Gets or sets the public mapped port. /// Gets or sets the public mapped port.
@ -96,7 +96,7 @@ namespace Jellyfin.Networking.Configuration
/// <summary> /// <summary>
/// Gets or sets a value indicating whether the http port should be mapped as part of UPnP automatic port forwarding.. /// Gets or sets a value indicating whether the http port should be mapped as part of UPnP automatic port forwarding..
/// </summary> /// </summary>
public bool UPnPCreateHttpPortMap { get; set; } = false; public bool UPnPCreateHttpPortMap { get; set; }
/// <summary> /// <summary>
/// Gets or sets the UDPPortRange /// Gets or sets the UDPPortRange
@ -105,12 +105,12 @@ namespace Jellyfin.Networking.Configuration
public string UDPPortRange { get; set; } = string.Empty; public string UDPPortRange { get; set; } = string.Empty;
/// <summary> /// <summary>
/// Gets or sets a value indicating whether IPV6 capability is enabled. /// Gets or sets a value indicating whether gets or sets IPV6 capability..
/// </summary> /// </summary>
public bool EnableIPV6 { get; set; } = false; public bool EnableIPV6 { get; set; }
/// <summary> /// <summary>
/// Gets or sets a value indicating whether IPV6 capability is enabled. /// Gets or sets a value indicating whether gets or sets IPV4 capability..
/// </summary> /// </summary>
public bool EnableIPV4 { get; set; } = true; public bool EnableIPV4 { get; set; } = true;
@ -118,7 +118,7 @@ namespace Jellyfin.Networking.Configuration
/// Gets or sets a value indicating whether detailed ssdp logs are sent to the console/log. /// 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 work.. /// "Emby.Dlna": "Debug" must be set in logging.default.json for this property to work..
/// </summary> /// </summary>
public bool EnableSSDPTracing { get; set; } = false; public bool EnableSSDPTracing { get; set; }
/// <summary> /// <summary>
/// Gets or sets the SSDPTracingFilter /// Gets or sets the SSDPTracingFilter
@ -162,7 +162,7 @@ namespace Jellyfin.Networking.Configuration
/// Gets or sets a value indicating whether all IPv6 interfaces should be treated as on the internal network. /// 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.. /// Depending on the address range implemented ULA ranges might not be used..
/// </summary> /// </summary>
public bool TrustAllIP6Interfaces { get; set; } = false; public bool TrustAllIP6Interfaces { get; set; }
/// <summary> /// <summary>
/// Gets or sets the ports that HDHomerun uses.. /// Gets or sets the ports that HDHomerun uses..
@ -178,7 +178,7 @@ namespace Jellyfin.Networking.Configuration
/// <summary> /// <summary>
/// Gets or sets a value indicating whether Autodiscovery tracing is enabled.. /// Gets or sets a value indicating whether Autodiscovery tracing is enabled..
/// </summary> /// </summary>
public bool AutoDiscoveryTracing { get; set; } = false; public bool AutoDiscoveryTracing { get; set; }
/// <summary> /// <summary>
/// Gets or sets a value indicating whether Autodiscovery is enabled.. /// Gets or sets a value indicating whether Autodiscovery is enabled..
@ -193,12 +193,12 @@ namespace Jellyfin.Networking.Configuration
/// <summary> /// <summary>
/// Gets or sets a value indicating whether <seealso cref="RemoteIPFilter"/> contains a blacklist or a whitelist. Default is a whitelist.. /// Gets or sets a value indicating whether <seealso cref="RemoteIPFilter"/> contains a blacklist or a whitelist. Default is a whitelist..
/// </summary> /// </summary>
public bool IsRemoteIPFilterBlacklist { get; set; } = false; public bool IsRemoteIPFilterBlacklist { get; set; }
/// <summary> /// <summary>
/// Gets or sets a value indicating whether to enable automatic port forwarding.. /// Gets or sets a value indicating whether to enable automatic port forwarding..
/// </summary> /// </summary>
public bool EnableUPnP { get; set; } = false; public bool EnableUPnP { get; set; }
/// <summary> /// <summary>
/// Gets or sets a value indicating whether access outside of the LAN is permitted.. /// Gets or sets a value indicating whether access outside of the LAN is permitted..

@ -1,7 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<AssemblyName>Jellyfin.Networking</AssemblyName>
<OutputType>Library</OutputType>
<TargetFramework>netstandard2.1</TargetFramework> <TargetFramework>netstandard2.1</TargetFramework>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo> <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<GenerateDocumentationFile>true</GenerateDocumentationFile> <GenerateDocumentationFile>true</GenerateDocumentationFile>
@ -15,7 +13,8 @@
<!-- Code Analyzers--> <!-- Code Analyzers-->
<ItemGroup Condition=" '$(Configuration)' == 'Debug' "> <ItemGroup Condition=" '$(Configuration)' == 'Debug' ">
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8" PrivateAssets="All" /> <PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.3.1" PrivateAssets="All" />
<PackageReference Include="SerilogAnalyzer" Version="0.15.0" PrivateAssets="All" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118" PrivateAssets="All" /> <PackageReference Include="StyleCop.Analyzers" Version="1.1.118" PrivateAssets="All" />
<PackageReference Include="SmartAnalyzers.MultithreadingAnalyzer" Version="1.1.31" PrivateAssets="All" /> <PackageReference Include="SmartAnalyzers.MultithreadingAnalyzer" Version="1.1.31" PrivateAssets="All" />
</ItemGroup> </ItemGroup>
@ -25,10 +24,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="3.1.7" /> <PackageReference Include="NetworkCollection" Version="1.0.6" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.7" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="3.1.7" />
<PackageReference Include="NetworkCollection" Version="1.0.4" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

@ -1,3 +1,5 @@
#pragma warning disable CA1021 // Avoid out parameters
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
@ -27,7 +29,7 @@ namespace Jellyfin.Networking.Manager
private readonly Dictionary<string, int> _interfaceNames; private readonly Dictionary<string, int> _interfaceNames;
/// <summary> /// <summary>
/// Threading lock for network interfaces. /// Threading lock for network properties.
/// </summary> /// </summary>
private readonly object _intLock = new object(); private readonly object _intLock = new object();
@ -104,7 +106,7 @@ namespace Jellyfin.Networking.Manager
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); _logger = logger ?? throw new ArgumentNullException(nameof(logger));
_configurationManager = configurationManager ?? throw new ArgumentNullException(nameof(configurationManager)); _configurationManager = configurationManager ?? throw new ArgumentNullException(nameof(configurationManager));
_interfaceAddresses = new NetCollection(unique: false); _interfaceAddresses = new NetCollection();
_macAddresses = new List<PhysicalAddress>(); _macAddresses = new List<PhysicalAddress>();
_interfaceNames = new Dictionary<string, int>(); _interfaceNames = new Dictionary<string, int>();
_publishedServerUrls = new Dictionary<IPNetAddress, string>(); _publishedServerUrls = new Dictionary<IPNetAddress, string>();
@ -199,7 +201,7 @@ namespace Jellyfin.Networking.Manager
/// <inheritdoc/> /// <inheritdoc/>
public bool IsExcluded(IPAddress ip) public bool IsExcluded(IPAddress ip)
{ {
return _excludedSubnets.Contains(ip); return _excludedSubnets.ContainsAddress(ip);
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -227,7 +229,7 @@ namespace Jellyfin.Networking.Manager
{ {
if (bracketed) if (bracketed)
{ {
AddToCollection(col, v.Substring(1, v.Length - 2)); AddToCollection(col, v[1..^1]);
} }
} }
else if (v.StartsWith('!')) else if (v.StartsWith('!'))
@ -329,6 +331,11 @@ namespace Jellyfin.Networking.Manager
public string GetBindInterface(IPObject source, out int? port) public string GetBindInterface(IPObject source, out int? port)
{ {
port = null; port = null;
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
// Do we have a source? // Do we have a source?
bool haveSource = !source.Address.Equals(IPAddress.None); bool haveSource = !source.Address.Equals(IPAddress.None);
bool isExternal = false; bool isExternal = false;
@ -470,7 +477,7 @@ namespace Jellyfin.Networking.Manager
} }
// As private addresses can be redefined by Configuration.LocalNetworkAddresses // As private addresses can be redefined by Configuration.LocalNetworkAddresses
return _lanSubnets.Contains(address) && !_excludedSubnets.Contains(address); return _lanSubnets.ContainsAddress(address) && !_excludedSubnets.ContainsAddress(address);
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -495,7 +502,7 @@ namespace Jellyfin.Networking.Manager
/// <inheritdoc/> /// <inheritdoc/>
public bool IsExcludedInterface(IPAddress address) public bool IsExcludedInterface(IPAddress address)
{ {
return _bindExclusions.Contains(address); return _bindExclusions.ContainsAddress(address);
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -503,7 +510,7 @@ namespace Jellyfin.Networking.Manager
{ {
if (filter == null) if (filter == null)
{ {
return NetCollection.AsNetworks(_lanSubnets.Exclude(_excludedSubnets)); return _lanSubnets.Exclude(_excludedSubnets).AsNetworks();
} }
return _lanSubnets.Exclude(filter); return _lanSubnets.Exclude(filter);
@ -512,7 +519,7 @@ namespace Jellyfin.Networking.Manager
/// <inheritdoc/> /// <inheritdoc/>
public bool IsValidInterfaceAddress(IPAddress address) public bool IsValidInterfaceAddress(IPAddress address)
{ {
return _interfaceAddresses.Contains(address); return _interfaceAddresses.ContainsAddress(address);
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -611,12 +618,32 @@ namespace Jellyfin.Networking.Manager
} }
} }
private void ConfigurationUpdated(object? sender, ConfigurationUpdateEventArgs evt) /// <summary>
/// Trys to identify the string and return an object of that class.
/// </summary>
/// <param name="addr">String to parse.</param>
/// <param name="result">IPObject to return.</param>
/// <returns>True if the value parsed successfully.</returns>
private static bool TryParse(string addr, out IPObject result)
{ {
if (evt.Key.Equals("network", StringComparison.Ordinal)) if (!string.IsNullOrEmpty(addr))
{ {
UpdateSettings(evt.NewConfiguration); // Is it an IP address
if (IPNetAddress.TryParse(addr, out IPNetAddress nw))
{
result = nw;
return true;
}
if (IPHost.TryParse(addr, out IPHost h))
{
result = h;
return true;
}
} }
result = IPNetAddress.None;
return false;
} }
/// <summary> /// <summary>
@ -625,7 +652,7 @@ namespace Jellyfin.Networking.Manager
/// </summary> /// </summary>
/// <param name="address">Address to convert.</param> /// <param name="address">Address to convert.</param>
/// <returns>URI save conversion of the address.</returns> /// <returns>URI save conversion of the address.</returns>
private string FormatIP6String(IPAddress address) private static string FormatIP6String(IPAddress address)
{ {
var str = address.ToString(); var str = address.ToString();
if (address.AddressFamily == AddressFamily.InterNetworkV6) if (address.AddressFamily == AddressFamily.InterNetworkV6)
@ -643,6 +670,14 @@ namespace Jellyfin.Networking.Manager
return str; return str;
} }
private void ConfigurationUpdated(object? sender, ConfigurationUpdateEventArgs evt)
{
if (evt.Key.Equals("network", StringComparison.Ordinal))
{
UpdateSettings(evt.NewConfiguration);
}
}
/// <summary> /// <summary>
/// Checks the string to see if it matches any interface names. /// Checks the string to see if it matches any interface names.
/// </summary> /// </summary>
@ -701,7 +736,7 @@ namespace Jellyfin.Networking.Manager
} }
} }
} }
else if (NetCollection.TryParse(token, out IPObject obj)) else if (TryParse(token, out IPObject obj))
{ {
if (!IsIP6Enabled) if (!IsIP6Enabled)
{ {
@ -896,7 +931,7 @@ namespace Jellyfin.Networking.Manager
// Create lists from user settings. // Create lists from user settings.
_lanSubnets = CreateIPCollection(subnets); _lanSubnets = CreateIPCollection(subnets);
_excludedSubnets = NetCollection.AsNetworks(CreateIPCollection(subnets, true)); _excludedSubnets = CreateIPCollection(subnets, true).AsNetworks();
// If no LAN addresses are specified - all private subnets are deemed to be the LAN // If no LAN addresses are specified - all private subnets are deemed to be the LAN
_usingPrivateAddresses = _lanSubnets.Count == 0; _usingPrivateAddresses = _lanSubnets.Count == 0;
@ -946,7 +981,7 @@ namespace Jellyfin.Networking.Manager
_logger.LogInformation("Defined LAN addresses : {0}", _lanSubnets); _logger.LogInformation("Defined LAN addresses : {0}", _lanSubnets);
_logger.LogInformation("Defined LAN exclusions : {0}", _excludedSubnets); _logger.LogInformation("Defined LAN exclusions : {0}", _excludedSubnets);
_logger.LogInformation("Using LAN addresses: {0}", NetCollection.AsNetworks(_lanSubnets.Exclude(_excludedSubnets))); _logger.LogInformation("Using LAN addresses: {0}", _lanSubnets.Exclude(_excludedSubnets).AsNetworks());
} }
} }
@ -986,7 +1021,7 @@ namespace Jellyfin.Networking.Manager
{ {
if (IsIP4Enabled && info.Address.AddressFamily == AddressFamily.InterNetwork) if (IsIP4Enabled && info.Address.AddressFamily == AddressFamily.InterNetwork)
{ {
IPNetAddress nw = new IPNetAddress(info.Address, info.IPv4Mask) IPNetAddress nw = new IPNetAddress(info.Address, IPObject.MaskToCidr(info.IPv4Mask))
{ {
// Keep the number of gateways on this interface, along with its index. // Keep the number of gateways on this interface, along with its index.
Tag = ipProperties.GetIPv4Properties().Index Tag = ipProperties.GetIPv4Properties().Index

Loading…
Cancel
Save