NetworkManager.cs 43 KB

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