From face3f8c6538624ab96e03801e958df26b708a7a Mon Sep 17 00:00:00 2001 From: Greenback Date: Fri, 30 Oct 2020 13:54:37 +0000 Subject: [PATCH] Updating to latest network code. --- .../Configuration/NetworkConfiguration.cs | 28 ++++---- .../Jellyfin.Networking.csproj | 10 +-- Jellyfin.Networking/Manager/NetworkManager.cs | 67 ++++++++++++++----- 3 files changed, 68 insertions(+), 37 deletions(-) diff --git a/Jellyfin.Networking/Configuration/NetworkConfiguration.cs b/Jellyfin.Networking/Configuration/NetworkConfiguration.cs index aa75ac305d..301e432511 100644 --- a/Jellyfin.Networking/Configuration/NetworkConfiguration.cs +++ b/Jellyfin.Networking/Configuration/NetworkConfiguration.cs @@ -10,8 +10,6 @@ namespace Jellyfin.Networking.Configuration /// public class NetworkConfiguration { - private string _baseUrl = string.Empty; - /// /// Gets the default http port. /// @@ -22,10 +20,12 @@ namespace Jellyfin.Networking.Configuration /// public const int DefaultHttpsPort = 8920; + private string _baseUrl = string.Empty; + /// /// Gets or sets a value indicating whether the server should force connections over HTTPS. /// - public bool RequireHttps { get; set; } = false; + public bool RequireHttps { get; set; } /// /// 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 - if (value[value.Length - 1] == '/') + if (value[^1] == '/') { // If baseUrl was configured with a trailing slash, remove it for consistency 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 /// provided for and . /// - public bool EnableHttps { get; set; } = false; + public bool EnableHttps { get; set; } /// /// Gets or sets the public mapped port. @@ -96,7 +96,7 @@ namespace Jellyfin.Networking.Configuration /// /// Gets or sets a value indicating whether the http port should be mapped as part of UPnP automatic port forwarding.. /// - public bool UPnPCreateHttpPortMap { get; set; } = false; + public bool UPnPCreateHttpPortMap { get; set; } /// /// Gets or sets the UDPPortRange @@ -105,12 +105,12 @@ namespace Jellyfin.Networking.Configuration public string UDPPortRange { get; set; } = string.Empty; /// - /// Gets or sets a value indicating whether IPV6 capability is enabled. + /// Gets or sets a value indicating whether gets or sets IPV6 capability.. /// - public bool EnableIPV6 { get; set; } = false; + public bool EnableIPV6 { get; set; } /// - /// Gets or sets a value indicating whether IPV6 capability is enabled. + /// Gets or sets a value indicating whether gets or sets IPV4 capability.. /// 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. /// "Emby.Dlna": "Debug" must be set in logging.default.json for this property to work.. /// - public bool EnableSSDPTracing { get; set; } = false; + public bool EnableSSDPTracing { get; set; } /// /// 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. /// Depending on the address range implemented ULA ranges might not be used.. /// - public bool TrustAllIP6Interfaces { get; set; } = false; + public bool TrustAllIP6Interfaces { get; set; } /// /// Gets or sets the ports that HDHomerun uses.. @@ -178,7 +178,7 @@ namespace Jellyfin.Networking.Configuration /// /// Gets or sets a value indicating whether Autodiscovery tracing is enabled.. /// - public bool AutoDiscoveryTracing { get; set; } = false; + public bool AutoDiscoveryTracing { get; set; } /// /// Gets or sets a value indicating whether Autodiscovery is enabled.. @@ -193,12 +193,12 @@ namespace Jellyfin.Networking.Configuration /// /// Gets or sets a value indicating whether contains a blacklist or a whitelist. Default is a whitelist.. /// - public bool IsRemoteIPFilterBlacklist { get; set; } = false; + public bool IsRemoteIPFilterBlacklist { get; set; } /// /// Gets or sets a value indicating whether to enable automatic port forwarding.. /// - public bool EnableUPnP { get; set; } = false; + public bool EnableUPnP { get; set; } /// /// Gets or sets a value indicating whether access outside of the LAN is permitted.. diff --git a/Jellyfin.Networking/Jellyfin.Networking.csproj b/Jellyfin.Networking/Jellyfin.Networking.csproj index 7f01f149ec..f9cdfc2907 100644 --- a/Jellyfin.Networking/Jellyfin.Networking.csproj +++ b/Jellyfin.Networking/Jellyfin.Networking.csproj @@ -1,7 +1,5 @@ - Jellyfin.Networking - Library netstandard2.1 false true @@ -15,7 +13,8 @@ - + + @@ -25,10 +24,7 @@ - - - - + diff --git a/Jellyfin.Networking/Manager/NetworkManager.cs b/Jellyfin.Networking/Manager/NetworkManager.cs index 6d6b7ebc4e..616774d043 100644 --- a/Jellyfin.Networking/Manager/NetworkManager.cs +++ b/Jellyfin.Networking/Manager/NetworkManager.cs @@ -1,3 +1,5 @@ +#pragma warning disable CA1021 // Avoid out parameters + using System; using System.Collections.Generic; using System.Globalization; @@ -27,7 +29,7 @@ namespace Jellyfin.Networking.Manager private readonly Dictionary _interfaceNames; /// - /// Threading lock for network interfaces. + /// Threading lock for network properties. /// private readonly object _intLock = new object(); @@ -104,7 +106,7 @@ namespace Jellyfin.Networking.Manager _logger = logger ?? throw new ArgumentNullException(nameof(logger)); _configurationManager = configurationManager ?? throw new ArgumentNullException(nameof(configurationManager)); - _interfaceAddresses = new NetCollection(unique: false); + _interfaceAddresses = new NetCollection(); _macAddresses = new List(); _interfaceNames = new Dictionary(); _publishedServerUrls = new Dictionary(); @@ -199,7 +201,7 @@ namespace Jellyfin.Networking.Manager /// public bool IsExcluded(IPAddress ip) { - return _excludedSubnets.Contains(ip); + return _excludedSubnets.ContainsAddress(ip); } /// @@ -227,7 +229,7 @@ namespace Jellyfin.Networking.Manager { if (bracketed) { - AddToCollection(col, v.Substring(1, v.Length - 2)); + AddToCollection(col, v[1..^1]); } } else if (v.StartsWith('!')) @@ -329,6 +331,11 @@ namespace Jellyfin.Networking.Manager public string GetBindInterface(IPObject source, out int? port) { port = null; + if (source == null) + { + throw new ArgumentNullException(nameof(source)); + } + // Do we have a source? bool haveSource = !source.Address.Equals(IPAddress.None); bool isExternal = false; @@ -470,7 +477,7 @@ namespace Jellyfin.Networking.Manager } // As private addresses can be redefined by Configuration.LocalNetworkAddresses - return _lanSubnets.Contains(address) && !_excludedSubnets.Contains(address); + return _lanSubnets.ContainsAddress(address) && !_excludedSubnets.ContainsAddress(address); } /// @@ -495,7 +502,7 @@ namespace Jellyfin.Networking.Manager /// public bool IsExcludedInterface(IPAddress address) { - return _bindExclusions.Contains(address); + return _bindExclusions.ContainsAddress(address); } /// @@ -503,7 +510,7 @@ namespace Jellyfin.Networking.Manager { if (filter == null) { - return NetCollection.AsNetworks(_lanSubnets.Exclude(_excludedSubnets)); + return _lanSubnets.Exclude(_excludedSubnets).AsNetworks(); } return _lanSubnets.Exclude(filter); @@ -512,7 +519,7 @@ namespace Jellyfin.Networking.Manager /// public bool IsValidInterfaceAddress(IPAddress address) { - return _interfaceAddresses.Contains(address); + return _interfaceAddresses.ContainsAddress(address); } /// @@ -611,12 +618,32 @@ namespace Jellyfin.Networking.Manager } } - private void ConfigurationUpdated(object? sender, ConfigurationUpdateEventArgs evt) + /// + /// Trys to identify the string and return an object of that class. + /// + /// String to parse. + /// IPObject to return. + /// True if the value parsed successfully. + 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; } /// @@ -625,7 +652,7 @@ namespace Jellyfin.Networking.Manager /// /// Address to convert. /// URI save conversion of the address. - private string FormatIP6String(IPAddress address) + private static string FormatIP6String(IPAddress address) { var str = address.ToString(); if (address.AddressFamily == AddressFamily.InterNetworkV6) @@ -643,6 +670,14 @@ namespace Jellyfin.Networking.Manager return str; } + private void ConfigurationUpdated(object? sender, ConfigurationUpdateEventArgs evt) + { + if (evt.Key.Equals("network", StringComparison.Ordinal)) + { + UpdateSettings(evt.NewConfiguration); + } + } + /// /// Checks the string to see if it matches any interface names. /// @@ -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) { @@ -896,7 +931,7 @@ namespace Jellyfin.Networking.Manager // Create lists from user settings. _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 _usingPrivateAddresses = _lanSubnets.Count == 0; @@ -946,7 +981,7 @@ namespace Jellyfin.Networking.Manager _logger.LogInformation("Defined LAN addresses : {0}", _lanSubnets); _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) { - 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. Tag = ipProperties.GetIPv4Properties().Index