-
Notifications
You must be signed in to change notification settings - Fork 264
/
main.rs
267 lines (250 loc) · 8.52 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#![feature(type_alias_impl_trait)]
use std::{collections::HashMap, process::exit};
use clap::{Parser, Subcommand, ValueEnum};
use tracing_subscriber::{filter::LevelFilter, fmt, prelude::*, EnvFilter};
use shadow_tls::{
sip003::parse_sip003_options, RunningArgs, TlsAddrs, TlsExtConfig, TlsNames, V3Mode,
WildcardSNI,
};
#[derive(Parser, Debug)]
#[clap(
author,
version,
about,
long_about = "A proxy to expose real tls handshake to the firewall.\nGithub: github.com/ihciah/shadow-tls"
)]
struct Args {
#[clap(subcommand)]
cmd: Commands,
#[clap(flatten)]
opts: Opts,
}
#[derive(Parser, Debug, Default, Clone)]
struct Opts {
#[clap(short, long, help = "Set parallelism manually")]
threads: Option<u8>,
#[clap(short, long, help = "Disable TCP_NODELAY")]
disable_nodelay: bool,
#[clap(long, help = "Use v3 protocol")]
v3: bool,
#[clap(long, help = "Strict mode(only for v3 protocol)")]
strict: bool,
}
#[derive(Subcommand, Debug)]
enum Commands {
#[clap(about = "Run client side")]
Client {
#[clap(
long = "listen",
default_value = "[::1]:8080",
help = "Shadow-tls client listen address(like \"[::1]:8080\")"
)]
listen: String,
#[clap(
long = "server",
help = "Your shadow-tls server address(like \"1.2.3.4:443\")"
)]
server_addr: String,
#[clap(
long = "sni",
help = "TLS handshake SNIs(like \"cloud.tencent.com\", \"captive.apple.com;cloud.tencent.com\")",
value_parser = parse_client_names
)]
tls_names: TlsNames,
#[clap(long = "password", help = "Password")]
password: String,
#[clap(
long = "alpn",
help = "Application-Layer Protocol Negotiation list(like \"http/1.1\", \"http/1.1;h2\")",
value_delimiter = ';'
)]
alpn: Option<Vec<String>>,
},
#[clap(about = "Run server side")]
Server {
#[clap(
long = "listen",
default_value = "[::]:443",
help = "Shadow-tls server listen address(like \"[::]:443\")"
)]
listen: String,
#[clap(
long = "server",
help = "Your data server address(like \"127.0.0.1:8080\")"
)]
server_addr: String,
#[clap(
long = "tls",
help = "TLS handshake server address(like \"cloud.tencent.com:443\", \"cloudflare.com:1.1.1.1:443;captive.apple.com;cloud.tencent.com\")",
value_parser = parse_server_addrs
)]
tls_addr: TlsAddrs,
#[clap(long = "password", help = "Password")]
password: String,
#[clap(
long = "wildcard-sni",
default_value = "off",
help = "Use sni:443 as handshake server without predefining mapping(useful for bypass billing system like airplane wifi without modifying server config)"
)]
wildcard_sni: WildcardSNI,
},
}
fn parse_client_names(addrs: &str) -> anyhow::Result<TlsNames> {
TlsNames::try_from(addrs)
}
fn parse_server_addrs(arg: &str) -> anyhow::Result<TlsAddrs> {
TlsAddrs::try_from(arg)
}
impl From<Args> for RunningArgs {
fn from(args: Args) -> Self {
let v3 = match (args.opts.v3, args.opts.strict) {
(true, true) => V3Mode::Strict,
(true, false) => V3Mode::Lossy,
(false, _) => V3Mode::Disabled,
};
match args.cmd {
Commands::Client {
listen,
server_addr,
tls_names,
password,
alpn,
} => Self::Client {
listen_addr: listen,
target_addr: server_addr,
tls_names,
tls_ext: TlsExtConfig::from(alpn),
password,
nodelay: !args.opts.disable_nodelay,
v3,
},
Commands::Server {
listen,
server_addr,
mut tls_addr,
password,
wildcard_sni,
} => {
tls_addr.set_wildcard_sni(wildcard_sni);
Self::Server {
listen_addr: listen,
target_addr: server_addr,
tls_addr,
password,
nodelay: !args.opts.disable_nodelay,
v3,
}
}
}
}
}
// SIP003 [https://shadowsocks.org/en/wiki/Plugin.html](https://shadowsocks.org/en/wiki/Plugin.html)
pub(crate) fn get_sip003_arg() -> Option<Args> {
macro_rules! env {
($key: expr) => {
match std::env::var($key).ok() {
None => return None,
Some(val) if val.is_empty() => return None,
Some(val) => val,
}
};
($key: expr, $fail_fn: expr) => {
match std::env::var($key).ok() {
None => return None,
Some(val) if val.is_empty() => {
$fail_fn();
return None;
}
Some(val) => val,
}
};
}
let ss_remote_host = env!("SS_REMOTE_HOST");
let ss_remote_port = env!("SS_REMOTE_PORT");
let ss_local_host = env!("SS_LOCAL_HOST");
let ss_local_port = env!("SS_LOCAL_PORT");
let ss_plugin_options = env!("SS_PLUGIN_OPTIONS", || {
tracing::error!("need SS_PLUGIN_OPTIONS when as SIP003 plugin");
exit(-1);
});
let opts = parse_sip003_options(&ss_plugin_options).unwrap();
let opts: HashMap<_, _> = opts.into_iter().collect();
let threads = opts.get("threads").map(|s| s.parse::<u8>().unwrap());
let v3 = opts.get("v3").is_some();
let passwd = opts
.get("passwd")
.expect("need passwd param(like passwd=123456)");
let args_opts = crate::Opts {
threads,
v3,
..Default::default()
};
let args = if opts.get("server").is_some() {
let tls_addr = opts
.get("tls")
.expect("tls param must be specified(like tls=xxx.com:443)");
let tls_addrs = parse_server_addrs(tls_addr)
.expect("tls param parse failed(like tls=xxx.com:443 or tls=yyy.com:1.2.3.4:443;zzz.com:443;xxx.com)");
let wildcard_sni =
WildcardSNI::from_str(opts.get("tls").map(AsRef::as_ref).unwrap_or_default(), true)
.expect("wildcard_sni format error");
Args {
cmd: crate::Commands::Server {
listen: format!("{ss_remote_host}:{ss_remote_port}"),
server_addr: format!("{ss_local_host}:{ss_local_port}"),
tls_addr: tls_addrs,
password: passwd.to_owned(),
wildcard_sni,
},
opts: args_opts,
}
} else {
let host = opts
.get("host")
.expect("need host param(like host=www.baidu.com)");
let hosts = parse_client_names(host).expect("tls names parse failed");
Args {
cmd: crate::Commands::Client {
listen: format!("{ss_local_host}:{ss_local_port}"),
server_addr: format!("{ss_remote_host}:{ss_remote_port}"),
tls_names: hosts,
password: passwd.to_owned(),
alpn: Default::default(),
},
opts: args_opts,
}
};
Some(args)
}
fn main() {
tracing_subscriber::registry()
.with(fmt::layer())
.with(
EnvFilter::builder()
.with_default_directive(LevelFilter::INFO.into())
.from_env_lossy()
.add_directive("rustls=off".parse().unwrap()),
)
.init();
let args = get_sip003_arg().unwrap_or_else(Args::parse);
let parallelism = get_parallelism(&args);
let running_args = RunningArgs::from(args);
tracing::info!("Start {parallelism}-thread {running_args}");
if let Err(e) = ctrlc::set_handler(|| std::process::exit(0)) {
tracing::error!("Unable to register signal handler: {e}");
}
let runnable = running_args.build().expect("unable to build runnable");
runnable.start(parallelism).into_iter().for_each(|t| {
if let Err(e) = t.join().expect("couldn't join on the associated thread") {
tracing::error!("Thread exit: {e}");
}
});
}
fn get_parallelism(args: &Args) -> usize {
if let Some(n) = args.opts.threads {
return n as usize;
}
std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(1)
}