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

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. namespace Shadowsocks.Model
  6. {
  7. [Serializable]
  8. public class Configuration
  9. {
  10. public Configuration()
  11. {
  12. }
  13. public List<Server> configs;
  14. public int index;
  15. public bool enabled;
  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 Server GetDefaultServer()
  89. {
  90. return new Server()
  91. {
  92. server = "",
  93. server_port = 8388,
  94. local_port = 1080,
  95. method = "aes-256-cfb",
  96. password = "",
  97. remarks = ""
  98. };
  99. }
  100. private static void Assert(bool condition)
  101. {
  102. if (!condition)
  103. {
  104. throw new Exception("assertion failure");
  105. }
  106. }
  107. private static void CheckPort(int port)
  108. {
  109. if (port <= 0 || port > 65535)
  110. {
  111. throw new ArgumentException("port out of range");
  112. }
  113. }
  114. private static void CheckPassword(string password)
  115. {
  116. if (string.IsNullOrEmpty(password))
  117. {
  118. throw new ArgumentException("password can not be blank");
  119. }
  120. }
  121. private static void CheckServer(string server)
  122. {
  123. if (string.IsNullOrEmpty(server))
  124. {
  125. throw new ArgumentException("server IP can not be blank");
  126. }
  127. }
  128. private static void CheckRemark(string remark)
  129. {
  130. //remark is optional
  131. }
  132. private class JsonSerializerStrategy : SimpleJson.PocoJsonSerializerStrategy
  133. {
  134. // convert string to int
  135. public override object DeserializeObject(object value, Type type)
  136. {
  137. if (type == typeof(Int32) && value.GetType() == typeof(string))
  138. {
  139. return Int32.Parse(value.ToString());
  140. }
  141. return base.DeserializeObject(value, type);
  142. }
  143. }
  144. }
  145. }