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

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