The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

Config.cs 10KB

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