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 10KB

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