The Free Software Media System - Server Backend & API
Merge branch 'master' into network-rewrite
Shadowghostc042f20224f2
diff --cc Jellyfin.Networking/Manager/NetworkManager.csindex f0f95f5fc8,afb0538205..1b1e91e9fe--- a/Jellyfin.Networking/Manager/NetworkManager.cs+++ b/Jellyfin.Networking/Manager/NetworkManager.cs@@@ -136,78 -179,67 +136,85 @@@ namespace Jellyfin.Networking.ManageGC.SuppressFinalize(this);}- /// <inheritdoc/>- public IReadOnlyCollection<PhysicalAddress> GetMacAddresses()+ /// <summary>+ /// Handler for network change events.+ /// </summary>+ /// <param name="sender">Sender.</param>+ /// <param name="e">A <see cref="NetworkAvailabilityEventArgs"/> containing network availability information.</param>+ private void OnNetworkAvailabilityChanged(object? sender, NetworkAvailabilityEventArgs e){- // Populated in construction - so always has values.- return _macAddresses;+ _logger.LogDebug("Network availability changed.");+ HandleNetworkChange();}- /// <inheritdoc/>- public bool IsGatewayInterface(IPObject? addressObj)+ /// <summary>+ /// Handler for network change events.+ /// </summary>+ /// <param name="sender">Sender.</param>+ /// <param name="e">An <see cref="EventArgs"/>.</param>+ private void OnNetworkAddressChanged(object? sender, EventArgs e){- var address = addressObj?.Address ?? IPAddress.None;- return _internalInterfaces.Any(i => i.Address.Equals(address) && i.Tag < 0);+ _logger.LogDebug("Network address change detected.");+ HandleNetworkChange();}- /// <inheritdoc/>- public bool IsGatewayInterface(IPAddress? addressObj)+ /// <summary>+ /// Triggers our event, and re-loads interface information.+ /// </summary>+ private void HandleNetworkChange(){- return _internalInterfaces.Any(i => i.Address.Equals(addressObj ?? IPAddress.None) && i.Tag < 0);+ lock (_networkEventLock)+ {+ 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;+ OnNetworkChange();+ }+ }}- /// <inheritdoc/>- public Collection<IPObject> GetLoopbacks()+ /// <summary>+ /// Waits for 2 seconds before re-initialising the settings, as typically these events fire multiple times in succession.+ /// </summary>+ private void OnNetworkChange(){- Collection<IPObject> nc = new Collection<IPObject>();- if (IsIP4Enabled)+ try{- nc.AddItem(IPAddress.Loopback);- }+ Thread.Sleep(2000);+ var networkConfig = _configurationManager.GetNetworkConfiguration();- InitialiseLan(networkConfig);- InitialiseInterfaces();- EnforceBindSettings(networkConfig);++ if (IsIPv6Enabled && !Socket.OSSupportsIPv6)++ {++ UpdateSettings(networkConfig);++ }++ else++ {++ InitialiseInterfaces();++ InitialiseLan(networkConfig);++ EnforceBindSettings(networkConfig);++ }- if (IsIP6Enabled)+ NetworkChanged?.Invoke(this, EventArgs.Empty);+ }+ finally{- nc.AddItem(IPAddress.IPv6Loopback);+ _eventfire = false;}-- return nc;- }-- /// <inheritdoc/>- public bool IsExcluded(IPAddress ip)- {- return _excludedSubnets.ContainsAddress(ip);- }-- /// <inheritdoc/>- public bool IsExcluded(EndPoint ip)- {- return ip is not null && IsExcluded(((IPEndPoint)ip).Address);}- /// <inheritdoc/>- public Collection<IPObject> CreateIPCollection(string[] values, bool negated = false)+ /// <summary>+ /// 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 addresses.+ /// </summary>+ private void InitialiseInterfaces(){- Collection<IPObject> col = new Collection<IPObject>();- if (values is null)+ lock (_initLock){- return col;- }+ _logger.LogDebug("Refreshing interfaces.");- for (int a = 0; a < values.Length; a++)- {- string v = values[a].Trim();+ var interfaces = new List<IPData>();+ var macAddresses = new List<PhysicalAddress>();try{@@@ -516,12 -588,19 +523,13 @@@/// <param name="configuration">The <see cref="NetworkConfiguration"/> to use.</param>public void UpdateSettings(object configuration){- NetworkConfiguration config = (NetworkConfiguration)configuration ?? throw new ArgumentNullException(nameof(configuration));+ ArgumentNullException.ThrowIfNull(configuration);- IsIP4Enabled = Socket.OSSupportsIPv4 && config.EnableIPV4;- IsIP6Enabled = Socket.OSSupportsIPv6 && config.EnableIPV6;- HappyEyeballs.HttpClientExtension.UseIPv6 = IsIP6Enabled;+ var config = (NetworkConfiguration)configuration;++ HappyEyeballs.HttpClientExtension.UseIPv6 = config.EnableIPv6;- if (!IsIP6Enabled && !IsIP4Enabled)- {- _logger.LogError("IPv4 and IPv6 cannot both be disabled.");- IsIP4Enabled = true;- }-- TrustAllIP6Interfaces = config.TrustAllIP6Interfaces;+ InitialiseLan(config);+ InitialiseRemote(config);if (string.IsNullOrEmpty(MockNetworkSettings)){