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.2 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. config.proxy.CheckConfig();
  61. return config;
  62. }
  63. catch (Exception e)
  64. {
  65. if (!(e is FileNotFoundException))
  66. Logging.LogUsefulException(e);
  67. return new Configuration
  68. {
  69. index = 0,
  70. isDefault = true,
  71. localPort = 1080,
  72. autoCheckUpdate = true,
  73. configs = new List<Server>()
  74. {
  75. GetDefaultServer()
  76. }
  77. };
  78. }
  79. }
  80. public static void Save(Configuration config)
  81. {
  82. if (config.index >= config.configs.Count)
  83. config.index = config.configs.Count - 1;
  84. if (config.index < -1)
  85. config.index = -1;
  86. if (config.index == -1 && config.strategy == null)
  87. config.index = 0;
  88. config.isDefault = false;
  89. try
  90. {
  91. using (StreamWriter sw = new StreamWriter(File.Open(CONFIG_FILE, FileMode.Create)))
  92. {
  93. string jsonString = JsonConvert.SerializeObject(config, Formatting.Indented);
  94. sw.Write(jsonString);
  95. sw.Flush();
  96. }
  97. }
  98. catch (IOException e)
  99. {
  100. Logging.LogUsefulException(e);
  101. }
  102. }
  103. public static Server GetDefaultServer()
  104. {
  105. return new Server();
  106. }
  107. private static void Assert(bool condition)
  108. {
  109. if (!condition)
  110. throw new Exception(I18N.GetString("assertion failure"));
  111. }
  112. public static void CheckPort(int port)
  113. {
  114. if (port <= 0 || port > 65535)
  115. throw new ArgumentException(I18N.GetString("Port out of range"));
  116. }
  117. public static void CheckLocalPort(int port)
  118. {
  119. CheckPort(port);
  120. if (port == 8123)
  121. throw new ArgumentException(I18N.GetString("Port can't be 8123"));
  122. }
  123. private static void CheckPassword(string password)
  124. {
  125. if (password.IsNullOrEmpty())
  126. throw new ArgumentException(I18N.GetString("Password can not be blank"));
  127. }
  128. public static void CheckServer(string server)
  129. {
  130. if (server.IsNullOrEmpty())
  131. throw new ArgumentException(I18N.GetString("Server IP can not be blank"));
  132. }
  133. public static void CheckTimeout(int timeout, int maxTimeout)
  134. {
  135. if (timeout <= 0 || timeout > maxTimeout)
  136. throw new ArgumentException(string.Format(
  137. I18N.GetString("Timeout is invalid, it should not exceed {0}"), maxTimeout));
  138. }
  139. }
  140. }