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 4.8 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. }
  42. public static Configuration Load()
  43. {
  44. try
  45. {
  46. string configContent = File.ReadAllText(CONFIG_FILE);
  47. Configuration config = JsonConvert.DeserializeObject<Configuration>(configContent);
  48. config.isDefault = false;
  49. if (config.localPort == 0)
  50. config.localPort = 1080;
  51. if (config.index == -1 && config.strategy == null)
  52. config.index = 0;
  53. if (config.logViewer == null)
  54. config.logViewer = new LogViewerConfig();
  55. if (config.proxy == null)
  56. config.proxy = new ProxyConfig();
  57. if (config.hotkey == null)
  58. config.hotkey = new HotkeyConfig();
  59. return config;
  60. }
  61. catch (Exception e)
  62. {
  63. if (!(e is FileNotFoundException))
  64. Logging.LogUsefulException(e);
  65. return new Configuration
  66. {
  67. index = 0,
  68. isDefault = true,
  69. localPort = 1080,
  70. autoCheckUpdate = true,
  71. configs = new List<Server>()
  72. {
  73. GetDefaultServer()
  74. }
  75. };
  76. }
  77. }
  78. public static void Save(Configuration config)
  79. {
  80. if (config.index >= config.configs.Count)
  81. config.index = config.configs.Count - 1;
  82. if (config.index < -1)
  83. config.index = -1;
  84. if (config.index == -1 && config.strategy == null)
  85. config.index = 0;
  86. config.isDefault = false;
  87. try
  88. {
  89. using (StreamWriter sw = new StreamWriter(File.Open(CONFIG_FILE, FileMode.Create)))
  90. {
  91. string jsonString = JsonConvert.SerializeObject(config, Formatting.Indented);
  92. sw.Write(jsonString);
  93. sw.Flush();
  94. }
  95. }
  96. catch (IOException e)
  97. {
  98. Logging.LogUsefulException(e);
  99. }
  100. }
  101. public static Server GetDefaultServer()
  102. {
  103. return new Server();
  104. }
  105. private static void Assert(bool condition)
  106. {
  107. if (!condition)
  108. throw new Exception(I18N.GetString("assertion failure"));
  109. }
  110. public static void CheckPort(int port)
  111. {
  112. if (port <= 0 || port > 65535)
  113. throw new ArgumentException(I18N.GetString("Port out of range"));
  114. }
  115. public static void CheckLocalPort(int port)
  116. {
  117. CheckPort(port);
  118. if (port == 8123)
  119. throw new ArgumentException(I18N.GetString("Port can't be 8123"));
  120. }
  121. private static void CheckPassword(string password)
  122. {
  123. if (password.IsNullOrEmpty())
  124. throw new ArgumentException(I18N.GetString("Password can not be blank"));
  125. }
  126. public static void CheckServer(string server)
  127. {
  128. if (server.IsNullOrEmpty())
  129. throw new ArgumentException(I18N.GetString("Server IP can not be blank"));
  130. }
  131. }
  132. }