NetworkManager.cs 42 KB

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