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

211 lines
6.2 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;
2016-11-08 18:44:23 +00:00
private readonly ICryptoProvider _cryptographyProvider;
public string RegKey
{
get { return _regKey; }
set
{
2018-09-12 17:26:21 +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");
}
}
2016-12-01 18:23:47 +00:00
private readonly ConcurrentDictionary<Guid, FeatureRegInfo> _updateRecords = new ConcurrentDictionary<Guid, FeatureRegInfo>();
2014-08-31 19:15:33 +00:00
private readonly object _fileLock = new object();
private string _regKey;
2016-11-08 18:44:23 +00:00
public MBLicenseFile(IApplicationPaths appPaths, IFileSystem fileSystem, ICryptoProvider cryptographyProvider)
{
_appPaths = appPaths;
2016-11-04 08:43:59 +00:00
_fileSystem = fileSystem;
_cryptographyProvider = cryptographyProvider;
Load();
}
2016-12-01 18:23:47 +00:00
private void SetUpdateRecord(Guid key, FeatureRegInfo value)
2014-08-31 19:15:33 +00:00
{
_updateRecords.AddOrUpdate(key, value, (k, v) => value);
}
2016-11-14 03:44:54 +00:00
private Guid GetKey(string featureId)
{
return new Guid(_cryptographyProvider.ComputeMD5(Encoding.Unicode.GetBytes(featureId)));
}
2016-12-01 18:23:47 +00:00
public void AddRegCheck(string featureId, DateTime expirationDate)
{
2016-11-14 03:44:54 +00:00
var key = GetKey(featureId);
2016-12-01 18:23:47 +00:00
var value = new FeatureRegInfo
{
ExpirationDate = expirationDate,
LastChecked = DateTime.UtcNow
};
2016-11-04 08:43:59 +00:00
SetUpdateRecord(key, value);
Save();
}
public void RemoveRegCheck(string featureId)
{
2016-11-14 03:44:54 +00:00
var key = GetKey(featureId);
2016-12-01 18:23:47 +00:00
FeatureRegInfo 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();
}
2016-12-01 18:23:47 +00:00
public FeatureRegInfo GetRegInfo(string featureId)
{
2016-11-14 03:44:54 +00:00
var key = GetKey(featureId);
2016-12-01 18:23:47 +00:00
FeatureRegInfo info = null;
_updateRecords.TryGetValue(key, out info);
if (info == null)
{
return null;
}
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
2016-12-01 18:23:47 +00:00
return info.LastChecked < DateTime.UtcNow ? info : null;
}
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)
{
2018-09-12 17:26:21 +00:00
_fileSystem.WriteAllBytes(licenseFile, Array.Empty<byte>());
2016-11-04 08:43:59 +00:00
}
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)
{
2018-09-12 17:26:21 +00:00
_fileSystem.WriteAllBytes(licenseFile, Array.Empty<byte>());
2016-11-04 08:43:59 +00:00
}
}
}
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))
{
2016-12-01 18:23:47 +00:00
var lineParts = contents[i + 1].Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
long ticks;
if (long.TryParse(lineParts[0], out ticks))
{
var info = new FeatureRegInfo
{
LastChecked = new DateTime(ticks)
};
if (lineParts.Length > 1 && long.TryParse(lineParts[1], out ticks))
{
info.ExpirationDate = new DateTime(ticks);
}
SetUpdateRecord(feat, info);
}
2016-11-04 23:57:21 +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())
{
lines.Add(pair.Key.ToString());
2016-12-01 18:23:47 +00:00
var dateLine = pair.Value.LastChecked.Ticks.ToString(CultureInfo.InvariantCulture) + "|" +
pair.Value.ExpirationDate.Ticks.ToString(CultureInfo.InvariantCulture);
lines.Add(dateLine);
}
2013-12-29 17:07:29 +00:00
var licenseFile = Filename;
2017-05-04 18:14:45 +00:00
_fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(licenseFile));
2016-11-04 08:43:59 +00:00
lock (_fileLock)
{
_fileSystem.WriteAllLines(licenseFile, lines);
}
}
}
2016-12-01 18:23:47 +00:00
internal class FeatureRegInfo
{
public DateTime ExpirationDate { get; set; }
public DateTime LastChecked { get; set; }
public FeatureRegInfo()
{
ExpirationDate = DateTime.MinValue;
}
}
}