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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System;
  2. using System.IO;
  3. using System.Threading;
  4. using System.Web;
  5. using System.Web.Caching;
  6. using Newtonsoft.Json;
  7. using Teknik.Utilities;
  8. using Teknik.Utilities.Cryptography;
  9. namespace Teknik.Configuration
  10. {
  11. public class Config
  12. {
  13. private const string _ConfigCacheKey = "ConfigCache";
  14. private static Config _Config { get; set; }
  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. _JsonSettings = new JsonSerializerSettings();
  98. _JsonSettings.Formatting = Formatting.Indented;
  99. SetDefaults();
  100. }
  101. public void SetDefaults()
  102. {
  103. DevEnvironment = false;
  104. Migrate = false;
  105. UseCdn = false;
  106. Title = string.Empty;
  107. Description = string.Empty;
  108. Author = string.Empty;
  109. Host = string.Empty;
  110. SupportEmail = string.Empty;
  111. NoReplyEmail = string.Empty;
  112. BitcoinAddress = string.Empty;
  113. Salt1 = string.Empty;
  114. Salt2 = string.Empty;
  115. CdnHost = string.Empty;
  116. IPBlacklistFile = string.Empty;
  117. ReferrerBlacklistFile = string.Empty;
  118. UserConfig = new UserConfig();
  119. EmailConfig = new EmailConfig();
  120. ContactConfig = new ContactConfig();
  121. GitConfig = new GitConfig();
  122. BlogConfig = new BlogConfig();
  123. UploadConfig = new UploadConfig();
  124. PasteConfig = new PasteConfig();
  125. ApiConfig = new ApiConfig();
  126. PodcastConfig = new PodcastConfig();
  127. StreamConfig = new StreamConfig();
  128. ShortenerConfig = new ShortenerConfig();
  129. VaultConfig = new VaultConfig();
  130. StatusConfig = new StatusConfig();
  131. LoggingConfig = new LoggingConfig();
  132. PiwikConfig = new PiwikConfig();
  133. IRCConfig = new IRCConfig();
  134. }
  135. public static Config Deserialize(string text)
  136. {
  137. return JsonConvert.DeserializeObject<Config>(text);
  138. }
  139. public static string Serialize(Config config)
  140. {
  141. return JsonConvert.SerializeObject(config, Formatting.Indented);
  142. }
  143. public static Config Load()
  144. {
  145. HttpContext context = HttpContext.Current;
  146. _Config = (Config)context.Cache[_ConfigCacheKey];
  147. if (_Config == null)
  148. {
  149. string path = AppDomain.CurrentDomain.GetData("DataDirectory").ToString();
  150. _Config = Load(path);
  151. context.Cache.Insert(_ConfigCacheKey, _Config, new CacheDependency(path));
  152. }
  153. return _Config;
  154. }
  155. public static Config Load(string path)
  156. {
  157. Config config = new Config();
  158. if (!File.Exists(Path.Combine(path, "Config.json")))
  159. {
  160. Save(Path.Combine(path, "Config.json"), config);
  161. }
  162. else
  163. {
  164. string configContents = File.ReadAllText(Path.Combine(path, "Config.json"));
  165. config = Deserialize(configContents);
  166. }
  167. return config;
  168. }
  169. public static void Save(Config config)
  170. {
  171. string path = AppDomain.CurrentDomain.GetData("DataDirectory").ToString();
  172. Save(Path.Combine(path, "Config.json"), config);
  173. }
  174. public static void Save(string path, Config config)
  175. {
  176. if (!Directory.Exists(Path.GetDirectoryName(path)))
  177. {
  178. Directory.CreateDirectory(Path.GetDirectoryName(path));
  179. }
  180. string configContents = Config.Serialize(config);
  181. File.WriteAllText(path, configContents);
  182. }
  183. }
  184. }