|
| 1 | +//! This is a binary running in the local environment |
| 2 | +//! |
| 3 | +//! You have to provide all needed configuration attributes via command line parameters, |
| 4 | +//! or you could specify a configuration file. The format of configuration file is defined |
| 5 | +//! in mod `config`. |
| 6 | +
|
| 7 | +use clap::{App, Arg}; |
| 8 | +use futures::{ |
| 9 | + future::{self, Either}, |
| 10 | + FutureExt, |
| 11 | +}; |
| 12 | +use log::{debug, error, info}; |
| 13 | +use tokio::runtime::Builder; |
| 14 | + |
| 15 | +use shadowsocks::{ |
| 16 | + plugin::PluginConfig, |
| 17 | + relay::socks5::Address, |
| 18 | + run_local, |
| 19 | + Config, |
| 20 | + ConfigType, |
| 21 | + Mode, |
| 22 | + ServerAddr, |
| 23 | + ServerConfig, |
| 24 | +}; |
| 25 | + |
| 26 | +mod logging; |
| 27 | +mod monitor; |
| 28 | + |
| 29 | +fn main() { |
| 30 | + let matches = App::new("shadowsocks") |
| 31 | + .version(shadowsocks::VERSION) |
| 32 | + .about("A fast tunnel proxy that helps you bypass firewalls.") |
| 33 | + .arg( |
| 34 | + Arg::with_name("VERBOSE") |
| 35 | + .short("v") |
| 36 | + .multiple(true) |
| 37 | + .help("Set the level of debug"), |
| 38 | + ) |
| 39 | + .arg(Arg::with_name("UDP_ONLY").short("u").help("Server mode UDP_ONLY")) |
| 40 | + .arg(Arg::with_name("TCP_AND_UDP").short("U").help("Server mode TCP_AND_UDP")) |
| 41 | + .arg( |
| 42 | + Arg::with_name("CONFIG") |
| 43 | + .short("c") |
| 44 | + .long("config") |
| 45 | + .takes_value(true) |
| 46 | + .help("Specify config file"), |
| 47 | + ) |
| 48 | + .arg( |
| 49 | + Arg::with_name("SERVER_ADDR") |
| 50 | + .short("s") |
| 51 | + .long("server-addr") |
| 52 | + .takes_value(true) |
| 53 | + .help("Server address"), |
| 54 | + ) |
| 55 | + .arg( |
| 56 | + Arg::with_name("LOCAL_ADDR") |
| 57 | + .short("b") |
| 58 | + .long("local-addr") |
| 59 | + .takes_value(true) |
| 60 | + .help("Local address, listen only to this address if specified"), |
| 61 | + ) |
| 62 | + .arg( |
| 63 | + Arg::with_name("PASSWORD") |
| 64 | + .short("k") |
| 65 | + .long("password") |
| 66 | + .takes_value(true) |
| 67 | + .help("Password"), |
| 68 | + ) |
| 69 | + .arg( |
| 70 | + Arg::with_name("ENCRYPT_METHOD") |
| 71 | + .short("m") |
| 72 | + .long("encrypt-method") |
| 73 | + .takes_value(true) |
| 74 | + .help("Encryption method"), |
| 75 | + ) |
| 76 | + .arg( |
| 77 | + Arg::with_name("PLUGIN") |
| 78 | + .long("plugin") |
| 79 | + .takes_value(true) |
| 80 | + .help("Enable SIP003 plugin"), |
| 81 | + ) |
| 82 | + .arg( |
| 83 | + Arg::with_name("PLUGIN_OPT") |
| 84 | + .long("plugin-opts") |
| 85 | + .takes_value(true) |
| 86 | + .help("Set SIP003 plugin options"), |
| 87 | + ) |
| 88 | + .arg( |
| 89 | + Arg::with_name("URL") |
| 90 | + .long("server-url") |
| 91 | + .takes_value(true) |
| 92 | + .help("Server address in SIP002 URL"), |
| 93 | + ) |
| 94 | + .arg( |
| 95 | + Arg::with_name("NO_DELAY") |
| 96 | + .long("no-delay") |
| 97 | + .takes_value(false) |
| 98 | + .help("Set no-delay option for socket"), |
| 99 | + ) |
| 100 | + .arg( |
| 101 | + Arg::with_name("NOFILE") |
| 102 | + .short("n") |
| 103 | + .long("nofile") |
| 104 | + .takes_value(true) |
| 105 | + .help("Set RLIMIT_NOFILE with both soft and hard limit (only for *nix systems)"), |
| 106 | + ) |
| 107 | + .get_matches(); |
| 108 | + |
| 109 | + let debug_level = matches.occurrences_of("VERBOSE"); |
| 110 | + logging::init(debug_level, "ssredir"); |
| 111 | + |
| 112 | + let mut has_provided_config = false; |
| 113 | + |
| 114 | + let mut config = match matches.value_of("CONFIG") { |
| 115 | + Some(cpath) => match Config::load_from_file(cpath, ConfigType::RedirLocal) { |
| 116 | + Ok(cfg) => { |
| 117 | + has_provided_config = true; |
| 118 | + cfg |
| 119 | + } |
| 120 | + Err(err) => { |
| 121 | + error!("{:?}", err); |
| 122 | + return; |
| 123 | + } |
| 124 | + }, |
| 125 | + None => Config::new(ConfigType::RedirLocal), |
| 126 | + }; |
| 127 | + |
| 128 | + let mut has_provided_server_config = match ( |
| 129 | + matches.value_of("SERVER_ADDR"), |
| 130 | + matches.value_of("PASSWORD"), |
| 131 | + matches.value_of("ENCRYPT_METHOD"), |
| 132 | + ) { |
| 133 | + (Some(svr_addr), Some(password), Some(method)) => { |
| 134 | + let method = match method.parse() { |
| 135 | + Ok(m) => m, |
| 136 | + Err(err) => { |
| 137 | + panic!("Does not support {:?} method: {:?}", method, err); |
| 138 | + } |
| 139 | + }; |
| 140 | + |
| 141 | + let sc = ServerConfig::new( |
| 142 | + svr_addr |
| 143 | + .parse::<ServerAddr>() |
| 144 | + .expect("`server-addr` invalid, \"IP:Port\" or \"Domain:Port\""), |
| 145 | + password.to_owned(), |
| 146 | + method, |
| 147 | + None, |
| 148 | + None, |
| 149 | + ); |
| 150 | + |
| 151 | + config.server.push(sc); |
| 152 | + true |
| 153 | + } |
| 154 | + (None, None, None) => { |
| 155 | + // Does not provide server config |
| 156 | + false |
| 157 | + } |
| 158 | + _ => { |
| 159 | + panic!("`server-addr`, `method` and `password` should be provided together"); |
| 160 | + } |
| 161 | + }; |
| 162 | + |
| 163 | + if let Some(url) = matches.value_of("URL") { |
| 164 | + let svr_addr = url.parse::<ServerConfig>().expect("Failed to parse `url`"); |
| 165 | + |
| 166 | + has_provided_server_config = true; |
| 167 | + |
| 168 | + config.server.push(svr_addr); |
| 169 | + } |
| 170 | + |
| 171 | + let has_provided_local_config = match matches.value_of("LOCAL_ADDR") { |
| 172 | + Some(local_addr) => { |
| 173 | + let local_addr = local_addr |
| 174 | + .parse::<ServerAddr>() |
| 175 | + .expect("`local-addr` invalid, \"IP:Port\" or \"Domain:Port\""); |
| 176 | + |
| 177 | + config.local = Some(local_addr); |
| 178 | + true |
| 179 | + } |
| 180 | + None => false, |
| 181 | + }; |
| 182 | + |
| 183 | + if !(has_provided_config || (has_provided_server_config && has_provided_local_config)) { |
| 184 | + println!("You have to specify a configuration file or pass arguments by argument list"); |
| 185 | + println!("{}", matches.usage()); |
| 186 | + return; |
| 187 | + } |
| 188 | + |
| 189 | + if let Some(url) = matches.value_of("FORWARD_ADDR") { |
| 190 | + let forward_addr = url.parse::<Address>().expect("Failed to parse `url`"); |
| 191 | + |
| 192 | + config.forward = Some(forward_addr); |
| 193 | + } |
| 194 | + |
| 195 | + if matches.is_present("UDP_ONLY") { |
| 196 | + if config.mode.enable_tcp() { |
| 197 | + config.mode = Mode::TcpAndUdp; |
| 198 | + } else { |
| 199 | + config.mode = Mode::UdpOnly; |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + if matches.is_present("TCP_AND_UDP") { |
| 204 | + config.mode = Mode::TcpAndUdp; |
| 205 | + } |
| 206 | + |
| 207 | + if matches.is_present("NO_DELAY") { |
| 208 | + config.no_delay = true; |
| 209 | + } |
| 210 | + |
| 211 | + if let Some(p) = matches.value_of("PLUGIN") { |
| 212 | + let plugin = PluginConfig { |
| 213 | + plugin: p.to_owned(), |
| 214 | + plugin_opt: matches.value_of("PLUGIN_OPT").map(ToOwned::to_owned), |
| 215 | + }; |
| 216 | + |
| 217 | + // Overrides config in file |
| 218 | + for svr in config.server.iter_mut() { |
| 219 | + svr.set_plugin(plugin.clone()); |
| 220 | + } |
| 221 | + }; |
| 222 | + |
| 223 | + if let Some(nofile) = matches.value_of("NOFILE") { |
| 224 | + config.nofile = Some( |
| 225 | + nofile |
| 226 | + .parse::<u64>() |
| 227 | + .expect("Expecting an unsigned integer for `nofile`"), |
| 228 | + ); |
| 229 | + } |
| 230 | + |
| 231 | + info!("ShadowSocks {}", shadowsocks::VERSION); |
| 232 | + |
| 233 | + debug!("Config: {:?}", config); |
| 234 | + |
| 235 | + let mut builder = Builder::new(); |
| 236 | + if cfg!(feature = "single-threaded") { |
| 237 | + builder.basic_scheduler(); |
| 238 | + } else { |
| 239 | + builder.threaded_scheduler(); |
| 240 | + } |
| 241 | + let mut runtime = builder.enable_all().build().expect("Unable to create Tokio Runtime"); |
| 242 | + let rt_handle = runtime.handle().clone(); |
| 243 | + |
| 244 | + runtime.block_on(async move { |
| 245 | + let abort_signal = monitor::create_signal_monitor(); |
| 246 | + match future::select(run_local(config, rt_handle).boxed(), abort_signal.boxed()).await { |
| 247 | + // Server future resolved without an error. This should never happen. |
| 248 | + Either::Left(_) => panic!("Server exited unexpectly"), |
| 249 | + // The abort signal future resolved. Means we should just exit. |
| 250 | + Either::Right(_) => (), |
| 251 | + } |
| 252 | + }) |
| 253 | +} |
0 commit comments