INetworkManager.cs 11 KB

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