Skip to content

Commit

Permalink
reformatted
Browse files Browse the repository at this point in the history
  • Loading branch information
zonyitoo committed May 10, 2018
1 parent 8e4a6d5 commit e095db3
Show file tree
Hide file tree
Showing 26 changed files with 268 additions and 302 deletions.
2 changes: 1 addition & 1 deletion rustfmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ condense_wildcard_suffixes = true
#fn_call_style = "Visual"
#chain_indent = "Visual"
normalize_comments = true
use_try_shorthand = true
use_try_shorthand = true
29 changes: 13 additions & 16 deletions src/bin/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! You have to provide all needed configuration attributes via command line parameters,
//! or you could specify a configuration file. The format of configuration file is defined
//! in mod `config`.
//!
//!

extern crate clap;
extern crate env_logger;
Expand All @@ -18,12 +18,12 @@ use std::env;
use std::io::{self, Write};
use std::net::SocketAddr;

use env_logger::Builder;
use env_logger::fmt::Formatter;
use env_logger::Builder;
use log::{LevelFilter, Record};

use shadowsocks::{run_local, Config, ConfigType, ServerAddr, ServerConfig};
use shadowsocks::plugin::PluginConfig;
use shadowsocks::{run_local, Config, ConfigType, ServerAddr, ServerConfig};

fn log_time(fmt: &mut Formatter, without_time: bool, record: &Record) -> io::Result<()> {
if without_time {
Expand Down Expand Up @@ -148,18 +148,16 @@ fn main() {
let mut has_provided_config = false;

let mut config = match matches.value_of("CONFIG") {
Some(cpath) => {
match Config::load_from_file(cpath, ConfigType::Local) {
Ok(cfg) => {
has_provided_config = true;
cfg
}
Err(err) => {
error!("{:?}", err);
return;
}
Some(cpath) => match Config::load_from_file(cpath, ConfigType::Local) {
Ok(cfg) => {
has_provided_config = true;
cfg
}
}
Err(err) => {
error!("{:?}", err);
return;
}
},
None => Config::new(),
};

Expand Down Expand Up @@ -201,8 +199,7 @@ fn main() {

let has_provided_local_config = match matches.value_of("LOCAL_ADDR") {
Some(local_addr) => {
let local_addr: SocketAddr = local_addr.parse()
.expect("`local-addr` is not a valid IP address");
let local_addr: SocketAddr = local_addr.parse().expect("`local-addr` is not a valid IP address");

config.local = Some(local_addr);
true
Expand Down
24 changes: 11 additions & 13 deletions src/bin/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ use std::io::{self, Write};

use clap::{App, Arg};

use env_logger::Builder;
use env_logger::fmt::Formatter;
use env_logger::Builder;
use log::{LevelFilter, Record};

use shadowsocks::{run_server, Config, ConfigType, ServerAddr, ServerConfig};
use shadowsocks::plugin::PluginConfig;
use shadowsocks::{run_server, Config, ConfigType, ServerAddr, ServerConfig};

fn log_time(fmt: &mut Formatter, without_time: bool, record: &Record) -> io::Result<()> {
if without_time {
Expand Down Expand Up @@ -129,18 +129,16 @@ fn main() {

let mut has_provided_config = false;
let mut config = match matches.value_of("CONFIG") {
Some(cpath) => {
match Config::load_from_file(cpath, ConfigType::Server) {
Ok(cfg) => {
has_provided_config = true;
cfg
}
Err(err) => {
error!("{:?}", err);
return;
}
Some(cpath) => match Config::load_from_file(cpath, ConfigType::Server) {
Ok(cfg) => {
has_provided_config = true;
cfg
}
}
Err(err) => {
error!("{:?}", err);
return;
}
},
None => Config::new(),
};

Expand Down
20 changes: 9 additions & 11 deletions src/bin/ssdns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,16 @@ fn main() {
let mut has_provided_config = false;

let mut config = match matches.value_of("CONFIG") {
Some(cpath) => {
match Config::load_from_file(cpath, ConfigType::Local) {
Ok(cfg) => {
has_provided_config = true;
cfg
}
Err(err) => {
error!("{:?}", err);
return;
}
Some(cpath) => match Config::load_from_file(cpath, ConfigType::Local) {
Ok(cfg) => {
has_provided_config = true;
cfg
}
}
Err(err) => {
error!("{:?}", err);
return;
}
},
None => Config::new(),
};

Expand Down
4 changes: 2 additions & 2 deletions src/bin/ssurl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ extern crate shadowsocks;

use clap::{App, Arg};

use qrcode::QrCode;
use qrcode::types::Color;
use qrcode::QrCode;

use shadowsocks::VERSION;
use shadowsocks::config::{Config, ConfigType, ServerConfig};
use shadowsocks::VERSION;

const BLACK: &'static str = "\x1b[40m \x1b[0m";
const WHITE: &'static str = "\x1b[47m \x1b[0m";
Expand Down
58 changes: 27 additions & 31 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,10 @@ impl FromStr for ServerAddr {
Err(..) => {
let mut sp = s.split(':');
match (sp.next(), sp.next()) {
(Some(dn), Some(port)) => {
match port.parse::<u16>() {
Ok(port) => Ok(ServerAddr::DomainName(dn.to_owned(), port)),
Err(..) => Err(ServerAddrError),
}
}
(Some(dn), Some(port)) => match port.parse::<u16>() {
Ok(port) => Ok(ServerAddr::DomainName(dn.to_owned(), port)),
Err(..) => Err(ServerAddrError),
},
_ => Err(ServerAddrError),
}
}
Expand Down Expand Up @@ -287,14 +285,12 @@ impl ServerConfig {

let user_info = parsed.username();
let account = match decode_config(user_info, URL_SAFE_NO_PAD) {
Ok(account) => {
match String::from_utf8(account) {
Ok(ac) => ac,
Err(..) => {
return Err(UrlParseError::InvalidAuthInfo);
}
Ok(account) => match String::from_utf8(account) {
Ok(ac) => ac,
Err(..) => {
return Err(UrlParseError::InvalidAuthInfo);
}
}
},
Err(err) => {
error!("Failed to parse UserInfo with Base64, err: {}", err);
return Err(UrlParseError::InvalidUserInfo);
Expand Down Expand Up @@ -679,17 +675,15 @@ impl Config {

match addr_str.parse::<Ipv4Addr>() {
Ok(ip) => Some(SocketAddr::V4(SocketAddrV4::new(ip, port))),
Err(..) => {
match addr_str.parse::<Ipv6Addr>() {
Ok(ip) => Some(SocketAddr::V6(SocketAddrV6::new(ip, port, 0, 0))),
Err(..) => {
return Err(Error::new(ErrorKind::Malformed,
"`local_address` is not a valid IP \
address",
None))
}
Err(..) => match addr_str.parse::<Ipv6Addr>() {
Ok(ip) => Some(SocketAddr::V6(SocketAddrV6::new(ip, port, 0, 0))),
Err(..) => {
return Err(Error::new(ErrorKind::Malformed,
"`local_address` is not a valid IP \
address",
None))
}
}
},
}
}
None => None,
Expand Down Expand Up @@ -739,15 +733,17 @@ impl Config {
let err = Error::new(ErrorKind::Malformed, "`dns` should be string", None);
return Err(err);
}
Some(dns) => match dns.parse::<IpAddr>() {
Err(..) => {
let err = Error::new(ErrorKind::Malformed, "`dns` should be IpAddr", None);
return Err(err);
}
Ok(addr) => {
config.dns = SocketAddr::new(addr, 53);
Some(dns) => {
match dns.parse::<IpAddr>() {
Err(..) => {
let err = Error::new(ErrorKind::Malformed, "`dns` should be IpAddr", None);
return Err(err);
}
Ok(addr) => {
config.dns = SocketAddr::new(addr, 53);
}
}
},
}
}
}

Expand Down
65 changes: 26 additions & 39 deletions src/crypto/cipher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,24 @@ pub enum CipherType {
Rc4,
Rc4Md5,

#[cfg(feature = "sodium")] ChaCha20,
#[cfg(feature = "sodium")] Salsa20,
#[cfg(feature = "sodium")] XSalsa20,
#[cfg(feature = "sodium")] ChaCha20Ietf,
#[cfg(feature = "sodium")]
ChaCha20,
#[cfg(feature = "sodium")]
Salsa20,
#[cfg(feature = "sodium")]
XSalsa20,
#[cfg(feature = "sodium")]
ChaCha20Ietf,

Aes128Gcm,
Aes256Gcm,

ChaCha20Poly1305,

#[cfg(feature = "miscreant")] Aes128PmacSiv,
#[cfg(feature = "miscreant")] Aes256PmacSiv,
#[cfg(feature = "miscreant")]
Aes128PmacSiv,
#[cfg(feature = "miscreant")]
Aes256PmacSiv,
}

/// Category of ciphers
Expand Down Expand Up @@ -215,35 +221,24 @@ impl CipherType {
match *self {
CipherType::Table | CipherType::Plain => 0,

CipherType::Aes128Cfb1 => {
symm::Cipher::aes_128_cfb1().iv_len()
.expect("iv_len should not be None")
}
CipherType::Aes128Cfb8 => {
symm::Cipher::aes_128_cfb8().iv_len()
.expect("iv_len should not be None")
}
CipherType::Aes128Cfb1 => symm::Cipher::aes_128_cfb1().iv_len()
.expect("iv_len should not be None"),
CipherType::Aes128Cfb8 => symm::Cipher::aes_128_cfb8().iv_len()
.expect("iv_len should not be None"),
CipherType::Aes128Cfb | CipherType::Aes128Cfb128 => {
symm::Cipher::aes_128_cfb128().iv_len()
.expect("iv_len should not be None")
}
CipherType::Aes256Cfb1 => {
symm::Cipher::aes_256_cfb1().iv_len()
.expect("iv_len should not be None")
}
CipherType::Aes256Cfb8 => {
symm::Cipher::aes_256_cfb8().iv_len()
.expect("iv_len should not be None")
}
CipherType::Aes256Cfb1 => symm::Cipher::aes_256_cfb1().iv_len()
.expect("iv_len should not be None"),
CipherType::Aes256Cfb8 => symm::Cipher::aes_256_cfb8().iv_len()
.expect("iv_len should not be None"),
CipherType::Aes256Cfb | CipherType::Aes256Cfb128 => {
symm::Cipher::aes_256_cfb128().iv_len()
.expect("iv_len should not be None")
}

CipherType::Rc4 => {
symm::Cipher::rc4().iv_len()
.expect("iv_len should not be None")
}
CipherType::Rc4 => symm::Cipher::rc4().iv_len().expect("iv_len should not be None"),
CipherType::Rc4Md5 => 16,

#[cfg(feature = "sodium")]
Expand Down Expand Up @@ -410,29 +405,21 @@ impl Display for CipherType {

#[cfg(test)]
mod test_cipher {
use crypto::{new_stream, CipherType, StreamCipher};
use crypto::CryptoMode;
use crypto::{new_stream, CipherType, StreamCipher};

#[test]
fn test_get_cipher() {
let key = CipherType::Aes128Cfb.bytes_to_key(b"PassWORD");
let iv = CipherType::Aes128Cfb.gen_init_vec();
let mut encryptor = new_stream(CipherType::Aes128Cfb,
&key[0..],
&iv[0..],
CryptoMode::Encrypt);
let mut decryptor = new_stream(CipherType::Aes128Cfb,
&key[0..],
&iv[0..],
CryptoMode::Decrypt);
let mut encryptor = new_stream(CipherType::Aes128Cfb, &key[0..], &iv[0..], CryptoMode::Encrypt);
let mut decryptor = new_stream(CipherType::Aes128Cfb, &key[0..], &iv[0..], CryptoMode::Decrypt);
let message = "HELLO WORLD";

let mut encrypted_msg = Vec::new();
encryptor.update(message.as_bytes(), &mut encrypted_msg)
.unwrap();
encryptor.update(message.as_bytes(), &mut encrypted_msg).unwrap();
let mut decrypted_msg = Vec::new();
decryptor.update(&encrypted_msg[..], &mut decrypted_msg)
.unwrap();
decryptor.update(&encrypted_msg[..], &mut decrypted_msg).unwrap();

assert!(message.as_bytes() == &decrypted_msg[..]);
}
Expand Down
15 changes: 8 additions & 7 deletions src/crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@ use std::convert::From;

use openssl::symm;

pub use self::aead::{new_aead_decryptor, new_aead_encryptor, AeadDecryptor, AeadEncryptor, BoxAeadDecryptor,
BoxAeadEncryptor};
pub use self::aead::{
new_aead_decryptor, new_aead_encryptor, AeadDecryptor, AeadEncryptor, BoxAeadDecryptor, BoxAeadEncryptor,
};
pub use self::cipher::{CipherCategory, CipherResult, CipherType};
pub use self::stream::{new_stream, StreamCipher, StreamCipherVariant};

pub mod aead;
pub mod cipher;
pub mod openssl;
pub mod digest;
pub mod table;
pub mod dummy;
pub mod openssl;
pub mod rc4_md5;
pub mod ring;
pub mod dummy;
pub mod aead;
pub mod stream;
#[cfg(feature = "miscreant")]
pub mod siv;
#[cfg(feature = "sodium")]
pub mod sodium;
pub mod stream;
pub mod table;

/// Crypto mode, encrypt or decrypt
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
Expand Down
Loading

0 comments on commit e095db3

Please sign in to comment.