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 5.4 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 List<Server> configs;
  12. // when strategy is set, index is ignored
  13. public string strategy;
  14. public int index;
  15. public bool global;
  16. public bool enabled;
  17. public bool shareOverLan;
  18. public bool isDefault;
  19. public int localPort;
  20. public string pacUrl;
  21. public bool useOnlinePac;
  22. public bool availabilityStatistics;
  23. public bool autoCheckUpdate;
  24. public bool isVerboseLogging;
  25. public LogViewerConfig logViewer;
  26. public ProxyConfig proxy;
  27. public HotkeyConfig hotkey;
  28. private static string CONFIG_FILE = "gui-config.json";
  29. public Server GetCurrentServer()
  30. {
  31. if (index >= 0 && index < configs.Count)
  32. return configs[index];
  33. else
  34. return GetDefaultServer();
  35. }
  36. public static void CheckServer(Server server)
  37. {
  38. CheckPort(server.server_port);
  39. CheckPassword(server.password);
  40. CheckServer(server.server);
  41. CheckTimeout(server.timeout, Server.MaxServerTimeoutSec);
  42. }
  43. public static Configuration Load()
  44. {
  45. try
  46. {
  47. string configContent = File.ReadAllText(CONFIG_FILE);
  48. Configuration config = JsonConvert.DeserializeObject<Configuration>(configContent);
  49. config.isDefault = false;
  50. if (config.localPort == 0)
  51. config.localPort = 1080;
  52. if (config.index == -1 && config.strategy == null)
  53. config.index = 0;
  54. if (config.logViewer == null)
  55. config.logViewer = new LogViewerConfig();
  56. if (config.proxy == null)
  57. config.proxy = new ProxyConfig();
  58. if (config.hotkey == null)
  59. config.hotkey = new HotkeyConfig();
  60. if (config.proxy.proxyType < ProxyConfig.PROXY_SOCKS5 || config.proxy.proxyType > ProxyConfig.PROXY_HTTP)
  61. {
  62. config.proxy.proxyType = ProxyConfig.PROXY_SOCKS5;
  63. }
  64. return config;
  65. }
  66. catch (Exception e)
  67. {
  68. if (!(e is FileNotFoundException))
  69. Logging.LogUsefulException(e);
  70. return new Configuration
  71. {
  72. index = 0,
  73. isDefault = true,
  74. localPort = 1080,
  75. autoCheckUpdate = true,
  76. configs = new List<Server>()
  77. {
  78. GetDefaultServer()
  79. }
  80. };
  81. }
  82. }
  83. public static void Save(Configuration config)
  84. {
  85. if (config.index >= config.configs.Count)
  86. config.index = config.configs.Count - 1;
  87. if (config.index < -1)
  88. config.index = -1;
  89. if (config.index == -1 && config.strategy == null)
  90. config.index = 0;
  91. config.isDefault = false;
  92. try
  93. {
  94. using (StreamWriter sw = new StreamWriter(File.Open(CONFIG_FILE, FileMode.Create)))
  95. {
  96. string jsonString = JsonConvert.SerializeObject(config, Formatting.Indented);
  97. sw.Write(jsonString);
  98. sw.Flush();
  99. }
  100. }
  101. catch (IOException e)
  102. {
  103. Logging.LogUsefulException(e);
  104. }
  105. }
  106. public static Server GetDefaultServer()
  107. {
  108. return new Server();
  109. }
  110. private static void Assert(bool condition)
  111. {
  112. if (!condition)
  113. throw new Exception(I18N.GetString("assertion failure"));
  114. }
  115. public static void CheckPort(int port)
  116. {
  117. if (port <= 0 || port > 65535)
  118. throw new ArgumentException(I18N.GetString("Port out of range"));
  119. }
  120. public static void CheckLocalPort(int port)
  121. {
  122. CheckPort(port);
  123. if (port == 8123)
  124. throw new ArgumentException(I18N.GetString("Port can't be 8123"));
  125. }
  126. private static void CheckPassword(string password)
  127. {
  128. if (password.IsNullOrEmpty())
  129. throw new ArgumentException(I18N.GetString("Password can not be blank"));
  130. }
  131. public static void CheckServer(string server)
  132. {
  133. if (server.IsNullOrEmpty())
  134. throw new ArgumentException(I18N.GetString("Server IP can not be blank"));
  135. }
  136. public static void CheckTimeout(int timeout, int maxTimeout)
  137. {
  138. if (timeout <= 0 || timeout > maxTimeout)
  139. throw new ArgumentException(string.Format(
  140. I18N.GetString("Timeout is invalid, it should not exceed {0}"), maxTimeout));
  141. }
  142. }
  143. }