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.2 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.Serialization.Json;
  4. using System.Text;
  5. using System.IO;
  6. using System.Diagnostics;
  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. [NonSerialized]
  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. DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Config));
  28. try
  29. {
  30. using (FileStream fs = File.OpenRead(@"config.json"))
  31. {
  32. Config config = ser.ReadObject(fs) as Config;
  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 = 1081,
  49. password = "barfoo!",
  50. isDefault = true
  51. };
  52. }
  53. }
  54. public static void Save(Config config)
  55. {
  56. DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Config));
  57. try
  58. {
  59. using (FileStream fs = File.Open(@"config.json", FileMode.Create))
  60. {
  61. ser.WriteObject(fs, config);
  62. }
  63. }
  64. catch (IOException e)
  65. {
  66. Console.Error.WriteLine(e);
  67. }
  68. }
  69. }
  70. }

No Description

Contributors (1)