NetworkManager.cs 45 KB

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