blob: 163a368bfb438a4a14361fe916608443c1c0ec07 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
using MediaBrowser.Common.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace MediaBrowser.Common.Implementations.Security
{
internal class MBLicenseFile
{
private readonly IApplicationPaths _appPaths;
private readonly string _filename;
public string RegKey
{
get { return _regKey; }
set
{
if (value != _regKey)
{
//if key is changed - clear out our saved validations
UpdateRecords.Clear();
_regKey = value;
}
}
}
public string LegacyKey { get; set; }
private Dictionary<Guid, DateTime> UpdateRecords { get; set; }
private readonly object _lck = new object();
private string _regKey;
public MBLicenseFile(IApplicationPaths appPaths)
{
_appPaths = appPaths;
_filename = Path.Combine(_appPaths.ConfigurationDirectoryPath, "mb.lic");
UpdateRecords = new Dictionary<Guid, DateTime>();
Load();
}
public void AddRegCheck(string featureId)
{
using (var provider = new MD5CryptoServiceProvider())
{
UpdateRecords[new Guid(provider.ComputeHash(Encoding.Unicode.GetBytes(featureId)))] = DateTime.UtcNow;
Save();
}
}
public DateTime LastChecked(string featureId)
{
using (var provider = new MD5CryptoServiceProvider())
{
DateTime last;
lock(_lck) UpdateRecords.TryGetValue(new Guid(provider.ComputeHash(Encoding.Unicode.GetBytes(featureId))), out last);
return last < DateTime.UtcNow ? last : DateTime.MinValue; // guard agains people just putting a large number in the file
}
}
private void Load()
{
string[] contents = null;
lock (_lck)
{
try
{
contents = File.ReadAllLines(_filename);
}
catch (FileNotFoundException)
{
(File.Create(_filename)).Close();
}
}
if (contents != null && contents.Length > 0)
{
//first line is reg key
RegKey = contents[0];
//next is legacy key
if (contents.Length > 1) LegacyKey = contents[1];
//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]);
UpdateRecords[feat] = new DateTime(Convert.ToInt64(contents[i + 1]));
}
}
}
public void Save()
{
//build our array
var lines = new List<string> {RegKey, LegacyKey};
foreach (var pair in UpdateRecords)
{
lines.Add(pair.Key.ToString());
lines.Add(pair.Value.Ticks.ToString());
}
lock(_lck) File.WriteAllLines(_filename, lines);
}
}
}
|