NetworkManager.cs 44 KB

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