From 8a4f81c9a7a03c6a45a8a9331f71ba06ae0ce521 Mon Sep 17 00:00:00 2001 From: Greenback Date: Fri, 9 Oct 2020 14:04:55 +0100 Subject: [PATCH] Updated networkmanager to the lastest. --- Jellyfin.Networking/Manager/NetworkManager.cs | 226 ++++++++---------- 1 file changed, 99 insertions(+), 127 deletions(-) diff --git a/Jellyfin.Networking/Manager/NetworkManager.cs b/Jellyfin.Networking/Manager/NetworkManager.cs index a094212d37..26614c85e0 100644 --- a/Jellyfin.Networking/Manager/NetworkManager.cs +++ b/Jellyfin.Networking/Manager/NetworkManager.cs @@ -45,6 +45,8 @@ namespace Jellyfin.Networking.Manager private readonly IConfigurationManager _configurationManager; + private readonly object _eventFireLock; + /// /// Holds the bind address overrides. /// @@ -106,6 +108,7 @@ namespace Jellyfin.Networking.Manager _macAddresses = new List(); _interfaceNames = new Dictionary(); _publishedServerUrls = new Dictionary(); + _eventFireLock = new object(); NetworkChange.NetworkAddressChanged += OnNetworkAddressChanged; NetworkChange.NetworkAvailabilityChanged += OnNetworkAvailabilityChanged; @@ -158,10 +161,7 @@ namespace Jellyfin.Networking.Manager public List GetMacAddresses() { // Populated in construction - so always has values. - lock (_intLock) - { - return _macAddresses.ToList(); - } + return _macAddresses.ToList(); } /// @@ -174,10 +174,7 @@ namespace Jellyfin.Networking.Manager _ => IPAddress.None }; - lock (_intLock) - { - return _internalInterfaces.Where(i => i.Address.Equals(address) && i.Tag < 0).Any(); - } + return _internalInterfaces.Any(i => i.Address.Equals(address) && i.Tag < 0); } /// @@ -224,14 +221,14 @@ namespace Jellyfin.Networking.Manager try { - if (v.StartsWith("[", StringComparison.OrdinalIgnoreCase) && v.EndsWith("]", StringComparison.OrdinalIgnoreCase)) + if (v.StartsWith('[') && v.EndsWith(']')) { if (bracketed) { - AddToCollection(col, v.Remove(v.Length - 1).Substring(1)); + AddToCollection(col, v.Substring(1, v.Length - 2)); } } - else if (v.StartsWith("!", StringComparison.OrdinalIgnoreCase)) + else if (v.StartsWith('!')) { if (bracketed) { @@ -255,42 +252,39 @@ namespace Jellyfin.Networking.Manager /// public NetCollection GetAllBindInterfaces(bool individualInterfaces = false) { - lock (_intLock) - { - int count = _bindAddresses.Count; + int count = _bindAddresses.Count; - if (count == 0) + if (count == 0) + { + if (_bindExclusions.Count > 0) { - if (_bindExclusions.Count > 0) - { - // Return all the interfaces except the ones specifically excluded. - return _interfaceAddresses.Exclude(_bindExclusions); - } - - if (individualInterfaces) - { - return new NetCollection(_interfaceAddresses); - } + // Return all the interfaces except the ones specifically excluded. + return _interfaceAddresses.Exclude(_bindExclusions); + } - // No bind address and no exclusions, so listen on all interfaces. - NetCollection result = new NetCollection(); + if (individualInterfaces) + { + return new NetCollection(_interfaceAddresses); + } - if (IsIP4Enabled) - { - result.Add(IPAddress.Any); - } + // No bind address and no exclusions, so listen on all interfaces. + NetCollection result = new NetCollection(); - if (IsIP6Enabled) - { - result.Add(IPAddress.IPv6Any); - } + if (IsIP4Enabled) + { + result.Add(IPAddress.Any); + } - return result; + if (IsIP6Enabled) + { + result.Add(IPAddress.IPv6Any); } - // Remove any excluded bind interfaces. - return _bindAddresses.Exclude(_bindExclusions); + return result; } + + // Remove any excluded bind interfaces. + return _bindAddresses.Exclude(_bindExclusions); } /// @@ -351,84 +345,78 @@ namespace Jellyfin.Networking.Manager isExternal = !IsInLocalNetwork(source); - if (MatchesPublishedServerUrl(source, isExternal, out string result, out port)) + if (MatchesPublishedServerUrl(source, isExternal, out string res, out port)) { - _logger.LogInformation("{0}: Using BindAddress {1}:{2}", source, result, port); - return result; + _logger.LogInformation("{0}: Using BindAddress {1}:{2}", source, res, port); + return res; } } - _logger.LogDebug("GetBindInterface: Souce: {0}, External: {1}:", haveSource, isExternal); + _logger.LogDebug("GetBindInterface: Source: {0}, External: {1}:", haveSource, isExternal); // No preference given, so move on to bind addresses. - lock (_intLock) + if (MatchesBindInterface(source, isExternal, out string result)) { - if (MatchesBindInterface(source, isExternal, out string result)) - { - return result; - } + return result; + } - if (isExternal && MatchesExternalInterface(source, out result)) - { - return result; - } + if (isExternal && MatchesExternalInterface(source, out result)) + { + return result; + } - // Get the first LAN interface address that isn't a loopback. - var interfaces = new NetCollection(_interfaceAddresses - .Exclude(_bindExclusions) - .Where(p => IsInLocalNetwork(p)) - .OrderBy(p => p.Tag)); + // Get the first LAN interface address that isn't a loopback. + var interfaces = new NetCollection(_interfaceAddresses + .Exclude(_bindExclusions) + .Where(p => IsInLocalNetwork(p)) + .OrderBy(p => p.Tag)); - if (interfaces.Count > 0) + if (interfaces.Count > 0) + { + if (haveSource) { - if (haveSource) + // Does the request originate in one of the interface subnets? + // (For systems with multiple internal network cards, and multiple subnets) + foreach (var intf in interfaces) { - // Does the request originate in one of the interface subnets? - // (For systems with multiple internal network cards, and multiple subnets) - foreach (var intf in interfaces) + if (intf.Contains(source)) { - if (intf.Contains(source)) - { - result = FormatIP6String(intf.Address); - _logger.LogDebug("{0}: GetBindInterface: Has source, matched best internal interface on range. {1}", source, result); - return result; - } + result = FormatIP6String(intf.Address); + _logger.LogDebug("{0}: GetBindInterface: Has source, matched best internal interface on range. {1}", source, result); + return result; } } - - result = FormatIP6String(interfaces.First().Address); - _logger.LogDebug("{0}: GetBindInterface: Matched first internal interface. {1}", source, result); - return result; } - // There isn't any others, so we'll use the loopback. - result = IsIP6Enabled ? "::" : "127.0.0.1"; - _logger.LogWarning("{0}: GetBindInterface: Loopback return.", source, result); + result = FormatIP6String(interfaces.First().Address); + _logger.LogDebug("{0}: GetBindInterface: Matched first internal interface. {1}", source, result); return result; } + + // There isn't any others, so we'll use the loopback. + result = IsIP6Enabled ? "::" : "127.0.0.1"; + _logger.LogWarning("{0}: GetBindInterface: Loopback return.", source, result); + return result; } /// public NetCollection GetInternalBindAddresses() { - lock (_intLock) - { - int count = _bindAddresses.Count; + int count = _bindAddresses.Count; - if (count == 0) + if (count == 0) + { + if (_bindExclusions.Count > 0) { - if (_bindExclusions.Count > 0) - { - // Return all the internal interfaces except the ones excluded. - return new NetCollection(_internalInterfaces.Where(p => !_bindExclusions.Contains(p))); - } - - // No bind address, so return all internal interfaces. - return new NetCollection(_internalInterfaces.Where(p => !p.IsLoopback())); + // Return all the internal interfaces except the ones excluded. + return new NetCollection(_internalInterfaces.Where(p => !_bindExclusions.Contains(p))); } - return new NetCollection(_bindAddresses); + // No bind address, so return all internal interfaces. + return new NetCollection(_internalInterfaces.Where(p => !p.IsLoopback())); } + + return new NetCollection(_bindAddresses); } /// @@ -450,11 +438,8 @@ namespace Jellyfin.Networking.Manager return true; } - lock (_intLock) - { - // As private addresses can be redefined by Configuration.LocalNetworkAddresses - return _lanSubnets.Contains(address) && !_excludedSubnets.Contains(address); - } + // As private addresses can be redefined by Configuration.LocalNetworkAddresses + return _lanSubnets.Contains(address) && !_excludedSubnets.Contains(address); } /// @@ -462,10 +447,7 @@ namespace Jellyfin.Networking.Manager { if (IPHost.TryParse(address, out IPHost ep)) { - lock (_intLock) - { - return _lanSubnets.Contains(ep) && !_excludedSubnets.Contains(ep); - } + return _lanSubnets.Contains(ep) && !_excludedSubnets.Contains(ep); } return false; @@ -485,11 +467,8 @@ namespace Jellyfin.Networking.Manager return true; } - lock (_intLock) - { - // As private addresses can be redefined by Configuration.LocalNetworkAddresses - return _lanSubnets.Contains(address) && !_excludedSubnets.Contains(address); - } + // As private addresses can be redefined by Configuration.LocalNetworkAddresses + return _lanSubnets.Contains(address) && !_excludedSubnets.Contains(address); } /// @@ -514,33 +493,24 @@ namespace Jellyfin.Networking.Manager /// public bool IsExcludedInterface(IPAddress address) { - lock (_intLock) - { - return _bindExclusions.Contains(address); - } + return _bindExclusions.Contains(address); } /// public NetCollection GetFilteredLANSubnets(NetCollection? filter = null) { - lock (_intLock) + if (filter == null) { - if (filter == null) - { - return NetCollection.AsNetworks(_lanSubnets.Exclude(_excludedSubnets)); - } - - return _lanSubnets.Exclude(filter); + return NetCollection.AsNetworks(_lanSubnets.Exclude(_excludedSubnets)); } + + return _lanSubnets.Exclude(filter); } /// public bool IsValidInterfaceAddress(IPAddress address) { - lock (_intLock) - { - return _interfaceAddresses.Contains(address); - } + return _interfaceAddresses.Contains(address); } /// @@ -583,7 +553,7 @@ namespace Jellyfin.Networking.Manager { NetworkConfiguration config = (NetworkConfiguration)configuration ?? throw new ArgumentNullException(nameof(configuration)); - IsIP4Enabled = Socket.OSSupportsIPv6 && config.EnableIPV4; + IsIP4Enabled = Socket.OSSupportsIPv4 && config.EnableIPV4; IsIP6Enabled = Socket.OSSupportsIPv6 && config.EnableIPV6; if (!IsIP6Enabled && !IsIP4Enabled) @@ -761,7 +731,7 @@ namespace Jellyfin.Networking.Manager /// Handler for network change events. /// /// Sender. - /// Network availablity information. + /// Network availability information. private void OnNetworkAvailabilityChanged(object? sender, NetworkAvailabilityEventArgs e) { _logger.LogDebug("Network availability changed."); @@ -805,12 +775,15 @@ namespace Jellyfin.Networking.Manager /// private void OnNetworkChanged() { - if (!_eventfire) + lock (_eventFireLock) { - _logger.LogDebug("Network Address Change Event."); - // As network events tend to fire one after the other only fire once every second. - _eventfire = true; - _ = OnNetworkChangeAsync(); + if (!_eventfire) + { + _logger.LogDebug("Network Address Change Event."); + // As network events tend to fire one after the other only fire once every second. + _eventfire = true; + OnNetworkChangeAsync().GetAwaiter().GetResult(); + } } } @@ -881,8 +854,7 @@ namespace Jellyfin.Networking.Manager lanAddresses = lanAddresses[0].Split(','); } - // TODO: end fix. - // TODO: end fix. + // TODO: end fix: https://github.com/jellyfin/jellyfin-web/issues/1334 // Add virtual machine interface names to the list of bind exclusions, so that they are auto-excluded. if (config.IgnoreVirtualInterfaces) @@ -924,7 +896,7 @@ namespace Jellyfin.Networking.Manager // If no LAN addresses are specified - all private subnets are deemed to be the LAN _usingPrivateAddresses = _lanSubnets.Count == 0; - // NOTE: The order of the commands in this statement matters, otherwise the lists won't initialise correctly. + // NOTE: The order of the commands in this statement matters. if (_usingPrivateAddresses) { _logger.LogDebug("Using LAN interface addresses as user provided no LAN details."); @@ -975,7 +947,7 @@ namespace Jellyfin.Networking.Manager /// /// Generate a list of all the interface ip addresses and submasks where that are in the active/unknown state. - /// Generate a list of all active mac addresses that aren't loopback addreses. + /// Generate a list of all active mac addresses that aren't loopback addresses. /// private void InitialiseInterfaces() {