MBLicenseFile.cs 6.1 KB

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