2018-06-15 02:57:03 +02:00
|
|
|
using Newtonsoft.Json;
|
2018-01-28 05:23:56 +01:00
|
|
|
using System;
|
2018-06-19 04:07:03 +02:00
|
|
|
using System.Collections.Generic;
|
2015-11-23 07:55:24 +01:00
|
|
|
using System.IO;
|
2015-07-16 23:54:53 +02:00
|
|
|
using System.Threading;
|
2017-04-15 08:28:34 +02:00
|
|
|
using Teknik.Utilities.Cryptography;
|
2015-07-16 23:54:53 +02:00
|
|
|
|
2015-12-21 09:58:09 +01:00
|
|
|
namespace Teknik.Configuration
|
2015-07-16 23:54:53 +02:00
|
|
|
{
|
|
|
|
public class Config
|
|
|
|
{
|
2018-01-28 21:49:37 +01:00
|
|
|
private const string _ConfigCacheKey = "ConfigCache";
|
2018-06-15 02:57:03 +02:00
|
|
|
private const string _ConfigFileName = "Config.json";
|
2016-10-08 01:49:46 +02:00
|
|
|
|
2018-01-28 21:49:37 +01:00
|
|
|
private static Config _Config { get; set; }
|
2018-06-15 02:57:03 +02:00
|
|
|
private static string _FileHash { get; set; }
|
|
|
|
|
|
|
|
private ReaderWriterLockSlim _ConfigRWLock;
|
|
|
|
private ReaderWriterLockSlim _ConfigFileRWLock;
|
2015-07-16 23:54:53 +02:00
|
|
|
private JsonSerializerSettings _JsonSettings;
|
|
|
|
|
2018-10-26 07:22:53 +02:00
|
|
|
private bool _DevEnvironment;
|
|
|
|
private bool _Migrate;
|
|
|
|
private bool _UseCdn;
|
2019-01-21 09:29:10 +01:00
|
|
|
private string _DbConnection;
|
2018-10-26 07:22:53 +02:00
|
|
|
private string _Title;
|
|
|
|
private string _Description;
|
|
|
|
private string _Author;
|
|
|
|
private string _Host;
|
|
|
|
private string _SupportEmail;
|
|
|
|
private string _NoReplyEmail;
|
|
|
|
private string _BitcoinAddress;
|
|
|
|
private string _Salt1;
|
|
|
|
private string _Salt2;
|
|
|
|
private string _CdnHost;
|
|
|
|
private string _IPBlacklistFile;
|
|
|
|
private string _ReferrerBlacklistFile;
|
|
|
|
private List<string> _PublicKeys;
|
|
|
|
private UserConfig _UserConfig;
|
|
|
|
private ContactConfig _ContactConfig;
|
|
|
|
private EmailConfig _EmailConfig;
|
|
|
|
private GitConfig _GitConfig;
|
|
|
|
private UploadConfig _UploadConfig;
|
|
|
|
private PasteConfig _PasteConfig;
|
|
|
|
private BlogConfig _BlogConfig;
|
|
|
|
private ApiConfig _ApiConfig;
|
|
|
|
private PodcastConfig _PodcastConfig;
|
|
|
|
private StreamConfig _StreamConfig;
|
|
|
|
private ShortenerConfig _ShortenerConfig;
|
|
|
|
private VaultConfig _VaultConfig;
|
|
|
|
private StatsConfig _StatsConfig;
|
|
|
|
private LoggingConfig _LoggingConfig;
|
|
|
|
private PiwikConfig _PiwikConfig;
|
|
|
|
private IRCConfig _IRCConfig;
|
2015-07-16 23:54:53 +02:00
|
|
|
|
2018-01-28 05:23:56 +01:00
|
|
|
public bool DevEnvironment { get { return _DevEnvironment; } set { _DevEnvironment = value; } }
|
|
|
|
public bool Migrate { get { return _Migrate; } set { _Migrate = value; } }
|
|
|
|
public bool UseCdn { get { return _UseCdn; } set { _UseCdn = value; } }
|
2015-12-16 21:46:54 +01:00
|
|
|
|
2019-01-21 09:29:10 +01:00
|
|
|
public string DbConnection { get { return _DbConnection; } set { _DbConnection = value; } }
|
|
|
|
|
2015-12-21 05:56:06 +01:00
|
|
|
// Site Information
|
2018-01-28 05:23:56 +01:00
|
|
|
public string Title { get { return _Title; } set { _Title = value; } }
|
|
|
|
public string Description { get { return _Description; } set { _Description = value; } }
|
|
|
|
public string Author { get { return _Author; } set { _Author = value; } }
|
|
|
|
public string Host { get { return _Host; } set { _Host = value; } }
|
|
|
|
public string SupportEmail { get { return _SupportEmail; } set { _SupportEmail = value; } }
|
|
|
|
public string NoReplyEmail { get { return _NoReplyEmail; } set { _NoReplyEmail = value; } }
|
|
|
|
public string BitcoinAddress { get { return _BitcoinAddress; } set { _BitcoinAddress = value; } }
|
|
|
|
public string Salt1 { get { return _Salt1; } set { _Salt1 = value; } }
|
|
|
|
public string Salt2 { get { return _Salt2; } set { _Salt2 = value; } }
|
|
|
|
public string CdnHost { get { return _CdnHost; } set { _CdnHost = value; } }
|
|
|
|
public string IPBlacklistFile { get { return _IPBlacklistFile;} set { _IPBlacklistFile = value; }}
|
|
|
|
public string ReferrerBlacklistFile { get { return _ReferrerBlacklistFile;} set { _ReferrerBlacklistFile = value; }}
|
2016-01-29 00:24:12 +01:00
|
|
|
|
2018-06-19 04:07:03 +02:00
|
|
|
public List<string> PublicKeys { get { return _PublicKeys; } set { _PublicKeys = value; } }
|
|
|
|
|
2016-01-29 00:24:12 +01:00
|
|
|
// User Configuration
|
2018-10-26 07:22:53 +02:00
|
|
|
public UserConfig UserConfig { get { return _UserConfig; } set { _UserConfig = value; } }
|
2015-12-16 21:46:54 +01:00
|
|
|
|
2016-01-28 08:43:41 +01:00
|
|
|
// Contact Configuration
|
2018-10-26 07:22:53 +02:00
|
|
|
public ContactConfig ContactConfig { get { return _ContactConfig; } set { _ContactConfig = value; } }
|
2016-01-28 08:43:41 +01:00
|
|
|
|
2016-01-25 07:31:01 +01:00
|
|
|
// Mail Server Configuration
|
2018-10-26 07:22:53 +02:00
|
|
|
public EmailConfig EmailConfig { get { return _EmailConfig; } set { _EmailConfig = value; } }
|
2015-12-16 21:46:54 +01:00
|
|
|
|
2016-01-29 00:24:12 +01:00
|
|
|
// Git Service Configuration
|
2018-10-26 07:22:53 +02:00
|
|
|
public GitConfig GitConfig { get { return _GitConfig; } set { _GitConfig = value; } }
|
2015-07-16 23:54:53 +02:00
|
|
|
|
2016-01-25 07:31:01 +01:00
|
|
|
// Blog Configuration
|
2018-10-26 07:22:53 +02:00
|
|
|
public BlogConfig BlogConfig { get { return _BlogConfig; } set { _BlogConfig = value; } }
|
2015-12-21 09:58:09 +01:00
|
|
|
|
|
|
|
// Upload Configuration
|
2018-10-26 07:22:53 +02:00
|
|
|
public UploadConfig UploadConfig { get { return _UploadConfig; } set { _UploadConfig = value; } }
|
2015-12-17 23:14:11 +01:00
|
|
|
|
2016-01-23 01:44:07 +01:00
|
|
|
// Paste Configuration
|
2018-10-26 07:22:53 +02:00
|
|
|
public PasteConfig PasteConfig { get { return _PasteConfig; } set { _PasteConfig = value; } }
|
2016-01-23 01:44:07 +01:00
|
|
|
|
2016-01-25 07:31:01 +01:00
|
|
|
// API Configuration
|
2018-10-26 07:22:53 +02:00
|
|
|
public ApiConfig ApiConfig { get { return _ApiConfig; } set { _ApiConfig = value; } }
|
2016-01-25 07:31:01 +01:00
|
|
|
|
|
|
|
// Podcast Configuration
|
2018-10-26 07:22:53 +02:00
|
|
|
public PodcastConfig PodcastConfig { get { return _PodcastConfig; } set { _PodcastConfig = value; } }
|
2016-01-21 23:47:07 +01:00
|
|
|
|
2016-02-03 00:02:45 +01:00
|
|
|
// Stream Configuration
|
2018-10-26 07:22:53 +02:00
|
|
|
public StreamConfig StreamConfig { get { return _StreamConfig; } set { _StreamConfig = value; } }
|
2016-02-03 00:02:45 +01:00
|
|
|
|
2016-02-22 08:42:38 +01:00
|
|
|
// Shortener Configuration
|
2018-10-26 07:22:53 +02:00
|
|
|
public ShortenerConfig ShortenerConfig { get { return _ShortenerConfig; } set { _ShortenerConfig = value; } }
|
2016-05-14 04:10:30 +02:00
|
|
|
|
2017-02-18 01:54:27 +01:00
|
|
|
// Vault Configuration
|
2018-10-26 07:22:53 +02:00
|
|
|
public VaultConfig VaultConfig { get { return _VaultConfig; } set { _VaultConfig = value; } }
|
2017-02-14 02:38:33 +01:00
|
|
|
|
2017-02-18 01:54:27 +01:00
|
|
|
// Status Configuration
|
2018-10-26 07:22:53 +02:00
|
|
|
public StatsConfig StatsConfig { get { return _StatsConfig; } set { _StatsConfig = value; } }
|
2017-02-18 01:54:27 +01:00
|
|
|
|
2017-01-11 08:39:15 +01:00
|
|
|
// Logging Configuration
|
2018-10-26 07:22:53 +02:00
|
|
|
public LoggingConfig LoggingConfig { get { return _LoggingConfig; } set { _LoggingConfig = value; } }
|
2017-01-11 08:39:15 +01:00
|
|
|
|
2016-02-26 01:41:31 +01:00
|
|
|
// Piwik Configuration
|
2018-10-26 07:22:53 +02:00
|
|
|
public PiwikConfig PiwikConfig { get { return _PiwikConfig; } set { _PiwikConfig = value; } }
|
2016-02-26 01:41:31 +01:00
|
|
|
|
2017-03-25 08:49:39 +01:00
|
|
|
// Piwik Configuration
|
2018-10-26 07:22:53 +02:00
|
|
|
public IRCConfig IRCConfig { get { return _IRCConfig; } set { _IRCConfig = value; } }
|
2017-03-25 08:49:39 +01:00
|
|
|
|
2015-07-16 23:54:53 +02:00
|
|
|
public Config()
|
|
|
|
{
|
2018-06-15 02:57:03 +02:00
|
|
|
_ConfigRWLock = new ReaderWriterLockSlim();
|
|
|
|
_ConfigFileRWLock = new ReaderWriterLockSlim();
|
2015-07-16 23:54:53 +02:00
|
|
|
_JsonSettings = new JsonSerializerSettings();
|
|
|
|
_JsonSettings.Formatting = Formatting.Indented;
|
|
|
|
|
|
|
|
SetDefaults();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetDefaults()
|
|
|
|
{
|
2018-01-28 05:23:56 +01:00
|
|
|
DevEnvironment = false;
|
|
|
|
Migrate = false;
|
|
|
|
UseCdn = false;
|
|
|
|
Title = string.Empty;
|
|
|
|
Description = string.Empty;
|
|
|
|
Author = string.Empty;
|
|
|
|
Host = string.Empty;
|
|
|
|
SupportEmail = string.Empty;
|
|
|
|
NoReplyEmail = string.Empty;
|
|
|
|
BitcoinAddress = string.Empty;
|
|
|
|
Salt1 = string.Empty;
|
|
|
|
Salt2 = string.Empty;
|
|
|
|
CdnHost = string.Empty;
|
|
|
|
IPBlacklistFile = string.Empty;
|
|
|
|
ReferrerBlacklistFile = string.Empty;
|
2018-06-19 04:07:03 +02:00
|
|
|
PublicKeys = new List<string>();
|
2018-01-28 05:23:56 +01:00
|
|
|
UserConfig = new UserConfig();
|
|
|
|
EmailConfig = new EmailConfig();
|
|
|
|
ContactConfig = new ContactConfig();
|
|
|
|
GitConfig = new GitConfig();
|
|
|
|
BlogConfig = new BlogConfig();
|
|
|
|
UploadConfig = new UploadConfig();
|
|
|
|
PasteConfig = new PasteConfig();
|
|
|
|
ApiConfig = new ApiConfig();
|
|
|
|
PodcastConfig = new PodcastConfig();
|
|
|
|
StreamConfig = new StreamConfig();
|
|
|
|
ShortenerConfig = new ShortenerConfig();
|
|
|
|
VaultConfig = new VaultConfig();
|
2018-05-11 05:27:54 +02:00
|
|
|
StatsConfig = new StatsConfig();
|
2018-01-28 05:23:56 +01:00
|
|
|
LoggingConfig = new LoggingConfig();
|
|
|
|
PiwikConfig = new PiwikConfig();
|
|
|
|
IRCConfig = new IRCConfig();
|
2015-07-16 23:54:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static Config Deserialize(string text)
|
|
|
|
{
|
|
|
|
return JsonConvert.DeserializeObject<Config>(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static string Serialize(Config config)
|
|
|
|
{
|
2015-12-24 07:36:33 +01:00
|
|
|
return JsonConvert.SerializeObject(config, Formatting.Indented);
|
2015-07-16 23:54:53 +02:00
|
|
|
}
|
2015-11-23 07:55:24 +01:00
|
|
|
|
2016-05-09 22:01:41 +02:00
|
|
|
public static Config Load(string path)
|
|
|
|
{
|
2018-06-15 02:57:03 +02:00
|
|
|
string newHash = string.Empty;
|
|
|
|
string fullPath = Path.Combine(path, _ConfigFileName);
|
|
|
|
|
|
|
|
if (!File.Exists(fullPath))
|
2015-12-24 07:36:33 +01:00
|
|
|
{
|
2018-06-15 02:57:03 +02:00
|
|
|
Config config = new Config();
|
|
|
|
Save(fullPath, config);
|
2015-12-24 07:36:33 +01:00
|
|
|
}
|
2018-06-15 02:57:03 +02:00
|
|
|
|
|
|
|
newHash = MD5.FileHash(fullPath);
|
|
|
|
|
|
|
|
if (_Config == null || _FileHash == null || newHash != _FileHash)
|
2015-11-23 20:17:57 +01:00
|
|
|
{
|
2018-06-15 02:57:03 +02:00
|
|
|
string configContents = File.ReadAllText(fullPath);
|
|
|
|
_Config = Deserialize(configContents);
|
|
|
|
_FileHash = newHash;
|
2015-11-23 20:17:57 +01:00
|
|
|
}
|
2018-06-15 02:57:03 +02:00
|
|
|
return _Config;
|
2016-01-30 06:26:44 +01:00
|
|
|
}
|
|
|
|
|
2015-12-24 07:36:33 +01:00
|
|
|
public static void Save(string path, Config config)
|
|
|
|
{
|
|
|
|
if (!Directory.Exists(Path.GetDirectoryName(path)))
|
|
|
|
{
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(path));
|
|
|
|
}
|
|
|
|
string configContents = Config.Serialize(config);
|
|
|
|
File.WriteAllText(path, configContents);
|
|
|
|
}
|
2015-07-16 23:54:53 +02:00
|
|
|
}
|
2018-01-28 05:23:56 +01:00
|
|
|
}
|