INetworkManager.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 a value indicating whether IPv6 is enabled.
  20. /// </summary>
  21. bool IsIpv6Enabled { get; }
  22. /// <summary>
  23. /// Gets a value indicating whether IPv4 is enabled.
  24. /// </summary>
  25. bool IsIpv4Enabled { get; }
  26. /// <summary>
  27. /// Calculates the list of interfaces to use for Kestrel.
  28. /// </summary>
  29. /// <returns>A List{IPData} object containing all the interfaces to bind.
  30. /// If all the interfaces are specified, and none are excluded, it returns zero items
  31. /// to represent any address.</returns>
  32. /// <param name="individualInterfaces">When false, return <see cref="IPAddress.Any"/> or <see cref="IPAddress.IPv6Any"/> for all interfaces.</param>
  33. List<IPData> GetAllBindInterfaces(bool individualInterfaces = false);
  34. /// <summary>
  35. /// Returns a collection containing the loopback interfaces.
  36. /// </summary>
  37. /// <returns>List{IPData}.</returns>
  38. List<IPData> GetLoopbacks();
  39. /// <summary>
  40. /// Retrieves the bind address to use in system url's. (Server Discovery, PlayTo, LiveTV, SystemInfo)
  41. /// If no bind addresses are specified, an internal interface address is selected.
  42. /// The priority of selection is as follows:-
  43. ///
  44. /// The value contained in the startup parameter --published-server-url.
  45. ///
  46. /// If the user specified custom subnet overrides, the correct subnet for the source address.
  47. ///
  48. /// If the user specified bind interfaces to use:-
  49. /// The bind interface that contains the source subnet.
  50. /// The first bind interface specified that suits best first the source's endpoint. eg. external or internal.
  51. ///
  52. /// If the source is from a public subnet address range and the user hasn't specified any bind addresses:-
  53. /// The first public interface that isn't a loopback and contains the source subnet.
  54. /// The first public interface that isn't a loopback. Priority is given to interfaces with gateways.
  55. /// An internal interface if there are no public ip addresses.
  56. ///
  57. /// If the source is from a private subnet address range and the user hasn't specified any bind addresses:-
  58. /// The first private interface that contains the source subnet.
  59. /// The first private interface that isn't a loopback. Priority is given to interfaces with gateways.
  60. ///
  61. /// If no interfaces meet any of these criteria, then a loopback address is returned.
  62. ///
  63. /// Interface that have been specifically excluded from binding are not used in any of the calculations.
  64. /// </summary>
  65. /// <param name="source">Source of the request.</param>
  66. /// <param name="port">Optional port returned, if it's part of an override.</param>
  67. /// <returns>IP Address to use, or loopback address if all else fails.</returns>
  68. string GetBindInterface(HttpRequest source, out int? port);
  69. /// <summary>
  70. /// Retrieves the bind address to use in system url's. (Server Discovery, PlayTo, LiveTV, SystemInfo)
  71. /// If no bind addresses are specified, an internal interface address is selected.
  72. /// (See <see cref="GetBindInterface(IPAddress, out int?)"/>.
  73. /// </summary>
  74. /// <param name="source">IP address of the request.</param>
  75. /// <param name="port">Optional port returned, if it's part of an override.</param>
  76. /// <returns>IP Address to use, or loopback address if all else fails.</returns>
  77. string GetBindInterface(IPAddress source, out int? port);
  78. /// <summary>
  79. /// Retrieves the bind address to use in system url's. (Server Discovery, PlayTo, LiveTV, SystemInfo)
  80. /// If no bind addresses are specified, an internal interface address is selected.
  81. /// (See <see cref="GetBindInterface(IPAddress, out int?)"/>.
  82. /// </summary>
  83. /// <param name="source">Source of the request.</param>
  84. /// <param name="port">Optional port returned, if it's part of an override.</param>
  85. /// <returns>IP Address to use, or loopback address if all else fails.</returns>
  86. string GetBindInterface(string source, out int? port);
  87. /// <summary>
  88. /// Get a list of all the MAC addresses associated with active interfaces.
  89. /// </summary>
  90. /// <returns>List of MAC addresses.</returns>
  91. IReadOnlyCollection<PhysicalAddress> GetMacAddresses();
  92. /// <summary>
  93. /// Returns true if the address is part of the user defined LAN.
  94. /// The configuration option TrustIP6Interfaces overrides this functions behaviour.
  95. /// </summary>
  96. /// <param name="address">IP to check.</param>
  97. /// <returns>True if endpoint is within the LAN range.</returns>
  98. bool IsInLocalNetwork(string address);
  99. /// <summary>
  100. /// Returns true if the address is part of the user defined LAN.
  101. /// The configuration option TrustIP6Interfaces overrides this functions behaviour.
  102. /// </summary>
  103. /// <param name="address">IP to check.</param>
  104. /// <returns>True if endpoint is within the LAN range.</returns>
  105. bool IsInLocalNetwork(IPAddress address);
  106. /// <summary>
  107. /// Attempts to convert the interface name to an IP address.
  108. /// eg. "eth1", or "enp3s5".
  109. /// </summary>
  110. /// <param name="intf">Interface name.</param>
  111. /// <param name="result">Resultant object's ip addresses, if successful.</param>
  112. /// <returns>Success of the operation.</returns>
  113. bool TryParseInterface(string intf, out Collection<IPData>? result);
  114. /// <summary>
  115. /// Returns all the internal Bind interface addresses.
  116. /// </summary>
  117. /// <returns>An internal list of interfaces addresses.</returns>
  118. List<IPData> GetInternalBindAddresses();
  119. /// <summary>
  120. /// Checks to see if <paramref name="remoteIp"/> has access.
  121. /// </summary>
  122. /// <param name="remoteIp">IP Address of client.</param>
  123. /// <returns><b>True</b> if has access, otherwise <b>false</b>.</returns>
  124. bool HasRemoteAccess(IPAddress remoteIp);
  125. }
  126. }