The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Config.cs 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.IO;
  4. using System.Threading;
  5. using Teknik.Utilities.Cryptography;
  6. namespace Teknik.Configuration
  7. {
  8. public class Config
  9. {
  10. private const string _ConfigCacheKey = "ConfigCache";
  11. private const string _ConfigFileName = "Config.json";
  12. private static Config _Config { get; set; }
  13. private static string _FileHash { get; set; }
  14. private ReaderWriterLockSlim _ConfigRWLock;
  15. private ReaderWriterLockSlim _ConfigFileRWLock;
  16. private JsonSerializerSettings _JsonSettings;
  17. private bool _DevEnvironment;
  18. private bool _Migrate;
  19. private bool _UseCdn;
  20. private string _Title;
  21. private string _Description;
  22. private string _Author;
  23. private string _Host;
  24. private string _SupportEmail;
  25. private string _NoReplyEmail;
  26. private string _BitcoinAddress;
  27. private string _Salt1;
  28. private string _Salt2;
  29. private string _CdnHost;
  30. private string _IPBlacklistFile;
  31. private string _ReferrerBlacklistFile;
  32. private UserConfig _UserConfig;
  33. private ContactConfig _ContactConfig;
  34. private EmailConfig _EmailConfig;
  35. private GitConfig _GitConfig;
  36. private UploadConfig _UploadConfig;
  37. private PasteConfig _PasteConfig;
  38. private BlogConfig _BlogConfig;
  39. private ApiConfig _ApiConfig;
  40. private PodcastConfig _PodcastConfig;
  41. private StreamConfig _StreamConfig;
  42. private ShortenerConfig _ShortenerConfig;
  43. private VaultConfig _VaultConfig;
  44. private StatsConfig _StatsConfig;
  45. private LoggingConfig _LoggingConfig;
  46. private PiwikConfig _PiwikConfig;
  47. private IRCConfig _IRCConfig;
  48. public bool DevEnvironment { get { return _DevEnvironment; } set { _DevEnvironment = value; } }
  49. public bool Migrate { get { return _Migrate; } set { _Migrate = value; } }
  50. public bool UseCdn { get { return _UseCdn; } set { _UseCdn = value; } }
  51. // Site Information
  52. public string Title { get { return _Title; } set { _Title = value; } }
  53. public string Description { get { return _Description; } set { _Description = value; } }
  54. public string Author { get { return _Author; } set { _Author = value; } }
  55. public string Host { get { return _Host; } set { _Host = value; } }
  56. public string SupportEmail { get { return _SupportEmail; } set { _SupportEmail = value; } }
  57. public string NoReplyEmail { get { return _NoReplyEmail; } set { _NoReplyEmail = value; } }
  58. public string BitcoinAddress { get { return _BitcoinAddress; } set { _BitcoinAddress = value; } }
  59. public string Salt1 { get { return _Salt1; } set { _Salt1 = value; } }
  60. public string Salt2 { get { return _Salt2; } set { _Salt2 = value; } }
  61. public string CdnHost { get { return _CdnHost; } set { _CdnHost = value; } }
  62. public string IPBlacklistFile { get { return _IPBlacklistFile;} set { _IPBlacklistFile = value; }}
  63. public string ReferrerBlacklistFile { get { return _ReferrerBlacklistFile;} set { _ReferrerBlacklistFile = value; }}
  64. // User Configuration
  65. public UserConfig UserConfig { get { return _UserConfig; } set { _UserConfig = value; } }
  66. // Contact Configuration
  67. public ContactConfig ContactConfig { get { return _ContactConfig; } set { _ContactConfig = value; } }
  68. // Mail Server Configuration
  69. public EmailConfig EmailConfig { get { return _EmailConfig; } set { _EmailConfig = value; } }
  70. // Git Service Configuration
  71. public GitConfig GitConfig { get { return _GitConfig; } set { _GitConfig = value; } }
  72. // Blog Configuration
  73. public BlogConfig BlogConfig { get { return _BlogConfig; } set { _BlogConfig = value; } }
  74. // Upload Configuration
  75. public UploadConfig UploadConfig { get { return _UploadConfig; } set { _UploadConfig = value; } }
  76. // Paste Configuration
  77. public PasteConfig PasteConfig { get { return _PasteConfig; } set { _PasteConfig = value; } }
  78. // API Configuration
  79. public ApiConfig ApiConfig { get { return _ApiConfig; } set { _ApiConfig = value; } }
  80. // Podcast Configuration
  81. public PodcastConfig PodcastConfig { get { return _PodcastConfig; } set { _PodcastConfig = value; } }
  82. // Stream Configuration
  83. public StreamConfig StreamConfig { get { return _StreamConfig; } set { _StreamConfig = value; } }
  84. // Shortener Configuration
  85. public ShortenerConfig ShortenerConfig { get { return _ShortenerConfig; } set { _ShortenerConfig = value; } }
  86. // Vault Configuration
  87. public VaultConfig VaultConfig { get { return _VaultConfig; } set { _VaultConfig = value; } }
  88. // Status Configuration
  89. public StatsConfig StatsConfig { get { return _StatsConfig; } set { _StatsConfig = value; } }
  90. // Logging Configuration
  91. public LoggingConfig LoggingConfig { get { return _LoggingConfig; } set { _LoggingConfig = value; } }
  92. // Piwik Configuration
  93. public PiwikConfig PiwikConfig { get { return _PiwikConfig; } set { _PiwikConfig = value; } }
  94. // Piwik Configuration
  95. public IRCConfig IRCConfig { get { return _IRCConfig; } set { _IRCConfig = value; } }
  96. public Config()
  97. {
  98. _ConfigRWLock = new ReaderWriterLockSlim();
  99. _ConfigFileRWLock = new ReaderWriterLockSlim();
  100. _JsonSettings = new JsonSerializerSettings();
  101. _JsonSettings.Formatting = Formatting.Indented;
  102. SetDefaults();
  103. }
  104. public void SetDefaults()
  105. {
  106. DevEnvironment = false;
  107. Migrate = false;
  108. UseCdn = false;
  109. Title = string.Empty;
  110. Description = string.Empty;
  111. Author = string.Empty;
  112. Host = string.Empty;
  113. SupportEmail = string.Empty;
  114. NoReplyEmail = string.Empty;
  115. BitcoinAddress = string.Empty;
  116. Salt1 = string.Empty;
  117. Salt2 = string.Empty;
  118. CdnHost = string.Empty;
  119. IPBlacklistFile = string.Empty;
  120. ReferrerBlacklistFile = string.Empty;
  121. UserConfig = new UserConfig();
  122. EmailConfig = new EmailConfig();
  123. ContactConfig = new ContactConfig();
  124. GitConfig = new GitConfig();
  125. BlogConfig = new BlogConfig();
  126. UploadConfig = new UploadConfig();
  127. PasteConfig = new PasteConfig();
  128. ApiConfig = new ApiConfig();
  129. PodcastConfig = new PodcastConfig();
  130. StreamConfig = new StreamConfig();
  131. ShortenerConfig = new ShortenerConfig();
  132. VaultConfig = new VaultConfig();
  133. StatsConfig = new StatsConfig();
  134. LoggingConfig = new LoggingConfig();
  135. PiwikConfig = new PiwikConfig();
  136. IRCConfig = new IRCConfig();
  137. }
  138. public static Config Deserialize(string text)
  139. {
  140. return JsonConvert.DeserializeObject<Config>(text);
  141. }
  142. public static string Serialize(Config config)
  143. {
  144. return JsonConvert.SerializeObject(config, Formatting.Indented);
  145. }
  146. public static Config Load(string path)
  147. {
  148. string newHash = string.Empty;
  149. string fullPath = Path.Combine(path, _ConfigFileName);
  150. if (!File.Exists(fullPath))
  151. {
  152. Config config = new Config();
  153. Save(fullPath, config);
  154. }
  155. newHash = MD5.FileHash(fullPath);
  156. if (_Config == null || _FileHash == null || newHash != _FileHash)
  157. {
  158. string configContents = File.ReadAllText(fullPath);
  159. _Config = Deserialize(configContents);
  160. _FileHash = newHash;
  161. }
  162. return _Config;
  163. }
  164. public static void Save(string path, Config config)
  165. {
  166. if (!Directory.Exists(Path.GetDirectoryName(path)))
  167. {
  168. Directory.CreateDirectory(Path.GetDirectoryName(path));
  169. }
  170. string configContents = Config.Serialize(config);
  171. File.WriteAllText(path, configContents);
  172. }
  173. }
  174. }