NetworkManager.cs 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Net.NetworkInformation;
  8. using System.Net.Sockets;
  9. using System.Threading;
  10. using Jellyfin.Networking.Configuration;
  11. using Jellyfin.Networking.Constants;
  12. using Jellyfin.Networking.Extensions;
  13. using MediaBrowser.Common.Configuration;
  14. using MediaBrowser.Common.Net;
  15. using MediaBrowser.Model.Net;
  16. using Microsoft.AspNetCore.Http;
  17. using Microsoft.AspNetCore.HttpOverrides;
  18. using Microsoft.Extensions.Logging;
  19. namespace Jellyfin.Networking.Manager
  20. {
  21. /// <summary>
  22. /// Class to take care of network interface management.
  23. /// </summary>
  24. public class NetworkManager : INetworkManager, IDisposable
  25. {
  26. /// <summary>
  27. /// Threading lock for network properties.
  28. /// </summary>
  29. private readonly object _initLock;
  30. private readonly ILogger<NetworkManager> _logger;
  31. private readonly IConfigurationManager _configurationManager;
  32. private readonly object _networkEventLock;
  33. /// <summary>
  34. /// Holds the published server URLs and the IPs to use them on.
  35. /// </summary>
  36. private IReadOnlyDictionary<IPData, string> _publishedServerUrls;
  37. private IReadOnlyList<IPNetwork> _remoteAddressFilter;
  38. /// <summary>
  39. /// Used to stop "event-racing conditions".
  40. /// </summary>
  41. private bool _eventfire;
  42. /// <summary>
  43. /// List of all interface MAC addresses.
  44. /// </summary>
  45. private IReadOnlyList<PhysicalAddress> _macAddresses;
  46. /// <summary>
  47. /// Dictionary containing interface addresses and their subnets.
  48. /// </summary>
  49. private IReadOnlyList<IPData> _interfaces;
  50. /// <summary>
  51. /// Unfiltered user defined LAN subnets (<see cref="NetworkConfiguration.LocalNetworkSubnets"/>)
  52. /// or internal interface network subnets if undefined by user.
  53. /// </summary>
  54. private IReadOnlyList<IPNetwork> _lanSubnets;
  55. /// <summary>
  56. /// User defined list of subnets to excluded from the LAN.
  57. /// </summary>
  58. private IReadOnlyList<IPNetwork> _excludedSubnets;
  59. /// <summary>
  60. /// True if this object is disposed.
  61. /// </summary>
  62. private bool _disposed;
  63. /// <summary>
  64. /// Initializes a new instance of the <see cref="NetworkManager"/> class.
  65. /// </summary>
  66. /// <param name="configurationManager">IServerConfigurationManager instance.</param>
  67. /// <param name="logger">Logger to use for messages.</param>
  68. #pragma warning disable CS8618 // Non-nullable field is uninitialized. : Values are set in UpdateSettings function. Compiler doesn't yet recognise this.
  69. public NetworkManager(IConfigurationManager configurationManager, ILogger<NetworkManager> logger)
  70. {
  71. ArgumentNullException.ThrowIfNull(logger);
  72. ArgumentNullException.ThrowIfNull(configurationManager);
  73. _logger = logger;
  74. _configurationManager = configurationManager;
  75. _initLock = new();
  76. _interfaces = new List<IPData>();
  77. _macAddresses = new List<PhysicalAddress>();
  78. _publishedServerUrls = new Dictionary<IPData, string>();
  79. _networkEventLock = new object();
  80. _remoteAddressFilter = new List<IPNetwork>();
  81. UpdateSettings(_configurationManager.GetNetworkConfiguration());
  82. NetworkChange.NetworkAddressChanged += OnNetworkAddressChanged;
  83. NetworkChange.NetworkAvailabilityChanged += OnNetworkAvailabilityChanged;
  84. _configurationManager.NamedConfigurationUpdated += ConfigurationUpdated;
  85. }
  86. #pragma warning restore CS8618 // Non-nullable field is uninitialized.
  87. /// <summary>
  88. /// Event triggered on network changes.
  89. /// </summary>
  90. public event EventHandler? NetworkChanged;
  91. /// <summary>
  92. /// Gets or sets a value indicating whether testing is taking place.
  93. /// </summary>
  94. public static string MockNetworkSettings { get; set; } = string.Empty;
  95. /// <summary>
  96. /// Gets a value indicating whether IP4 is enabled.
  97. /// </summary>
  98. public bool IsIPv4Enabled => _configurationManager.GetNetworkConfiguration().EnableIPv4;
  99. /// <summary>
  100. /// Gets a value indicating whether IP6 is enabled.
  101. /// </summary>
  102. public bool IsIPv6Enabled => _configurationManager.GetNetworkConfiguration().EnableIPv6;
  103. /// <summary>
  104. /// Gets a value indicating whether is all IPv6 interfaces are trusted as internal.
  105. /// </summary>
  106. public bool TrustAllIPv6Interfaces { get; private set; }
  107. /// <summary>
  108. /// Gets the Published server override list.
  109. /// </summary>
  110. public IReadOnlyDictionary<IPData, string> PublishedServerUrls => _publishedServerUrls;
  111. /// <inheritdoc/>
  112. public void Dispose()
  113. {
  114. Dispose(true);
  115. GC.SuppressFinalize(this);
  116. }
  117. /// <summary>
  118. /// Handler for network change events.
  119. /// </summary>
  120. /// <param name="sender">Sender.</param>
  121. /// <param name="e">A <see cref="NetworkAvailabilityEventArgs"/> containing network availability information.</param>
  122. private void OnNetworkAvailabilityChanged(object? sender, NetworkAvailabilityEventArgs e)
  123. {
  124. _logger.LogDebug("Network availability changed.");
  125. HandleNetworkChange();
  126. }
  127. /// <summary>
  128. /// Handler for network change events.
  129. /// </summary>
  130. /// <param name="sender">Sender.</param>
  131. /// <param name="e">An <see cref="EventArgs"/>.</param>
  132. private void OnNetworkAddressChanged(object? sender, EventArgs e)
  133. {
  134. _logger.LogDebug("Network address change detected.");
  135. HandleNetworkChange();
  136. }
  137. /// <summary>
  138. /// Triggers our event, and re-loads interface information.
  139. /// </summary>
  140. private void HandleNetworkChange()
  141. {
  142. lock (_networkEventLock)
  143. {
  144. if (!_eventfire)
  145. {
  146. _logger.LogDebug("Network Address Change Event.");
  147. // As network events tend to fire one after the other only fire once every second.
  148. _eventfire = true;
  149. OnNetworkChange();
  150. }
  151. }
  152. }
  153. /// <summary>
  154. /// Waits for 2 seconds before re-initialising the settings, as typically these events fire multiple times in succession.
  155. /// </summary>
  156. private void OnNetworkChange()
  157. {
  158. try
  159. {
  160. Thread.Sleep(2000);
  161. var networkConfig = _configurationManager.GetNetworkConfiguration();
  162. if (IsIPv6Enabled && !Socket.OSSupportsIPv6)
  163. {
  164. UpdateSettings(networkConfig);
  165. }
  166. else
  167. {
  168. InitialiseInterfaces();
  169. InitialiseLan(networkConfig);
  170. EnforceBindSettings(networkConfig);
  171. }
  172. NetworkChanged?.Invoke(this, EventArgs.Empty);
  173. }
  174. finally
  175. {
  176. _eventfire = false;
  177. }
  178. }
  179. /// <summary>
  180. /// Generate a list of all the interface ip addresses and submasks where that are in the active/unknown state.
  181. /// Generate a list of all active mac addresses that aren't loopback addresses.
  182. /// </summary>
  183. private void InitialiseInterfaces()
  184. {
  185. lock (_initLock)
  186. {
  187. _logger.LogDebug("Refreshing interfaces.");
  188. var interfaces = new List<IPData>();
  189. var macAddresses = new List<PhysicalAddress>();
  190. try
  191. {
  192. var nics = NetworkInterface.GetAllNetworkInterfaces()
  193. .Where(i => i.SupportsMulticast && i.OperationalStatus == OperationalStatus.Up);
  194. foreach (NetworkInterface adapter in nics)
  195. {
  196. try
  197. {
  198. var ipProperties = adapter.GetIPProperties();
  199. var mac = adapter.GetPhysicalAddress();
  200. // Populate MAC list
  201. if (adapter.NetworkInterfaceType != NetworkInterfaceType.Loopback && PhysicalAddress.None.Equals(mac))
  202. {
  203. macAddresses.Add(mac);
  204. }
  205. // Populate interface list
  206. foreach (var info in ipProperties.UnicastAddresses)
  207. {
  208. if (IsIPv4Enabled && info.Address.AddressFamily == AddressFamily.InterNetwork)
  209. {
  210. var interfaceObject = new IPData(info.Address, new IPNetwork(info.Address, info.PrefixLength), adapter.Name);
  211. interfaceObject.Index = ipProperties.GetIPv4Properties().Index;
  212. interfaceObject.Name = adapter.Name;
  213. interfaces.Add(interfaceObject);
  214. }
  215. else if (IsIPv6Enabled && info.Address.AddressFamily == AddressFamily.InterNetworkV6)
  216. {
  217. var interfaceObject = new IPData(info.Address, new IPNetwork(info.Address, info.PrefixLength), adapter.Name);
  218. interfaceObject.Index = ipProperties.GetIPv6Properties().Index;
  219. interfaceObject.Name = adapter.Name;
  220. interfaces.Add(interfaceObject);
  221. }
  222. }
  223. }
  224. #pragma warning disable CA1031 // Do not catch general exception types
  225. catch (Exception ex)
  226. #pragma warning restore CA1031 // Do not catch general exception types
  227. {
  228. // Ignore error, and attempt to continue.
  229. _logger.LogError(ex, "Error encountered parsing interfaces.");
  230. }
  231. }
  232. }
  233. #pragma warning disable CA1031 // Do not catch general exception types
  234. catch (Exception ex)
  235. #pragma warning restore CA1031 // Do not catch general exception types
  236. {
  237. _logger.LogError(ex, "Error obtaining interfaces.");
  238. }
  239. // If no interfaces are found, fallback to loopback interfaces.
  240. if (interfaces.Count == 0)
  241. {
  242. _logger.LogWarning("No interface information available. Using loopback interface(s).");
  243. if (IsIPv4Enabled && !IsIPv6Enabled)
  244. {
  245. interfaces.Add(new IPData(IPAddress.Loopback, new IPNetwork(IPAddress.Loopback, 8), "lo"));
  246. }
  247. if (!IsIPv4Enabled && IsIPv6Enabled)
  248. {
  249. interfaces.Add(new IPData(IPAddress.IPv6Loopback, new IPNetwork(IPAddress.IPv6Loopback, 128), "lo"));
  250. }
  251. }
  252. _logger.LogDebug("Discovered {NumberOfInterfaces} interfaces.", interfaces.Count);
  253. _logger.LogDebug("Interfaces addresses: {Addresses}", interfaces.OrderByDescending(s => s.AddressFamily == AddressFamily.InterNetwork).Select(s => s.Address.ToString()));
  254. _macAddresses = macAddresses;
  255. _interfaces = interfaces;
  256. }
  257. }
  258. /// <summary>
  259. /// Initialises internal LAN cache.
  260. /// </summary>
  261. private void InitialiseLan(NetworkConfiguration config)
  262. {
  263. lock (_initLock)
  264. {
  265. _logger.LogDebug("Refreshing LAN information.");
  266. // Get configuration options
  267. var subnets = config.LocalNetworkSubnets;
  268. // If no LAN addresses are specified, all private subnets and Loopback are deemed to be the LAN
  269. if (!NetworkExtensions.TryParseToSubnets(subnets, out var lanSubnets, false) || lanSubnets.Count == 0)
  270. {
  271. _logger.LogDebug("Using LAN interface addresses as user provided no LAN details.");
  272. var fallbackLanSubnets = new List<IPNetwork>();
  273. if (IsIPv6Enabled)
  274. {
  275. fallbackLanSubnets.Add(Network.IPv6RFC4291Loopback); // RFC 4291 (Loopback)
  276. fallbackLanSubnets.Add(Network.IPv6RFC4291SiteLocal); // RFC 4291 (Site local)
  277. fallbackLanSubnets.Add(Network.IPv6RFC4193UniqueLocal); // RFC 4193 (Unique local)
  278. }
  279. if (IsIPv4Enabled)
  280. {
  281. fallbackLanSubnets.Add(Network.IPv4RFC5735Loopback); // RFC 5735 (Loopback)
  282. fallbackLanSubnets.Add(Network.IPv4RFC1918PrivateClassA); // RFC 1918 (private Class A)
  283. fallbackLanSubnets.Add(Network.IPv4RFC1918PrivateClassB); // RFC 1918 (private Class B)
  284. fallbackLanSubnets.Add(Network.IPv4RFC1918PrivateClassC); // RFC 1918 (private Class C)
  285. }
  286. _lanSubnets = fallbackLanSubnets;
  287. }
  288. else
  289. {
  290. _lanSubnets = lanSubnets;
  291. }
  292. _excludedSubnets = NetworkExtensions.TryParseToSubnets(subnets, out var excludedSubnets, true)
  293. ? excludedSubnets
  294. : new List<IPNetwork>();
  295. _logger.LogInformation("Defined LAN addresses: {0}", _lanSubnets.Select(s => s.Prefix + "/" + s.PrefixLength));
  296. _logger.LogInformation("Defined LAN exclusions: {0}", _excludedSubnets.Select(s => s.Prefix + "/" + s.PrefixLength));
  297. _logger.LogInformation("Using LAN addresses: {0}", _lanSubnets.Where(s => !_excludedSubnets.Contains(s)).Select(s => s.Prefix + "/" + s.PrefixLength));
  298. }
  299. }
  300. /// <summary>
  301. /// Enforce bind addresses and exclusions on available interfaces.
  302. /// </summary>
  303. private void EnforceBindSettings(NetworkConfiguration config)
  304. {
  305. lock (_initLock)
  306. {
  307. // Respect explicit bind addresses
  308. var interfaces = _interfaces.ToList();
  309. var localNetworkAddresses = config.LocalNetworkAddresses;
  310. if (localNetworkAddresses.Length > 0 && !string.IsNullOrWhiteSpace(localNetworkAddresses[0]))
  311. {
  312. var bindAddresses = localNetworkAddresses.Select(p => NetworkExtensions.TryParseToSubnet(p, out var network)
  313. ? network.Prefix
  314. : (interfaces.Where(x => x.Name.Equals(p, StringComparison.OrdinalIgnoreCase))
  315. .Select(x => x.Address)
  316. .FirstOrDefault() ?? IPAddress.None))
  317. .Where(x => x != IPAddress.None)
  318. .ToHashSet();
  319. interfaces = interfaces.Where(x => bindAddresses.Contains(x.Address)).ToList();
  320. if (bindAddresses.Contains(IPAddress.Loopback))
  321. {
  322. interfaces.Add(new IPData(IPAddress.Loopback, Network.IPv4RFC5735Loopback, "lo"));
  323. }
  324. if (bindAddresses.Contains(IPAddress.IPv6Loopback))
  325. {
  326. interfaces.Add(new IPData(IPAddress.IPv6Loopback, Network.IPv6RFC4291Loopback, "lo"));
  327. }
  328. }
  329. // Remove all interfaces matching any virtual machine interface prefix
  330. if (config.IgnoreVirtualInterfaces)
  331. {
  332. // Remove potentially existing * and split config string into prefixes
  333. var virtualInterfacePrefixes = config.VirtualInterfaceNames
  334. .Select(i => i.Replace("*", string.Empty, StringComparison.OrdinalIgnoreCase));
  335. // Check all interfaces for matches against the prefixes and remove them
  336. if (_interfaces.Count > 0)
  337. {
  338. foreach (var virtualInterfacePrefix in virtualInterfacePrefixes)
  339. {
  340. interfaces.RemoveAll(x => x.Name.StartsWith(virtualInterfacePrefix, StringComparison.OrdinalIgnoreCase));
  341. }
  342. }
  343. }
  344. // Remove all IPv4 interfaces if IPv4 is disabled
  345. if (!IsIPv4Enabled)
  346. {
  347. interfaces.RemoveAll(x => x.AddressFamily == AddressFamily.InterNetwork);
  348. }
  349. // Remove all IPv6 interfaces if IPv6 is disabled
  350. if (!IsIPv6Enabled)
  351. {
  352. interfaces.RemoveAll(x => x.AddressFamily == AddressFamily.InterNetworkV6);
  353. }
  354. _logger.LogInformation("Using bind addresses: {0}", interfaces.OrderByDescending(x => x.AddressFamily == AddressFamily.InterNetwork).Select(x => x.Address));
  355. _interfaces = interfaces;
  356. }
  357. }
  358. /// <summary>
  359. /// Initialises the remote address values.
  360. /// </summary>
  361. private void InitialiseRemote(NetworkConfiguration config)
  362. {
  363. lock (_initLock)
  364. {
  365. // Parse config values into filter collection
  366. var remoteIPFilter = config.RemoteIPFilter;
  367. if (remoteIPFilter.Any() && !string.IsNullOrWhiteSpace(remoteIPFilter.First()))
  368. {
  369. // Parse all IPs with netmask to a subnet
  370. var remoteAddressFilter = new List<IPNetwork>();
  371. var remoteFilteredSubnets = remoteIPFilter.Where(x => x.Contains('/', StringComparison.OrdinalIgnoreCase)).ToArray();
  372. if (NetworkExtensions.TryParseToSubnets(remoteFilteredSubnets, out var remoteAddressFilterResult, false))
  373. {
  374. remoteAddressFilter = remoteAddressFilterResult.ToList();
  375. }
  376. // Parse everything else as an IP and construct subnet with a single IP
  377. var remoteFilteredIPs = remoteIPFilter.Where(x => !x.Contains('/', StringComparison.OrdinalIgnoreCase));
  378. foreach (var ip in remoteFilteredIPs)
  379. {
  380. if (IPAddress.TryParse(ip, out var ipp))
  381. {
  382. remoteAddressFilter.Add(new IPNetwork(ipp, ipp.AddressFamily == AddressFamily.InterNetwork ? Network.MinimumIPv4PrefixSize : Network.MinimumIPv6PrefixSize));
  383. }
  384. }
  385. _remoteAddressFilter = remoteAddressFilter;
  386. }
  387. }
  388. }
  389. /// <summary>
  390. /// Parses the user defined overrides into the dictionary object.
  391. /// Overrides are the equivalent of localised publishedServerUrl, enabling
  392. /// different addresses to be advertised over different subnets.
  393. /// format is subnet=ipaddress|host|uri
  394. /// when subnet = 0.0.0.0, any external address matches.
  395. /// </summary>
  396. private void InitialiseOverrides(NetworkConfiguration config)
  397. {
  398. lock (_initLock)
  399. {
  400. var publishedServerUrls = new Dictionary<IPData, string>();
  401. var overrides = config.PublishedServerUriBySubnet;
  402. foreach (var entry in overrides)
  403. {
  404. var parts = entry.Split('=');
  405. if (parts.Length != 2)
  406. {
  407. _logger.LogError("Unable to parse bind override: {Entry}", entry);
  408. return;
  409. }
  410. var replacement = parts[1].Trim();
  411. var identifier = parts[0];
  412. if (string.Equals(identifier, "all", StringComparison.OrdinalIgnoreCase))
  413. {
  414. publishedServerUrls[new IPData(IPAddress.Broadcast, null)] = replacement;
  415. }
  416. else if (string.Equals(identifier, "external", StringComparison.OrdinalIgnoreCase))
  417. {
  418. publishedServerUrls[new IPData(IPAddress.Any, Network.IPv4Any)] = replacement;
  419. publishedServerUrls[new IPData(IPAddress.IPv6Any, Network.IPv6Any)] = replacement;
  420. }
  421. else if (string.Equals(identifier, "internal", StringComparison.OrdinalIgnoreCase))
  422. {
  423. foreach (var lan in _lanSubnets)
  424. {
  425. var lanPrefix = lan.Prefix;
  426. publishedServerUrls[new IPData(lanPrefix, new IPNetwork(lanPrefix, lan.PrefixLength))] = replacement;
  427. }
  428. }
  429. else if (NetworkExtensions.TryParseToSubnet(identifier, out var result) && result is not null)
  430. {
  431. var data = new IPData(result.Prefix, result);
  432. publishedServerUrls[data] = replacement;
  433. }
  434. else if (TryParseInterface(identifier, out var ifaces))
  435. {
  436. foreach (var iface in ifaces)
  437. {
  438. publishedServerUrls[iface] = replacement;
  439. }
  440. }
  441. else
  442. {
  443. _logger.LogError("Unable to parse bind override: {Entry}", entry);
  444. }
  445. }
  446. _publishedServerUrls = publishedServerUrls;
  447. }
  448. }
  449. private void ConfigurationUpdated(object? sender, ConfigurationUpdateEventArgs evt)
  450. {
  451. if (evt.Key.Equals(NetworkConfigurationStore.StoreKey, StringComparison.Ordinal))
  452. {
  453. UpdateSettings((NetworkConfiguration)evt.NewConfiguration);
  454. }
  455. }
  456. /// <summary>
  457. /// Reloads all settings and re-initialises the instance.
  458. /// </summary>
  459. /// <param name="configuration">The <see cref="NetworkConfiguration"/> to use.</param>
  460. public void UpdateSettings(object configuration)
  461. {
  462. ArgumentNullException.ThrowIfNull(configuration);
  463. var config = (NetworkConfiguration)configuration;
  464. HappyEyeballs.HttpClientExtension.UseIPv6 = config.EnableIPv6;
  465. InitialiseLan(config);
  466. InitialiseRemote(config);
  467. if (string.IsNullOrEmpty(MockNetworkSettings))
  468. {
  469. InitialiseInterfaces();
  470. }
  471. else // Used in testing only.
  472. {
  473. // Format is <IPAddress>,<Index>,<Name>: <next interface>. Set index to -ve to simulate a gateway.
  474. var interfaceList = MockNetworkSettings.Split('|');
  475. var interfaces = new List<IPData>();
  476. foreach (var details in interfaceList)
  477. {
  478. var parts = details.Split(',');
  479. if (NetworkExtensions.TryParseToSubnet(parts[0], out var subnet))
  480. {
  481. var address = subnet.Prefix;
  482. var index = int.Parse(parts[1], CultureInfo.InvariantCulture);
  483. if (address.AddressFamily == AddressFamily.InterNetwork || address.AddressFamily == AddressFamily.InterNetworkV6)
  484. {
  485. var data = new IPData(address, subnet, parts[2]);
  486. data.Index = index;
  487. interfaces.Add(data);
  488. }
  489. }
  490. else
  491. {
  492. _logger.LogWarning("Could not parse mock interface settings: {Part}", details);
  493. }
  494. }
  495. _interfaces = interfaces;
  496. }
  497. EnforceBindSettings(config);
  498. InitialiseOverrides(config);
  499. }
  500. /// <summary>
  501. /// Protected implementation of Dispose pattern.
  502. /// </summary>
  503. /// <param name="disposing"><c>True</c> to dispose the managed state.</param>
  504. protected virtual void Dispose(bool disposing)
  505. {
  506. if (!_disposed)
  507. {
  508. if (disposing)
  509. {
  510. _configurationManager.NamedConfigurationUpdated -= ConfigurationUpdated;
  511. NetworkChange.NetworkAddressChanged -= OnNetworkAddressChanged;
  512. NetworkChange.NetworkAvailabilityChanged -= OnNetworkAvailabilityChanged;
  513. }
  514. _disposed = true;
  515. }
  516. }
  517. /// <inheritdoc/>
  518. public bool TryParseInterface(string intf, [NotNullWhen(true)] out IReadOnlyList<IPData>? result)
  519. {
  520. if (string.IsNullOrEmpty(intf)
  521. || _interfaces is null
  522. || _interfaces.Count == 0)
  523. {
  524. result = null;
  525. return false;
  526. }
  527. // Match all interfaces starting with names starting with token
  528. result = _interfaces
  529. .Where(i => i.Name.Equals(intf, StringComparison.OrdinalIgnoreCase)
  530. && ((IsIPv4Enabled && i.Address.AddressFamily == AddressFamily.InterNetwork)
  531. || (IsIPv6Enabled && i.Address.AddressFamily == AddressFamily.InterNetworkV6)))
  532. .OrderBy(x => x.Index)
  533. .ToArray();
  534. return result.Count > 0;
  535. }
  536. /// <inheritdoc/>
  537. public bool HasRemoteAccess(IPAddress remoteIP)
  538. {
  539. var config = _configurationManager.GetNetworkConfiguration();
  540. if (config.EnableRemoteAccess)
  541. {
  542. // Comma separated list of IP addresses or IP/netmask entries for networks that will be allowed to connect remotely.
  543. // If left blank, all remote addresses will be allowed.
  544. if (_remoteAddressFilter.Any() && !_lanSubnets.Any(x => x.Contains(remoteIP)))
  545. {
  546. // remoteAddressFilter is a whitelist or blacklist.
  547. var matches = _remoteAddressFilter.Count(remoteNetwork => remoteNetwork.Contains(remoteIP));
  548. if ((!config.IsRemoteIPFilterBlacklist && matches > 0)
  549. || (config.IsRemoteIPFilterBlacklist && matches == 0))
  550. {
  551. return true;
  552. }
  553. return false;
  554. }
  555. }
  556. else if (!_lanSubnets.Any(x => x.Contains(remoteIP)))
  557. {
  558. // Remote not enabled. So everyone should be LAN.
  559. return false;
  560. }
  561. return true;
  562. }
  563. /// <inheritdoc/>
  564. public IReadOnlyList<PhysicalAddress> GetMacAddresses()
  565. {
  566. // Populated in construction - so always has values.
  567. return _macAddresses;
  568. }
  569. /// <inheritdoc/>
  570. public IReadOnlyList<IPData> GetLoopbacks()
  571. {
  572. if (!IsIPv4Enabled && !IsIPv6Enabled)
  573. {
  574. return Array.Empty<IPData>();
  575. }
  576. var loopbackNetworks = new List<IPData>();
  577. if (IsIPv4Enabled)
  578. {
  579. loopbackNetworks.Add(new IPData(IPAddress.Loopback, Network.IPv4RFC5735Loopback, "lo"));
  580. }
  581. if (IsIPv6Enabled)
  582. {
  583. loopbackNetworks.Add(new IPData(IPAddress.IPv6Loopback, Network.IPv6RFC4291Loopback, "lo"));
  584. }
  585. return loopbackNetworks;
  586. }
  587. /// <inheritdoc/>
  588. public IReadOnlyList<IPData> GetAllBindInterfaces(bool individualInterfaces = false)
  589. {
  590. if (_interfaces.Count != 0)
  591. {
  592. return _interfaces;
  593. }
  594. // No bind address and no exclusions, so listen on all interfaces.
  595. var result = new List<IPData>();
  596. if (individualInterfaces)
  597. {
  598. result.AddRange(_interfaces);
  599. return result;
  600. }
  601. if (IsIPv4Enabled && IsIPv6Enabled)
  602. {
  603. // Kestrel source code shows it uses Sockets.DualMode - so this also covers IPAddress.Any by default
  604. result.Add(new IPData(IPAddress.IPv6Any, Network.IPv6Any));
  605. }
  606. else if (IsIPv4Enabled)
  607. {
  608. result.Add(new IPData(IPAddress.Any, Network.IPv4Any));
  609. }
  610. else if (IsIPv6Enabled)
  611. {
  612. // Cannot use IPv6Any as Kestrel will bind to IPv4 addresses too.
  613. foreach (var iface in _interfaces)
  614. {
  615. if (iface.AddressFamily == AddressFamily.InterNetworkV6)
  616. {
  617. result.Add(iface);
  618. }
  619. }
  620. }
  621. return result;
  622. }
  623. /// <inheritdoc/>
  624. public string GetBindAddress(string source, out int? port)
  625. {
  626. if (!NetworkExtensions.TryParseHost(source, out var addresses, IsIPv4Enabled, IsIPv6Enabled))
  627. {
  628. addresses = Array.Empty<IPAddress>();
  629. }
  630. var result = GetBindAddress(addresses.FirstOrDefault(), out port);
  631. return result;
  632. }
  633. /// <inheritdoc/>
  634. public string GetBindAddress(HttpRequest source, out int? port)
  635. {
  636. var result = GetBindAddress(source.Host.Host, out port);
  637. port ??= source.Host.Port;
  638. return result;
  639. }
  640. /// <inheritdoc/>
  641. public string GetBindAddress(IPAddress? source, out int? port, bool skipOverrides = false)
  642. {
  643. port = null;
  644. string result;
  645. if (source is not null)
  646. {
  647. if (IsIPv4Enabled && !IsIPv6Enabled && source.AddressFamily == AddressFamily.InterNetworkV6)
  648. {
  649. _logger.LogWarning("IPv6 is disabled in Jellyfin, but enabled in the OS. This may affect how the interface is selected.");
  650. }
  651. if (!IsIPv4Enabled && IsIPv6Enabled && source.AddressFamily == AddressFamily.InterNetwork)
  652. {
  653. _logger.LogWarning("IPv4 is disabled in Jellyfin, but enabled in the OS. This may affect how the interface is selected.");
  654. }
  655. bool isExternal = !_lanSubnets.Any(network => network.Contains(source));
  656. _logger.LogDebug("Trying to get bind address for source {Source} - External: {IsExternal}", source, isExternal);
  657. if (!skipOverrides && MatchesPublishedServerUrl(source, isExternal, out result))
  658. {
  659. return result;
  660. }
  661. // No preference given, so move on to bind addresses.
  662. if (MatchesBindInterface(source, isExternal, out result))
  663. {
  664. return result;
  665. }
  666. if (isExternal && MatchesExternalInterface(source, out result))
  667. {
  668. return result;
  669. }
  670. }
  671. // Get the first LAN interface address that's not excluded and not a loopback address.
  672. // Get all available interfaces, prefer local interfaces
  673. var availableInterfaces = _interfaces.Where(x => !IPAddress.IsLoopback(x.Address))
  674. .OrderByDescending(x => IsInLocalNetwork(x.Address))
  675. .ThenBy(x => x.Index)
  676. .ToList();
  677. if (availableInterfaces.Count == 0)
  678. {
  679. // There isn't any others, so we'll use the loopback.
  680. result = IsIPv4Enabled && !IsIPv6Enabled ? "127.0.0.1" : "::1";
  681. _logger.LogWarning("{Source}: Only loopback {Result} returned, using that as bind address.", source, result);
  682. return result;
  683. }
  684. // If no source address is given, use the preferred (first) interface
  685. if (source is null)
  686. {
  687. result = NetworkExtensions.FormatIPString(availableInterfaces.First().Address);
  688. _logger.LogDebug("{Source}: Using first internal interface as bind address: {Result}", source, result);
  689. return result;
  690. }
  691. // Does the request originate in one of the interface subnets?
  692. // (For systems with multiple internal network cards, and multiple subnets)
  693. foreach (var intf in availableInterfaces)
  694. {
  695. if (intf.Subnet.Contains(source))
  696. {
  697. result = NetworkExtensions.FormatIPString(intf.Address);
  698. _logger.LogDebug("{Source}: Found interface with matching subnet, using it as bind address: {Result}", source, result);
  699. return result;
  700. }
  701. }
  702. // Fallback to first available interface
  703. result = NetworkExtensions.FormatIPString(availableInterfaces[0].Address);
  704. _logger.LogDebug("{Source}: No matching interfaces found, using preferred interface as bind address: {Result}", source, result);
  705. return result;
  706. }
  707. /// <inheritdoc/>
  708. public IReadOnlyList<IPData> GetInternalBindAddresses()
  709. {
  710. // Select all local bind addresses
  711. return _interfaces.Where(x => IsInLocalNetwork(x.Address))
  712. .OrderBy(x => x.Index)
  713. .ToList();
  714. }
  715. /// <inheritdoc/>
  716. public bool IsInLocalNetwork(string address)
  717. {
  718. if (NetworkExtensions.TryParseToSubnet(address, out var subnet))
  719. {
  720. return IPAddress.IsLoopback(subnet.Prefix) || (_lanSubnets.Any(x => x.Contains(subnet.Prefix)) && !_excludedSubnets.Any(x => x.Contains(subnet.Prefix)));
  721. }
  722. if (NetworkExtensions.TryParseHost(address, out var addresses, IsIPv4Enabled, IsIPv6Enabled))
  723. {
  724. foreach (var ept in addresses)
  725. {
  726. if (IPAddress.IsLoopback(ept) || (_lanSubnets.Any(x => x.Contains(ept)) && !_excludedSubnets.Any(x => x.Contains(ept))))
  727. {
  728. return true;
  729. }
  730. }
  731. }
  732. return false;
  733. }
  734. /// <inheritdoc/>
  735. public bool IsInLocalNetwork(IPAddress address)
  736. {
  737. ArgumentNullException.ThrowIfNull(address);
  738. // See conversation at https://github.com/jellyfin/jellyfin/pull/3515.
  739. if ((TrustAllIPv6Interfaces && address.AddressFamily == AddressFamily.InterNetworkV6)
  740. || address.Equals(IPAddress.Loopback)
  741. || address.Equals(IPAddress.IPv6Loopback))
  742. {
  743. return true;
  744. }
  745. // As private addresses can be redefined by Configuration.LocalNetworkAddresses
  746. return CheckIfLanAndNotExcluded(address);
  747. }
  748. private bool CheckIfLanAndNotExcluded(IPAddress address)
  749. {
  750. foreach (var lanSubnet in _lanSubnets)
  751. {
  752. if (lanSubnet.Contains(address))
  753. {
  754. foreach (var excludedSubnet in _excludedSubnets)
  755. {
  756. if (excludedSubnet.Contains(address))
  757. {
  758. return false;
  759. }
  760. }
  761. return true;
  762. }
  763. }
  764. return false;
  765. }
  766. /// <summary>
  767. /// Attempts to match the source against the published server URL overrides.
  768. /// </summary>
  769. /// <param name="source">IP source address to use.</param>
  770. /// <param name="isInExternalSubnet">True if the source is in an external subnet.</param>
  771. /// <param name="bindPreference">The published server URL that matches the source address.</param>
  772. /// <returns><c>true</c> if a match is found, <c>false</c> otherwise.</returns>
  773. private bool MatchesPublishedServerUrl(IPAddress source, bool isInExternalSubnet, out string bindPreference)
  774. {
  775. bindPreference = string.Empty;
  776. int? port = null;
  777. var validPublishedServerUrls = _publishedServerUrls.Where(x => x.Key.Address.Equals(IPAddress.Any)
  778. || x.Key.Address.Equals(IPAddress.IPv6Any)
  779. || x.Key.Subnet.Contains(source))
  780. .DistinctBy(x => x.Key)
  781. .OrderBy(x => x.Key.Address.Equals(IPAddress.Any)
  782. || x.Key.Address.Equals(IPAddress.IPv6Any))
  783. .ToList();
  784. // Check for user override.
  785. foreach (var data in validPublishedServerUrls)
  786. {
  787. if (isInExternalSubnet && (data.Key.Address.Equals(IPAddress.Any) || data.Key.Address.Equals(IPAddress.IPv6Any)))
  788. {
  789. // External.
  790. bindPreference = data.Value;
  791. break;
  792. }
  793. // Get address interface.
  794. var intf = _interfaces.OrderBy(x => x.Index).FirstOrDefault(x => data.Key.Subnet.Contains(x.Address));
  795. if (intf?.Address is not null)
  796. {
  797. // Match IP address.
  798. bindPreference = data.Value;
  799. break;
  800. }
  801. }
  802. if (string.IsNullOrEmpty(bindPreference))
  803. {
  804. _logger.LogDebug("{Source}: No matching bind address override found", source);
  805. return false;
  806. }
  807. // Has it got a port defined?
  808. var parts = bindPreference.Split(':');
  809. if (parts.Length > 1)
  810. {
  811. if (int.TryParse(parts[1], out int p))
  812. {
  813. bindPreference = parts[0];
  814. port = p;
  815. }
  816. }
  817. if (port is not null)
  818. {
  819. _logger.LogDebug("{Source}: Matching bind address override found: {Address}:{Port}", source, bindPreference, port);
  820. }
  821. else
  822. {
  823. _logger.LogDebug("{Source}: Matching bind address override found: {Address}", source, bindPreference);
  824. }
  825. return true;
  826. }
  827. /// <summary>
  828. /// Attempts to match the source against the user defined bind interfaces.
  829. /// </summary>
  830. /// <param name="source">IP source address to use.</param>
  831. /// <param name="isInExternalSubnet">True if the source is in the external subnet.</param>
  832. /// <param name="result">The result, if a match is found.</param>
  833. /// <returns><c>true</c> if a match is found, <c>false</c> otherwise.</returns>
  834. private bool MatchesBindInterface(IPAddress source, bool isInExternalSubnet, out string result)
  835. {
  836. result = string.Empty;
  837. int count = _interfaces.Count;
  838. if (count == 1 && (_interfaces[0].Equals(IPAddress.Any) || _interfaces[0].Equals(IPAddress.IPv6Any)))
  839. {
  840. // Ignore IPAny addresses.
  841. count = 0;
  842. }
  843. if (count == 0)
  844. {
  845. return false;
  846. }
  847. IPAddress? bindAddress = null;
  848. if (isInExternalSubnet)
  849. {
  850. var externalInterfaces = _interfaces.Where(x => !IsInLocalNetwork(x.Address))
  851. .OrderBy(x => x.Index)
  852. .ToList();
  853. if (externalInterfaces.Count > 0)
  854. {
  855. // Check to see if any of the external bind interfaces are in the same subnet as the source.
  856. // If none exists, this will select the first external interface if there is one.
  857. bindAddress = externalInterfaces
  858. .OrderByDescending(x => x.Subnet.Contains(source))
  859. .ThenBy(x => x.Index)
  860. .Select(x => x.Address)
  861. .First();
  862. result = NetworkExtensions.FormatIPString(bindAddress);
  863. _logger.LogDebug("{Source}: External request received, matching external bind address found: {Result}", source, result);
  864. return true;
  865. }
  866. _logger.LogWarning("{Source}: External request received, no matching external bind address found, trying internal addresses.", source);
  867. }
  868. else
  869. {
  870. // Check to see if any of the internal bind interfaces are in the same subnet as the source.
  871. // If none exists, this will select the first internal interface if there is one.
  872. bindAddress = _interfaces.Where(x => IsInLocalNetwork(x.Address))
  873. .OrderByDescending(x => x.Subnet.Contains(source))
  874. .ThenBy(x => x.Index)
  875. .Select(x => x.Address)
  876. .FirstOrDefault();
  877. if (bindAddress is not null)
  878. {
  879. result = NetworkExtensions.FormatIPString(bindAddress);
  880. _logger.LogDebug("{Source}: Internal request received, matching internal bind address found: {Result}", source, result);
  881. return true;
  882. }
  883. }
  884. return false;
  885. }
  886. /// <summary>
  887. /// Attempts to match the source against external interfaces.
  888. /// </summary>
  889. /// <param name="source">IP source address to use.</param>
  890. /// <param name="result">The result, if a match is found.</param>
  891. /// <returns><c>true</c> if a match is found, <c>false</c> otherwise.</returns>
  892. private bool MatchesExternalInterface(IPAddress source, out string result)
  893. {
  894. // Get the first external interface address that isn't a loopback.
  895. var extResult = _interfaces.Where(p => !IsInLocalNetwork(p.Address)).OrderBy(x => x.Index).ToArray();
  896. // No external interface found
  897. if (extResult.Length == 0)
  898. {
  899. result = string.Empty;
  900. _logger.LogWarning("{Source}: External request received, but no external interface found. Need to route through internal network.", source);
  901. return false;
  902. }
  903. // Does the request originate in one of the interface subnets?
  904. // (For systems with multiple network cards and/or multiple subnets)
  905. foreach (var intf in extResult)
  906. {
  907. if (intf.Subnet.Contains(source))
  908. {
  909. result = NetworkExtensions.FormatIPString(intf.Address);
  910. _logger.LogDebug("{Source}: Found external interface with matching subnet, using it as bind address: {Result}", source, result);
  911. return true;
  912. }
  913. }
  914. // Fallback to first external interface.
  915. result = NetworkExtensions.FormatIPString(extResult[0].Address);
  916. _logger.LogDebug("{Source}: Using first external interface as bind address: {Result}", source, result);
  917. return true;
  918. }
  919. }
  920. }