MBLicenseFile.cs 3.5 KB

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