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

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