You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Configuration.cs 10 kB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using Newtonsoft.Json;
  5. using NLog;
  6. using Shadowsocks.Controller;
  7. namespace Shadowsocks.Model
  8. {
  9. [Serializable]
  10. public class Configuration
  11. {
  12. [JsonIgnore]
  13. private static Logger logger = LogManager.GetCurrentClassLogger();
  14. public string version;
  15. public List<Server> configs;
  16. // when strategy is set, index is ignored
  17. public string strategy;
  18. public int index;
  19. public bool global;
  20. public bool enabled;
  21. public bool shareOverLan;
  22. public bool isDefault;
  23. public bool isIPv6Enabled = false;
  24. public int localPort;
  25. public bool portableMode = true;
  26. public bool showPluginOutput;
  27. public string pacUrl;
  28. public string gfwListUrl;
  29. public bool useOnlinePac;
  30. public bool secureLocalPac = true;
  31. public bool availabilityStatistics;
  32. public bool autoCheckUpdate;
  33. public bool checkPreRelease;
  34. public bool isVerboseLogging;
  35. //public NLogConfig.LogLevel logLevel;
  36. public LogViewerConfig logViewer;
  37. public ProxyConfig proxy;
  38. public HotkeyConfig hotkey;
  39. [JsonIgnore]
  40. NLogConfig nLogConfig;
  41. private static readonly string CONFIG_FILE = "gui-config.json";
  42. private static readonly NLogConfig.LogLevel verboseLogLevel =
  43. #if DEBUG
  44. NLogConfig.LogLevel.Trace;
  45. #else
  46. NLogConfig.LogLevel.Debug;
  47. #endif
  48. [JsonIgnore]
  49. public bool updated = false;
  50. [JsonIgnore]
  51. public string localHost => GetLocalHost();
  52. private string GetLocalHost()
  53. {
  54. return isIPv6Enabled ? "[::1]" : "127.0.0.1";
  55. }
  56. public Server GetCurrentServer()
  57. {
  58. if (index >= 0 && index < configs.Count)
  59. return configs[index];
  60. else
  61. return GetDefaultServer();
  62. }
  63. public static void CheckServer(Server server)
  64. {
  65. CheckServer(server.server);
  66. CheckPort(server.server_port);
  67. CheckPassword(server.password);
  68. CheckTimeout(server.timeout, Server.MaxServerTimeoutSec);
  69. }
  70. public static bool ChecksServer(Server server)
  71. {
  72. try
  73. {
  74. CheckServer(server);
  75. return true;
  76. }
  77. catch (Exception)
  78. {
  79. return false;
  80. }
  81. }
  82. public static Configuration Load()
  83. {
  84. try
  85. {
  86. string configContent = File.ReadAllText(CONFIG_FILE);
  87. Configuration config = JsonConvert.DeserializeObject<Configuration>(configContent);
  88. config.isDefault = false;
  89. if (UpdateChecker.Asset.CompareVersion(UpdateChecker.Version, config.version ?? "0") > 0)
  90. {
  91. config.updated = true;
  92. }
  93. if (config.configs == null)
  94. config.configs = new List<Server>();
  95. if (config.configs.Count == 0)
  96. config.configs.Add(GetDefaultServer());
  97. if (config.localPort == 0)
  98. config.localPort = 1080;
  99. if (config.index == -1 && config.strategy == null)
  100. config.index = 0;
  101. if (config.logViewer == null)
  102. config.logViewer = new LogViewerConfig();
  103. if (config.proxy == null)
  104. config.proxy = new ProxyConfig();
  105. if (config.hotkey == null)
  106. config.hotkey = new HotkeyConfig();
  107. if (!System.Net.Sockets.Socket.OSSupportsIPv6)
  108. {
  109. config.isIPv6Enabled = false; // disable IPv6 if os not support
  110. }
  111. //TODO if remote host(server) do not support IPv6 (or DNS resolve AAAA TYPE record) disable IPv6?
  112. config.proxy.CheckConfig();
  113. try
  114. {
  115. config.nLogConfig = NLogConfig.LoadXML();
  116. switch (config.nLogConfig.GetLogLevel())
  117. {
  118. case NLogConfig.LogLevel.Fatal:
  119. case NLogConfig.LogLevel.Error:
  120. case NLogConfig.LogLevel.Warn:
  121. case NLogConfig.LogLevel.Info:
  122. config.isVerboseLogging = false;
  123. break;
  124. case NLogConfig.LogLevel.Debug:
  125. case NLogConfig.LogLevel.Trace:
  126. config.isVerboseLogging = true;
  127. break;
  128. }
  129. }
  130. catch (Exception e)
  131. {
  132. // todo: route the error to UI since there is no log file in this scenario
  133. logger.Error(e, "Cannot get the log level from NLog config file. Please check if the nlog config file exists with corresponding XML nodes.");
  134. }
  135. return config;
  136. }
  137. catch (Exception e)
  138. {
  139. if (!(e is FileNotFoundException))
  140. logger.LogUsefulException(e);
  141. return new Configuration
  142. {
  143. index = 0,
  144. isDefault = true,
  145. localPort = 1080,
  146. autoCheckUpdate = true,
  147. configs = new List<Server>()
  148. {
  149. GetDefaultServer()
  150. },
  151. logViewer = new LogViewerConfig(),
  152. proxy = new ProxyConfig(),
  153. hotkey = new HotkeyConfig(),
  154. };
  155. }
  156. }
  157. public static void Save(Configuration config)
  158. {
  159. config.version = UpdateChecker.Version;
  160. if (config.index >= config.configs.Count)
  161. config.index = config.configs.Count - 1;
  162. if (config.index < -1)
  163. config.index = -1;
  164. if (config.index == -1 && config.strategy == null)
  165. config.index = 0;
  166. config.isDefault = false;
  167. try
  168. {
  169. using (StreamWriter sw = new StreamWriter(File.Open(CONFIG_FILE, FileMode.Create)))
  170. {
  171. string jsonString = JsonConvert.SerializeObject(config, Formatting.Indented);
  172. sw.Write(jsonString);
  173. sw.Flush();
  174. }
  175. try
  176. {
  177. // apply changs to NLog.config
  178. config.nLogConfig.SetLogLevel(config.isVerboseLogging? verboseLogLevel : NLogConfig.LogLevel.Info);
  179. NLogConfig.SaveXML(config.nLogConfig);
  180. }
  181. catch (Exception e)
  182. {
  183. logger.Error(e, "Cannot set the log level to NLog config file. Please check if the nlog config file exists with corresponding XML nodes.");
  184. }
  185. }
  186. catch (IOException e)
  187. {
  188. logger.LogUsefulException(e);
  189. }
  190. }
  191. public static Server AddDefaultServerOrServer(Configuration config, Server server = null, int? index = null)
  192. {
  193. if (config != null && config.configs != null)
  194. {
  195. server = (server ?? GetDefaultServer());
  196. config.configs.Insert(index.GetValueOrDefault(config.configs.Count), server);
  197. //if (index.HasValue)
  198. // config.configs.Insert(index.Value, server);
  199. //else
  200. // config.configs.Add(server);
  201. }
  202. return server;
  203. }
  204. public static Server GetDefaultServer()
  205. {
  206. return new Server();
  207. }
  208. private static void Assert(bool condition)
  209. {
  210. if (!condition)
  211. throw new Exception(I18N.GetString("assertion failure"));
  212. }
  213. public static void CheckPort(int port)
  214. {
  215. if (port <= 0 || port > 65535)
  216. throw new ArgumentException(I18N.GetString("Port out of range"));
  217. }
  218. public static void CheckLocalPort(int port)
  219. {
  220. CheckPort(port);
  221. if (port == 8123)
  222. throw new ArgumentException(I18N.GetString("Port can't be 8123"));
  223. }
  224. private static void CheckPassword(string password)
  225. {
  226. if (password.IsNullOrEmpty())
  227. throw new ArgumentException(I18N.GetString("Password can not be blank"));
  228. }
  229. public static void CheckServer(string server)
  230. {
  231. if (server.IsNullOrEmpty())
  232. throw new ArgumentException(I18N.GetString("Server IP can not be blank"));
  233. }
  234. public static void CheckTimeout(int timeout, int maxTimeout)
  235. {
  236. if (timeout <= 0 || timeout > maxTimeout)
  237. throw new ArgumentException(
  238. I18N.GetString("Timeout is invalid, it should not exceed {0}", maxTimeout));
  239. }
  240. public static void CheckProxyAuthUser(string user)
  241. {
  242. if (user.IsNullOrEmpty())
  243. throw new ArgumentException(I18N.GetString("Auth user can not be blank"));
  244. }
  245. public static void CheckProxyAuthPwd(string pwd)
  246. {
  247. if (pwd.IsNullOrEmpty())
  248. throw new ArgumentException(I18N.GetString("Auth pwd can not be blank"));
  249. }
  250. }
  251. }