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
9 years ago
10 years ago
9 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using Shadowsocks.Controller;
  5. using Newtonsoft.Json;
  6. namespace Shadowsocks.Model
  7. {
  8. [Serializable]
  9. public class Configuration
  10. {
  11. public List<Server> configs;
  12. // when strategy is set, index is ignored
  13. public string strategy;
  14. public int index;
  15. public bool global;
  16. public bool enabled;
  17. public bool shareOverLan;
  18. public bool isDefault;
  19. public int localPort;
  20. public string pacUrl;
  21. public bool useOnlinePac;
  22. public bool availabilityStatistics;
  23. public bool autoCheckUpdate;
  24. public LogViewerConfig logViewer;
  25. private static string CONFIG_FILE = "gui-config.json";
  26. public Server GetCurrentServer()
  27. {
  28. if (index >= 0 && index < configs.Count)
  29. return configs[index];
  30. else
  31. return GetDefaultServer();
  32. }
  33. public static void CheckServer(Server server)
  34. {
  35. CheckPort(server.server_port);
  36. CheckPassword(server.password);
  37. CheckServer(server.server);
  38. }
  39. public static Configuration Load()
  40. {
  41. try
  42. {
  43. string configContent = File.ReadAllText(CONFIG_FILE);
  44. Configuration config = JsonConvert.DeserializeObject<Configuration>(configContent);
  45. config.isDefault = false;
  46. if (config.localPort == 0)
  47. config.localPort = 1080;
  48. if (config.index == -1 && config.strategy == null)
  49. config.index = 0;
  50. return config;
  51. }
  52. catch (Exception e)
  53. {
  54. if (!(e is FileNotFoundException))
  55. Logging.LogUsefulException(e);
  56. return new Configuration
  57. {
  58. index = 0,
  59. isDefault = true,
  60. localPort = 1080,
  61. autoCheckUpdate = true,
  62. configs = new List<Server>()
  63. {
  64. GetDefaultServer()
  65. }
  66. };
  67. }
  68. }
  69. public static void Save(Configuration config)
  70. {
  71. if (config.index >= config.configs.Count)
  72. config.index = config.configs.Count - 1;
  73. if (config.index < -1)
  74. config.index = -1;
  75. if (config.index == -1 && config.strategy == null)
  76. config.index = 0;
  77. config.isDefault = false;
  78. try
  79. {
  80. using (StreamWriter sw = new StreamWriter(File.Open(CONFIG_FILE, FileMode.Create)))
  81. {
  82. string jsonString = JsonConvert.SerializeObject(config, Formatting.Indented);
  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. private static void Assert(bool condition)
  97. {
  98. if (!condition)
  99. throw new Exception(I18N.GetString("assertion failure"));
  100. }
  101. public static void CheckPort(int port)
  102. {
  103. if (port <= 0 || port > 65535)
  104. throw new ArgumentException(I18N.GetString("Port out of range"));
  105. }
  106. public static void CheckLocalPort(int port)
  107. {
  108. CheckPort(port);
  109. if (port == 8123)
  110. throw new ArgumentException(I18N.GetString("Port can't be 8123"));
  111. }
  112. private static void CheckPassword(string password)
  113. {
  114. if (password.IsNullOrEmpty())
  115. throw new ArgumentException(I18N.GetString("Password can not be blank"));
  116. }
  117. private static void CheckServer(string server)
  118. {
  119. if (server.IsNullOrEmpty())
  120. throw new ArgumentException(I18N.GetString("Server IP can not be blank"));
  121. }
  122. }
  123. }