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

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using Shadowsocks.Controller;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Text;
  6. using System.Windows.Forms;
  7. namespace Shadowsocks.Model
  8. {
  9. [Serializable]
  10. public class Configuration
  11. {
  12. public List<Server> configs;
  13. // when strategy is set, index is ignored
  14. public string strategy;
  15. public int index;
  16. public bool global;
  17. public bool enabled;
  18. public bool shareOverLan;
  19. public bool isDefault;
  20. public int localPort;
  21. public string pacUrl;
  22. public bool useOnlinePac;
  23. public bool availabilityStatistics;
  24. public bool autoCheckUpdate;
  25. public LogViewerConfig logViewer;
  26. private static string CONFIG_FILE = "gui-config.json";
  27. public Server GetCurrentServer()
  28. {
  29. if (index >= 0 && index < configs.Count)
  30. {
  31. return configs[index];
  32. }
  33. else
  34. {
  35. return GetDefaultServer();
  36. }
  37. }
  38. public static void CheckServer(Server server)
  39. {
  40. CheckPort(server.server_port);
  41. CheckPassword(server.password);
  42. CheckServer(server.server);
  43. }
  44. public static Configuration Load()
  45. {
  46. try
  47. {
  48. string configContent = File.ReadAllText(CONFIG_FILE);
  49. Configuration config = SimpleJson.SimpleJson.DeserializeObject<Configuration>(configContent, new JsonSerializerStrategy());
  50. config.isDefault = false;
  51. if (config.localPort == 0)
  52. {
  53. config.localPort = 1080;
  54. }
  55. if (config.index == -1)
  56. {
  57. if (config.strategy == null)
  58. {
  59. config.index = 0;
  60. }
  61. }
  62. return config;
  63. }
  64. catch (Exception e)
  65. {
  66. if (!(e is FileNotFoundException))
  67. {
  68. Console.WriteLine(e);
  69. }
  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. {
  87. config.index = config.configs.Count - 1;
  88. }
  89. if (config.index < -1)
  90. {
  91. config.index = -1;
  92. }
  93. if (config.index == -1)
  94. {
  95. if (config.strategy == null)
  96. {
  97. config.index = 0;
  98. }
  99. }
  100. config.isDefault = false;
  101. try
  102. {
  103. using (StreamWriter sw = new StreamWriter(File.Open(CONFIG_FILE, FileMode.Create)))
  104. {
  105. string jsonString = SimpleJson.SimpleJson.SerializeObject(config);
  106. sw.Write(jsonString);
  107. sw.Flush();
  108. }
  109. }
  110. catch (IOException e)
  111. {
  112. Console.Error.WriteLine(e);
  113. }
  114. }
  115. public static Server GetDefaultServer()
  116. {
  117. return new Server();
  118. }
  119. private static void Assert(bool condition)
  120. {
  121. if (!condition)
  122. {
  123. throw new Exception(I18N.GetString("assertion failure"));
  124. }
  125. }
  126. public static void CheckPort(int port)
  127. {
  128. if (port <= 0 || port > 65535)
  129. {
  130. throw new ArgumentException(I18N.GetString("Port out of range"));
  131. }
  132. }
  133. public static void CheckLocalPort(int port)
  134. {
  135. CheckPort(port);
  136. if (port == 8123)
  137. {
  138. throw new ArgumentException(I18N.GetString("Port can't be 8123"));
  139. }
  140. }
  141. private static void CheckPassword(string password)
  142. {
  143. if (string.IsNullOrEmpty(password))
  144. {
  145. throw new ArgumentException(I18N.GetString("Password can not be blank"));
  146. }
  147. }
  148. private static void CheckServer(string server)
  149. {
  150. if (string.IsNullOrEmpty(server))
  151. {
  152. throw new ArgumentException(I18N.GetString("Server IP can not be blank"));
  153. }
  154. }
  155. private class JsonSerializerStrategy : SimpleJson.PocoJsonSerializerStrategy
  156. {
  157. // convert string to int
  158. public override object DeserializeObject(object value, Type type)
  159. {
  160. if (type == typeof(Int32) && value.GetType() == typeof(string))
  161. {
  162. return Int32.Parse(value.ToString());
  163. }
  164. return base.DeserializeObject(value, type);
  165. }
  166. }
  167. }
  168. }