MBLicenseFile.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using MediaBrowser.Common.Configuration;
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Security.Cryptography;
  9. using System.Text;
  10. namespace MediaBrowser.Common.Implementations.Security
  11. {
  12. internal class MBLicenseFile
  13. {
  14. private readonly IApplicationPaths _appPaths;
  15. public string RegKey
  16. {
  17. get { return _regKey; }
  18. set
  19. {
  20. if (value != _regKey)
  21. {
  22. //if key is changed - clear out our saved validations
  23. _updateRecords.Clear();
  24. _regKey = value;
  25. }
  26. }
  27. }
  28. private string Filename
  29. {
  30. get
  31. {
  32. return Path.Combine(_appPaths.ConfigurationDirectoryPath, "mb.lic");
  33. }
  34. }
  35. private readonly ConcurrentDictionary<Guid, DateTime> _updateRecords = new ConcurrentDictionary<Guid, DateTime>();
  36. private readonly object _fileLock = new object();
  37. private string _regKey;
  38. public MBLicenseFile(IApplicationPaths appPaths)
  39. {
  40. _appPaths = appPaths;
  41. Load();
  42. }
  43. private void SetUpdateRecord(Guid key, DateTime value)
  44. {
  45. _updateRecords.AddOrUpdate(key, value, (k, v) => value);
  46. }
  47. public void AddRegCheck(string featureId)
  48. {
  49. using (var provider = new MD5CryptoServiceProvider())
  50. {
  51. var key = new Guid(provider.ComputeHash(Encoding.Unicode.GetBytes(featureId)));
  52. var value = DateTime.UtcNow;
  53. SetUpdateRecord(key, value);
  54. Save();
  55. }
  56. }
  57. public void RemoveRegCheck(string featureId)
  58. {
  59. using (var provider = new MD5CryptoServiceProvider())
  60. {
  61. var key = new Guid(provider.ComputeHash(Encoding.Unicode.GetBytes(featureId)));
  62. DateTime val;
  63. _updateRecords.TryRemove(key, out val);
  64. Save();
  65. }
  66. }
  67. public DateTime LastChecked(string featureId)
  68. {
  69. using (var provider = new MD5CryptoServiceProvider())
  70. {
  71. DateTime last;
  72. _updateRecords.TryGetValue(new Guid(provider.ComputeHash(Encoding.Unicode.GetBytes(featureId))), out last);
  73. // guard agains people just putting a large number in the file
  74. return last < DateTime.UtcNow ? last : DateTime.MinValue;
  75. }
  76. }
  77. private void Load()
  78. {
  79. string[] contents = null;
  80. var licenseFile = Filename;
  81. lock (_fileLock)
  82. {
  83. try
  84. {
  85. contents = File.ReadAllLines(licenseFile);
  86. }
  87. catch (DirectoryNotFoundException)
  88. {
  89. File.Create(licenseFile).Close();
  90. }
  91. catch (FileNotFoundException)
  92. {
  93. File.Create(licenseFile).Close();
  94. }
  95. }
  96. if (contents != null && contents.Length > 0)
  97. {
  98. //first line is reg key
  99. RegKey = contents[0];
  100. //next is legacy key
  101. if (contents.Length > 1)
  102. {
  103. // Don't need this anymore
  104. }
  105. //the rest of the lines should be pairs of features and timestamps
  106. for (var i = 2; i < contents.Length; i = i + 2)
  107. {
  108. var feat = Guid.Parse(contents[i]);
  109. SetUpdateRecord(feat, new DateTime(Convert.ToInt64(contents[i + 1])));
  110. }
  111. }
  112. }
  113. public void Save()
  114. {
  115. //build our array
  116. var lines = new List<string>
  117. {
  118. RegKey,
  119. // Legacy key
  120. string.Empty
  121. };
  122. foreach (var pair in _updateRecords
  123. .ToList())
  124. {
  125. lines.Add(pair.Key.ToString());
  126. lines.Add(pair.Value.Ticks.ToString(CultureInfo.InvariantCulture));
  127. }
  128. var licenseFile = Filename;
  129. Directory.CreateDirectory(Path.GetDirectoryName(licenseFile));
  130. lock (_fileLock) File.WriteAllLines(licenseFile, lines);
  131. }
  132. }
  133. }