jellyfin-server/Emby.Server.Implementations/Security/MBLicenseFile.cs

169 lines
5.0 KiB
C#
Raw Normal View History

2016-10-29 05:40:15 +00:00
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.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;
2016-11-04 08:43:59 +00:00
namespace Emby.Server.Implementations.Security
{
internal class MBLicenseFile
{
private readonly IApplicationPaths _appPaths;
2016-11-04 08:43:59 +00:00
private readonly IFileSystem _fileSystem;
private readonly ICryptographyProvider _cryptographyProvider;
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;
2016-11-04 08:43:59 +00:00
public MBLicenseFile(IApplicationPaths appPaths, IFileSystem fileSystem, ICryptographyProvider cryptographyProvider)
{
_appPaths = appPaths;
2016-11-04 08:43:59 +00:00
_fileSystem = fileSystem;
_cryptographyProvider = cryptographyProvider;
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)
{
2016-11-04 08:43:59 +00:00
var key = new Guid(_cryptographyProvider.GetMD5Bytes(Encoding.Unicode.GetBytes(featureId)));
var value = DateTime.UtcNow;
2016-11-04 08:43:59 +00:00
SetUpdateRecord(key, value);
Save();
}
public void RemoveRegCheck(string featureId)
{
2016-11-04 08:43:59 +00:00
var key = new Guid(_cryptographyProvider.GetMD5Bytes(Encoding.Unicode.GetBytes(featureId)));
DateTime val;
2014-08-31 19:15:33 +00:00
2016-11-04 08:43:59 +00:00
_updateRecords.TryRemove(key, out val);
2016-11-04 08:43:59 +00:00
Save();
}
public DateTime LastChecked(string featureId)
{
2016-11-04 08:43:59 +00:00
DateTime last;
_updateRecords.TryGetValue(new Guid(_cryptographyProvider.GetMD5Bytes(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;
}
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
{
2016-11-04 08:43:59 +00:00
contents = _fileSystem.ReadAllLines(licenseFile);
}
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)
{
2016-11-04 08:43:59 +00:00
lock (_fileLock)
{
_fileSystem.WriteAllBytes(licenseFile, new byte[] { });
}
}
}
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)
{
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])));
}
}
}
}
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;
2016-11-04 08:43:59 +00:00
_fileSystem.CreateDirectory(Path.GetDirectoryName(licenseFile));
lock (_fileLock)
{
_fileSystem.WriteAllLines(licenseFile, lines);
}
}
}
}