2016-10-29 05:40:15 +00:00
|
|
|
|
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.Text;
|
2016-10-29 05:40:15 +00:00
|
|
|
|
using MediaBrowser.Common.Configuration;
|
2016-11-04 08:43:59 +00:00
|
|
|
|
using MediaBrowser.Model.Cryptography;
|
|
|
|
|
using MediaBrowser.Model.IO;
|
2013-04-13 21:09:13 +00:00
|
|
|
|
|
2016-11-04 08:43:59 +00:00
|
|
|
|
namespace Emby.Server.Implementations.Security
|
2013-04-13 21:09:13 +00:00
|
|
|
|
{
|
|
|
|
|
internal class MBLicenseFile
|
|
|
|
|
{
|
|
|
|
|
private readonly IApplicationPaths _appPaths;
|
2016-11-04 08:43:59 +00:00
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2016-11-08 18:44:23 +00:00
|
|
|
|
private readonly ICryptoProvider _cryptographyProvider;
|
2013-04-13 21:09:13 +00:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2016-11-08 18:44:23 +00:00
|
|
|
|
public MBLicenseFile(IApplicationPaths appPaths, IFileSystem fileSystem, ICryptoProvider cryptographyProvider)
|
2013-04-13 21:09:13 +00:00
|
|
|
|
{
|
|
|
|
|
_appPaths = appPaths;
|
2016-11-04 08:43:59 +00:00
|
|
|
|
_fileSystem = fileSystem;
|
|
|
|
|
_cryptographyProvider = cryptographyProvider;
|
2013-04-13 21:09:13 +00:00
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2016-11-08 18:44:23 +00:00
|
|
|
|
var key = new Guid(_cryptographyProvider.ComputeMD5(Encoding.Unicode.GetBytes(featureId)));
|
2016-11-04 08:43:59 +00:00
|
|
|
|
var value = DateTime.UtcNow;
|
2014-01-25 21:11:09 +00:00
|
|
|
|
|
2016-11-04 08:43:59 +00:00
|
|
|
|
SetUpdateRecord(key, value);
|
|
|
|
|
Save();
|
2014-01-25 21:11:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RemoveRegCheck(string featureId)
|
|
|
|
|
{
|
2016-11-08 18:44:23 +00:00
|
|
|
|
var key = new Guid(_cryptographyProvider.ComputeMD5(Encoding.Unicode.GetBytes(featureId)));
|
2016-11-04 08:43:59 +00:00
|
|
|
|
DateTime val;
|
2014-08-31 19:15:33 +00:00
|
|
|
|
|
2016-11-04 08:43:59 +00:00
|
|
|
|
_updateRecords.TryRemove(key, out val);
|
2013-04-13 21:09:13 +00:00
|
|
|
|
|
2016-11-04 08:43:59 +00:00
|
|
|
|
Save();
|
2013-04-13 21:09:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DateTime LastChecked(string featureId)
|
|
|
|
|
{
|
2016-11-04 08:43:59 +00:00
|
|
|
|
DateTime last;
|
2016-11-08 18:44:23 +00:00
|
|
|
|
_updateRecords.TryGetValue(new Guid(_cryptographyProvider.ComputeMD5(Encoding.Unicode.GetBytes(featureId))), out last);
|
2014-08-31 19:15:33 +00:00
|
|
|
|
|
2016-11-04 08:43:59 +00:00
|
|
|
|
// 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
|
|
|
|
|
{
|
2016-11-04 08:43:59 +00:00
|
|
|
|
contents = _fileSystem.ReadAllLines(licenseFile);
|
2013-04-13 21:09:13 +00:00
|
|
|
|
}
|
2016-11-04 08:43:59 +00:00
|
|
|
|
catch (FileNotFoundException)
|
2014-12-23 03:58:14 +00:00
|
|
|
|
{
|
2016-11-04 08:43:59 +00:00
|
|
|
|
lock (_fileLock)
|
|
|
|
|
{
|
|
|
|
|
_fileSystem.WriteAllBytes(licenseFile, new byte[] {});
|
|
|
|
|
}
|
2014-12-23 03:58:14 +00:00
|
|
|
|
}
|
2016-11-04 08:43:59 +00:00
|
|
|
|
catch (IOException)
|
2013-04-13 21:09:13 +00:00
|
|
|
|
{
|
2016-11-04 08:43:59 +00:00
|
|
|
|
lock (_fileLock)
|
|
|
|
|
{
|
|
|
|
|
_fileSystem.WriteAllBytes(licenseFile, new byte[] { });
|
|
|
|
|
}
|
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)
|
|
|
|
|
{
|
2016-11-04 23:57:21 +00:00
|
|
|
|
var line = contents[i];
|
|
|
|
|
if (string.IsNullOrWhiteSpace(line))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2014-08-31 19:15:33 +00:00
|
|
|
|
|
2016-11-04 23:57:21 +00:00
|
|
|
|
Guid feat;
|
|
|
|
|
if (Guid.TryParse(line, out feat))
|
|
|
|
|
{
|
|
|
|
|
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;
|
2016-11-04 08:43:59 +00:00
|
|
|
|
_fileSystem.CreateDirectory(Path.GetDirectoryName(licenseFile));
|
|
|
|
|
lock (_fileLock)
|
|
|
|
|
{
|
|
|
|
|
_fileSystem.WriteAllLines(licenseFile, lines);
|
|
|
|
|
}
|
2013-04-13 21:09:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|