NetworkManager.cs 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  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 SemaphoreSlim _networkEvent;
  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. _networkEvent = new SemaphoreSlim(1, 1);
  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. _networkEvent.Wait();
  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. _networkEvent.Release();
  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.TryParseSubnets(subnets, out _lanSubnets, false);
  254. _ = NetworkExtensions.TryParseSubnets(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 => IPAddress.TryParse(p, out var addresses)
  290. ? addresses
  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.TryParseSubnets(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 ipParts = parts[0].Split("/");
  383. if (string.Equals(parts[0], "all", StringComparison.OrdinalIgnoreCase))
  384. {
  385. _publishedServerUrls[new IPData(IPAddress.Broadcast, null)] = replacement;
  386. }
  387. else if (string.Equals(parts[0], "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(parts[0], "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 (IPAddress.TryParse(ipParts[0], out IPAddress? result))
  401. {
  402. var data = new IPData(result, null);
  403. if (ipParts.Length > 1 && int.TryParse(ipParts[1], out var netmask))
  404. {
  405. data.Subnet = new IPNetwork(result, netmask);
  406. }
  407. _publishedServerUrls[data] = replacement;
  408. }
  409. else if (TryParseInterface(ipParts[0], out var ifaces))
  410. {
  411. foreach (var iface in ifaces)
  412. {
  413. _publishedServerUrls[iface] = replacement;
  414. }
  415. }
  416. else
  417. {
  418. _logger.LogError("Unable to parse bind override: {Entry}", entry);
  419. }
  420. }
  421. }
  422. }
  423. }
  424. private void ConfigurationUpdated(object? sender, ConfigurationUpdateEventArgs evt)
  425. {
  426. if (evt.Key.Equals("network", StringComparison.Ordinal))
  427. {
  428. UpdateSettings((NetworkConfiguration)evt.NewConfiguration);
  429. }
  430. }
  431. /// <summary>
  432. /// Reloads all settings and re-initialises the instance.
  433. /// </summary>
  434. /// <param name="configuration">The <see cref="NetworkConfiguration"/> to use.</param>
  435. public void UpdateSettings(object configuration)
  436. {
  437. ArgumentNullException.ThrowIfNull(configuration);
  438. var config = (NetworkConfiguration)configuration;
  439. InitialiseLan(config);
  440. InitialiseRemote(config);
  441. if (string.IsNullOrEmpty(MockNetworkSettings))
  442. {
  443. InitialiseInterfaces();
  444. }
  445. else // Used in testing only.
  446. {
  447. // Format is <IPAddress>,<Index>,<Name>: <next interface>. Set index to -ve to simulate a gateway.
  448. var interfaceList = MockNetworkSettings.Split('|');
  449. foreach (var details in interfaceList)
  450. {
  451. var parts = details.Split(',');
  452. var split = parts[0].Split("/");
  453. var address = IPAddress.Parse(split[0]);
  454. var network = new IPNetwork(address, int.Parse(split[1], CultureInfo.InvariantCulture));
  455. var index = int.Parse(parts[1], CultureInfo.InvariantCulture);
  456. if (address.AddressFamily == AddressFamily.InterNetwork || address.AddressFamily == AddressFamily.InterNetworkV6)
  457. {
  458. var data = new IPData(address, network, parts[2]);
  459. data.Index = index;
  460. _interfaces.Add(data);
  461. }
  462. }
  463. }
  464. EnforceBindSettings(config);
  465. InitialiseOverrides(config);
  466. }
  467. /// <summary>
  468. /// Protected implementation of Dispose pattern.
  469. /// </summary>
  470. /// <param name="disposing"><c>True</c> to dispose the managed state.</param>
  471. protected virtual void Dispose(bool disposing)
  472. {
  473. if (!_disposed)
  474. {
  475. if (disposing)
  476. {
  477. _configurationManager.NamedConfigurationUpdated -= ConfigurationUpdated;
  478. NetworkChange.NetworkAddressChanged -= OnNetworkAddressChanged;
  479. NetworkChange.NetworkAvailabilityChanged -= OnNetworkAvailabilityChanged;
  480. _networkEvent.Dispose();
  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 (IPAddress.TryParse(address, out var ep))
  697. {
  698. return IPAddress.IsLoopback(ep) || (_lanSubnets.Any(x => x.Contains(ep)) && !_excludedSubnets.Any(x => x.Contains(ep)));
  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. }