jellyfin/MediaBrowser.Common.Implementations/Security/MBLicenseFile.cs

158 lines
4.4 KiB
C#
Raw Normal View History

using MediaBrowser.Common.Configuration;
using System;
2014-08-31 19:15:33 +00:00
using System.Collections.Concurrent;
using System.Collections.Generic;
2014-08-31 19:15:33 +00:00
using System.Globalization;
using System.IO;
2014-08-31 19:15:33 +00:00
using System.Linq;
using System.Security.Cryptography;
using System.Text;
2013-06-10 16:57:30 +00:00
namespace MediaBrowser.Common.Implementations.Security
{
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();
_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();
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);
}
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);
Save();
}
}
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);
Save();
}
}
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;
}
}
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)
{
try
{
2015-09-13 21:32:02 +00:00
contents = _fileSystem.ReadAllLines(licenseFile);
}
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
}
catch (FileNotFoundException)
{
2015-09-13 21:32:02 +00:00
(_fileSystem.CreateFile(licenseFile)).Close();
}
}
if (contents != null && contents.Length > 0)
{
//first line is reg key
RegKey = contents[0];
2014-08-31 19:15:33 +00:00
//next is legacy key
2014-08-31 19:15:33 +00:00
if (contents.Length > 1)
{
// Don't need this anymore
}
//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])));
}
}
}
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())
{
lines.Add(pair.Key.ToString());
2014-08-31 19:15:33 +00:00
lines.Add(pair.Value.Ticks.ToString(CultureInfo.InvariantCulture));
}
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);
}
}
}