INetworkManager.cs 6.4 KB

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