Pārlūkot izejas kodu

Add option to toggle if ignore virtual interfaces

Some VPN like ZerotierOne owns IP address but no gateway, and there is no
good idea in NetworkManager.GetIPsDefault() to filter such virtual interfaces,
so just provide one option to let user decide it.
Xu Fasheng 6 gadi atpakaļ
vecāks
revīzija
cf4e64f430

+ 1 - 1
Emby.Dlna/Main/DlnaEntryPoint.cs

@@ -171,7 +171,7 @@ namespace Emby.Dlna.Main
                 {
                     var enableMultiSocketBinding = _environmentInfo.OperatingSystem == MediaBrowser.Model.System.OperatingSystem.Windows;
 
-                    _communicationsServer = new SsdpCommunicationsServer(_socketFactory, _networkManager, _logger, enableMultiSocketBinding)
+                    _communicationsServer = new SsdpCommunicationsServer(_config, _socketFactory, _networkManager, _logger, enableMultiSocketBinding)
                     {
                         IsShared = true
                     };

+ 1 - 1
Emby.Server.Implementations/ApplicationHost.cs

@@ -1577,7 +1577,7 @@ namespace Emby.Server.Implementations
 
             if (addresses.Count == 0)
             {
-                addresses.AddRange(NetworkManager.GetLocalIpAddresses());
+                addresses.AddRange(NetworkManager.GetLocalIpAddresses(ServerConfigurationManager.Configuration.IgnoreVirtualInterfaces));
             }
 
             var resultList = new List<IpAddressInfo>();

+ 6 - 6
Emby.Server.Implementations/Networking/NetworkManager.cs

@@ -79,13 +79,13 @@ namespace Emby.Server.Implementations.Networking
         private IpAddressInfo[] _localIpAddresses;
         private readonly object _localIpAddressSyncLock = new object();
 
-        public IpAddressInfo[] GetLocalIpAddresses()
+        public IpAddressInfo[] GetLocalIpAddresses(bool ignoreVirtualInterface = true)
         {
             lock (_localIpAddressSyncLock)
             {
                 if (_localIpAddresses == null)
                 {
-                    var addresses = GetLocalIpAddressesInternal().Result.Select(ToIpAddressInfo).ToArray();
+                    var addresses = GetLocalIpAddressesInternal(ignoreVirtualInterface).Result.Select(ToIpAddressInfo).ToArray();
 
                     _localIpAddresses = addresses;
 
@@ -95,9 +95,9 @@ namespace Emby.Server.Implementations.Networking
             }
         }
 
-        private async Task<List<IPAddress>> GetLocalIpAddressesInternal()
+        private async Task<List<IPAddress>> GetLocalIpAddressesInternal(bool ignoreVirtualInterface)
         {
-            var list = GetIPsDefault()
+            var list = GetIPsDefault(ignoreVirtualInterface)
                 .ToList();
 
             if (list.Count == 0)
@@ -383,7 +383,7 @@ namespace Emby.Server.Implementations.Networking
             return Dns.GetHostAddressesAsync(hostName);
         }
 
-        private List<IPAddress> GetIPsDefault()
+        private List<IPAddress> GetIPsDefault(bool ignoreVirtualInterface)
         {
             NetworkInterface[] interfaces;
 
@@ -414,7 +414,7 @@ namespace Emby.Server.Implementations.Networking
                     // Try to exclude virtual adapters
                     // http://stackoverflow.com/questions/8089685/c-sharp-finding-my-machines-local-ip-address-and-not-the-vms
                     var addr = ipProperties.GatewayAddresses.FirstOrDefault();
-                    if (addr == null || string.Equals(addr.Address.ToString(), "0.0.0.0", StringComparison.OrdinalIgnoreCase))
+                    if (addr == null || (ignoreVirtualInterface && string.Equals(addr.Address.ToString(), "0.0.0.0", StringComparison.OrdinalIgnoreCase)))
                     {
                         return new List<IPAddress>();
                     }

+ 1 - 1
MediaBrowser.Common/Net/INetworkManager.cs

@@ -53,7 +53,7 @@ namespace MediaBrowser.Common.Net
         /// <returns><c>true</c> if [is in local network] [the specified endpoint]; otherwise, <c>false</c>.</returns>
         bool IsInLocalNetwork(string endpoint);
 
-        IpAddressInfo[] GetLocalIpAddresses();
+        IpAddressInfo[] GetLocalIpAddresses(bool ignoreVirtualInterface);
 
         IpAddressInfo ParseIpAddress(string ipAddress);
 

+ 2 - 0
MediaBrowser.Model/Configuration/ServerConfiguration.cs

@@ -178,6 +178,7 @@ namespace MediaBrowser.Model.Configuration
         public string[] LocalNetworkSubnets { get; set; }
         public string[] LocalNetworkAddresses { get; set; }
         public string[] CodecsUsed { get; set; }
+        public bool IgnoreVirtualInterfaces { get; set; }
         public bool EnableExternalContentInSuggestions { get; set; }
         public bool RequireHttps { get; set; }
         public bool IsBehindProxy { get; set; }
@@ -205,6 +206,7 @@ namespace MediaBrowser.Model.Configuration
             CodecsUsed = Array.Empty<string>();
             ImageExtractionTimeoutMs = 0;
             PathSubstitutions = Array.Empty<PathSubstitution>();
+            IgnoreVirtualInterfaces = false;
             EnableSimpleArtistDetection = true;
 
             DisplaySpecialsWithinSeasons = true;

+ 1 - 0
RSSDP/RSSDP.csproj

@@ -3,6 +3,7 @@
   <ItemGroup>
     <ProjectReference Include="..\MediaBrowser.Model\MediaBrowser.Model.csproj" />
     <ProjectReference Include="..\MediaBrowser.Common\MediaBrowser.Common.csproj" />
+    <ProjectReference Include="..\MediaBrowser.Controller\MediaBrowser.Controller.csproj" />
   </ItemGroup>
 
   <PropertyGroup>

+ 6 - 2
RSSDP/SsdpCommunicationsServer.cs

@@ -9,6 +9,7 @@ using System.Threading.Tasks;
 using MediaBrowser.Common.Net;
 using Microsoft.Extensions.Logging;
 using MediaBrowser.Model.Net;
+using MediaBrowser.Controller.Configuration;
 
 namespace Rssdp.Infrastructure
 {
@@ -45,6 +46,7 @@ namespace Rssdp.Infrastructure
         private readonly ILogger _logger;
         private ISocketFactory _SocketFactory;
         private readonly INetworkManager _networkManager;
+        private readonly IServerConfigurationManager _config;
 
         private int _LocalPort;
         private int _MulticastTtl;
@@ -74,9 +76,11 @@ namespace Rssdp.Infrastructure
         /// Minimum constructor.
         /// </summary>
         /// <exception cref="ArgumentNullException">The <paramref name="socketFactory"/> argument is null.</exception>
-        public SsdpCommunicationsServer(ISocketFactory socketFactory, INetworkManager networkManager, ILogger logger, bool enableMultiSocketBinding)
+        public SsdpCommunicationsServer(IServerConfigurationManager config, ISocketFactory socketFactory,
+            INetworkManager networkManager, ILogger logger, bool enableMultiSocketBinding)
             : this(socketFactory, 0, SsdpConstants.SsdpDefaultMulticastTimeToLive, networkManager, logger, enableMultiSocketBinding)
         {
+            _config = config;
         }
 
         /// <summary>
@@ -363,7 +367,7 @@ namespace Rssdp.Infrastructure
 
             if (_enableMultiSocketBinding)
             {
-                foreach (var address in _networkManager.GetLocalIpAddresses())
+                foreach (var address in _networkManager.GetLocalIpAddresses(_config.Configuration.IgnoreVirtualInterfaces))
                 {
                     if (address.AddressFamily == IpAddressFamily.InterNetworkV6)
                     {