add serialize to json methods for Config

This commit is contained in:
Y. T. Chung
2015-06-21 15:21:28 +08:00
parent 5cf6a8c841
commit 4f52975d17
4 changed files with 78 additions and 12 deletions

View File

@@ -6,7 +6,7 @@ This is a port of [shadowsocks](https://github.com/clowwindy/shadowsocks).
shadowsocks is a fast tunnel proxy that helps you bypass firewalls.
*Currently developing and testing with rust-nightly*
It is unstable! If you encounter any problems, please open an issue.
## Dependences
@@ -54,7 +54,7 @@ Create a shadowsocks' configuration file. Example
Detailed explaination could be found in [shadowsocks' documentation](https://github.com/clowwindy/shadowsocks/wiki).
In shadowsocks-rust, we also have a extended configuration file format, which is able to define more than one servers:
In shadowsocks-rust, we also have an extended configuration file format, which is able to define more than one servers:
```json
{
@@ -93,7 +93,7 @@ List all available arguments with `-h`.
1. `socks5-tool` is to demonstrate how to write a Socks5 client.
2. `ssurl` is for encoding and decoding Shadowsocks URLs. Example: `ss://YWVzLTI1Ni1jZmI6aGVsbG93b3JsZF9mdWNrQDEyNy4wLjAuMTo4Mzg4`
2. `ssurl` is for encoding and decoding ShadowSocks URLs. Example: `ss://YWVzLTI1Ni1jZmI6aGVsbG93b3JsZF9mdWNrQDEyNy4wLjAuMTo4Mzg4`
## Notes
@@ -103,7 +103,7 @@ It supports the following features:
* Crypto algorithms defined in `Cargo.toml`
* **Load balancing**
Currently it uses [simplesched](https://github.com/zonyitoo/simplesched) as the backend support library and it is not support Windows, <del>LoL</del>.
Currently it uses [simplesched](https://github.com/zonyitoo/simplesched) as the backend support library and it does not support Windows, <del>LoL</del>.
## TODO

View File

@@ -5,10 +5,11 @@ extern crate shadowsocks;
use std::str;
use rustc_serialize::base64::{FromBase64, ToBase64, URL_SAFE};
use rustc_serialize::json::{ToJson, as_pretty_json};
use clap::{App, Arg};
use shadowsocks::config::{Config, ConfigType};
use shadowsocks::config::{Config, ConfigType, ServerConfig};
fn encode(filename: &str) {
let config = Config::load_from_file(filename, ConfigType::Server).unwrap();
@@ -49,12 +50,11 @@ fn decode(encoded: &str) {
_ => panic!("Malformed input"),
};
println!("{{");
println!(" \"server\": \"{}\",", addr);
println!(" \"server_port\": {},", port);
println!(" \"method\": \"{}\",", method);
println!(" \"password\": \"{}\"", pwd);
println!("}}");
let svrconfig = ServerConfig::basic(addr.to_owned(), port.parse().unwrap(),
pwd.to_owned(), method.parse().unwrap());
let svrconfig_json = svrconfig.to_json();
println!("{}", as_pretty_json(&svrconfig_json));
}
fn main() {

View File

@@ -91,6 +91,36 @@ pub struct ServerConfig {
pub dns_cache_capacity: usize,
}
impl ServerConfig {
pub fn basic(addr: String, port: u16, password: String, method: CipherType) -> ServerConfig {
ServerConfig {
addr: addr,
port: port,
password: password,
method: method,
timeout: None,
dns_cache_capacity: DEFAULT_DNS_CACHE_CAPACITY,
}
}
}
impl json::ToJson for ServerConfig {
fn to_json(&self) -> json::Json {
use serialize::json::Json;
let mut obj = json::Object::new();
obj.insert("address".to_owned(), Json::String(self.addr.clone()));
obj.insert("port".to_owned(), Json::U64(self.port as u64));
obj.insert("password".to_owned(), Json::String(self.password.clone()));
obj.insert("method".to_owned(), Json::String(self.method.to_string()));
if let Some(t) = self.timeout {
obj.insert("timeout".to_owned(), Json::U64(t as u64));
}
obj.insert("dns_cache_capacity".to_owned(), Json::U64(self.dns_cache_capacity as u64));
Json::Object(obj)
}
}
/// Listening address
pub type ClientConfig = SocketAddr;
@@ -398,3 +428,39 @@ impl Config {
})
}
}
impl json::ToJson for Config {
fn to_json(&self) -> json::Json {
use serialize::json::Json;
let mut obj = json::Object::new();
if self.server.len() == 1 {
// Official format
obj.insert("server".to_owned(), Json::String(self.server[0].addr.clone()));
obj.insert("server_port".to_owned(), Json::U64(self.server[0].port as u64));
obj.insert("password".to_owned(), Json::String(self.server[0].password.clone()));
obj.insert("method".to_owned(), Json::String(self.server[0].method.to_string()));
if let Some(t) = self.server[0].timeout {
obj.insert("timeout".to_owned(), Json::U64(t as u64));
}
} else {
let arr: json::Array = self.server.iter().map(|s| s.to_json()).collect();
obj.insert("servers".to_owned(), Json::Array(arr));
}
if let Some(l) = self.local {
obj.insert("local_address".to_owned(), Json::String(l.ip().to_string()));
obj.insert("local_port".to_owned(), Json::U64(l.port() as u64));
}
Json::Object(obj)
}
}
impl fmt::Display for Config {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use serialize::json::ToJson;
write!(f, "{}", self.to_json())
}
}

View File

@@ -23,7 +23,7 @@
#![crate_name = "shadowsocks"]
#![feature(box_syntax, libc, test, slice_patterns, lookup_host, convert)]
#![feature(slice_bytes, vec_push_all, vec_resize)]
#![feature(slice_bytes, vec_push_all, vec_resize, ip_addr)]
extern crate rustc_serialize as serialize;
#[macro_use]