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