INetworkManager.cs 11 KB

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