update ulna interface binding
This commit is contained in:
parent
dff283a321
commit
0ffc5ebace
|
@ -7,6 +7,7 @@ using System.Net;
|
|||
using System.Net.NetworkInformation;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
using MoreLinq;
|
||||
|
||||
namespace MediaBrowser.Common.Implementations.Networking
|
||||
{
|
||||
|
@ -31,14 +32,14 @@ namespace MediaBrowser.Common.Implementations.Networking
|
|||
}
|
||||
}
|
||||
|
||||
private volatile List<string> _localIpAddresses;
|
||||
private volatile List<IPAddress> _localIpAddresses;
|
||||
private readonly object _localIpAddressSyncLock = new object();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the machine's local ip address
|
||||
/// </summary>
|
||||
/// <returns>IPAddress.</returns>
|
||||
public IEnumerable<string> GetLocalIpAddresses()
|
||||
public IEnumerable<IPAddress> GetLocalIpAddresses()
|
||||
{
|
||||
if (_localIpAddresses == null)
|
||||
{
|
||||
|
@ -58,25 +59,24 @@ namespace MediaBrowser.Common.Implementations.Networking
|
|||
return _localIpAddresses;
|
||||
}
|
||||
|
||||
private IEnumerable<string> GetLocalIpAddressesInternal()
|
||||
private IEnumerable<IPAddress> GetLocalIpAddressesInternal()
|
||||
{
|
||||
var list = GetIPsDefault()
|
||||
.Where(i => !IPAddress.IsLoopback(i))
|
||||
.Select(i => i.ToString())
|
||||
.Where(FilterIpAddress)
|
||||
.ToList();
|
||||
|
||||
if (list.Count > 0)
|
||||
if (list.Count == 0)
|
||||
{
|
||||
return list;
|
||||
list.AddRange(GetLocalIpAddressesFallback());
|
||||
}
|
||||
|
||||
return GetLocalIpAddressesFallback().Where(FilterIpAddress);
|
||||
return list.Where(i => !IPAddress.IsLoopback(i)).Where(FilterIpAddress).DistinctBy(i => i.ToString());
|
||||
}
|
||||
|
||||
private bool FilterIpAddress(string address)
|
||||
private bool FilterIpAddress(IPAddress address)
|
||||
{
|
||||
if (address.StartsWith("169.", StringComparison.OrdinalIgnoreCase))
|
||||
var addressString = address.ToString ();
|
||||
|
||||
if (addressString.StartsWith("169.", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
@ -164,8 +164,7 @@ namespace MediaBrowser.Common.Implementations.Networking
|
|||
{
|
||||
var prefix = addressString.Substring(0, lengthMatch);
|
||||
|
||||
if (GetLocalIpAddresses()
|
||||
.Any(i => i.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)))
|
||||
if (GetLocalIpAddresses().Any(i => i.ToString().StartsWith(prefix, StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
@ -209,33 +208,47 @@ namespace MediaBrowser.Common.Implementations.Networking
|
|||
return Dns.GetHostAddresses(hostName);
|
||||
}
|
||||
|
||||
private IEnumerable<IPAddress> GetIPsDefault()
|
||||
{
|
||||
foreach (var adapter in NetworkInterface.GetAllNetworkInterfaces())
|
||||
{
|
||||
var props = adapter.GetIPProperties();
|
||||
var gateways = from ga in props.GatewayAddresses
|
||||
where !ga.Address.Equals(IPAddress.Any)
|
||||
select true;
|
||||
private List<IPAddress> GetIPsDefault()
|
||||
{
|
||||
NetworkInterface[] interfaces;
|
||||
|
||||
if (!gateways.Any())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
try
|
||||
{
|
||||
interfaces = NetworkInterface.GetAllNetworkInterfaces();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.ErrorException("Error in GetAllNetworkInterfaces", ex);
|
||||
return new List<IPAddress>();
|
||||
}
|
||||
|
||||
foreach (var uni in props.UnicastAddresses)
|
||||
{
|
||||
var address = uni.Address;
|
||||
if (address.AddressFamily != AddressFamily.InterNetwork)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
yield return address;
|
||||
}
|
||||
}
|
||||
}
|
||||
return interfaces.SelectMany(network => {
|
||||
|
||||
private IEnumerable<string> GetLocalIpAddressesFallback()
|
||||
try
|
||||
{
|
||||
Logger.Debug("Found interface: {0}. Type: {1}. Status: {2}", network.Name, network.NetworkInterfaceType, network.OperationalStatus);
|
||||
|
||||
var properties = network.GetIPProperties();
|
||||
var ipV4 = properties.GetIPv4Properties();
|
||||
if (null == ipV4)
|
||||
return new List<IPAddress>();
|
||||
|
||||
return properties.UnicastAddresses
|
||||
.Where(i => i.Address.AddressFamily == AddressFamily.InterNetwork && !IPAddress.IsLoopback(i.Address))
|
||||
.Select(i => i.Address)
|
||||
.ToList();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.ErrorException("Error querying network interface", ex);
|
||||
return new List<IPAddress>();
|
||||
}
|
||||
|
||||
}).DistinctBy(i => i.ToString())
|
||||
.ToList();
|
||||
}
|
||||
|
||||
private IEnumerable<IPAddress> GetLocalIpAddressesFallback()
|
||||
{
|
||||
var host = Dns.GetHostEntry(Dns.GetHostName());
|
||||
|
||||
|
@ -243,7 +256,6 @@ namespace MediaBrowser.Common.Implementations.Networking
|
|||
// It's not fool-proof so ultimately the consumer will have to examine them and decide
|
||||
return host.AddressList
|
||||
.Where(i => i.AddressFamily == AddressFamily.InterNetwork)
|
||||
.Select(i => i.ToString())
|
||||
.Reverse();
|
||||
}
|
||||
|
||||
|
|
|
@ -11,7 +11,7 @@ namespace MediaBrowser.Common.Net
|
|||
/// Gets the machine's local ip address
|
||||
/// </summary>
|
||||
/// <returns>IPAddress.</returns>
|
||||
IEnumerable<string> GetLocalIpAddresses();
|
||||
IEnumerable<IPAddress> GetLocalIpAddresses();
|
||||
|
||||
/// <summary>
|
||||
/// Gets a random port number that is currently available
|
||||
|
|
|
@ -150,11 +150,12 @@ namespace MediaBrowser.Dlna.Main
|
|||
{
|
||||
foreach (var address in _network.GetLocalIpAddresses())
|
||||
{
|
||||
var guid = address.GetMD5();
|
||||
var addressString = address.ToString ();
|
||||
var guid = addressString.GetMD5();
|
||||
|
||||
var descriptorURI = "/dlna/" + guid.ToString("N") + "/description.xml";
|
||||
|
||||
var uri = new Uri(_appHost.GetLocalApiUrl(address) + descriptorURI);
|
||||
var uri = new Uri(_appHost.GetLocalApiUrl(addressString) + descriptorURI);
|
||||
|
||||
var services = new List<string>
|
||||
{
|
||||
|
@ -166,7 +167,7 @@ namespace MediaBrowser.Dlna.Main
|
|||
"uuid:" + guid.ToString("N")
|
||||
};
|
||||
|
||||
_ssdpHandler.RegisterNotification(guid, uri, IPAddress.Parse(address), services);
|
||||
_ssdpHandler.RegisterNotification(guid, uri, address, services);
|
||||
|
||||
_registeredServerIds.Add(guid.ToString("N"));
|
||||
}
|
||||
|
|
|
@ -36,40 +36,63 @@ namespace MediaBrowser.Dlna.Ssdp
|
|||
_appHost = appHost;
|
||||
}
|
||||
|
||||
private List<IPAddress> GetLocalIpAddresses()
|
||||
{
|
||||
NetworkInterface[] interfaces;
|
||||
|
||||
try
|
||||
{
|
||||
interfaces = NetworkInterface.GetAllNetworkInterfaces();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.ErrorException("Error in GetAllNetworkInterfaces", ex);
|
||||
return new List<IPAddress>();
|
||||
}
|
||||
|
||||
return interfaces.SelectMany(network => {
|
||||
|
||||
try
|
||||
{
|
||||
_logger.Debug("Found interface: {0}. Type: {1}. Status: {2}", network.Name, network.NetworkInterfaceType, network.OperationalStatus);
|
||||
|
||||
var properties = network.GetIPProperties();
|
||||
var ipV4 = properties.GetIPv4Properties();
|
||||
if (null == ipV4)
|
||||
return new List<IPAddress>();
|
||||
|
||||
return properties.UnicastAddresses
|
||||
.Where(i => i.Address.AddressFamily == AddressFamily.InterNetwork && !IPAddress.IsLoopback(i.Address))
|
||||
.Select(i => i.Address)
|
||||
.ToList();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.ErrorException("Error querying network interface", ex);
|
||||
return new List<IPAddress>();
|
||||
}
|
||||
|
||||
})
|
||||
.Distinct()
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public void Start(SsdpHandler ssdpHandler)
|
||||
{
|
||||
_ssdpHandler = ssdpHandler;
|
||||
_ssdpHandler.MessageReceived += _ssdpHandler_MessageReceived;
|
||||
|
||||
foreach (var network in GetNetworkInterfaces())
|
||||
{
|
||||
_logger.Debug("Found interface: {0}. Type: {1}. Status: {2}", network.Name, network.NetworkInterfaceType, network.OperationalStatus);
|
||||
|
||||
if (!network.SupportsMulticast || OperationalStatus.Up != network.OperationalStatus || !network.GetIPProperties().MulticastAddresses.Any())
|
||||
continue;
|
||||
|
||||
var properties = network.GetIPProperties();
|
||||
var ipV4 = properties.GetIPv4Properties();
|
||||
if (null == ipV4)
|
||||
continue;
|
||||
|
||||
var localIps = properties.UnicastAddresses
|
||||
.Where(i => i.Address.AddressFamily == AddressFamily.InterNetwork)
|
||||
.Select(i => i.Address)
|
||||
.ToList();
|
||||
|
||||
foreach (var localIp in localIps)
|
||||
{
|
||||
try
|
||||
{
|
||||
CreateListener(localIp);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.ErrorException("Failed to Initilize Socket", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach (var localIp in GetLocalIpAddresses())
|
||||
{
|
||||
try
|
||||
{
|
||||
CreateListener(localIp);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.ErrorException("Failed to Initilize Socket", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void _ssdpHandler_MessageReceived(object sender, SsdpMessageEventArgs e)
|
||||
|
@ -107,30 +130,18 @@ namespace MediaBrowser.Dlna.Ssdp
|
|||
}
|
||||
}
|
||||
|
||||
private IEnumerable<NetworkInterface> GetNetworkInterfaces()
|
||||
{
|
||||
try
|
||||
{
|
||||
return NetworkInterface.GetAllNetworkInterfaces();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.ErrorException("Error in GetAllNetworkInterfaces", ex);
|
||||
return new List<NetworkInterface>();
|
||||
}
|
||||
}
|
||||
private void CreateListener(IPAddress localIp)
|
||||
{
|
||||
Task.Factory.StartNew(async (o) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var endPoint = new IPEndPoint(localIp, 1900);
|
||||
_logger.Info("Creating SSDP listener on {0}", localIp);
|
||||
|
||||
var endPoint = new IPEndPoint(localIp, 1900);
|
||||
|
||||
var socket = GetMulticastSocket(localIp, endPoint);
|
||||
|
||||
_logger.Info("Creating SSDP listener on {0}", localIp);
|
||||
|
||||
var receiveBuffer = new byte[64000];
|
||||
|
||||
CreateNotifier(localIp);
|
||||
|
|
|
@ -1154,6 +1154,7 @@ namespace MediaBrowser.Server.Startup.Common
|
|||
get
|
||||
{
|
||||
var localAddresses = NetworkManager.GetLocalIpAddresses()
|
||||
.Select(i => i.ToString())
|
||||
.ToList();
|
||||
|
||||
var httpServerAddresses = HttpServer.LocalEndPoints
|
||||
|
|
Loading…
Reference in New Issue
Block a user