INetworkManager.cs 10 KB

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