NetworkManager.cs 43 KB

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