MBLicenseFile.cs 3.3 KB

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