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
|
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
|
_updateRecords.Clear();
|
|
|
|
|
_regKey = value;
|
2013-04-13 21:09:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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();
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
2013-04-13 21:09:13 +00:00
|
|
|
|
{
|
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
|
|
|
|
|
};
|
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-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);
|
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
|
|
|
|
}
|
|
|
|
|
|
2016-12-01 18:23:47 +00:00
|
|
|
|
public FeatureRegInfo GetRegInfo(string featureId)
|
2013-04-13 21:09:13 +00:00
|
|
|
|
{
|
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;
|
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)
|
|
|
|
|
{
|
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)
|
2013-04-13 21:09:13 +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
|
|
|
|
}
|
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))
|
|
|
|
|
{
|
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
|
|
|
|
}
|
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());
|
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-04-13 21:09:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
2013-04-13 21:09:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-04-13 21:09:13 +00:00
|
|
|
|
}
|