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