2
0

MBLicenseFile.cs 6.2 KB

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