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