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.
This commit is contained in:
Xu Fasheng 2019-02-21 22:35:31 +08:00
parent e7e7d96f51
commit cf4e64f430
7 changed files with 18 additions and 11 deletions

View File

@ -171,7 +171,7 @@ namespace Emby.Dlna.Main
{ {
var enableMultiSocketBinding = _environmentInfo.OperatingSystem == MediaBrowser.Model.System.OperatingSystem.Windows; 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 IsShared = true
}; };

View File

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

View File

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

View File

@ -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> /// <returns><c>true</c> if [is in local network] [the specified endpoint]; otherwise, <c>false</c>.</returns>
bool IsInLocalNetwork(string endpoint); bool IsInLocalNetwork(string endpoint);
IpAddressInfo[] GetLocalIpAddresses(); IpAddressInfo[] GetLocalIpAddresses(bool ignoreVirtualInterface);
IpAddressInfo ParseIpAddress(string ipAddress); IpAddressInfo ParseIpAddress(string ipAddress);

View File

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

View File

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

View File

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