MBLicenseFile.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using MediaBrowser.Common.Configuration;
  9. using MediaBrowser.Model.Cryptography;
  10. using MediaBrowser.Model.IO;
  11. namespace Emby.Server.Implementations.Security
  12. {
  13. internal class MBLicenseFile
  14. {
  15. private readonly IApplicationPaths _appPaths;
  16. private readonly IFileSystem _fileSystem;
  17. private readonly ICryptoProvider _cryptographyProvider;
  18. public string RegKey
  19. {
  20. get { return _regKey; }
  21. set
  22. {
  23. if (value != _regKey)
  24. {
  25. //if key is changed - clear out our saved validations
  26. _updateRecords.Clear();
  27. _regKey = value;
  28. }
  29. }
  30. }
  31. private string Filename
  32. {
  33. get
  34. {
  35. return Path.Combine(_appPaths.ConfigurationDirectoryPath, "mb.lic");
  36. }
  37. }
  38. private readonly ConcurrentDictionary<Guid, FeatureRegInfo> _updateRecords = new ConcurrentDictionary<Guid, FeatureRegInfo>();
  39. private readonly object _fileLock = new object();
  40. private string _regKey;
  41. public MBLicenseFile(IApplicationPaths appPaths, IFileSystem fileSystem, ICryptoProvider cryptographyProvider)
  42. {
  43. _appPaths = appPaths;
  44. _fileSystem = fileSystem;
  45. _cryptographyProvider = cryptographyProvider;
  46. Load();
  47. }
  48. private void SetUpdateRecord(Guid key, FeatureRegInfo value)
  49. {
  50. _updateRecords.AddOrUpdate(key, value, (k, v) => value);
  51. }
  52. private Guid GetKey(string featureId)
  53. {
  54. return new Guid(_cryptographyProvider.ComputeMD5(Encoding.Unicode.GetBytes(featureId)));
  55. }
  56. public void AddRegCheck(string featureId, DateTime expirationDate)
  57. {
  58. var key = GetKey(featureId);
  59. var value = new FeatureRegInfo
  60. {
  61. ExpirationDate = expirationDate,
  62. LastChecked = DateTime.UtcNow
  63. };
  64. SetUpdateRecord(key, value);
  65. Save();
  66. }
  67. public void RemoveRegCheck(string featureId)
  68. {
  69. var key = GetKey(featureId);
  70. FeatureRegInfo val;
  71. _updateRecords.TryRemove(key, out val);
  72. Save();
  73. }
  74. public FeatureRegInfo GetRegInfo(string featureId)
  75. {
  76. var key = GetKey(featureId);
  77. FeatureRegInfo info = null;
  78. _updateRecords.TryGetValue(key, out info);
  79. if (info == null)
  80. {
  81. return null;
  82. }
  83. // guard agains people just putting a large number in the file
  84. return info.LastChecked < DateTime.UtcNow ? info : null;
  85. }
  86. private void Load()
  87. {
  88. string[] contents = null;
  89. var licenseFile = Filename;
  90. lock (_fileLock)
  91. {
  92. try
  93. {
  94. contents = _fileSystem.ReadAllLines(licenseFile);
  95. }
  96. catch (FileNotFoundException)
  97. {
  98. lock (_fileLock)
  99. {
  100. _fileSystem.WriteAllBytes(licenseFile, new byte[] { });
  101. }
  102. }
  103. catch (IOException)
  104. {
  105. lock (_fileLock)
  106. {
  107. _fileSystem.WriteAllBytes(licenseFile, new byte[] { });
  108. }
  109. }
  110. }
  111. if (contents != null && contents.Length > 0)
  112. {
  113. //first line is reg key
  114. RegKey = contents[0];
  115. //next is legacy key
  116. if (contents.Length > 1)
  117. {
  118. // Don't need this anymore
  119. }
  120. //the rest of the lines should be pairs of features and timestamps
  121. for (var i = 2; i < contents.Length; i = i + 2)
  122. {
  123. var line = contents[i];
  124. if (string.IsNullOrWhiteSpace(line))
  125. {
  126. continue;
  127. }
  128. Guid feat;
  129. if (Guid.TryParse(line, out feat))
  130. {
  131. var lineParts = contents[i + 1].Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
  132. long ticks;
  133. if (long.TryParse(lineParts[0], out ticks))
  134. {
  135. var info = new FeatureRegInfo
  136. {
  137. LastChecked = new DateTime(ticks)
  138. };
  139. if (lineParts.Length > 1 && long.TryParse(lineParts[1], out ticks))
  140. {
  141. info.ExpirationDate = new DateTime(ticks);
  142. }
  143. SetUpdateRecord(feat, info);
  144. }
  145. }
  146. }
  147. }
  148. }
  149. public void Save()
  150. {
  151. //build our array
  152. var lines = new List<string>
  153. {
  154. RegKey,
  155. // Legacy key
  156. string.Empty
  157. };
  158. foreach (var pair in _updateRecords
  159. .ToList())
  160. {
  161. lines.Add(pair.Key.ToString());
  162. var dateLine = pair.Value.LastChecked.Ticks.ToString(CultureInfo.InvariantCulture) + "|" +
  163. pair.Value.ExpirationDate.Ticks.ToString(CultureInfo.InvariantCulture);
  164. lines.Add(dateLine);
  165. }
  166. var licenseFile = Filename;
  167. _fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(licenseFile));
  168. lock (_fileLock)
  169. {
  170. _fileSystem.WriteAllLines(licenseFile, lines);
  171. }
  172. }
  173. }
  174. internal class FeatureRegInfo
  175. {
  176. public DateTime ExpirationDate { get; set; }
  177. public DateTime LastChecked { get; set; }
  178. public FeatureRegInfo()
  179. {
  180. ExpirationDate = DateTime.MinValue;
  181. }
  182. }
  183. }