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
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 shareOverLan;
  17. public bool isDefault;
  18. private static string CONFIG_FILE = "gui-config.json";
  19. public Server GetCurrentServer()
  20. {
  21. if (index >= 0 && index < configs.Count)
  22. {
  23. return configs[index];
  24. }
  25. else
  26. {
  27. return GetDefaultServer();
  28. }
  29. }
  30. public static void CheckServer(Server server)
  31. {
  32. CheckPort(server.local_port);
  33. CheckPort(server.server_port);
  34. CheckPassword(server.password);
  35. CheckServer(server.server);
  36. CheckRemark(server.remarks);
  37. }
  38. public static Configuration Load()
  39. {
  40. try
  41. {
  42. string configContent = File.ReadAllText(CONFIG_FILE);
  43. Configuration config = SimpleJson.SimpleJson.DeserializeObject<Configuration>(configContent, new JsonSerializerStrategy());
  44. config.isDefault = false;
  45. return config;
  46. }
  47. catch (Exception e)
  48. {
  49. if (!(e is FileNotFoundException))
  50. {
  51. Console.WriteLine(e);
  52. }
  53. return new Configuration
  54. {
  55. index = 0,
  56. isDefault = true,
  57. configs = new List<Server>()
  58. {
  59. GetDefaultServer()
  60. }
  61. };
  62. }
  63. }
  64. public static void Save(Configuration config)
  65. {
  66. if (config.index >= config.configs.Count)
  67. {
  68. config.index = config.configs.Count - 1;
  69. }
  70. if (config.index < 0)
  71. {
  72. config.index = 0;
  73. }
  74. config.isDefault = false;
  75. try
  76. {
  77. using (StreamWriter sw = new StreamWriter(File.Open(CONFIG_FILE, FileMode.Create)))
  78. {
  79. string jsonString = SimpleJson.SimpleJson.SerializeObject(config);
  80. sw.Write(jsonString);
  81. sw.Flush();
  82. }
  83. }
  84. catch (IOException e)
  85. {
  86. Console.Error.WriteLine(e);
  87. }
  88. }
  89. public static Server GetDefaultServer()
  90. {
  91. return new Server()
  92. {
  93. server = "",
  94. server_port = 8388,
  95. local_port = 1080,
  96. method = "aes-256-cfb",
  97. password = "",
  98. remarks = ""
  99. };
  100. }
  101. private static void Assert(bool condition)
  102. {
  103. if (!condition)
  104. {
  105. throw new Exception("assertion failure");
  106. }
  107. }
  108. private static void CheckPort(int port)
  109. {
  110. if (port <= 0 || port > 65535)
  111. {
  112. throw new ArgumentException("port out of range");
  113. }
  114. }
  115. private static void CheckPassword(string password)
  116. {
  117. if (string.IsNullOrEmpty(password))
  118. {
  119. throw new ArgumentException("password can not be blank");
  120. }
  121. }
  122. private static void CheckServer(string server)
  123. {
  124. if (string.IsNullOrEmpty(server))
  125. {
  126. throw new ArgumentException("server IP can not be blank");
  127. }
  128. }
  129. private static void CheckRemark(string remark)
  130. {
  131. //remark is optional
  132. }
  133. private class JsonSerializerStrategy : SimpleJson.PocoJsonSerializerStrategy
  134. {
  135. // convert string to int
  136. public override object DeserializeObject(object value, Type type)
  137. {
  138. if (type == typeof(Int32) && value.GetType() == typeof(string))
  139. {
  140. return Int32.Parse(value.ToString());
  141. }
  142. return base.DeserializeObject(value, type);
  143. }
  144. }
  145. }
  146. }