2
0
Эх сурвалжийг харах

Updating to latest network code.

Greenback 4 жил өмнө
parent
commit
face3f8c65

+ 14 - 14
Jellyfin.Networking/Configuration/NetworkConfiguration.cs

@@ -10,8 +10,6 @@ namespace Jellyfin.Networking.Configuration
     /// </summary>
     public class NetworkConfiguration
     {
-        private string _baseUrl = string.Empty;
-
         /// <summary>
         /// Gets the default http port.
         /// </summary>
@@ -22,10 +20,12 @@ namespace Jellyfin.Networking.Configuration
         /// </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; } = false;
+        public bool RequireHttps { get; set; }
 
         /// <summary>
         /// 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 <see cref="ServerConfiguration.CertificatePath"/> and <see cref="ServerConfiguration.CertificatePassword"/>.
         /// </remarks>
-        public bool EnableHttps { get; set; } = false;
+        public bool EnableHttps { get; set; }
 
         /// <summary>
         /// Gets or sets the public mapped port.
@@ -96,7 +96,7 @@ namespace Jellyfin.Networking.Configuration
         /// <summary>
         /// Gets or sets a value indicating whether the http port should be mapped as part of UPnP automatic port forwarding..
         /// </summary>
-        public bool UPnPCreateHttpPortMap { get; set; } = false;
+        public bool UPnPCreateHttpPortMap { get; set; }
 
         /// <summary>
         /// Gets or sets the UDPPortRange
@@ -105,12 +105,12 @@ namespace Jellyfin.Networking.Configuration
         public string UDPPortRange { get; set; } = string.Empty;
 
         /// <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>
-        public bool EnableIPV6 { get; set; } = false;
+        public bool EnableIPV6 { get; set; }
 
         /// <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>
         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..
         /// </summary>
-        public bool EnableSSDPTracing { get; set; } = false;
+        public bool EnableSSDPTracing { get; set; }
 
         /// <summary>
         /// 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..
         /// </summary>
-        public bool TrustAllIP6Interfaces { get; set; } = false;
+        public bool TrustAllIP6Interfaces { get; set; }
 
         /// <summary>
         /// Gets or sets the ports that HDHomerun uses..
@@ -178,7 +178,7 @@ namespace Jellyfin.Networking.Configuration
         /// <summary>
         /// Gets or sets a value indicating whether Autodiscovery tracing is enabled..
         /// </summary>
-        public bool AutoDiscoveryTracing { get; set; } = false;
+        public bool AutoDiscoveryTracing { get; set; }
 
         /// <summary>
         /// Gets or sets a value indicating whether Autodiscovery is enabled..
@@ -193,12 +193,12 @@ namespace Jellyfin.Networking.Configuration
         /// <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; } = false;
+        public bool IsRemoteIPFilterBlacklist { get; set; }
 
         /// <summary>
         /// Gets or sets a value indicating whether to enable automatic port forwarding..
         /// </summary>
-        public bool EnableUPnP { get; set; } = false;
+        public bool EnableUPnP { get; set; }
 
         /// <summary>
         /// Gets or sets a value indicating whether access outside of the LAN is permitted..

+ 3 - 7
Jellyfin.Networking/Jellyfin.Networking.csproj

@@ -1,7 +1,5 @@
 <Project Sdk="Microsoft.NET.Sdk">
   <PropertyGroup>
-    <AssemblyName>Jellyfin.Networking</AssemblyName>
-    <OutputType>Library</OutputType>
     <TargetFramework>netstandard2.1</TargetFramework>
     <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
     <GenerateDocumentationFile>true</GenerateDocumentationFile>
@@ -15,7 +13,8 @@
 
   <!-- Code Analyzers-->
   <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="SmartAnalyzers.MultithreadingAnalyzer" Version="1.1.31" PrivateAssets="All" />
   </ItemGroup>
@@ -25,10 +24,7 @@
   </PropertyGroup>
 
   <ItemGroup>
-    <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="3.1.7" />
-    <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" />
+    <PackageReference Include="NetworkCollection" Version="1.0.6" />
   </ItemGroup>
 
   <ItemGroup>

+ 51 - 16
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<string, int> _interfaceNames;
 
         /// <summary>
-        /// Threading lock for network interfaces.
+        /// Threading lock for network properties.
         /// </summary>
         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<PhysicalAddress>();
             _interfaceNames = new Dictionary<string, int>();
             _publishedServerUrls = new Dictionary<IPNetAddress, string>();
@@ -199,7 +201,7 @@ namespace Jellyfin.Networking.Manager
         /// <inheritdoc/>
         public bool IsExcluded(IPAddress ip)
         {
-            return _excludedSubnets.Contains(ip);
+            return _excludedSubnets.ContainsAddress(ip);
         }
 
         /// <inheritdoc/>
@@ -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);
         }
 
         /// <inheritdoc/>
@@ -495,7 +502,7 @@ namespace Jellyfin.Networking.Manager
         /// <inheritdoc/>
         public bool IsExcludedInterface(IPAddress address)
         {
-            return _bindExclusions.Contains(address);
+            return _bindExclusions.ContainsAddress(address);
         }
 
         /// <inheritdoc/>
@@ -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
         /// <inheritdoc/>
         public bool IsValidInterfaceAddress(IPAddress address)
         {
-            return _interfaceAddresses.Contains(address);
+            return _interfaceAddresses.ContainsAddress(address);
         }
 
         /// <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>
@@ -625,7 +652,7 @@ namespace Jellyfin.Networking.Manager
         /// </summary>
         /// <param name="address">Address to convert.</param>
         /// <returns>URI save conversion of the address.</returns>
-        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);
+            }
+        }
+
         /// <summary>
         /// Checks the string to see if it matches any interface names.
         /// </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)
                 {
@@ -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