INetworkManager.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. using System.Net.NetworkInformation;
  5. using MediaBrowser.Model.Net;
  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 IPv4 is enabled.
  20. /// </summary>
  21. bool IsIPv4Enabled { get; }
  22. /// <summary>
  23. /// Gets a value indicating whether IPv6 is enabled.
  24. /// </summary>
  25. bool IsIPv6Enabled { get; }
  26. /// <summary>
  27. /// Calculates the list of interfaces to use for Kestrel.
  28. /// </summary>
  29. /// <returns>A IReadOnlyList{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. IReadOnlyList<IPData> GetAllBindInterfaces(bool individualInterfaces = false);
  34. /// <summary>
  35. /// Returns a list containing the loopback interfaces.
  36. /// </summary>
  37. /// <returns>IReadOnlyList{IPData}.</returns>
  38. IReadOnlyList<IPData> GetLoopbacks();
  39. /// <summary>
  40. /// Retrieves the bind address to use in system URLs. (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.
  55. /// The first internal interface that isn't a loopback.
  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.
  60. ///
  61. /// If no interfaces meet any of these criteria, then a loopback address is returned.
  62. ///
  63. /// Interfaces 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 GetBindAddress(HttpRequest source, out int? port);
  69. /// <summary>
  70. /// Retrieves the bind address to use in system URLs. (Server Discovery, PlayTo, LiveTV, SystemInfo)
  71. /// If no bind addresses are specified, an internal interface address is selected.
  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. /// <param name="skipOverrides">Optional boolean denoting if published server overrides should be ignored. Defaults to false.</param>
  76. /// <returns>IP address to use, or loopback address if all else fails.</returns>
  77. string GetBindAddress(IPAddress source, out int? port, bool skipOverrides = false);
  78. /// <summary>
  79. /// Retrieves the bind address to use in system URLs. (Server Discovery, PlayTo, LiveTV, SystemInfo)
  80. /// If no bind addresses are specified, an internal interface address is selected.
  81. /// (See <see cref="GetBindAddress(IPAddress, out int?, bool)"/>.
  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 GetBindAddress(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. IReadOnlyList<PhysicalAddress> GetMacAddresses();
  92. /// <summary>
  93. /// Returns true if the address is part of the user defined LAN.
  94. /// </summary>
  95. /// <param name="address">IP to check.</param>
  96. /// <returns>True if endpoint is within the LAN range.</returns>
  97. bool IsInLocalNetwork(string address);
  98. /// <summary>
  99. /// Returns true if the address is part of the user defined LAN.
  100. /// </summary>
  101. /// <param name="address">IP to check.</param>
  102. /// <returns>True if endpoint is within the LAN range.</returns>
  103. bool IsInLocalNetwork(IPAddress address);
  104. /// <summary>
  105. /// Attempts to convert the interface name to an IP address.
  106. /// eg. "eth1", or "enp3s5".
  107. /// </summary>
  108. /// <param name="intf">Interface name.</param>
  109. /// <param name="result">Resulting object's IP addresses, if successful.</param>
  110. /// <returns>Success of the operation.</returns>
  111. bool TryParseInterface(string intf, out IReadOnlyList<IPData> result);
  112. /// <summary>
  113. /// Returns all internal (LAN) bind interface addresses.
  114. /// </summary>
  115. /// <returns>An list of internal (LAN) interfaces addresses.</returns>
  116. IReadOnlyList<IPData> GetInternalBindAddresses();
  117. /// <summary>
  118. /// Checks if <paramref name="remoteIP"/> has access to the server.
  119. /// </summary>
  120. /// <param name="remoteIP">IP address of the client.</param>
  121. /// <returns><b>True</b> if it has access, otherwise <b>false</b>.</returns>
  122. bool HasRemoteAccess(IPAddress remoteIP);
  123. }
  124. }