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.

Config.cs 3.5 kB

12 years ago
12 years ago
12 years ago
10 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
10 years ago
12 years ago
10 years ago
12 years ago
10 years ago
12 years ago
10 years ago
12 years ago
12 years ago
12 years ago
10 years ago
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using System.Diagnostics;
  6. using SimpleJson;
  7. namespace shadowsocks_csharp.Model
  8. {
  9. [Serializable]
  10. public class Config
  11. {
  12. public bool enabled;
  13. public string server;
  14. public int server_port;
  15. public int local_port;
  16. public string password;
  17. public string method;
  18. public bool isDefault;
  19. private static void assert(bool condition)
  20. {
  21. if(!condition)
  22. {
  23. throw new Exception("assertion failure");
  24. }
  25. }
  26. public static Config Load()
  27. {
  28. try
  29. {
  30. using (StreamReader sr = new StreamReader(File.OpenRead(@"config.json")))
  31. {
  32. Config config = SimpleJson.SimpleJson.DeserializeObject<Config>(sr.ReadToEnd());
  33. assert(!string.IsNullOrEmpty(config.server));
  34. assert(!string.IsNullOrEmpty(config.password));
  35. assert(config.local_port > 0);
  36. assert(config.server_port > 0);
  37. config.isDefault = false;
  38. return config;
  39. }
  40. }
  41. catch (Exception e)
  42. {
  43. Console.WriteLine(e);
  44. return new Config
  45. {
  46. server = "127.0.0.1",
  47. server_port = 8388,
  48. local_port = 1080,
  49. password = "barfoo!",
  50. method = "table",
  51. enabled = true,
  52. isDefault = true
  53. };
  54. }
  55. }
  56. private static void checkPort(int port)
  57. {
  58. if (port <= 0 || port > 65535)
  59. {
  60. throw new ArgumentException("port out of range");
  61. }
  62. }
  63. private static void checkPassword(string password)
  64. {
  65. if (string.IsNullOrEmpty(password))
  66. {
  67. throw new ArgumentException("password can not be blank");
  68. }
  69. }
  70. private static void checkServer(string server)
  71. {
  72. if (string.IsNullOrEmpty(server))
  73. {
  74. throw new ArgumentException("server can not be blank");
  75. }
  76. }
  77. public static void Save(Config config)
  78. {
  79. checkPort(config.local_port);
  80. checkPort(config.server_port);
  81. checkPassword(config.password);
  82. checkServer(config.server);
  83. try
  84. {
  85. using (StreamWriter sw = new StreamWriter(File.Open(@"config.json", FileMode.Create)))
  86. {
  87. string jsonString = SimpleJson.SimpleJson.SerializeObject(new
  88. {
  89. server = config.server,
  90. server_port = config.server_port,
  91. local_port = config.local_port,
  92. password = config.password,
  93. method = config.method,
  94. enabled = config.enabled
  95. });
  96. sw.Write(jsonString);
  97. sw.Flush();
  98. }
  99. }
  100. catch (IOException e)
  101. {
  102. Console.Error.WriteLine(e);
  103. }
  104. }
  105. }
  106. }