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 7.6 kB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using System.Windows.Forms;
  6. using System.Text.RegularExpressions;
  7. namespace Shadowsocks.Model
  8. {
  9. [Serializable]
  10. public class Configuration
  11. {
  12. public List<Server> configs;
  13. public int index;
  14. public bool enabled;
  15. public bool shareOverLan;
  16. public bool isDefault;
  17. private static string CONFIG_FILE = "gui-config.json";
  18. public Server GetCurrentServer()
  19. {
  20. if (index >= 0 && index < configs.Count)
  21. {
  22. return configs[index];
  23. }
  24. else
  25. {
  26. return GetDefaultServer();
  27. }
  28. }
  29. public static void CheckServer(Server server)
  30. {
  31. CheckPort(server.local_port);
  32. CheckPort(server.server_port);
  33. CheckPassword(server.password);
  34. CheckServer(server.server);
  35. CheckRemark(server.remarks);
  36. }
  37. public static Configuration Load()
  38. {
  39. try
  40. {
  41. string configContent = File.ReadAllText(CONFIG_FILE);
  42. Configuration config = SimpleJson.SimpleJson.DeserializeObject<Configuration>(configContent, new JsonSerializerStrategy());
  43. config.isDefault = false;
  44. return config;
  45. }
  46. catch (Exception e)
  47. {
  48. if (!(e is FileNotFoundException))
  49. {
  50. Console.WriteLine(e);
  51. }
  52. return new Configuration
  53. {
  54. index = 0,
  55. isDefault = true,
  56. configs = new List<Server>()
  57. {
  58. GetDefaultServer()
  59. }
  60. };
  61. }
  62. }
  63. public static void Save(Configuration config)
  64. {
  65. if (config.index >= config.configs.Count)
  66. {
  67. config.index = config.configs.Count - 1;
  68. }
  69. if (config.index < 0)
  70. {
  71. config.index = 0;
  72. }
  73. config.isDefault = false;
  74. try
  75. {
  76. using (StreamWriter sw = new StreamWriter(File.Open(CONFIG_FILE, FileMode.Create)))
  77. {
  78. string jsonString = SimpleJson.SimpleJson.SerializeObject(config);
  79. sw.Write(jsonString);
  80. sw.Flush();
  81. }
  82. }
  83. catch (IOException e)
  84. {
  85. Console.Error.WriteLine(e);
  86. }
  87. }
  88. public static bool load_uri(string uri, ref Server server)
  89. {
  90. Regex regex_ss_head = new Regex("^(?i:(ss://))");
  91. Regex regex_ss = new Regex("^(?i:(aes-256-cfb|aes-128-cfb|aes-192-cfb|aes-256-ofb|aes-128-ofb|aes-192-ofb|aes-128-ctr|aes-192-ctr|aes-256-ctr|aes-128-cfb8|aes-192-cfb8|aes-256-cfb8|aes-128-cfb1|aes-192-cfb1|aes-256-cfb1|bf-cfb|camellia-128-cfb|camellia-192-cfb|camellia-256-cfb|cast5-cfb|des-cfb|idea-cfb|rc2-cfb|rc4-md5|seed-cfb|salsa20-ctr)):[a-zA-Z0-9\\.\\-]+@((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|([a-zA-Z0-9\\-]+\\.)*[a-zA-Z0-9\\-]+\\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))\\:[0-9]+");
  92. Regex regexbase64 = new Regex("^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?");
  93. string uri_core = "";
  94. try
  95. {
  96. if (regex_ss_head.IsMatch(uri))
  97. {
  98. string[] sArray = Regex.Split(uri, "://", RegexOptions.IgnoreCase);
  99. uri_core = sArray[1].ToString();
  100. }
  101. else
  102. {
  103. uri_core = uri;
  104. }
  105. if (regex_ss.IsMatch(uri_core))
  106. {
  107. server = parse_ss_uri(uri_core);
  108. return true;
  109. }
  110. else
  111. {
  112. if (regexbase64.IsMatch(uri_core))
  113. {
  114. byte[] arr = System.Convert.FromBase64String(uri_core);
  115. string uri_core2 = System.Text.Encoding.UTF8.GetString(arr);
  116. if (regex_ss.IsMatch(uri_core2))
  117. {
  118. server = parse_ss_uri(uri_core2);
  119. return true;
  120. }
  121. else
  122. {
  123. return false;
  124. }
  125. }
  126. else
  127. {
  128. return false;
  129. }
  130. }
  131. }
  132. catch (IOException e)
  133. {
  134. Console.Error.WriteLine(e);
  135. }
  136. return false;
  137. }
  138. public static Server GetDefaultServer()
  139. {
  140. return new Server()
  141. {
  142. server = "",
  143. server_port = 8388,
  144. local_port = 1080,
  145. method = "aes-256-cfb",
  146. password = "",
  147. remarks = ""
  148. };
  149. }
  150. private static void Assert(bool condition)
  151. {
  152. if (!condition)
  153. {
  154. throw new Exception("assertion failure");
  155. }
  156. }
  157. private static Server parse_ss_uri(string uri)
  158. {
  159. string[] sArray=uri.Split(new char[2]{':','@'});
  160. Server server = Configuration.GetDefaultServer();
  161. server.method = sArray[0].ToString();
  162. server.password = sArray[1].ToString();
  163. server.server = sArray[2].ToString();
  164. server.server_port = int.Parse(sArray[3].ToString());
  165. return server;
  166. }
  167. private static void CheckPort(int port)
  168. {
  169. if (port <= 0 || port > 65535)
  170. {
  171. throw new ArgumentException("port out of range");
  172. }
  173. }
  174. private static void CheckPassword(string password)
  175. {
  176. if (string.IsNullOrEmpty(password))
  177. {
  178. throw new ArgumentException("password can not be blank");
  179. }
  180. }
  181. private static void CheckServer(string server)
  182. {
  183. if (string.IsNullOrEmpty(server))
  184. {
  185. throw new ArgumentException("server IP can not be blank");
  186. }
  187. }
  188. private static void CheckRemark(string remark)
  189. {
  190. //remark is optional
  191. }
  192. private class JsonSerializerStrategy : SimpleJson.PocoJsonSerializerStrategy
  193. {
  194. // convert string to int
  195. public override object DeserializeObject(object value, Type type)
  196. {
  197. if (type == typeof(Int32) && value.GetType() == typeof(string))
  198. {
  199. return Int32.Parse(value.ToString());
  200. }
  201. return base.DeserializeObject(value, type);
  202. }
  203. }
  204. }
  205. }