INetworkManager.cs 11 KB

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