INetworkManager.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #nullable enable
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.Net;
  6. using System.Net.NetworkInformation;
  7. using Jellyfin.Networking.Configuration;
  8. using MediaBrowser.Common.Net;
  9. using Microsoft.AspNetCore.Http;
  10. namespace Jellyfin.Networking.Manager
  11. {
  12. /// <summary>
  13. /// Interface for the NetworkManager class.
  14. /// </summary>
  15. public interface INetworkManager
  16. {
  17. /// <summary>
  18. /// Event triggered on network changes.
  19. /// </summary>
  20. event EventHandler NetworkChanged;
  21. /// <summary>
  22. /// Gets the published server urls list.
  23. /// </summary>
  24. Dictionary<IPNetAddress, string> PublishedServerUrls { get; }
  25. /// <summary>
  26. /// Gets a value indicating whether is all IPv6 interfaces are trusted as internal.
  27. /// </summary>
  28. bool TrustAllIP6Interfaces { get; }
  29. /// <summary>
  30. /// Gets the remote address filter.
  31. /// </summary>
  32. Collection<IPObject> RemoteAddressFilter { get; }
  33. /// <summary>
  34. /// Gets or sets a value indicating whether iP6 is enabled.
  35. /// </summary>
  36. bool IsIP6Enabled { get; set; }
  37. /// <summary>
  38. /// Gets or sets a value indicating whether iP4 is enabled.
  39. /// </summary>
  40. bool IsIP4Enabled { get; set; }
  41. /// <summary>
  42. /// Calculates the list of interfaces to use for Kestrel.
  43. /// </summary>
  44. /// <returns>A Collection{IPObject} object containing all the interfaces to bind.
  45. /// If all the interfaces are specified, and none are excluded, it returns zero items
  46. /// to represent any address.</returns>
  47. /// <param name="individualInterfaces">When false, return <see cref="IPAddress.Any"/> or <see cref="IPAddress.IPv6Any"/> for all interfaces.</param>
  48. Collection<IPObject> GetAllBindInterfaces(bool individualInterfaces = false);
  49. /// <summary>
  50. /// Returns a collection containing the loopback interfaces.
  51. /// </summary>
  52. /// <returns>Collection{IPObject}.</returns>
  53. Collection<IPObject> GetLoopbacks();
  54. /// <summary>
  55. /// Retrieves the bind address to use in system url's. (Server Discovery, PlayTo, LiveTV, SystemInfo)
  56. /// If no bind addresses are specified, an internal interface address is selected.
  57. /// The priority of selection is as follows:-
  58. ///
  59. /// The value contained in the startup parameter --published-server-url.
  60. ///
  61. /// If the user specified custom subnet overrides, the correct subnet for the source address.
  62. ///
  63. /// If the user specified bind interfaces to use:-
  64. /// The bind interface that contains the source subnet.
  65. /// The first bind interface specified that suits best first the source's endpoint. eg. external or internal.
  66. ///
  67. /// If the source is from a public subnet address range and the user hasn't specified any bind addresses:-
  68. /// The first public interface that isn't a loopback and contains the source subnet.
  69. /// The first public interface that isn't a loopback. Priority is given to interfaces with gateways.
  70. /// An internal interface if there are no public ip addresses.
  71. ///
  72. /// If the source is from a private subnet address range and the user hasn't specified any bind addresses:-
  73. /// The first private interface that contains the source subnet.
  74. /// The first private interface that isn't a loopback. Priority is given to interfaces with gateways.
  75. ///
  76. /// If no interfaces meet any of these criteria, then a loopback address is returned.
  77. ///
  78. /// Interface that have been specifically excluded from binding are not used in any of the calculations.
  79. /// </summary>
  80. /// <param name="source">Source of the request.</param>
  81. /// <param name="port">Optional port returned, if it's part of an override.</param>
  82. /// <returns>IP Address to use, or loopback address if all else fails.</returns>
  83. string GetBindInterface(IPObject source, out int? port);
  84. /// <summary>
  85. /// Retrieves the bind address to use in system url's. (Server Discovery, PlayTo, LiveTV, SystemInfo)
  86. /// If no bind addresses are specified, an internal interface address is selected.
  87. /// (See <see cref="GetBindInterface(IPObject, out int?)"/>.
  88. /// </summary>
  89. /// <param name="source">Source of the request.</param>
  90. /// <param name="port">Optional port returned, if it's part of an override.</param>
  91. /// <returns>IP Address to use, or loopback address if all else fails.</returns>
  92. string GetBindInterface(HttpRequest source, out int? port);
  93. /// <summary>
  94. /// Retrieves the bind address to use in system url's. (Server Discovery, PlayTo, LiveTV, SystemInfo)
  95. /// If no bind addresses are specified, an internal interface address is selected.
  96. /// (See <see cref="GetBindInterface(IPObject, out int?)"/>.
  97. /// </summary>
  98. /// <param name="source">IP address of the request.</param>
  99. /// <param name="port">Optional port returned, if it's part of an override.</param>
  100. /// <returns>IP Address to use, or loopback address if all else fails.</returns>
  101. string GetBindInterface(IPAddress source, out int? port);
  102. /// <summary>
  103. /// Retrieves the bind address to use in system url's. (Server Discovery, PlayTo, LiveTV, SystemInfo)
  104. /// If no bind addresses are specified, an internal interface address is selected.
  105. /// (See <see cref="GetBindInterface(IPObject, out int?)"/>.
  106. /// </summary>
  107. /// <param name="source">Source of the request.</param>
  108. /// <param name="port">Optional port returned, if it's part of an override.</param>
  109. /// <returns>IP Address to use, or loopback address if all else fails.</returns>
  110. string GetBindInterface(string source, out int? port);
  111. /// <summary>
  112. /// Checks to see if the ip address is specifically excluded in LocalNetworkAddresses.
  113. /// </summary>
  114. /// <param name="address">IP address to check.</param>
  115. /// <returns>True if it is.</returns>
  116. bool IsExcludedInterface(IPAddress address);
  117. /// <summary>
  118. /// Get a list of all the MAC addresses associated with active interfaces.
  119. /// </summary>
  120. /// <returns>List of MAC addresses.</returns>
  121. IReadOnlyCollection<PhysicalAddress> GetMacAddresses();
  122. /// <summary>
  123. /// Checks to see if the IP Address provided matches an interface that has a gateway.
  124. /// </summary>
  125. /// <param name="addressObj">IP to check. Can be an IPAddress or an IPObject.</param>
  126. /// <returns>Result of the check.</returns>
  127. bool IsGatewayInterface(IPObject? addressObj);
  128. /// <summary>
  129. /// Checks to see if the IP Address provided matches an interface that has a gateway.
  130. /// </summary>
  131. /// <param name="addressObj">IP to check. Can be an IPAddress or an IPObject.</param>
  132. /// <returns>Result of the check.</returns>
  133. bool IsGatewayInterface(IPAddress? addressObj);
  134. /// <summary>
  135. /// Returns true if the address is a private address.
  136. /// The config option TrustIP6Interfaces overrides this functions behaviour.
  137. /// </summary>
  138. /// <param name="address">Address to check.</param>
  139. /// <returns>True or False.</returns>
  140. bool IsPrivateAddressRange(IPObject address);
  141. /// <summary>
  142. /// Returns true if the address is part of the user defined LAN.
  143. /// The config option TrustIP6Interfaces overrides this functions behaviour.
  144. /// </summary>
  145. /// <param name="address">IP to check.</param>
  146. /// <returns>True if endpoint is within the LAN range.</returns>
  147. bool IsInLocalNetwork(string address);
  148. /// <summary>
  149. /// Returns true if the address is part of the user defined LAN.
  150. /// The config option TrustIP6Interfaces overrides this functions behaviour.
  151. /// </summary>
  152. /// <param name="address">IP to check.</param>
  153. /// <returns>True if endpoint is within the LAN range.</returns>
  154. bool IsInLocalNetwork(IPObject address);
  155. /// <summary>
  156. /// Returns true if the address is part of the user defined LAN.
  157. /// The config option TrustIP6Interfaces overrides this functions behaviour.
  158. /// </summary>
  159. /// <param name="address">IP to check.</param>
  160. /// <returns>True if endpoint is within the LAN range.</returns>
  161. bool IsInLocalNetwork(IPAddress address);
  162. /// <summary>
  163. /// Attempts to convert the token to an IP address, permitting for interface descriptions and indexes.
  164. /// eg. "eth1", or "TP-LINK Wireless USB Adapter".
  165. /// </summary>
  166. /// <param name="token">Token to parse.</param>
  167. /// <param name="result">Resultant object's ip addresses, if successful.</param>
  168. /// <returns>Success of the operation.</returns>
  169. bool TryParseInterface(string token, out Collection<IPObject>? result);
  170. /// <summary>
  171. /// Parses an array of strings into a Collection{IPObject}.
  172. /// </summary>
  173. /// <param name="values">Values to parse.</param>
  174. /// <param name="bracketed">When true, only include values in []. When false, ignore bracketed values.</param>
  175. /// <returns>IPCollection object containing the value strings.</returns>
  176. Collection<IPObject> CreateIPCollection(string[] values, bool bracketed = false);
  177. /// <summary>
  178. /// Returns all the internal Bind interface addresses.
  179. /// </summary>
  180. /// <returns>An internal list of interfaces addresses.</returns>
  181. Collection<IPObject> GetInternalBindAddresses();
  182. /// <summary>
  183. /// Checks to see if an IP address is still a valid interface address.
  184. /// </summary>
  185. /// <param name="address">IP address to check.</param>
  186. /// <returns>True if it is.</returns>
  187. bool IsValidInterfaceAddress(IPAddress address);
  188. /// <summary>
  189. /// Returns true if the IP address is in the excluded list.
  190. /// </summary>
  191. /// <param name="ip">IP to check.</param>
  192. /// <returns>True if excluded.</returns>
  193. bool IsExcluded(IPAddress ip);
  194. /// <summary>
  195. /// Returns true if the IP address is in the excluded list.
  196. /// </summary>
  197. /// <param name="ip">IP to check.</param>
  198. /// <returns>True if excluded.</returns>
  199. bool IsExcluded(EndPoint ip);
  200. /// <summary>
  201. /// Gets the filtered LAN ip addresses.
  202. /// </summary>
  203. /// <param name="filter">Optional filter for the list.</param>
  204. /// <returns>Returns a filtered list of LAN addresses.</returns>
  205. Collection<IPObject> GetFilteredLANSubnets(Collection<IPObject>? filter = null);
  206. }
  207. }