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 2.5 kB

12 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
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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
  8. {
  9. [Serializable]
  10. public class Config
  11. {
  12. public string server;
  13. public int server_port;
  14. public int local_port;
  15. public string password;
  16. public string method;
  17. public bool isDefault;
  18. private static void assert(bool condition)
  19. {
  20. if(!condition)
  21. {
  22. throw new Exception("assertion failure");
  23. }
  24. }
  25. public static Config Load()
  26. {
  27. try
  28. {
  29. using (StreamReader sr = new StreamReader(File.OpenRead(@"config.json")))
  30. {
  31. Config config = SimpleJson.SimpleJson.DeserializeObject<Config>(sr.ReadToEnd());
  32. assert(!string.IsNullOrEmpty(config.server));
  33. assert(!string.IsNullOrEmpty(config.password));
  34. assert(config.local_port > 0);
  35. assert(config.server_port > 0);
  36. config.isDefault = false;
  37. return config;
  38. }
  39. }
  40. catch (Exception e)
  41. {
  42. Console.WriteLine(e);
  43. return new Config
  44. {
  45. server = "127.0.0.1",
  46. server_port = 8388,
  47. local_port = 1081,
  48. password = "barfoo!",
  49. method = "table",
  50. isDefault = true
  51. };
  52. }
  53. }
  54. public static void Save(Config config)
  55. {
  56. try
  57. {
  58. using (StreamWriter sw = new StreamWriter(File.Open(@"config.json", FileMode.Create)))
  59. {
  60. string jsonString = SimpleJson.SimpleJson.SerializeObject(new
  61. {
  62. server = config.server,
  63. server_port = config.server_port,
  64. local_port = config.local_port,
  65. password = config.password,
  66. method = config.method
  67. });
  68. sw.Write(jsonString);
  69. sw.Flush();
  70. }
  71. }
  72. catch (IOException e)
  73. {
  74. Console.Error.WriteLine(e);
  75. }
  76. }
  77. }
  78. }

No Description