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

10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. }
  36. public static Configuration Load()
  37. {
  38. try
  39. {
  40. string configContent = File.ReadAllText(CONFIG_FILE);
  41. Configuration config = SimpleJson.SimpleJson.DeserializeObject<Configuration>(configContent, new JsonSerializerStrategy());
  42. config.isDefault = false;
  43. return config;
  44. }
  45. catch (Exception e)
  46. {
  47. if (!(e is FileNotFoundException))
  48. {
  49. Console.WriteLine(e);
  50. }
  51. return new Configuration
  52. {
  53. index = 0,
  54. configs = new List<Server>()
  55. {
  56. getDefaultServer()
  57. }
  58. };
  59. }
  60. }
  61. public static void Save(Configuration config)
  62. {
  63. try
  64. {
  65. config.isDefault = false;
  66. using (StreamWriter sw = new StreamWriter(File.Open(CONFIG_FILE, FileMode.Create)))
  67. {
  68. string jsonString = SimpleJson.SimpleJson.SerializeObject(config);
  69. sw.Write(jsonString);
  70. sw.Flush();
  71. }
  72. }
  73. catch (IOException e)
  74. {
  75. Console.Error.WriteLine(e);
  76. }
  77. }
  78. private static Server getDefaultServer()
  79. {
  80. return new Server()
  81. {
  82. server = "",
  83. server_port = 8388,
  84. local_port = 1080,
  85. method = "aes-256-cfb",
  86. password = ""
  87. };
  88. }
  89. private static void assert(bool condition)
  90. {
  91. if (!condition)
  92. {
  93. throw new Exception("assertion failure");
  94. }
  95. }
  96. private static void checkPort(int port)
  97. {
  98. if (port <= 0 || port > 65535)
  99. {
  100. throw new ArgumentException("port out of range");
  101. }
  102. }
  103. private static void checkPassword(string password)
  104. {
  105. if (string.IsNullOrEmpty(password))
  106. {
  107. throw new ArgumentException("password can not be blank");
  108. }
  109. }
  110. private static void checkServer(string server)
  111. {
  112. if (string.IsNullOrEmpty(server))
  113. {
  114. throw new ArgumentException("server can not be blank");
  115. }
  116. }
  117. private class JsonSerializerStrategy : SimpleJson.PocoJsonSerializerStrategy
  118. {
  119. // convert string to int
  120. public override object DeserializeObject(object value, Type type)
  121. {
  122. if (type == typeof(Int32) && value.GetType() == typeof(string))
  123. {
  124. return Int32.Parse(value.ToString());
  125. }
  126. return base.DeserializeObject(value, type);
  127. }
  128. }
  129. }
  130. }