aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Server.Implementations/Connect
diff options
context:
space:
mode:
authorLuke <luke.pulverenti@gmail.com>2015-10-26 18:50:19 -0400
committerLuke <luke.pulverenti@gmail.com>2015-10-26 18:50:19 -0400
commit35778ebc02e5931142a1fe31a256b7488a07c5c2 (patch)
treeced0290be8820f5e507b51ca4c5165212b1879d1 /MediaBrowser.Server.Implementations/Connect
parentc0dc8d055bfd4d2f58591083beb9e9128357aad6 (diff)
parent8d77308593c3b16b733b0109323770d9dfe7e166 (diff)
Merge pull request #1222 from MediaBrowser/dev
3.0.5768.7
Diffstat (limited to 'MediaBrowser.Server.Implementations/Connect')
-rw-r--r--MediaBrowser.Server.Implementations/Connect/ConnectEntryPoint.cs33
-rw-r--r--MediaBrowser.Server.Implementations/Connect/ConnectManager.cs55
2 files changed, 61 insertions, 27 deletions
diff --git a/MediaBrowser.Server.Implementations/Connect/ConnectEntryPoint.cs b/MediaBrowser.Server.Implementations/Connect/ConnectEntryPoint.cs
index 8a659fb65c..9ad04ebbde 100644
--- a/MediaBrowser.Server.Implementations/Connect/ConnectEntryPoint.cs
+++ b/MediaBrowser.Server.Implementations/Connect/ConnectEntryPoint.cs
@@ -10,6 +10,8 @@ using System.IO;
using System.Net;
using System.Text;
using System.Threading;
+using CommonIO;
+using MediaBrowser.Common.IO;
namespace MediaBrowser.Server.Implementations.Connect
{
@@ -23,8 +25,9 @@ namespace MediaBrowser.Server.Implementations.Connect
private readonly INetworkManager _networkManager;
private readonly IApplicationHost _appHost;
+ private readonly IFileSystem _fileSystem;
- public ConnectEntryPoint(IHttpClient httpClient, IApplicationPaths appPaths, ILogger logger, INetworkManager networkManager, IConnectManager connectManager, IApplicationHost appHost)
+ public ConnectEntryPoint(IHttpClient httpClient, IApplicationPaths appPaths, ILogger logger, INetworkManager networkManager, IConnectManager connectManager, IApplicationHost appHost, IFileSystem fileSystem)
{
_httpClient = httpClient;
_appPaths = appPaths;
@@ -32,6 +35,7 @@ namespace MediaBrowser.Server.Implementations.Connect
_networkManager = networkManager;
_connectManager = connectManager;
_appHost = appHost;
+ _fileSystem = fileSystem;
}
public void Run()
@@ -45,14 +49,23 @@ namespace MediaBrowser.Server.Implementations.Connect
private async void TimerCallback(object state)
{
+ var index = 0;
+
foreach (var ipLookupUrl in _ipLookups)
{
try
{
+ // Sometimes whatismyipaddress might fail, but it won't do us any good having users raise alarms over it.
+ var logErrors = index > 0;
+
+#if DEBUG
+ logErrors = true;
+#endif
using (var stream = await _httpClient.Get(new HttpRequestOptions
{
Url = ipLookupUrl,
- UserAgent = "Emby Server/" + _appHost.ApplicationVersion
+ UserAgent = "Emby/" + _appHost.ApplicationVersion,
+ LogErrors = logErrors
}).ConfigureAwait(false))
{
@@ -60,7 +73,7 @@ namespace MediaBrowser.Server.Implementations.Connect
{
var address = await reader.ReadToEndAsync().ConfigureAwait(false);
- if (IsValid(address))
+ if (IsValid(address, ipLookupUrl))
{
((ConnectManager)_connectManager).OnWanAddressResolved(address);
CacheAddress(address);
@@ -76,6 +89,8 @@ namespace MediaBrowser.Server.Implementations.Connect
{
_logger.ErrorException("Error getting connection info", ex);
}
+
+ index++;
}
}
@@ -90,8 +105,8 @@ namespace MediaBrowser.Server.Implementations.Connect
try
{
- Directory.CreateDirectory(Path.GetDirectoryName(path));
- File.WriteAllText(path, address, Encoding.UTF8);
+ _fileSystem.CreateDirectory(Path.GetDirectoryName(path));
+ _fileSystem.WriteAllText(path, address, Encoding.UTF8);
}
catch (Exception ex)
{
@@ -105,9 +120,9 @@ namespace MediaBrowser.Server.Implementations.Connect
try
{
- var endpoint = File.ReadAllText(path, Encoding.UTF8);
+ var endpoint = _fileSystem.ReadAllText(path, Encoding.UTF8);
- if (IsValid(endpoint))
+ if (IsValid(endpoint, "cache"))
{
((ConnectManager)_connectManager).OnWanAddressResolved(endpoint);
}
@@ -122,14 +137,14 @@ namespace MediaBrowser.Server.Implementations.Connect
}
}
- private bool IsValid(string address)
+ 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", address);
+ _logger.Error("{0} is not a valid ip address. Source: {1}", address, source);
}
return valid;
diff --git a/MediaBrowser.Server.Implementations/Connect/ConnectManager.cs b/MediaBrowser.Server.Implementations/Connect/ConnectManager.cs
index 7cd96c5f38..f1de09d567 100644
--- a/MediaBrowser.Server.Implementations/Connect/ConnectManager.cs
+++ b/MediaBrowser.Server.Implementations/Connect/ConnectManager.cs
@@ -23,6 +23,8 @@ using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
+using CommonIO;
+using MediaBrowser.Common.IO;
namespace MediaBrowser.Server.Implementations.Connect
{
@@ -40,6 +42,7 @@ namespace MediaBrowser.Server.Implementations.Connect
private readonly IUserManager _userManager;
private readonly IProviderManager _providerManager;
private readonly ISecurityManager _securityManager;
+ private readonly IFileSystem _fileSystem;
private ConnectData _data = new ConnectData();
@@ -104,7 +107,7 @@ namespace MediaBrowser.Server.Implementations.Connect
IEncryptionManager encryption,
IHttpClient httpClient,
IServerApplicationHost appHost,
- IServerConfigurationManager config, IUserManager userManager, IProviderManager providerManager, ISecurityManager securityManager)
+ IServerConfigurationManager config, IUserManager userManager, IProviderManager providerManager, ISecurityManager securityManager, IFileSystem fileSystem)
{
_logger = logger;
_appPaths = appPaths;
@@ -116,6 +119,7 @@ namespace MediaBrowser.Server.Implementations.Connect
_userManager = userManager;
_providerManager = providerManager;
_securityManager = securityManager;
+ _fileSystem = fileSystem;
_userManager.UserConfigurationUpdated += _userManager_UserConfigurationUpdated;
_config.ConfigurationUpdated += _config_ConfigurationUpdated;
@@ -315,7 +319,7 @@ namespace MediaBrowser.Server.Implementations.Connect
try
{
- Directory.CreateDirectory(Path.GetDirectoryName(path));
+ _fileSystem.CreateDirectory(Path.GetDirectoryName(path));
var json = _json.SerializeToString(_data);
@@ -323,7 +327,7 @@ namespace MediaBrowser.Server.Implementations.Connect
lock (_dataFileLock)
{
- File.WriteAllText(path, encrypted, Encoding.UTF8);
+ _fileSystem.WriteAllText(path, encrypted, Encoding.UTF8);
}
}
catch (Exception ex)
@@ -340,7 +344,7 @@ namespace MediaBrowser.Server.Implementations.Connect
{
lock (_dataFileLock)
{
- var encrypted = File.ReadAllText(path, Encoding.UTF8);
+ var encrypted = _fileSystem.ReadAllText(path, Encoding.UTF8);
var json = _encryption.DecryptString(encrypted);
@@ -932,7 +936,7 @@ namespace MediaBrowser.Server.Implementations.Connect
{
var length = response.ContentLength;
- if (length != new FileInfo(user.GetImageInfo(ImageType.Primary, 0).Path).Length)
+ if (length != _fileSystem.GetFileInfo(user.GetImageInfo(ImageType.Primary, 0).Path).Length)
{
changed = true;
}
@@ -943,7 +947,7 @@ namespace MediaBrowser.Server.Implementations.Connect
{
await _providerManager.SaveImage(user, imageUrl, _connectImageSemaphore, ImageType.Primary, null, CancellationToken.None).ConfigureAwait(false);
- await user.RefreshMetadata(new MetadataRefreshOptions
+ await user.RefreshMetadata(new MetadataRefreshOptions(_fileSystem)
{
ForceSave = true,
@@ -1076,20 +1080,25 @@ namespace MediaBrowser.Server.Implementations.Connect
var url = GetConnectUrl("keyAssociation");
- url += "?serverId=" + ConnectServerId;
- url += "&supporterKey=" + _securityManager.SupporterKey;
-
var options = new HttpRequestOptions
{
Url = url,
CancellationToken = CancellationToken.None
};
+ var postData = new Dictionary<string, string>
+ {
+ {"serverId", ConnectServerId},
+ {"supporterKey", _securityManager.SupporterKey}
+ };
+
+ options.SetPostData(postData);
+
SetServerAccessToken(options);
SetApplicationHeader(options);
// No need to examine the response
- using (var stream = (await _httpClient.SendAsync(options, "GET").ConfigureAwait(false)).Content)
+ using (var stream = (await _httpClient.SendAsync(options, "POST").ConfigureAwait(false)).Content)
{
return _json.DeserializeFromStream<ConnectSupporterSummary>(stream);
}
@@ -1104,16 +1113,21 @@ namespace MediaBrowser.Server.Implementations.Connect
var url = GetConnectUrl("keyAssociation");
- url += "?serverId=" + ConnectServerId;
- url += "&supporterKey=" + _securityManager.SupporterKey;
- url += "&userId=" + id;
-
var options = new HttpRequestOptions
{
Url = url,
CancellationToken = CancellationToken.None
};
+ var postData = new Dictionary<string, string>
+ {
+ {"serverId", ConnectServerId},
+ {"supporterKey", _securityManager.SupporterKey},
+ {"userId", id}
+ };
+
+ options.SetPostData(postData);
+
SetServerAccessToken(options);
SetApplicationHeader(options);
@@ -1132,16 +1146,21 @@ namespace MediaBrowser.Server.Implementations.Connect
var url = GetConnectUrl("keyAssociation");
- url += "?serverId=" + ConnectServerId;
- url += "&supporterKey=" + _securityManager.SupporterKey;
- url += "&userId=" + id;
-
var options = new HttpRequestOptions
{
Url = url,
CancellationToken = CancellationToken.None
};
+ var postData = new Dictionary<string, string>
+ {
+ {"serverId", ConnectServerId},
+ {"supporterKey", _securityManager.SupporterKey},
+ {"userId", id}
+ };
+
+ options.SetPostData(postData);
+
SetServerAccessToken(options);
SetApplicationHeader(options);