|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.IO;
- using System.Diagnostics;
- using SimpleJson;
-
- namespace shadowsocks_csharp.Model
- {
- [Serializable]
- public class Config
- {
- public bool enabled;
- public string server;
- public int server_port;
- public int local_port;
- public string password;
- public string method;
-
- public bool isDefault;
-
- private static void assert(bool condition)
- {
- if(!condition)
- {
- throw new Exception("assertion failure");
- }
- }
-
- public static Config Load()
- {
- try
- {
- using (StreamReader sr = new StreamReader(File.OpenRead(@"config.json")))
- {
- Config config = SimpleJson.SimpleJson.DeserializeObject<Config>(sr.ReadToEnd());
- assert(!string.IsNullOrEmpty(config.server));
- assert(!string.IsNullOrEmpty(config.password));
- assert(config.local_port > 0);
- assert(config.server_port > 0);
- config.isDefault = false;
- return config;
- }
- }
- catch (Exception e)
- {
- Console.WriteLine(e);
- return new Config
- {
- server = "127.0.0.1",
- server_port = 8388,
- local_port = 1080,
- password = "barfoo!",
- method = "table",
- enabled = true,
- isDefault = true
- };
- }
- }
-
- private static void checkPort(int port)
- {
- if (port <= 0 || port > 65535)
- {
- throw new ArgumentException("port out of range");
- }
- }
-
- private static void checkPassword(string password)
- {
- if (string.IsNullOrEmpty(password))
- {
- throw new ArgumentException("password can not be blank");
- }
- }
-
- private static void checkServer(string server)
- {
- if (string.IsNullOrEmpty(server))
- {
- throw new ArgumentException("server can not be blank");
- }
- }
-
- public static void Save(Config config)
- {
- checkPort(config.local_port);
- checkPort(config.server_port);
- checkPassword(config.password);
- checkServer(config.server);
- try
- {
- using (StreamWriter sw = new StreamWriter(File.Open(@"config.json", FileMode.Create)))
- {
- string jsonString = SimpleJson.SimpleJson.SerializeObject(new
- {
- server = config.server,
- server_port = config.server_port,
- local_port = config.local_port,
- password = config.password,
- method = config.method,
- enabled = config.enabled
- });
- sw.Write(jsonString);
- sw.Flush();
- }
- }
- catch (IOException e)
- {
- Console.Error.WriteLine(e);
- }
- }
- }
- }
|