@ -94,7 +94,6 @@ using MediaBrowser.Model.Serialization;
using MediaBrowser.Model.Services ;
using MediaBrowser.Model.Services ;
using MediaBrowser.Model.System ;
using MediaBrowser.Model.System ;
using MediaBrowser.Model.Tasks ;
using MediaBrowser.Model.Tasks ;
using MediaBrowser.Model.Updates ;
using MediaBrowser.Providers.Chapters ;
using MediaBrowser.Providers.Chapters ;
using MediaBrowser.Providers.Manager ;
using MediaBrowser.Providers.Manager ;
using MediaBrowser.Providers.Plugins.TheTvdb ;
using MediaBrowser.Providers.Plugins.TheTvdb ;
@ -1143,9 +1142,6 @@ namespace Emby.Server.Implementations
ItemsByNamePath = ApplicationPaths . InternalMetadataPath ,
ItemsByNamePath = ApplicationPaths . InternalMetadataPath ,
InternalMetadataPath = ApplicationPaths . InternalMetadataPath ,
InternalMetadataPath = ApplicationPaths . InternalMetadataPath ,
CachePath = ApplicationPaths . CachePath ,
CachePath = ApplicationPaths . CachePath ,
HttpServerPortNumber = HttpPort ,
SupportsHttps = SupportsHttps ,
HttpsPortNumber = HttpsPort ,
OperatingSystem = OperatingSystem . Id . ToString ( ) ,
OperatingSystem = OperatingSystem . Id . ToString ( ) ,
OperatingSystemDisplayName = OperatingSystem . Name ,
OperatingSystemDisplayName = OperatingSystem . Name ,
CanSelfRestart = CanSelfRestart ,
CanSelfRestart = CanSelfRestart ,
@ -1181,23 +1177,22 @@ namespace Emby.Server.Implementations
} ;
} ;
}
}
public bool EnableHttps = > SupportsHttps & & ServerConfigurationManager . Configuration . EnableHttps ;
/// <inheritdoc/>
public bool ListenWithHttps = > Certificate ! = null & & ServerConfigurationManager . Configuration . EnableHttps ;
public bool SupportsHttps = > Certificate ! = null | | ServerConfigurationManager . Configuration . IsBehindProxy ;
public async Task < string > GetLocalApiUrl ( CancellationToken cancellationToken , bool forceHttp = false )
/// <inheritdoc/>
public async Task < string > GetLocalApiUrl ( CancellationToken cancellationToken )
{
{
try
try
{
{
// Return the first matched address, if found, or the first known local address
// Return the first matched address, if found, or the first known local address
var addresses = await GetLocalIpAddressesInternal ( false , 1 , cancellationToken ) . ConfigureAwait ( false ) ;
var addresses = await GetLocalIpAddressesInternal ( false , 1 , cancellationToken ) . ConfigureAwait ( false ) ;
if ( addresses . Count = = 0 )
foreach ( var address in addresses )
{
{
return GetLocalApiUrl ( address , forceHttp ) ;
return null ;
}
}
return null ;
return GetLocalApiUrl ( addresses . First ( ) ) ;
}
}
catch ( Exception ex )
catch ( Exception ex )
{
{
@ -1224,7 +1219,7 @@ namespace Emby.Server.Implementations
}
}
/// <inheritdoc />
/// <inheritdoc />
public string GetLocalApiUrl ( IPAddress ipAddress , bool forceHttp = false )
public string GetLocalApiUrl ( IPAddress ipAddress )
{
{
if ( ipAddress . AddressFamily = = AddressFamily . InterNetworkV6 )
if ( ipAddress . AddressFamily = = AddressFamily . InterNetworkV6 )
{
{
@ -1234,29 +1229,30 @@ namespace Emby.Server.Implementations
str . CopyTo ( span . Slice ( 1 ) ) ;
str . CopyTo ( span . Slice ( 1 ) ) ;
span [ ^ 1 ] = ']' ;
span [ ^ 1 ] = ']' ;
return GetLocalApiUrl ( span , forceHttp );
return GetLocalApiUrl ( span );
}
}
return GetLocalApiUrl ( ipAddress . ToString ( ) , forceHttp );
return GetLocalApiUrl ( ipAddress . ToString ( ) );
}
}
/// <inheritdoc />
/// <inheritdoc/>
public string GetLocalApiUrl ( ReadOnlySpan < char > host , bool forceHttp = false )
public string GetLoopbackHttpApiUrl ( )
{
var url = new StringBuilder ( 64 ) ;
bool useHttps = EnableHttps & & ! forceHttp ;
url . Append ( useHttps ? "https://" : "http://" )
. Append ( host )
. Append ( ':' )
. Append ( useHttps ? HttpsPort : HttpPort ) ;
string baseUrl = ServerConfigurationManager . Configuration . BaseUrl ;
if ( baseUrl . Length ! = 0 )
{
{
url . Append ( baseUrl ) ;
return GetLocalApiUrl ( "127.0.0.1" , Uri . UriSchemeHttp , HttpPort ) ;
}
}
return url . ToString ( ) ;
/// <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 Task < List < IPAddress > > GetLocalIpAddresses ( CancellationToken cancellationToken )
@ -1290,7 +1286,7 @@ namespace Emby.Server.Implementations
}
}
}
}
var valid = await Is IpAddressValidAsync( address , cancellationToken ) . ConfigureAwait ( false ) ;
var valid = await Is Local IpAddressValidAsync( address , cancellationToken ) . ConfigureAwait ( false ) ;
if ( valid )
if ( valid )
{
{
resultList . Add ( address ) ;
resultList . Add ( address ) ;
@ -1324,7 +1320,7 @@ namespace Emby.Server.Implementations
private readonly ConcurrentDictionary < string , bool > _validAddressResults = new ConcurrentDictionary < string , bool > ( StringComparer . OrdinalIgnoreCase ) ;
private readonly ConcurrentDictionary < string , bool > _validAddressResults = new ConcurrentDictionary < string , bool > ( StringComparer . OrdinalIgnoreCase ) ;
private async Task < bool > Is IpAddressValidAsync( IPAddress address , CancellationToken cancellationToken )
private async Task < bool > Is Local IpAddressValidAsync( IPAddress address , CancellationToken cancellationToken )
{
{
if ( address . Equals ( IPAddress . Loopback )
if ( address . Equals ( IPAddress . Loopback )
| | address . Equals ( IPAddress . IPv6Loopback ) )
| | address . Equals ( IPAddress . IPv6Loopback ) )
@ -1332,8 +1328,7 @@ namespace Emby.Server.Implementations
return true ;
return true ;
}
}
var apiUrl = GetLocalApiUrl ( address ) ;
var apiUrl = GetLocalApiUrl ( address ) + "/system/ping" ;
apiUrl + = "/system/ping" ;
if ( _validAddressResults . TryGetValue ( apiUrl , out var cachedResult ) )
if ( _validAddressResults . TryGetValue ( apiUrl , out var cachedResult ) )
{
{