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 7.6 kB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using Newtonsoft.Json;
  5. using Shadowsocks.Controller;
  6. namespace Shadowsocks.Model
  7. {
  8. [Serializable]
  9. public class Configuration
  10. {
  11. public string version;
  12. public List<Server> configs;
  13. // when strategy is set, index is ignored
  14. public string strategy;
  15. public int index;
  16. public bool global;
  17. public bool enabled;
  18. public bool shareOverLan;
  19. public bool isDefault;
  20. public bool isIPv6Enabled = false;
  21. public int localPort;
  22. public bool portableMode = true;
  23. public string pacUrl;
  24. public bool useOnlinePac;
  25. public bool secureLocalPac = true;
  26. public bool availabilityStatistics;
  27. public bool autoCheckUpdate;
  28. public bool checkPreRelease;
  29. public bool isVerboseLogging;
  30. public LogViewerConfig logViewer;
  31. public ProxyConfig proxy;
  32. public HotkeyConfig hotkey;
  33. private static readonly string CONFIG_FILE = "gui-config.json";
  34. [JsonIgnore]
  35. public string localHost => GetLocalHost();
  36. private string GetLocalHost() {
  37. return isIPv6Enabled ? "[::1]" : "127.0.0.1";
  38. }
  39. public Server GetCurrentServer()
  40. {
  41. if (index >= 0 && index < configs.Count)
  42. return configs[index];
  43. else
  44. return GetDefaultServer();
  45. }
  46. public static void CheckServer(Server server)
  47. {
  48. CheckServer(server.server);
  49. CheckPort(server.server_port);
  50. CheckPassword(server.password);
  51. CheckTimeout(server.timeout, Server.MaxServerTimeoutSec);
  52. }
  53. public static bool ChecksServer(Server server)
  54. {
  55. try
  56. {
  57. CheckServer(server);
  58. return true;
  59. }
  60. catch (Exception)
  61. {
  62. return false;
  63. }
  64. }
  65. public static Configuration Load()
  66. {
  67. try
  68. {
  69. string configContent = File.ReadAllText(CONFIG_FILE);
  70. Configuration config = JsonConvert.DeserializeObject<Configuration>(configContent);
  71. config.isDefault = false;
  72. if (config.configs == null)
  73. config.configs = new List<Server>();
  74. if (config.configs.Count == 0)
  75. config.configs.Add(GetDefaultServer());
  76. if (config.localPort == 0)
  77. config.localPort = 1080;
  78. if (config.index == -1 && config.strategy == null)
  79. config.index = 0;
  80. if (config.logViewer == null)
  81. config.logViewer = new LogViewerConfig();
  82. if (config.proxy == null)
  83. config.proxy = new ProxyConfig();
  84. if (config.hotkey == null)
  85. config.hotkey = new HotkeyConfig();
  86. if (!System.Net.Sockets.Socket.OSSupportsIPv6) {
  87. config.isIPv6Enabled = false; // disable IPv6 if os not support
  88. }
  89. //TODO if remote host(server) do not support IPv6 (or DNS resolve AAAA TYPE record) disable IPv6?
  90. config.proxy.CheckConfig();
  91. return config;
  92. }
  93. catch (Exception e)
  94. {
  95. if (!(e is FileNotFoundException))
  96. Logging.LogUsefulException(e);
  97. return new Configuration
  98. {
  99. index = 0,
  100. isDefault = true,
  101. localPort = 1080,
  102. autoCheckUpdate = true,
  103. configs = new List<Server>()
  104. {
  105. GetDefaultServer()
  106. },
  107. logViewer = new LogViewerConfig(),
  108. proxy = new ProxyConfig(),
  109. hotkey = new HotkeyConfig()
  110. };
  111. }
  112. }
  113. public static void Save(Configuration config)
  114. {
  115. config.version = UpdateChecker.Version;
  116. if (config.index >= config.configs.Count)
  117. config.index = config.configs.Count - 1;
  118. if (config.index < -1)
  119. config.index = -1;
  120. if (config.index == -1 && config.strategy == null)
  121. config.index = 0;
  122. config.isDefault = false;
  123. try
  124. {
  125. using (StreamWriter sw = new StreamWriter(File.Open(CONFIG_FILE, FileMode.Create)))
  126. {
  127. string jsonString = JsonConvert.SerializeObject(config, Formatting.Indented);
  128. sw.Write(jsonString);
  129. sw.Flush();
  130. }
  131. }
  132. catch (IOException e)
  133. {
  134. Logging.LogUsefulException(e);
  135. }
  136. }
  137. public static Server AddDefaultServerOrServer(Configuration config, Server server = null, int? index = null)
  138. {
  139. if (config != null && config.configs != null)
  140. {
  141. server = (server ?? GetDefaultServer());
  142. config.configs.Insert(index.GetValueOrDefault(config.configs.Count), server);
  143. //if (index.HasValue)
  144. // config.configs.Insert(index.Value, server);
  145. //else
  146. // config.configs.Add(server);
  147. }
  148. return server;
  149. }
  150. public static Server GetDefaultServer()
  151. {
  152. return new Server();
  153. }
  154. private static void Assert(bool condition)
  155. {
  156. if (!condition)
  157. throw new Exception(I18N.GetString("assertion failure"));
  158. }
  159. public static void CheckPort(int port)
  160. {
  161. if (port <= 0 || port > 65535)
  162. throw new ArgumentException(I18N.GetString("Port out of range"));
  163. }
  164. public static void CheckLocalPort(int port)
  165. {
  166. CheckPort(port);
  167. if (port == 8123)
  168. throw new ArgumentException(I18N.GetString("Port can't be 8123"));
  169. }
  170. private static void CheckPassword(string password)
  171. {
  172. if (password.IsNullOrEmpty())
  173. throw new ArgumentException(I18N.GetString("Password can not be blank"));
  174. }
  175. public static void CheckServer(string server)
  176. {
  177. if (server.IsNullOrEmpty())
  178. throw new ArgumentException(I18N.GetString("Server IP can not be blank"));
  179. }
  180. public static void CheckTimeout(int timeout, int maxTimeout)
  181. {
  182. if (timeout <= 0 || timeout > maxTimeout)
  183. throw new ArgumentException(
  184. I18N.GetString("Timeout is invalid, it should not exceed {0}", maxTimeout));
  185. }
  186. public static void CheckProxyAuthUser(string user)
  187. {
  188. if (user.IsNullOrEmpty())
  189. throw new ArgumentException(I18N.GetString("Auth user can not be blank"));
  190. }
  191. public static void CheckProxyAuthPwd(string pwd)
  192. {
  193. if (pwd.IsNullOrEmpty())
  194. throw new ArgumentException(I18N.GetString("Auth pwd can not be blank"));
  195. }
  196. }
  197. }