Merge pull request #1589 from MediaBrowser/dev

Dev
This commit is contained in:
Luke 2016-03-25 13:17:58 -04:00
commit 89f86a0b2b
7 changed files with 122 additions and 63 deletions

View File

@ -196,9 +196,13 @@ namespace MediaBrowser.Api
return name; return name;
} }
return libraryManager.RootFolder var items = libraryManager.GetItemList(new InternalItemsQuery
.GetRecursiveChildren(i => i is IHasArtist) {
.Cast<IHasArtist>() IncludeItemTypes = new[] { typeof(Audio).Name, typeof(MusicVideo).Name, typeof(MusicAlbum).Name }
});
return items
.OfType<IHasArtist>()
.SelectMany(i => i.AllArtists) .SelectMany(i => i.AllArtists)
.DistinctNames() .DistinctNames()
.FirstOrDefault(i => .FirstOrDefault(i =>
@ -239,8 +243,12 @@ namespace MediaBrowser.Api
return name; return name;
} }
return libraryManager.RootFolder var items = libraryManager.GetItemList(new InternalItemsQuery
.GetRecursiveChildren(i => i is Game) {
IncludeItemTypes = new[] { typeof(Game).Name }
});
return items
.SelectMany(i => i.Genres) .SelectMany(i => i.Genres)
.DistinctNames() .DistinctNames()
.FirstOrDefault(i => .FirstOrDefault(i =>

View File

@ -126,6 +126,23 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
} }
} }
private void AddIpv4Option(HttpWebRequest request, HttpRequestOptions options)
{
if (!options.PreferIpv4)
{
return;
}
request.ServicePoint.BindIPEndPointDelegate = (servicePount, remoteEndPoint, retryCount) =>
{
if (remoteEndPoint.AddressFamily == AddressFamily.InterNetwork)
{
return new IPEndPoint(IPAddress.Any, 0);
}
throw new InvalidOperationException("no IPv4 address");
};
}
private WebRequest GetRequest(HttpRequestOptions options, string method, bool enableHttpCompression) private WebRequest GetRequest(HttpRequestOptions options, string method, bool enableHttpCompression)
{ {
var request = CreateWebRequest(options.Url); var request = CreateWebRequest(options.Url);
@ -133,6 +150,8 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
if (httpWebRequest != null) if (httpWebRequest != null)
{ {
AddIpv4Option(httpWebRequest, options);
AddRequestHeaders(httpWebRequest, options); AddRequestHeaders(httpWebRequest, options);
httpWebRequest.AutomaticDecompression = enableHttpCompression ? DecompressionMethods.Deflate : DecompressionMethods.None; httpWebRequest.AutomaticDecompression = enableHttpCompression ? DecompressionMethods.Deflate : DecompressionMethods.None;

View File

@ -96,6 +96,7 @@ namespace MediaBrowser.Common.Net
public TimeSpan CacheLength { get; set; } public TimeSpan CacheLength { get; set; }
public int TimeoutMs { get; set; } public int TimeoutMs { get; set; }
public bool PreferIpv4 { get; set; }
private string GetHeaderValue(string name) private string GetHeaderValue(string name)
{ {

View File

@ -100,13 +100,13 @@ namespace MediaBrowser.Providers.Omdb
if (enableMultipleResults) if (enableMultipleResults)
{ {
url += "&s=" + WebUtility.UrlEncode(name); url += "&s=" + WebUtility.UrlEncode(name);
isSearch = true;
} }
else else
{ {
url += "&t=" + WebUtility.UrlEncode(name); url += "&t=" + WebUtility.UrlEncode(name);
} }
url += "&type=" + type; url += "&type=" + type;
isSearch = true;
} }
else else
{ {

View File

@ -8,6 +8,7 @@ using MediaBrowser.Model.Net;
using System; using System;
using System.IO; using System.IO;
using System.Net; using System.Net;
using System.Net.Sockets;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -47,44 +48,26 @@ namespace MediaBrowser.Server.Implementations.Connect
_timer = new PeriodicTimer(TimerCallback, null, TimeSpan.FromSeconds(5), TimeSpan.FromHours(3)); _timer = new PeriodicTimer(TimerCallback, null, TimeSpan.FromSeconds(5), TimeSpan.FromHours(3));
} }
private readonly string[] _ipLookups = { "http://bot.whatismyipaddress.com", "https://connect.emby.media/service/ip" }; private readonly string[] _ipLookups =
{
"http://bot.whatismyipaddress.com",
"https://connect.emby.media/service/ip"
};
private async void TimerCallback(object state) private async void TimerCallback(object state)
{ {
var index = 0; IPAddress validIpAddress = null;
foreach (var ipLookupUrl in _ipLookups) foreach (var ipLookupUrl in _ipLookups)
{ {
try try
{ {
// Sometimes whatismyipaddress might fail, but it won't do us any good having users raise alarms over it. validIpAddress = await GetIpAddress(ipLookupUrl).ConfigureAwait(false);
var logErrors = index > 0;
#if DEBUG // Try to find the ipv4 address, if present
logErrors = true; if (validIpAddress.AddressFamily == AddressFamily.InterNetwork)
#endif
using (var stream = await _httpClient.Get(new HttpRequestOptions
{ {
Url = ipLookupUrl, break;
UserAgent = "Emby/" + _appHost.ApplicationVersion,
LogErrors = logErrors,
// Seeing block length errors with our server
EnableHttpCompression = false
}).ConfigureAwait(false))
{
using (var reader = new StreamReader(stream))
{
var address = await reader.ReadToEndAsync().ConfigureAwait(false);
if (IsValid(address, ipLookupUrl))
{
((ConnectManager)_connectManager).OnWanAddressResolved(address);
CacheAddress(address);
return;
}
}
} }
} }
catch (HttpException) catch (HttpException)
@ -94,8 +77,66 @@ namespace MediaBrowser.Server.Implementations.Connect
{ {
_logger.ErrorException("Error getting connection info", ex); _logger.ErrorException("Error getting connection info", ex);
} }
}
index++; // If this produced an ipv6 address, try again
if (validIpAddress == null || validIpAddress.AddressFamily == AddressFamily.InterNetworkV6)
{
foreach (var ipLookupUrl in _ipLookups)
{
try
{
validIpAddress = await GetIpAddress(ipLookupUrl, true).ConfigureAwait(false);
// Try to find the ipv4 address, if present
if (validIpAddress.AddressFamily == AddressFamily.InterNetwork)
{
break;
}
}
catch (HttpException)
{
}
catch (Exception ex)
{
_logger.ErrorException("Error getting connection info", ex);
}
}
}
if (validIpAddress != null)
{
((ConnectManager)_connectManager).OnWanAddressResolved(validIpAddress);
CacheAddress(validIpAddress);
}
}
private async Task<IPAddress> GetIpAddress(string lookupUrl, bool preferIpv4 = false)
{
// Sometimes whatismyipaddress might fail, but it won't do us any good having users raise alarms over it.
var logErrors = false;
#if DEBUG
logErrors = true;
#endif
using (var stream = await _httpClient.Get(new HttpRequestOptions
{
Url = lookupUrl,
UserAgent = "Emby/" + _appHost.ApplicationVersion,
LogErrors = logErrors,
// Seeing block length errors with our server
EnableHttpCompression = false,
PreferIpv4 = preferIpv4
}).ConfigureAwait(false))
{
using (var reader = new StreamReader(stream))
{
var addressString = await reader.ReadToEndAsync().ConfigureAwait(false);
return IPAddress.Parse(addressString);
}
} }
} }
@ -104,14 +145,14 @@ namespace MediaBrowser.Server.Implementations.Connect
get { return Path.Combine(_appPaths.DataPath, "wan.txt"); } get { return Path.Combine(_appPaths.DataPath, "wan.txt"); }
} }
private void CacheAddress(string address) private void CacheAddress(IPAddress address)
{ {
var path = CacheFilePath; var path = CacheFilePath;
try try
{ {
_fileSystem.CreateDirectory(Path.GetDirectoryName(path)); _fileSystem.CreateDirectory(Path.GetDirectoryName(path));
_fileSystem.WriteAllText(path, address, Encoding.UTF8); _fileSystem.WriteAllText(path, address.ToString(), Encoding.UTF8);
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -126,10 +167,11 @@ namespace MediaBrowser.Server.Implementations.Connect
try try
{ {
var endpoint = _fileSystem.ReadAllText(path, Encoding.UTF8); var endpoint = _fileSystem.ReadAllText(path, Encoding.UTF8);
IPAddress ipAddress;
if (IsValid(endpoint, "cache")) if (IPAddress.TryParse(endpoint, out ipAddress))
{ {
((ConnectManager)_connectManager).OnWanAddressResolved(endpoint); ((ConnectManager)_connectManager).OnWanAddressResolved(ipAddress);
} }
} }
catch (IOException) catch (IOException)
@ -142,19 +184,6 @@ namespace MediaBrowser.Server.Implementations.Connect
} }
} }
private bool IsValid(string address, string source)
{
IPAddress ipAddress;
var valid = IPAddress.TryParse(address, out ipAddress);
if (!valid)
{
_logger.Error("{0} is not a valid ip address. Source: {1}", address, source);
}
return valid;
}
public void Dispose() public void Dispose()
{ {
if (_timer != null) if (_timer != null)

View File

@ -19,6 +19,7 @@ using System.Globalization;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Net.Sockets;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -53,7 +54,7 @@ namespace MediaBrowser.Server.Implementations.Connect
get { return _data.AccessKey; } get { return _data.AccessKey; }
} }
public string DiscoveredWanIpAddress { get; private set; } private IPAddress DiscoveredWanIpAddress { get; set; }
public string WanIpAddress public string WanIpAddress
{ {
@ -61,9 +62,16 @@ namespace MediaBrowser.Server.Implementations.Connect
{ {
var address = _config.Configuration.WanDdns; var address = _config.Configuration.WanDdns;
if (string.IsNullOrWhiteSpace(address)) if (string.IsNullOrWhiteSpace(address) && DiscoveredWanIpAddress != null)
{ {
address = DiscoveredWanIpAddress; if (DiscoveredWanIpAddress.AddressFamily == AddressFamily.InterNetworkV6)
{
address = "[" + DiscoveredWanIpAddress + "]";
}
else
{
address = DiscoveredWanIpAddress.ToString();
}
} }
return address; return address;
@ -81,12 +89,6 @@ namespace MediaBrowser.Server.Implementations.Connect
if (!ip.StartsWith("http://", StringComparison.OrdinalIgnoreCase) && if (!ip.StartsWith("http://", StringComparison.OrdinalIgnoreCase) &&
!ip.StartsWith("https://", StringComparison.OrdinalIgnoreCase)) !ip.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
{ {
// Handle ipv6
if (ip.IndexOf(':') != -1)
{
ip = "[" + ip + "]";
}
ip = (_appHost.EnableHttps ? "https://" : "http://") + ip; ip = (_appHost.EnableHttps ? "https://" : "http://") + ip;
} }
@ -130,7 +132,7 @@ namespace MediaBrowser.Server.Implementations.Connect
LoadCachedData(); LoadCachedData();
} }
internal void OnWanAddressResolved(string address) internal void OnWanAddressResolved(IPAddress address)
{ {
DiscoveredWanIpAddress = address; DiscoveredWanIpAddress = address;

View File

@ -100,7 +100,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts.SatIp
{ {
if (string.IsNullOrWhiteSpace(info.M3UUrl)) if (string.IsNullOrWhiteSpace(info.M3UUrl))
{ {
//return; return;
} }
await _liveTvManager.SaveTunerHost(new TunerHostInfo await _liveTvManager.SaveTunerHost(new TunerHostInfo