NetworkManager.cs 45 KB

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