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 9.3 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. [JsonIgnore]
  43. public string localHost => GetLocalHost();
  44. private string GetLocalHost() {
  45. return isIPv6Enabled ? "[::1]" : "127.0.0.1";
  46. }
  47. public Server GetCurrentServer()
  48. {
  49. if (index >= 0 && index < configs.Count)
  50. return configs[index];
  51. else
  52. return GetDefaultServer();
  53. }
  54. public static void CheckServer(Server server)
  55. {
  56. CheckServer(server.server);
  57. CheckPort(server.server_port);
  58. CheckPassword(server.password);
  59. CheckTimeout(server.timeout, Server.MaxServerTimeoutSec);
  60. }
  61. public static bool ChecksServer(Server server)
  62. {
  63. try
  64. {
  65. CheckServer(server);
  66. return true;
  67. }
  68. catch (Exception)
  69. {
  70. return false;
  71. }
  72. }
  73. public static Configuration Load()
  74. {
  75. try
  76. {
  77. string configContent = File.ReadAllText(CONFIG_FILE);
  78. Configuration config = JsonConvert.DeserializeObject<Configuration>(configContent);
  79. config.isDefault = false;
  80. if (config.configs == null)
  81. config.configs = new List<Server>();
  82. if (config.configs.Count == 0)
  83. config.configs.Add(GetDefaultServer());
  84. if (config.localPort == 0)
  85. config.localPort = 1080;
  86. if (config.index == -1 && config.strategy == null)
  87. config.index = 0;
  88. if (config.logViewer == null)
  89. config.logViewer = new LogViewerConfig();
  90. if (config.proxy == null)
  91. config.proxy = new ProxyConfig();
  92. if (config.hotkey == null)
  93. config.hotkey = new HotkeyConfig();
  94. if (!System.Net.Sockets.Socket.OSSupportsIPv6) {
  95. config.isIPv6Enabled = false; // disable IPv6 if os not support
  96. }
  97. //TODO if remote host(server) do not support IPv6 (or DNS resolve AAAA TYPE record) disable IPv6?
  98. config.proxy.CheckConfig();
  99. try
  100. {
  101. config.nLogConfig = NLogConfig.LoadXML();
  102. switch (config.nLogConfig.GetLogLevel())
  103. {
  104. case NLogConfig.LogLevel.Fatal:
  105. case NLogConfig.LogLevel.Error:
  106. case NLogConfig.LogLevel.Warn:
  107. case NLogConfig.LogLevel.Info:
  108. config.isVerboseLogging = false;
  109. break;
  110. case NLogConfig.LogLevel.Debug:
  111. case NLogConfig.LogLevel.Trace:
  112. config.isVerboseLogging = true;
  113. break;
  114. }
  115. }
  116. catch (Exception e)
  117. {
  118. logger.Error(e, "Cannot get the log level from NLog config file.");
  119. }
  120. return config;
  121. }
  122. catch (Exception e)
  123. {
  124. if (!(e is FileNotFoundException))
  125. logger.LogUsefulException(e);
  126. return new Configuration
  127. {
  128. index = 0,
  129. isDefault = true,
  130. localPort = 1080,
  131. autoCheckUpdate = true,
  132. configs = new List<Server>()
  133. {
  134. GetDefaultServer()
  135. },
  136. logViewer = new LogViewerConfig(),
  137. proxy = new ProxyConfig(),
  138. hotkey = new HotkeyConfig(),
  139. };
  140. }
  141. }
  142. public static void Save(Configuration config)
  143. {
  144. config.version = UpdateChecker.Version;
  145. if (config.index >= config.configs.Count)
  146. config.index = config.configs.Count - 1;
  147. if (config.index < -1)
  148. config.index = -1;
  149. if (config.index == -1 && config.strategy == null)
  150. config.index = 0;
  151. config.isDefault = false;
  152. try
  153. {
  154. using (StreamWriter sw = new StreamWriter(File.Open(CONFIG_FILE, FileMode.Create)))
  155. {
  156. string jsonString = JsonConvert.SerializeObject(config, Formatting.Indented);
  157. sw.Write(jsonString);
  158. sw.Flush();
  159. }
  160. try
  161. {
  162. // apply changs to NLog.config
  163. config.nLogConfig.SetLogLevel(config.isVerboseLogging? NLogConfig.LogLevel.Trace: NLogConfig.LogLevel.Info);
  164. NLogConfig.SaveXML(config.nLogConfig);
  165. }
  166. catch(Exception e)
  167. {
  168. logger.Error(e, "Cannot set the log level");
  169. }
  170. }
  171. catch (IOException e)
  172. {
  173. logger.LogUsefulException(e);
  174. }
  175. }
  176. public static Server AddDefaultServerOrServer(Configuration config, Server server = null, int? index = null)
  177. {
  178. if (config != null && config.configs != null)
  179. {
  180. server = (server ?? GetDefaultServer());
  181. config.configs.Insert(index.GetValueOrDefault(config.configs.Count), server);
  182. //if (index.HasValue)
  183. // config.configs.Insert(index.Value, server);
  184. //else
  185. // config.configs.Add(server);
  186. }
  187. return server;
  188. }
  189. public static Server GetDefaultServer()
  190. {
  191. return new Server();
  192. }
  193. private static void Assert(bool condition)
  194. {
  195. if (!condition)
  196. throw new Exception(I18N.GetString("assertion failure"));
  197. }
  198. public static void CheckPort(int port)
  199. {
  200. if (port <= 0 || port > 65535)
  201. throw new ArgumentException(I18N.GetString("Port out of range"));
  202. }
  203. public static void CheckLocalPort(int port)
  204. {
  205. CheckPort(port);
  206. if (port == 8123)
  207. throw new ArgumentException(I18N.GetString("Port can't be 8123"));
  208. }
  209. private static void CheckPassword(string password)
  210. {
  211. if (password.IsNullOrEmpty())
  212. throw new ArgumentException(I18N.GetString("Password can not be blank"));
  213. }
  214. public static void CheckServer(string server)
  215. {
  216. if (server.IsNullOrEmpty())
  217. throw new ArgumentException(I18N.GetString("Server IP can not be blank"));
  218. }
  219. public static void CheckTimeout(int timeout, int maxTimeout)
  220. {
  221. if (timeout <= 0 || timeout > maxTimeout)
  222. throw new ArgumentException(
  223. I18N.GetString("Timeout is invalid, it should not exceed {0}", maxTimeout));
  224. }
  225. public static void CheckProxyAuthUser(string user)
  226. {
  227. if (user.IsNullOrEmpty())
  228. throw new ArgumentException(I18N.GetString("Auth user can not be blank"));
  229. }
  230. public static void CheckProxyAuthPwd(string pwd)
  231. {
  232. if (pwd.IsNullOrEmpty())
  233. throw new ArgumentException(I18N.GetString("Auth pwd can not be blank"));
  234. }
  235. }
  236. }