2020-09-12 15:41:37 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2023-07-02 10:34:50 +00:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2020-09-12 15:41:37 +00:00
|
|
|
using System.Net;
|
|
|
|
using System.Net.NetworkInformation;
|
2023-02-16 17:15:12 +00:00
|
|
|
using MediaBrowser.Model.Net;
|
2020-09-14 14:46:38 +00:00
|
|
|
using Microsoft.AspNetCore.Http;
|
2020-09-12 15:41:37 +00:00
|
|
|
|
2020-09-14 14:46:38 +00:00
|
|
|
namespace MediaBrowser.Common.Net
|
2020-09-12 15:41:37 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Interface for the NetworkManager class.
|
|
|
|
/// </summary>
|
|
|
|
public interface INetworkManager
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Event triggered on network changes.
|
|
|
|
/// </summary>
|
|
|
|
event EventHandler NetworkChanged;
|
|
|
|
|
|
|
|
/// <summary>
|
2022-07-21 20:09:54 +00:00
|
|
|
/// Gets a value indicating whether IPv4 is enabled.
|
2020-09-12 15:41:37 +00:00
|
|
|
/// </summary>
|
2023-02-15 21:34:44 +00:00
|
|
|
bool IsIPv4Enabled { get; }
|
2020-09-12 15:41:37 +00:00
|
|
|
|
|
|
|
/// <summary>
|
2022-07-21 20:09:54 +00:00
|
|
|
/// Gets a value indicating whether IPv6 is enabled.
|
2020-09-12 15:41:37 +00:00
|
|
|
/// </summary>
|
2023-02-15 21:34:44 +00:00
|
|
|
bool IsIPv6Enabled { get; }
|
2020-09-30 16:51:17 +00:00
|
|
|
|
2020-09-12 15:41:37 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Calculates the list of interfaces to use for Kestrel.
|
|
|
|
/// </summary>
|
2023-02-20 20:51:15 +00:00
|
|
|
/// <returns>A IReadOnlyList{IPData} object containing all the interfaces to bind.
|
2020-09-12 15:41:37 +00:00
|
|
|
/// If all the interfaces are specified, and none are excluded, it returns zero items
|
|
|
|
/// to represent any address.</returns>
|
2020-09-30 16:51:17 +00:00
|
|
|
/// <param name="individualInterfaces">When false, return <see cref="IPAddress.Any"/> or <see cref="IPAddress.IPv6Any"/> for all interfaces.</param>
|
2022-07-20 12:29:30 +00:00
|
|
|
IReadOnlyList<IPData> GetAllBindInterfaces(bool individualInterfaces = false);
|
2020-09-12 15:41:37 +00:00
|
|
|
|
|
|
|
/// <summary>
|
2022-07-20 12:29:30 +00:00
|
|
|
/// Returns a list containing the loopback interfaces.
|
2020-09-12 15:41:37 +00:00
|
|
|
/// </summary>
|
2023-02-20 20:51:15 +00:00
|
|
|
/// <returns>IReadOnlyList{IPData}.</returns>
|
2022-07-20 12:29:30 +00:00
|
|
|
IReadOnlyList<IPData> GetLoopbacks();
|
2020-09-12 15:41:37 +00:00
|
|
|
|
|
|
|
/// <summary>
|
2022-07-21 20:09:54 +00:00
|
|
|
/// Retrieves the bind address to use in system URLs. (Server Discovery, PlayTo, LiveTV, SystemInfo)
|
2020-09-12 15:41:37 +00:00
|
|
|
/// If no bind addresses are specified, an internal interface address is selected.
|
|
|
|
/// The priority of selection is as follows:-
|
|
|
|
///
|
|
|
|
/// The value contained in the startup parameter --published-server-url.
|
|
|
|
///
|
|
|
|
/// If the user specified custom subnet overrides, the correct subnet for the source address.
|
|
|
|
///
|
|
|
|
/// If the user specified bind interfaces to use:-
|
|
|
|
/// The bind interface that contains the source subnet.
|
|
|
|
/// The first bind interface specified that suits best first the source's endpoint. eg. external or internal.
|
|
|
|
///
|
|
|
|
/// If the source is from a public subnet address range and the user hasn't specified any bind addresses:-
|
|
|
|
/// The first public interface that isn't a loopback and contains the source subnet.
|
2022-07-21 20:09:54 +00:00
|
|
|
/// The first public interface that isn't a loopback.
|
|
|
|
/// The first internal interface that isn't a loopback.
|
2020-09-12 15:41:37 +00:00
|
|
|
///
|
|
|
|
/// If the source is from a private subnet address range and the user hasn't specified any bind addresses:-
|
|
|
|
/// The first private interface that contains the source subnet.
|
2022-07-21 20:09:54 +00:00
|
|
|
/// The first private interface that isn't a loopback.
|
2020-09-12 15:41:37 +00:00
|
|
|
///
|
|
|
|
/// If no interfaces meet any of these criteria, then a loopback address is returned.
|
|
|
|
///
|
2022-07-21 20:09:54 +00:00
|
|
|
/// Interfaces that have been specifically excluded from binding are not used in any of the calculations.
|
2020-09-12 15:41:37 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="source">Source of the request.</param>
|
|
|
|
/// <param name="port">Optional port returned, if it's part of an override.</param>
|
2022-07-21 20:09:54 +00:00
|
|
|
/// <returns>IP address to use, or loopback address if all else fails.</returns>
|
2023-02-16 17:15:12 +00:00
|
|
|
string GetBindAddress(HttpRequest source, out int? port);
|
2020-09-14 14:46:38 +00:00
|
|
|
|
|
|
|
/// <summary>
|
2022-07-21 20:09:54 +00:00
|
|
|
/// Retrieves the bind address to use in system URLs. (Server Discovery, PlayTo, LiveTV, SystemInfo)
|
2020-09-14 14:46:38 +00:00
|
|
|
/// If no bind addresses are specified, an internal interface address is selected.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="source">IP address of the request.</param>
|
|
|
|
/// <param name="port">Optional port returned, if it's part of an override.</param>
|
2023-02-16 17:15:12 +00:00
|
|
|
/// <param name="skipOverrides">Optional boolean denoting if published server overrides should be ignored. Defaults to false.</param>
|
2022-07-21 20:09:54 +00:00
|
|
|
/// <returns>IP address to use, or loopback address if all else fails.</returns>
|
2023-11-30 16:01:39 +00:00
|
|
|
string GetBindAddress(IPAddress? source, out int? port, bool skipOverrides = false);
|
2020-09-14 14:46:38 +00:00
|
|
|
|
|
|
|
/// <summary>
|
2022-07-21 20:09:54 +00:00
|
|
|
/// Retrieves the bind address to use in system URLs. (Server Discovery, PlayTo, LiveTV, SystemInfo)
|
2020-09-14 14:46:38 +00:00
|
|
|
/// If no bind addresses are specified, an internal interface address is selected.
|
2023-02-16 17:15:12 +00:00
|
|
|
/// (See <see cref="GetBindAddress(IPAddress, out int?, bool)"/>.
|
2020-09-14 14:46:38 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="source">Source of the request.</param>
|
|
|
|
/// <param name="port">Optional port returned, if it's part of an override.</param>
|
2022-07-21 20:09:54 +00:00
|
|
|
/// <returns>IP address to use, or loopback address if all else fails.</returns>
|
2023-02-16 17:15:12 +00:00
|
|
|
string GetBindAddress(string source, out int? port);
|
2020-09-12 15:41:37 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Get a list of all the MAC addresses associated with active interfaces.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>List of MAC addresses.</returns>
|
2022-07-20 12:29:30 +00:00
|
|
|
IReadOnlyList<PhysicalAddress> GetMacAddresses();
|
2020-09-12 15:41:37 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns true if the address is part of the user defined LAN.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="address">IP to check.</param>
|
|
|
|
/// <returns>True if endpoint is within the LAN range.</returns>
|
|
|
|
bool IsInLocalNetwork(string address);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns true if the address is part of the user defined LAN.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="address">IP to check.</param>
|
|
|
|
/// <returns>True if endpoint is within the LAN range.</returns>
|
|
|
|
bool IsInLocalNetwork(IPAddress address);
|
|
|
|
|
|
|
|
/// <summary>
|
2022-07-19 19:28:04 +00:00
|
|
|
/// Attempts to convert the interface name to an IP address.
|
|
|
|
/// eg. "eth1", or "enp3s5".
|
2020-09-12 15:41:37 +00:00
|
|
|
/// </summary>
|
2022-07-19 19:28:04 +00:00
|
|
|
/// <param name="intf">Interface name.</param>
|
2022-07-21 20:09:54 +00:00
|
|
|
/// <param name="result">Resulting object's IP addresses, if successful.</param>
|
2020-09-12 15:41:37 +00:00
|
|
|
/// <returns>Success of the operation.</returns>
|
2023-07-02 10:34:50 +00:00
|
|
|
bool TryParseInterface(string intf, [NotNullWhen(true)] out IReadOnlyList<IPData>? result);
|
2020-09-12 15:41:37 +00:00
|
|
|
|
|
|
|
/// <summary>
|
2022-07-21 20:09:54 +00:00
|
|
|
/// Returns all internal (LAN) bind interface addresses.
|
2020-09-12 15:41:37 +00:00
|
|
|
/// </summary>
|
2022-07-21 20:09:54 +00:00
|
|
|
/// <returns>An list of internal (LAN) interfaces addresses.</returns>
|
2022-07-20 12:29:30 +00:00
|
|
|
IReadOnlyList<IPData> GetInternalBindAddresses();
|
2021-03-11 21:36:58 +00:00
|
|
|
|
|
|
|
/// <summary>
|
2023-02-17 18:27:36 +00:00
|
|
|
/// Checks if <paramref name="remoteIP"/> has access to the server.
|
2021-03-11 21:36:58 +00:00
|
|
|
/// </summary>
|
2023-02-17 18:27:36 +00:00
|
|
|
/// <param name="remoteIP">IP address of the client.</param>
|
2022-07-21 20:09:54 +00:00
|
|
|
/// <returns><b>True</b> if it has access, otherwise <b>false</b>.</returns>
|
2023-02-17 18:27:36 +00:00
|
|
|
bool HasRemoteAccess(IPAddress remoteIP);
|
2020-09-12 15:41:37 +00:00
|
|
|
}
|
|
|
|
}
|