2013-09-13 20:45:12 +00:00
|
|
|
|
using MediaBrowser.Common.Configuration;
|
2013-04-13 21:09:13 +00:00
|
|
|
|
using MediaBrowser.Common.Net;
|
2013-09-13 20:45:12 +00:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
2014-01-25 21:11:09 +00:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2013-04-13 21:09:13 +00:00
|
|
|
|
using MediaBrowser.Model.Serialization;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2013-06-10 16:57:30 +00:00
|
|
|
|
namespace MediaBrowser.Common.Implementations.Security
|
2013-04-13 21:09:13 +00:00
|
|
|
|
{
|
|
|
|
|
public static class MBRegistration
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
private static MBLicenseFile _licenseFile;
|
2014-02-28 04:49:02 +00:00
|
|
|
|
private const string MBValidateUrl = Constants.Constants.MbAdminUrl + "service/registration/validate";
|
2013-04-13 21:09:13 +00:00
|
|
|
|
|
|
|
|
|
private static IApplicationPaths _appPaths;
|
2013-09-13 20:45:12 +00:00
|
|
|
|
private static INetworkManager _networkManager;
|
2014-01-25 21:11:09 +00:00
|
|
|
|
private static ILogger _logger;
|
2014-02-28 04:49:02 +00:00
|
|
|
|
private static IApplicationHost _applicationHost;
|
2013-04-13 21:09:13 +00:00
|
|
|
|
|
|
|
|
|
private static MBLicenseFile LicenseFile
|
|
|
|
|
{
|
|
|
|
|
get { return _licenseFile ?? (_licenseFile = new MBLicenseFile(_appPaths)); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string SupporterKey
|
|
|
|
|
{
|
|
|
|
|
get { return LicenseFile.RegKey; }
|
|
|
|
|
set { LicenseFile.RegKey = value; LicenseFile.Save(); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string LegacyKey
|
|
|
|
|
{
|
|
|
|
|
get { return LicenseFile.LegacyKey; }
|
|
|
|
|
set { LicenseFile.LegacyKey = value; LicenseFile.Save(); }
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-28 04:49:02 +00:00
|
|
|
|
public static void Init(IApplicationPaths appPaths, INetworkManager networkManager, ILogManager logManager, IApplicationHost appHost)
|
2013-04-13 21:09:13 +00:00
|
|
|
|
{
|
|
|
|
|
// Ugly alert (static init)
|
|
|
|
|
|
|
|
|
|
_appPaths = appPaths;
|
2013-09-13 20:45:12 +00:00
|
|
|
|
_networkManager = networkManager;
|
2014-01-25 21:11:09 +00:00
|
|
|
|
_logger = logManager.GetLogger("SecurityManager");
|
2014-02-28 04:49:02 +00:00
|
|
|
|
_applicationHost = appHost;
|
2013-04-13 21:09:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-25 21:11:09 +00:00
|
|
|
|
public static async Task<MBRegistrationRecord> GetRegistrationStatus(IHttpClient httpClient, IJsonSerializer jsonSerializer, string feature, string mb2Equivalent = null, string version = null)
|
2013-04-13 21:09:13 +00:00
|
|
|
|
{
|
2014-01-25 21:11:09 +00:00
|
|
|
|
//check the reg file first to alleviate strain on the MB admin server - must actually check in every 30 days tho
|
2014-02-28 04:49:02 +00:00
|
|
|
|
var reg = new RegRecord { registered = LicenseFile.LastChecked(feature) > DateTime.UtcNow.AddDays(-30) };
|
2013-04-13 21:09:13 +00:00
|
|
|
|
|
2014-01-25 21:11:09 +00:00
|
|
|
|
if (!reg.registered)
|
2013-04-13 21:09:13 +00:00
|
|
|
|
{
|
2014-01-25 21:11:09 +00:00
|
|
|
|
var mac = _networkManager.GetMacAddress();
|
2014-02-28 04:49:02 +00:00
|
|
|
|
var data = new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{ "feature", feature },
|
|
|
|
|
{ "key", SupporterKey },
|
|
|
|
|
{ "mac", mac },
|
|
|
|
|
{ "mb2equiv", mb2Equivalent },
|
|
|
|
|
{ "legacykey", LegacyKey },
|
|
|
|
|
{ "ver", version },
|
|
|
|
|
{ "platform", Environment.OSVersion.VersionString },
|
|
|
|
|
{ "isservice", _applicationHost.IsRunningAsService.ToString().ToLower() }
|
|
|
|
|
};
|
2014-01-25 21:11:09 +00:00
|
|
|
|
|
|
|
|
|
try
|
2013-04-13 21:09:13 +00:00
|
|
|
|
{
|
2014-01-25 21:11:09 +00:00
|
|
|
|
using (var json = await httpClient.Post(MBValidateUrl, data, CancellationToken.None).ConfigureAwait(false))
|
|
|
|
|
{
|
|
|
|
|
reg = jsonSerializer.DeserializeFromStream<RegRecord>(json);
|
|
|
|
|
}
|
2013-04-13 21:09:13 +00:00
|
|
|
|
|
2014-01-25 21:11:09 +00:00
|
|
|
|
if (reg.registered)
|
|
|
|
|
{
|
|
|
|
|
LicenseFile.AddRegCheck(feature);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
LicenseFile.RemoveRegCheck(feature);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
2013-04-13 21:09:13 +00:00
|
|
|
|
{
|
2014-01-25 21:11:09 +00:00
|
|
|
|
_logger.ErrorException("Error checking registration status of {0}", e, feature);
|
2013-04-13 21:09:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-28 04:49:02 +00:00
|
|
|
|
return new MBRegistrationRecord { IsRegistered = reg.registered, ExpirationDate = reg.expDate, RegChecked = true };
|
2013-04-13 21:09:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class RegRecord
|
|
|
|
|
{
|
|
|
|
|
public string featId { get; set; }
|
|
|
|
|
public bool registered { get; set; }
|
|
|
|
|
public DateTime expDate { get; set; }
|
|
|
|
|
}
|
|
|
|
|
}
|