2013-04-13 21:09:13 +00:00
|
|
|
|
using MediaBrowser.Common.Configuration;
|
|
|
|
|
using System;
|
2014-08-31 19:15:33 +00:00
|
|
|
|
using System.Collections.Concurrent;
|
2013-04-13 21:09:13 +00:00
|
|
|
|
using System.Collections.Generic;
|
2014-08-31 19:15:33 +00:00
|
|
|
|
using System.Globalization;
|
2013-04-13 21:09:13 +00:00
|
|
|
|
using System.IO;
|
2014-08-31 19:15:33 +00:00
|
|
|
|
using System.Linq;
|
2013-04-13 21:09:13 +00:00
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
2013-06-10 16:57:30 +00:00
|
|
|
|
namespace MediaBrowser.Common.Implementations.Security
|
2013-04-13 21:09:13 +00:00
|
|
|
|
{
|
|
|
|
|
internal class MBLicenseFile
|
|
|
|
|
{
|
|
|
|
|
private readonly IApplicationPaths _appPaths;
|
|
|
|
|
|
|
|
|
|
public string RegKey
|
|
|
|
|
{
|
|
|
|
|
get { return _regKey; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value != _regKey)
|
|
|
|
|
{
|
|
|
|
|
//if key is changed - clear out our saved validations
|
2014-08-31 19:15:33 +00:00
|
|
|
|
_updateRecords.Clear();
|
2013-04-13 21:09:13 +00:00
|
|
|
|
_regKey = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-29 17:07:29 +00:00
|
|
|
|
private string Filename
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return Path.Combine(_appPaths.ConfigurationDirectoryPath, "mb.lic");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-31 19:15:33 +00:00
|
|
|
|
private readonly ConcurrentDictionary<Guid, DateTime> _updateRecords = new ConcurrentDictionary<Guid, DateTime>();
|
|
|
|
|
private readonly object _fileLock = new object();
|
2013-04-13 21:09:13 +00:00
|
|
|
|
private string _regKey;
|
|
|
|
|
|
|
|
|
|
public MBLicenseFile(IApplicationPaths appPaths)
|
|
|
|
|
{
|
|
|
|
|
_appPaths = appPaths;
|
|
|
|
|
|
|
|
|
|
Load();
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-31 19:15:33 +00:00
|
|
|
|
private void SetUpdateRecord(Guid key, DateTime value)
|
|
|
|
|
{
|
|
|
|
|
_updateRecords.AddOrUpdate(key, value, (k, v) => value);
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-13 21:09:13 +00:00
|
|
|
|
public void AddRegCheck(string featureId)
|
|
|
|
|
{
|
|
|
|
|
using (var provider = new MD5CryptoServiceProvider())
|
|
|
|
|
{
|
2014-08-31 19:15:33 +00:00
|
|
|
|
var key = new Guid(provider.ComputeHash(Encoding.Unicode.GetBytes(featureId)));
|
|
|
|
|
var value = DateTime.UtcNow;
|
|
|
|
|
|
|
|
|
|
SetUpdateRecord(key, value);
|
2013-04-13 21:09:13 +00:00
|
|
|
|
Save();
|
2014-01-25 21:11:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RemoveRegCheck(string featureId)
|
|
|
|
|
{
|
|
|
|
|
using (var provider = new MD5CryptoServiceProvider())
|
|
|
|
|
{
|
2014-08-31 19:15:33 +00:00
|
|
|
|
var key = new Guid(provider.ComputeHash(Encoding.Unicode.GetBytes(featureId)));
|
|
|
|
|
DateTime val;
|
|
|
|
|
|
|
|
|
|
_updateRecords.TryRemove(key, out val);
|
|
|
|
|
|
2014-01-25 21:11:09 +00:00
|
|
|
|
Save();
|
2013-04-13 21:09:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DateTime LastChecked(string featureId)
|
|
|
|
|
{
|
|
|
|
|
using (var provider = new MD5CryptoServiceProvider())
|
|
|
|
|
{
|
|
|
|
|
DateTime last;
|
2014-08-31 19:15:33 +00:00
|
|
|
|
_updateRecords.TryGetValue(new Guid(provider.ComputeHash(Encoding.Unicode.GetBytes(featureId))), out last);
|
|
|
|
|
|
|
|
|
|
// guard agains people just putting a large number in the file
|
|
|
|
|
return last < DateTime.UtcNow ? last : DateTime.MinValue;
|
2013-04-13 21:09:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Load()
|
|
|
|
|
{
|
|
|
|
|
string[] contents = null;
|
2013-12-29 17:07:29 +00:00
|
|
|
|
var licenseFile = Filename;
|
2014-08-31 19:15:33 +00:00
|
|
|
|
lock (_fileLock)
|
2013-04-13 21:09:13 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2015-09-13 21:32:02 +00:00
|
|
|
|
contents = _fileSystem.ReadAllLines(licenseFile);
|
2013-04-13 21:09:13 +00:00
|
|
|
|
}
|
2014-12-23 03:58:14 +00:00
|
|
|
|
catch (DirectoryNotFoundException)
|
|
|
|
|
{
|
2015-09-13 21:32:02 +00:00
|
|
|
|
(_fileSystem.CreateFile(licenseFile)).Close();
|
2014-12-23 03:58:14 +00:00
|
|
|
|
}
|
2013-04-13 21:09:13 +00:00
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
|
{
|
2015-09-13 21:32:02 +00:00
|
|
|
|
(_fileSystem.CreateFile(licenseFile)).Close();
|
2013-04-13 21:09:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (contents != null && contents.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
//first line is reg key
|
|
|
|
|
RegKey = contents[0];
|
2014-08-31 19:15:33 +00:00
|
|
|
|
|
2013-04-13 21:09:13 +00:00
|
|
|
|
//next is legacy key
|
2014-08-31 19:15:33 +00:00
|
|
|
|
if (contents.Length > 1)
|
|
|
|
|
{
|
|
|
|
|
// Don't need this anymore
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-13 21:09:13 +00:00
|
|
|
|
//the rest of the lines should be pairs of features and timestamps
|
|
|
|
|
for (var i = 2; i < contents.Length; i = i + 2)
|
|
|
|
|
{
|
|
|
|
|
var feat = Guid.Parse(contents[i]);
|
2014-08-31 19:15:33 +00:00
|
|
|
|
|
|
|
|
|
SetUpdateRecord(feat, new DateTime(Convert.ToInt64(contents[i + 1])));
|
2013-04-13 21:09:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Save()
|
|
|
|
|
{
|
|
|
|
|
//build our array
|
2014-08-31 19:15:33 +00:00
|
|
|
|
var lines = new List<string>
|
|
|
|
|
{
|
|
|
|
|
RegKey,
|
|
|
|
|
|
|
|
|
|
// Legacy key
|
|
|
|
|
string.Empty
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
foreach (var pair in _updateRecords
|
|
|
|
|
.ToList())
|
2013-04-13 21:09:13 +00:00
|
|
|
|
{
|
|
|
|
|
lines.Add(pair.Key.ToString());
|
2014-08-31 19:15:33 +00:00
|
|
|
|
lines.Add(pair.Value.Ticks.ToString(CultureInfo.InvariantCulture));
|
2013-04-13 21:09:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-29 17:07:29 +00:00
|
|
|
|
var licenseFile = Filename;
|
2015-09-13 21:32:02 +00:00
|
|
|
|
_fileSystem.CreateDirectory(Path.GetDirectoryName(licenseFile));
|
|
|
|
|
lock (_fileLock) _fileSystem.WriteAllLines(licenseFile, lines);
|
2013-04-13 21:09:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|