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.5 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 bool isVerboseLogging;
  25. public LogViewerConfig logViewer;
  26. public bool useProxy;
  27. public string proxyServer;
  28. public int proxyPort;
  29. private static string CONFIG_FILE = "gui-config.json";
  30. public Server GetCurrentServer()
  31. {
  32. if (index >= 0 && index < configs.Count)
  33. return configs[index];
  34. else
  35. return GetDefaultServer();
  36. }
  37. public static void CheckServer(Server server)
  38. {
  39. CheckPort(server.server_port);
  40. CheckPassword(server.password);
  41. CheckServer(server.server);
  42. }
  43. public static Configuration Load()
  44. {
  45. try
  46. {
  47. string configContent = File.ReadAllText(CONFIG_FILE);
  48. Configuration config = JsonConvert.DeserializeObject<Configuration>(configContent);
  49. config.isDefault = false;
  50. if (config.localPort == 0)
  51. config.localPort = 1080;
  52. if (config.index == -1 && config.strategy == null)
  53. config.index = 0;
  54. return config;
  55. }
  56. catch (Exception e)
  57. {
  58. if (!(e is FileNotFoundException))
  59. Logging.LogUsefulException(e);
  60. return new Configuration
  61. {
  62. index = 0,
  63. isDefault = true,
  64. localPort = 1080,
  65. autoCheckUpdate = true,
  66. configs = new List<Server>()
  67. {
  68. GetDefaultServer()
  69. }
  70. };
  71. }
  72. }
  73. public static void Save(Configuration config)
  74. {
  75. if (config.index >= config.configs.Count)
  76. config.index = config.configs.Count - 1;
  77. if (config.index < -1)
  78. config.index = -1;
  79. if (config.index == -1 && config.strategy == null)
  80. config.index = 0;
  81. config.isDefault = false;
  82. try
  83. {
  84. using (StreamWriter sw = new StreamWriter(File.Open(CONFIG_FILE, FileMode.Create)))
  85. {
  86. string jsonString = JsonConvert.SerializeObject(config, Formatting.Indented);
  87. sw.Write(jsonString);
  88. sw.Flush();
  89. }
  90. }
  91. catch (IOException e)
  92. {
  93. Console.Error.WriteLine(e);
  94. }
  95. }
  96. public static Server GetDefaultServer()
  97. {
  98. return new Server();
  99. }
  100. private static void Assert(bool condition)
  101. {
  102. if (!condition)
  103. throw new Exception(I18N.GetString("assertion failure"));
  104. }
  105. public static void CheckPort(int port)
  106. {
  107. if (port <= 0 || port > 65535)
  108. throw new ArgumentException(I18N.GetString("Port out of range"));
  109. }
  110. public static void CheckLocalPort(int port)
  111. {
  112. CheckPort(port);
  113. if (port == 8123)
  114. throw new ArgumentException(I18N.GetString("Port can't be 8123"));
  115. }
  116. private static void CheckPassword(string password)
  117. {
  118. if (password.IsNullOrEmpty())
  119. throw new ArgumentException(I18N.GetString("Password can not be blank"));
  120. }
  121. public static void CheckServer(string server)
  122. {
  123. if (server.IsNullOrEmpty())
  124. throw new ArgumentException(I18N.GetString("Server IP can not be blank"));
  125. }
  126. }
  127. }