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.9 kB

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