INetworkManager.cs 11 KB

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