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

175 lines
5 KiB
C#
Raw Normal View History

2016-10-29 07:40:15 +02:00
using System;
2014-08-31 21:15:33 +02:00
using System.Collections.Concurrent;
using System.Collections.Generic;
2014-08-31 21:15:33 +02:00
using System.Globalization;
using System.IO;
2014-08-31 21:15:33 +02:00
using System.Linq;
using System.Text;
2016-10-29 07:40:15 +02:00
using MediaBrowser.Common.Configuration;
2016-11-04 09:43:59 +01:00
using MediaBrowser.Model.Cryptography;
using MediaBrowser.Model.IO;
2016-11-04 09:43:59 +01:00
namespace Emby.Server.Implementations.Security
{
internal class MBLicenseFile
{
private readonly IApplicationPaths _appPaths;
2016-11-04 09:43:59 +01:00
private readonly IFileSystem _fileSystem;
2016-11-08 19:44:23 +01:00
private readonly ICryptoProvider _cryptographyProvider;
public string RegKey
{
get { return _regKey; }
set
{
if (value != _regKey)
{
//if key is changed - clear out our saved validations
2014-08-31 21:15:33 +02:00
_updateRecords.Clear();
_regKey = value;
}
}
}
2013-12-29 18:07:29 +01:00
private string Filename
{
get
{
return Path.Combine(_appPaths.ConfigurationDirectoryPath, "mb.lic");
}
}
2014-08-31 21:15:33 +02:00
private readonly ConcurrentDictionary<Guid, DateTime> _updateRecords = new ConcurrentDictionary<Guid, DateTime>();
private readonly object _fileLock = new object();
private string _regKey;
2016-11-08 19:44:23 +01:00
public MBLicenseFile(IApplicationPaths appPaths, IFileSystem fileSystem, ICryptoProvider cryptographyProvider)
{
_appPaths = appPaths;
2016-11-04 09:43:59 +01:00
_fileSystem = fileSystem;
_cryptographyProvider = cryptographyProvider;
Load();
}
2014-08-31 21:15:33 +02:00
private void SetUpdateRecord(Guid key, DateTime value)
{
_updateRecords.AddOrUpdate(key, value, (k, v) => value);
}
2016-11-14 04:44:54 +01:00
private Guid GetKey(string featureId)
{
return new Guid(_cryptographyProvider.ComputeMD5(Encoding.Unicode.GetBytes(featureId)));
}
public void AddRegCheck(string featureId)
{
2016-11-14 04:44:54 +01:00
var key = GetKey(featureId);
2016-11-04 09:43:59 +01:00
var value = DateTime.UtcNow;
2016-11-04 09:43:59 +01:00
SetUpdateRecord(key, value);
Save();
}
public void RemoveRegCheck(string featureId)
{
2016-11-14 04:44:54 +01:00
var key = GetKey(featureId);
2016-11-04 09:43:59 +01:00
DateTime val;
2014-08-31 21:15:33 +02:00
2016-11-04 09:43:59 +01:00
_updateRecords.TryRemove(key, out val);
2016-11-04 09:43:59 +01:00
Save();
}
public DateTime LastChecked(string featureId)
{
2016-11-14 04:44:54 +01:00
var key = GetKey(featureId);
2016-11-04 09:43:59 +01:00
DateTime last;
2016-11-14 04:44:54 +01:00
_updateRecords.TryGetValue(key, out last);
2014-08-31 21:15:33 +02:00
2016-11-04 09:43:59 +01: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 18:07:29 +01:00
var licenseFile = Filename;
2014-08-31 21:15:33 +02:00
lock (_fileLock)
{
try
{
2016-11-04 09:43:59 +01:00
contents = _fileSystem.ReadAllLines(licenseFile);
}
2016-11-04 09:43:59 +01:00
catch (FileNotFoundException)
2014-12-23 04:58:14 +01:00
{
2016-11-04 09:43:59 +01:00
lock (_fileLock)
{
_fileSystem.WriteAllBytes(licenseFile, new byte[] {});
}
2014-12-23 04:58:14 +01:00
}
2016-11-04 09:43:59 +01:00
catch (IOException)
{
2016-11-04 09:43:59 +01: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 21:15:33 +02:00
//next is legacy key
2014-08-31 21:15:33 +02: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-05 00:57:21 +01:00
var line = contents[i];
if (string.IsNullOrWhiteSpace(line))
{
continue;
}
2014-08-31 21:15:33 +02:00
2016-11-05 00:57:21 +01: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 21:15:33 +02: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 21:15:33 +02:00
lines.Add(pair.Value.Ticks.ToString(CultureInfo.InvariantCulture));
}
2013-12-29 18:07:29 +01:00
var licenseFile = Filename;
2016-11-04 09:43:59 +01:00
_fileSystem.CreateDirectory(Path.GetDirectoryName(licenseFile));
lock (_fileLock)
{
_fileSystem.WriteAllLines(licenseFile, lines);
}
}
}
}