|
@@ -15,6 +15,7 @@ using System.Security.Cryptography.X509Certificates;
|
|
using System.Text;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Threading.Tasks;
|
|
|
|
+using System.Xml.Serialization;
|
|
using Emby.Dlna;
|
|
using Emby.Dlna;
|
|
using Emby.Dlna.Main;
|
|
using Emby.Dlna.Main;
|
|
using Emby.Dlna.Ssdp;
|
|
using Emby.Dlna.Ssdp;
|
|
@@ -46,6 +47,8 @@ using Emby.Server.Implementations.SyncPlay;
|
|
using Emby.Server.Implementations.TV;
|
|
using Emby.Server.Implementations.TV;
|
|
using Emby.Server.Implementations.Updates;
|
|
using Emby.Server.Implementations.Updates;
|
|
using Jellyfin.Api.Helpers;
|
|
using Jellyfin.Api.Helpers;
|
|
|
|
+using Jellyfin.Networking.Configuration;
|
|
|
|
+using Jellyfin.Networking.Manager;
|
|
using MediaBrowser.Common;
|
|
using MediaBrowser.Common;
|
|
using MediaBrowser.Common.Configuration;
|
|
using MediaBrowser.Common.Configuration;
|
|
using MediaBrowser.Common.Events;
|
|
using MediaBrowser.Common.Events;
|
|
@@ -97,6 +100,8 @@ using MediaBrowser.Providers.Manager;
|
|
using MediaBrowser.Providers.Plugins.Tmdb;
|
|
using MediaBrowser.Providers.Plugins.Tmdb;
|
|
using MediaBrowser.Providers.Subtitles;
|
|
using MediaBrowser.Providers.Subtitles;
|
|
using MediaBrowser.XbmcMetadata.Providers;
|
|
using MediaBrowser.XbmcMetadata.Providers;
|
|
|
|
+using Microsoft.AspNetCore.DataProtection.Repositories;
|
|
|
|
+using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Logging;
|
|
@@ -117,7 +122,6 @@ namespace Emby.Server.Implementations
|
|
private static readonly string[] _relevantEnvVarPrefixes = { "JELLYFIN_", "DOTNET_", "ASPNETCORE_" };
|
|
private static readonly string[] _relevantEnvVarPrefixes = { "JELLYFIN_", "DOTNET_", "ASPNETCORE_" };
|
|
|
|
|
|
private readonly IFileSystem _fileSystemManager;
|
|
private readonly IFileSystem _fileSystemManager;
|
|
- private readonly INetworkManager _networkManager;
|
|
|
|
private readonly IXmlSerializer _xmlSerializer;
|
|
private readonly IXmlSerializer _xmlSerializer;
|
|
private readonly IJsonSerializer _jsonSerializer;
|
|
private readonly IJsonSerializer _jsonSerializer;
|
|
private readonly IStartupOptions _startupOptions;
|
|
private readonly IStartupOptions _startupOptions;
|
|
@@ -158,6 +162,11 @@ namespace Emby.Server.Implementations
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// Gets the <see cref="INetworkManager"/> singleton instance.
|
|
|
|
+ /// </summary>
|
|
|
|
+ public INetworkManager NetManager { get; internal set; }
|
|
|
|
+
|
|
/// <summary>
|
|
/// <summary>
|
|
/// Occurs when [has pending restart changed].
|
|
/// Occurs when [has pending restart changed].
|
|
/// </summary>
|
|
/// </summary>
|
|
@@ -210,7 +219,7 @@ namespace Emby.Server.Implementations
|
|
private readonly List<IDisposable> _disposableParts = new List<IDisposable>();
|
|
private readonly List<IDisposable> _disposableParts = new List<IDisposable>();
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
- /// Gets the configuration manager.
|
|
|
|
|
|
+ /// Gets or sets the configuration manager.
|
|
/// </summary>
|
|
/// </summary>
|
|
/// <value>The configuration manager.</value>
|
|
/// <value>The configuration manager.</value>
|
|
protected IConfigurationManager ConfigurationManager { get; set; }
|
|
protected IConfigurationManager ConfigurationManager { get; set; }
|
|
@@ -243,14 +252,12 @@ namespace Emby.Server.Implementations
|
|
/// <param name="loggerFactory">Instance of the <see cref="ILoggerFactory"/> interface.</param>
|
|
/// <param name="loggerFactory">Instance of the <see cref="ILoggerFactory"/> interface.</param>
|
|
/// <param name="options">Instance of the <see cref="IStartupOptions"/> interface.</param>
|
|
/// <param name="options">Instance of the <see cref="IStartupOptions"/> interface.</param>
|
|
/// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
|
|
/// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
|
|
- /// <param name="networkManager">Instance of the <see cref="INetworkManager"/> interface.</param>
|
|
|
|
/// <param name="serviceCollection">Instance of the <see cref="IServiceCollection"/> interface.</param>
|
|
/// <param name="serviceCollection">Instance of the <see cref="IServiceCollection"/> interface.</param>
|
|
public ApplicationHost(
|
|
public ApplicationHost(
|
|
IServerApplicationPaths applicationPaths,
|
|
IServerApplicationPaths applicationPaths,
|
|
ILoggerFactory loggerFactory,
|
|
ILoggerFactory loggerFactory,
|
|
IStartupOptions options,
|
|
IStartupOptions options,
|
|
IFileSystem fileSystem,
|
|
IFileSystem fileSystem,
|
|
- INetworkManager networkManager,
|
|
|
|
IServiceCollection serviceCollection)
|
|
IServiceCollection serviceCollection)
|
|
{
|
|
{
|
|
_xmlSerializer = new MyXmlSerializer();
|
|
_xmlSerializer = new MyXmlSerializer();
|
|
@@ -258,14 +265,17 @@ namespace Emby.Server.Implementations
|
|
|
|
|
|
ServiceCollection = serviceCollection;
|
|
ServiceCollection = serviceCollection;
|
|
|
|
|
|
- _networkManager = networkManager;
|
|
|
|
- networkManager.LocalSubnetsFn = GetConfiguredLocalSubnets;
|
|
|
|
-
|
|
|
|
ApplicationPaths = applicationPaths;
|
|
ApplicationPaths = applicationPaths;
|
|
LoggerFactory = loggerFactory;
|
|
LoggerFactory = loggerFactory;
|
|
_fileSystemManager = fileSystem;
|
|
_fileSystemManager = fileSystem;
|
|
|
|
|
|
ConfigurationManager = new ServerConfigurationManager(ApplicationPaths, LoggerFactory, _xmlSerializer, _fileSystemManager);
|
|
ConfigurationManager = new ServerConfigurationManager(ApplicationPaths, LoggerFactory, _xmlSerializer, _fileSystemManager);
|
|
|
|
+ // Have to migrate settings here as migration subsystem not yet initialised.
|
|
|
|
+ MigrateNetworkConfiguration();
|
|
|
|
+
|
|
|
|
+ // Have to pre-register the NetworkConfigurationFactory, as the configuration sub-system is not yet initialised.
|
|
|
|
+ ConfigurationManager.RegisterConfiguration<NetworkConfigurationFactory>();
|
|
|
|
+ NetManager = new NetworkManager((IServerConfigurationManager)ConfigurationManager, LoggerFactory.CreateLogger<NetworkManager>());
|
|
|
|
|
|
Logger = LoggerFactory.CreateLogger<ApplicationHost>();
|
|
Logger = LoggerFactory.CreateLogger<ApplicationHost>();
|
|
|
|
|
|
@@ -279,8 +289,6 @@ namespace Emby.Server.Implementations
|
|
|
|
|
|
fileSystem.AddShortcutHandler(new MbLinkShortcutHandler(fileSystem));
|
|
fileSystem.AddShortcutHandler(new MbLinkShortcutHandler(fileSystem));
|
|
|
|
|
|
- _networkManager.NetworkChanged += OnNetworkChanged;
|
|
|
|
-
|
|
|
|
CertificateInfo = new CertificateInfo
|
|
CertificateInfo = new CertificateInfo
|
|
{
|
|
{
|
|
Path = ServerConfigurationManager.Configuration.CertificatePath,
|
|
Path = ServerConfigurationManager.Configuration.CertificatePath,
|
|
@@ -293,6 +301,22 @@ namespace Emby.Server.Implementations
|
|
ApplicationUserAgent = Name.Replace(' ', '-') + "/" + ApplicationVersionString;
|
|
ApplicationUserAgent = Name.Replace(' ', '-') + "/" + ApplicationVersionString;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// Temporary function to migration network settings out of system.xml and into network.xml.
|
|
|
|
+ /// TODO: remove at the point when a fixed migration path has been decided upon.
|
|
|
|
+ /// </summary>
|
|
|
|
+ private void MigrateNetworkConfiguration()
|
|
|
|
+ {
|
|
|
|
+ string path = Path.Combine(ConfigurationManager.CommonApplicationPaths.ConfigurationDirectoryPath, "network.xml");
|
|
|
|
+ if (!File.Exists(path))
|
|
|
|
+ {
|
|
|
|
+ var networkSettings = new NetworkConfiguration();
|
|
|
|
+ ClassMigrationHelper.CopyProperties(ServerConfigurationManager.Configuration, networkSettings);
|
|
|
|
+ _xmlSerializer.SerializeToFile(networkSettings, path);
|
|
|
|
+ Logger?.LogDebug("Successfully migrated network settings.");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
public string ExpandVirtualPath(string path)
|
|
public string ExpandVirtualPath(string path)
|
|
{
|
|
{
|
|
var appPaths = ApplicationPaths;
|
|
var appPaths = ApplicationPaths;
|
|
@@ -309,16 +333,6 @@ namespace Emby.Server.Implementations
|
|
.Replace(appPaths.InternalMetadataPath, appPaths.VirtualInternalMetadataPath, StringComparison.OrdinalIgnoreCase);
|
|
.Replace(appPaths.InternalMetadataPath, appPaths.VirtualInternalMetadataPath, StringComparison.OrdinalIgnoreCase);
|
|
}
|
|
}
|
|
|
|
|
|
- private string[] GetConfiguredLocalSubnets()
|
|
|
|
- {
|
|
|
|
- return ServerConfigurationManager.Configuration.LocalNetworkSubnets;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private void OnNetworkChanged(object sender, EventArgs e)
|
|
|
|
- {
|
|
|
|
- _validAddressResults.Clear();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
/// <inheritdoc />
|
|
/// <inheritdoc />
|
|
public Version ApplicationVersion { get; }
|
|
public Version ApplicationVersion { get; }
|
|
|
|
|
|
@@ -485,14 +499,15 @@ namespace Emby.Server.Implementations
|
|
/// <inheritdoc/>
|
|
/// <inheritdoc/>
|
|
public void Init()
|
|
public void Init()
|
|
{
|
|
{
|
|
- HttpPort = ServerConfigurationManager.Configuration.HttpServerPortNumber;
|
|
|
|
- HttpsPort = ServerConfigurationManager.Configuration.HttpsPortNumber;
|
|
|
|
|
|
+ var networkConfiguration = ServerConfigurationManager.GetNetworkConfiguration();
|
|
|
|
+ HttpPort = networkConfiguration.HttpServerPortNumber;
|
|
|
|
+ HttpsPort = networkConfiguration.HttpsPortNumber;
|
|
|
|
|
|
// Safeguard against invalid configuration
|
|
// Safeguard against invalid configuration
|
|
if (HttpPort == HttpsPort)
|
|
if (HttpPort == HttpsPort)
|
|
{
|
|
{
|
|
- HttpPort = ServerConfiguration.DefaultHttpPort;
|
|
|
|
- HttpsPort = ServerConfiguration.DefaultHttpsPort;
|
|
|
|
|
|
+ HttpPort = NetworkConfiguration.DefaultHttpPort;
|
|
|
|
+ HttpsPort = NetworkConfiguration.DefaultHttpsPort;
|
|
}
|
|
}
|
|
|
|
|
|
DiscoverTypes();
|
|
DiscoverTypes();
|
|
@@ -521,7 +536,7 @@ namespace Emby.Server.Implementations
|
|
ServiceCollection.AddSingleton(_fileSystemManager);
|
|
ServiceCollection.AddSingleton(_fileSystemManager);
|
|
ServiceCollection.AddSingleton<TmdbClientManager>();
|
|
ServiceCollection.AddSingleton<TmdbClientManager>();
|
|
|
|
|
|
- ServiceCollection.AddSingleton(_networkManager);
|
|
|
|
|
|
+ ServiceCollection.AddSingleton(NetManager);
|
|
|
|
|
|
ServiceCollection.AddSingleton<IIsoManager, IsoManager>();
|
|
ServiceCollection.AddSingleton<IIsoManager, IsoManager>();
|
|
|
|
|
|
@@ -902,9 +917,10 @@ namespace Emby.Server.Implementations
|
|
// Don't do anything if these haven't been set yet
|
|
// Don't do anything if these haven't been set yet
|
|
if (HttpPort != 0 && HttpsPort != 0)
|
|
if (HttpPort != 0 && HttpsPort != 0)
|
|
{
|
|
{
|
|
|
|
+ var networkConfiguration = ServerConfigurationManager.GetNetworkConfiguration();
|
|
// Need to restart if ports have changed
|
|
// Need to restart if ports have changed
|
|
- if (ServerConfigurationManager.Configuration.HttpServerPortNumber != HttpPort ||
|
|
|
|
- ServerConfigurationManager.Configuration.HttpsPortNumber != HttpsPort)
|
|
|
|
|
|
+ if (networkConfiguration.HttpServerPortNumber != HttpPort ||
|
|
|
|
+ networkConfiguration.HttpsPortNumber != HttpsPort)
|
|
{
|
|
{
|
|
if (ServerConfigurationManager.Configuration.IsPortAuthorized)
|
|
if (ServerConfigurationManager.Configuration.IsPortAuthorized)
|
|
{
|
|
{
|
|
@@ -1147,6 +1163,9 @@ namespace Emby.Server.Implementations
|
|
// Xbmc
|
|
// Xbmc
|
|
yield return typeof(ArtistNfoProvider).Assembly;
|
|
yield return typeof(ArtistNfoProvider).Assembly;
|
|
|
|
|
|
|
|
+ // Network
|
|
|
|
+ yield return typeof(NetworkManager).Assembly;
|
|
|
|
+
|
|
foreach (var i in GetAssembliesWithPartsInternal())
|
|
foreach (var i in GetAssembliesWithPartsInternal())
|
|
{
|
|
{
|
|
yield return i;
|
|
yield return i;
|
|
@@ -1158,13 +1177,10 @@ namespace Emby.Server.Implementations
|
|
/// <summary>
|
|
/// <summary>
|
|
/// Gets the system status.
|
|
/// Gets the system status.
|
|
/// </summary>
|
|
/// </summary>
|
|
- /// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
|
+ /// <param name="source">Where this request originated.</param>
|
|
/// <returns>SystemInfo.</returns>
|
|
/// <returns>SystemInfo.</returns>
|
|
- public async Task<SystemInfo> GetSystemInfo(CancellationToken cancellationToken)
|
|
|
|
|
|
+ public SystemInfo GetSystemInfo(IPAddress source)
|
|
{
|
|
{
|
|
- var localAddress = await GetLocalApiUrl(cancellationToken).ConfigureAwait(false);
|
|
|
|
- var transcodingTempPath = ConfigurationManager.GetTranscodePath();
|
|
|
|
-
|
|
|
|
return new SystemInfo
|
|
return new SystemInfo
|
|
{
|
|
{
|
|
HasPendingRestart = HasPendingRestart,
|
|
HasPendingRestart = HasPendingRestart,
|
|
@@ -1184,9 +1200,9 @@ namespace Emby.Server.Implementations
|
|
CanSelfRestart = CanSelfRestart,
|
|
CanSelfRestart = CanSelfRestart,
|
|
CanLaunchWebBrowser = CanLaunchWebBrowser,
|
|
CanLaunchWebBrowser = CanLaunchWebBrowser,
|
|
HasUpdateAvailable = HasUpdateAvailable,
|
|
HasUpdateAvailable = HasUpdateAvailable,
|
|
- TranscodingTempPath = transcodingTempPath,
|
|
|
|
|
|
+ TranscodingTempPath = ConfigurationManager.GetTranscodePath(),
|
|
ServerName = FriendlyName,
|
|
ServerName = FriendlyName,
|
|
- LocalAddress = localAddress,
|
|
|
|
|
|
+ LocalAddress = GetSmartApiUrl(source),
|
|
SupportsLibraryMonitor = true,
|
|
SupportsLibraryMonitor = true,
|
|
EncoderLocation = _mediaEncoder.EncoderLocation,
|
|
EncoderLocation = _mediaEncoder.EncoderLocation,
|
|
SystemArchitecture = RuntimeInformation.OSArchitecture,
|
|
SystemArchitecture = RuntimeInformation.OSArchitecture,
|
|
@@ -1195,14 +1211,12 @@ namespace Emby.Server.Implementations
|
|
}
|
|
}
|
|
|
|
|
|
public IEnumerable<WakeOnLanInfo> GetWakeOnLanInfo()
|
|
public IEnumerable<WakeOnLanInfo> GetWakeOnLanInfo()
|
|
- => _networkManager.GetMacAddresses()
|
|
|
|
|
|
+ => NetManager.GetMacAddresses()
|
|
.Select(i => new WakeOnLanInfo(i))
|
|
.Select(i => new WakeOnLanInfo(i))
|
|
.ToList();
|
|
.ToList();
|
|
|
|
|
|
- public async Task<PublicSystemInfo> GetPublicSystemInfo(CancellationToken cancellationToken)
|
|
|
|
|
|
+ public PublicSystemInfo GetPublicSystemInfo(IPAddress source)
|
|
{
|
|
{
|
|
- var localAddress = await GetLocalApiUrl(cancellationToken).ConfigureAwait(false);
|
|
|
|
-
|
|
|
|
return new PublicSystemInfo
|
|
return new PublicSystemInfo
|
|
{
|
|
{
|
|
Version = ApplicationVersionString,
|
|
Version = ApplicationVersionString,
|
|
@@ -1210,193 +1224,98 @@ namespace Emby.Server.Implementations
|
|
Id = SystemId,
|
|
Id = SystemId,
|
|
OperatingSystem = OperatingSystem.Id.ToString(),
|
|
OperatingSystem = OperatingSystem.Id.ToString(),
|
|
ServerName = FriendlyName,
|
|
ServerName = FriendlyName,
|
|
- LocalAddress = localAddress,
|
|
|
|
|
|
+ LocalAddress = GetSmartApiUrl(source),
|
|
StartupWizardCompleted = ConfigurationManager.CommonConfiguration.IsStartupWizardCompleted
|
|
StartupWizardCompleted = ConfigurationManager.CommonConfiguration.IsStartupWizardCompleted
|
|
};
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
/// <inheritdoc/>
|
|
/// <inheritdoc/>
|
|
- public bool ListenWithHttps => Certificate != null && ServerConfigurationManager.Configuration.EnableHttps;
|
|
|
|
|
|
+ public bool ListenWithHttps => Certificate != null && ServerConfigurationManager.GetNetworkConfiguration().EnableHttps;
|
|
|
|
|
|
/// <inheritdoc/>
|
|
/// <inheritdoc/>
|
|
- public async Task<string> GetLocalApiUrl(CancellationToken cancellationToken)
|
|
|
|
|
|
+ public string GetSmartApiUrl(IPAddress ipAddress, int? port = null)
|
|
{
|
|
{
|
|
- try
|
|
|
|
|
|
+ // Published server ends with a /
|
|
|
|
+ if (_startupOptions.PublishedServerUrl != null)
|
|
{
|
|
{
|
|
- // Return the first matched address, if found, or the first known local address
|
|
|
|
- var addresses = await GetLocalIpAddressesInternal(false, 1, cancellationToken).ConfigureAwait(false);
|
|
|
|
- if (addresses.Count == 0)
|
|
|
|
- {
|
|
|
|
- return null;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return GetLocalApiUrl(addresses[0]);
|
|
|
|
|
|
+ // Published server ends with a '/', so we need to remove it.
|
|
|
|
+ return _startupOptions.PublishedServerUrl.ToString().Trim('/');
|
|
}
|
|
}
|
|
- catch (Exception ex)
|
|
|
|
|
|
+
|
|
|
|
+ string smart = NetManager.GetBindInterface(ipAddress, out port);
|
|
|
|
+ // If the smartAPI doesn't start with http then treat it as a host or ip.
|
|
|
|
+ if (smart.StartsWith("http", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
{
|
|
- Logger.LogError(ex, "Error getting local Ip address information");
|
|
|
|
|
|
+ return smart.Trim('/');
|
|
}
|
|
}
|
|
|
|
|
|
- return null;
|
|
|
|
|
|
+ return GetLocalApiUrl(smart.Trim('/'), null, port);
|
|
}
|
|
}
|
|
|
|
|
|
- /// <summary>
|
|
|
|
- /// Removes the scope id from IPv6 addresses.
|
|
|
|
- /// </summary>
|
|
|
|
- /// <param name="address">The IPv6 address.</param>
|
|
|
|
- /// <returns>The IPv6 address without the scope id.</returns>
|
|
|
|
- private ReadOnlySpan<char> RemoveScopeId(ReadOnlySpan<char> address)
|
|
|
|
|
|
+ /// <inheritdoc/>
|
|
|
|
+ public string GetSmartApiUrl(HttpRequest request, int? port = null)
|
|
{
|
|
{
|
|
- var index = address.IndexOf('%');
|
|
|
|
- if (index == -1)
|
|
|
|
|
|
+ // Published server ends with a /
|
|
|
|
+ if (_startupOptions.PublishedServerUrl != null)
|
|
{
|
|
{
|
|
- return address;
|
|
|
|
|
|
+ // Published server ends with a '/', so we need to remove it.
|
|
|
|
+ return _startupOptions.PublishedServerUrl.ToString().Trim('/');
|
|
}
|
|
}
|
|
|
|
|
|
- return address.Slice(0, index);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /// <inheritdoc />
|
|
|
|
- public string GetLocalApiUrl(IPAddress ipAddress)
|
|
|
|
- {
|
|
|
|
- if (ipAddress.AddressFamily == AddressFamily.InterNetworkV6)
|
|
|
|
|
|
+ string smart = NetManager.GetBindInterface(request, out port);
|
|
|
|
+ // If the smartAPI doesn't start with http then treat it as a host or ip.
|
|
|
|
+ if (smart.StartsWith("http", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
{
|
|
- var str = RemoveScopeId(ipAddress.ToString());
|
|
|
|
- Span<char> span = new char[str.Length + 2];
|
|
|
|
- span[0] = '[';
|
|
|
|
- str.CopyTo(span.Slice(1));
|
|
|
|
- span[^1] = ']';
|
|
|
|
-
|
|
|
|
- return GetLocalApiUrl(span);
|
|
|
|
|
|
+ return smart.Trim('/');
|
|
}
|
|
}
|
|
|
|
|
|
- return GetLocalApiUrl(ipAddress.ToString());
|
|
|
|
|
|
+ return GetLocalApiUrl(smart.Trim('/'), request.Scheme, port);
|
|
}
|
|
}
|
|
|
|
|
|
/// <inheritdoc/>
|
|
/// <inheritdoc/>
|
|
- public string GetLoopbackHttpApiUrl()
|
|
|
|
- {
|
|
|
|
- return GetLocalApiUrl("127.0.0.1", Uri.UriSchemeHttp, HttpPort);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /// <inheritdoc/>
|
|
|
|
- public string GetLocalApiUrl(ReadOnlySpan<char> host, string scheme = null, int? port = null)
|
|
|
|
- {
|
|
|
|
- // NOTE: If no BaseUrl is set then UriBuilder appends a trailing slash, but if there is no BaseUrl it does
|
|
|
|
- // not. For consistency, always trim the trailing slash.
|
|
|
|
- return new UriBuilder
|
|
|
|
- {
|
|
|
|
- Scheme = scheme ?? (ListenWithHttps ? Uri.UriSchemeHttps : Uri.UriSchemeHttp),
|
|
|
|
- Host = host.ToString(),
|
|
|
|
- Port = port ?? (ListenWithHttps ? HttpsPort : HttpPort),
|
|
|
|
- Path = ServerConfigurationManager.Configuration.BaseUrl
|
|
|
|
- }.ToString().TrimEnd('/');
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public Task<List<IPAddress>> GetLocalIpAddresses(CancellationToken cancellationToken)
|
|
|
|
|
|
+ public string GetSmartApiUrl(string hostname, int? port = null)
|
|
{
|
|
{
|
|
- return GetLocalIpAddressesInternal(true, 0, cancellationToken);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private async Task<List<IPAddress>> GetLocalIpAddressesInternal(bool allowLoopback, int limit, CancellationToken cancellationToken)
|
|
|
|
- {
|
|
|
|
- var addresses = ServerConfigurationManager
|
|
|
|
- .Configuration
|
|
|
|
- .LocalNetworkAddresses
|
|
|
|
- .Select(x => NormalizeConfiguredLocalAddress(x))
|
|
|
|
- .Where(i => i != null)
|
|
|
|
- .ToList();
|
|
|
|
-
|
|
|
|
- if (addresses.Count == 0)
|
|
|
|
|
|
+ // Published server ends with a /
|
|
|
|
+ if (_startupOptions.PublishedServerUrl != null)
|
|
{
|
|
{
|
|
- addresses.AddRange(_networkManager.GetLocalIpAddresses());
|
|
|
|
|
|
+ // Published server ends with a '/', so we need to remove it.
|
|
|
|
+ return _startupOptions.PublishedServerUrl.ToString().Trim('/');
|
|
}
|
|
}
|
|
|
|
|
|
- var resultList = new List<IPAddress>();
|
|
|
|
|
|
+ string smart = NetManager.GetBindInterface(hostname, out port);
|
|
|
|
|
|
- foreach (var address in addresses)
|
|
|
|
|
|
+ // If the smartAPI doesn't start with http then treat it as a host or ip.
|
|
|
|
+ if (smart.StartsWith("http", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
{
|
|
- if (!allowLoopback)
|
|
|
|
- {
|
|
|
|
- if (address.Equals(IPAddress.Loopback) || address.Equals(IPAddress.IPv6Loopback))
|
|
|
|
- {
|
|
|
|
- continue;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (await IsLocalIpAddressValidAsync(address, cancellationToken).ConfigureAwait(false))
|
|
|
|
- {
|
|
|
|
- resultList.Add(address);
|
|
|
|
-
|
|
|
|
- if (limit > 0 && resultList.Count >= limit)
|
|
|
|
- {
|
|
|
|
- return resultList;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ return smart.Trim('/');
|
|
}
|
|
}
|
|
|
|
|
|
- return resultList;
|
|
|
|
|
|
+ return GetLocalApiUrl(smart.Trim('/'), null, port);
|
|
}
|
|
}
|
|
|
|
|
|
- public IPAddress NormalizeConfiguredLocalAddress(ReadOnlySpan<char> address)
|
|
|
|
|
|
+ /// <inheritdoc/>
|
|
|
|
+ public string GetLoopbackHttpApiUrl()
|
|
{
|
|
{
|
|
- var index = address.Trim('/').IndexOf('/');
|
|
|
|
- if (index != -1)
|
|
|
|
|
|
+ if (NetManager.IsIP6Enabled)
|
|
{
|
|
{
|
|
- address = address.Slice(index + 1);
|
|
|
|
|
|
+ return GetLocalApiUrl("::1", Uri.UriSchemeHttp, HttpPort);
|
|
}
|
|
}
|
|
|
|
|
|
- if (IPAddress.TryParse(address.Trim('/'), out IPAddress result))
|
|
|
|
- {
|
|
|
|
- return result;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return null;
|
|
|
|
|
|
+ return GetLocalApiUrl("127.0.0.1", Uri.UriSchemeHttp, HttpPort);
|
|
}
|
|
}
|
|
|
|
|
|
- private readonly ConcurrentDictionary<string, bool> _validAddressResults = new ConcurrentDictionary<string, bool>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
-
|
|
|
|
- private async Task<bool> IsLocalIpAddressValidAsync(IPAddress address, CancellationToken cancellationToken)
|
|
|
|
|
|
+ /// <inheritdoc/>
|
|
|
|
+ public string GetLocalApiUrl(string host, string scheme = null, int? port = null)
|
|
{
|
|
{
|
|
- if (address.Equals(IPAddress.Loopback)
|
|
|
|
- || address.Equals(IPAddress.IPv6Loopback))
|
|
|
|
- {
|
|
|
|
- return true;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- var apiUrl = GetLocalApiUrl(address) + "/system/ping";
|
|
|
|
-
|
|
|
|
- if (_validAddressResults.TryGetValue(apiUrl, out var cachedResult))
|
|
|
|
- {
|
|
|
|
- return cachedResult;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- try
|
|
|
|
- {
|
|
|
|
- using var request = new HttpRequestMessage(HttpMethod.Post, apiUrl);
|
|
|
|
- using var response = await _httpClientFactory.CreateClient(NamedClient.Default)
|
|
|
|
- .SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
|
|
|
|
-
|
|
|
|
- await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
|
|
|
|
- var result = await System.Text.Json.JsonSerializer.DeserializeAsync<string>(stream, JsonDefaults.GetOptions(), cancellationToken).ConfigureAwait(false);
|
|
|
|
- var valid = string.Equals(Name, result, StringComparison.OrdinalIgnoreCase);
|
|
|
|
-
|
|
|
|
- _validAddressResults.AddOrUpdate(apiUrl, valid, (k, v) => valid);
|
|
|
|
- Logger.LogDebug("Ping test result to {0}. Success: {1}", apiUrl, valid);
|
|
|
|
- return valid;
|
|
|
|
- }
|
|
|
|
- catch (OperationCanceledException)
|
|
|
|
- {
|
|
|
|
- Logger.LogDebug("Ping test result to {0}. Success: {1}", apiUrl, "Cancelled");
|
|
|
|
- throw;
|
|
|
|
- }
|
|
|
|
- catch (Exception ex)
|
|
|
|
|
|
+ // NOTE: If no BaseUrl is set then UriBuilder appends a trailing slash, but if there is no BaseUrl it does
|
|
|
|
+ // not. For consistency, always trim the trailing slash.
|
|
|
|
+ return new UriBuilder
|
|
{
|
|
{
|
|
- Logger.LogDebug(ex, "Ping test result to {0}. Success: {1}", apiUrl, false);
|
|
|
|
-
|
|
|
|
- _validAddressResults.AddOrUpdate(apiUrl, false, (k, v) => false);
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
|
|
+ Scheme = scheme ?? (ListenWithHttps ? Uri.UriSchemeHttps : Uri.UriSchemeHttp),
|
|
|
|
+ Host = host,
|
|
|
|
+ Port = port ?? (ListenWithHttps ? HttpsPort : HttpPort),
|
|
|
|
+ Path = ServerConfigurationManager.GetNetworkConfiguration().BaseUrl
|
|
|
|
+ }.ToString().TrimEnd('/');
|
|
}
|
|
}
|
|
|
|
|
|
public string FriendlyName =>
|
|
public string FriendlyName =>
|