INetworkManager.cs 6.4 KB

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