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