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.3 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
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using Shadowsocks.Controller;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Text;
  6. using System.Windows.Forms;
  7. namespace Shadowsocks.Model
  8. {
  9. [Serializable]
  10. public class Configuration
  11. {
  12. public List<Server> configs;
  13. public int index;
  14. public bool global;
  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. }
  37. public static Configuration Load()
  38. {
  39. try
  40. {
  41. string configContent = File.ReadAllText(CONFIG_FILE);
  42. Configuration config = SimpleJson.SimpleJson.DeserializeObject<Configuration>(configContent, new JsonSerializerStrategy());
  43. config.isDefault = false;
  44. return config;
  45. }
  46. catch (Exception e)
  47. {
  48. if (!(e is FileNotFoundException))
  49. {
  50. Console.WriteLine(e);
  51. }
  52. return new Configuration
  53. {
  54. index = 0,
  55. isDefault = true,
  56. configs = new List<Server>()
  57. {
  58. GetDefaultServer()
  59. }
  60. };
  61. }
  62. }
  63. public static void Save(Configuration config)
  64. {
  65. if (config.index >= config.configs.Count)
  66. {
  67. config.index = config.configs.Count - 1;
  68. }
  69. if (config.index < 0)
  70. {
  71. config.index = 0;
  72. }
  73. config.isDefault = false;
  74. try
  75. {
  76. using (StreamWriter sw = new StreamWriter(File.Open(CONFIG_FILE, FileMode.Create)))
  77. {
  78. string jsonString = SimpleJson.SimpleJson.SerializeObject(config);
  79. sw.Write(jsonString);
  80. sw.Flush();
  81. }
  82. }
  83. catch (IOException e)
  84. {
  85. Console.Error.WriteLine(e);
  86. }
  87. }
  88. public static Server GetDefaultServer()
  89. {
  90. return new Server();
  91. }
  92. private static void Assert(bool condition)
  93. {
  94. if (!condition)
  95. {
  96. throw new Exception(I18N.GetString("assertion failure"));
  97. }
  98. }
  99. private static void CheckPort(int port)
  100. {
  101. if (port <= 0 || port > 65535)
  102. {
  103. throw new ArgumentException(I18N.GetString("Port out of range"));
  104. }
  105. }
  106. private static void CheckPassword(string password)
  107. {
  108. if (string.IsNullOrEmpty(password))
  109. {
  110. throw new ArgumentException(I18N.GetString("Password can not be blank"));
  111. }
  112. }
  113. private static void CheckServer(string server)
  114. {
  115. if (string.IsNullOrEmpty(server))
  116. {
  117. throw new ArgumentException(I18N.GetString("Server IP can not be blank"));
  118. }
  119. }
  120. private class JsonSerializerStrategy : SimpleJson.PocoJsonSerializerStrategy
  121. {
  122. // convert string to int
  123. public override object DeserializeObject(object value, Type type)
  124. {
  125. if (type == typeof(Int32) && value.GetType() == typeof(string))
  126. {
  127. return Int32.Parse(value.ToString());
  128. }
  129. return base.DeserializeObject(value, type);
  130. }
  131. }
  132. }
  133. }