NetworkManager.cs 44 KB

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