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