-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
server.rs
575 lines (482 loc) · 18.5 KB
/
server.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
//! Shadowsocks Manager server
#[cfg(unix)]
use std::path::PathBuf;
use std::{collections::HashMap, io, net::SocketAddr, sync::Arc, time::Duration};
use log::{error, info, trace};
use shadowsocks::{
config::{Mode, ServerConfig, ServerType},
context::{Context, SharedContext},
crypto::v1::CipherKind,
dns_resolver::DnsResolver,
manager::protocol::{
self,
AddRequest,
AddResponse,
ErrorResponse,
ListResponse,
ManagerRequest,
PingResponse,
RemoveRequest,
RemoveResponse,
StatRequest,
},
net::{AcceptOpts, ConnectOpts},
plugin::PluginConfig,
ManagerListener,
ServerAddr,
};
use tokio::{sync::Mutex, task::JoinHandle};
use crate::{
acl::AccessControl,
config::{ManagerConfig, ManagerServerHost, ManagerServerMode, SecurityConfig},
net::FlowStat,
server::Server,
};
enum ServerInstanceMode {
Builtin {
flow_stat: Arc<FlowStat>,
abortable: JoinHandle<io::Result<()>>,
},
#[cfg(unix)]
Standalone { flow_stat: u64 },
}
struct ServerInstance {
mode: ServerInstanceMode,
svr_cfg: ServerConfig,
}
impl Drop for ServerInstance {
fn drop(&mut self) {
#[allow(irrefutable_let_patterns)]
if let ServerInstanceMode::Builtin { ref abortable, .. } = self.mode {
abortable.abort();
}
}
}
impl ServerInstance {
fn flow_stat(&self) -> u64 {
match self.mode {
ServerInstanceMode::Builtin { ref flow_stat, .. } => flow_stat.tx() + flow_stat.rx(),
#[cfg(unix)]
ServerInstanceMode::Standalone { flow_stat } => flow_stat,
}
}
}
/// Manager server
pub struct Manager {
context: SharedContext,
servers: Mutex<HashMap<u16, ServerInstance>>,
svr_cfg: ManagerConfig,
connect_opts: ConnectOpts,
accept_opts: AcceptOpts,
udp_expiry_duration: Option<Duration>,
udp_capacity: Option<usize>,
acl: Option<Arc<AccessControl>>,
ipv6_first: bool,
security: SecurityConfig,
}
impl Manager {
/// Create a new manager server from configuration
pub fn new(svr_cfg: ManagerConfig) -> Manager {
Manager::with_context(svr_cfg, Context::new_shared(ServerType::Server))
}
/// Create a new manager server with context and configuration
pub(crate) fn with_context(svr_cfg: ManagerConfig, context: SharedContext) -> Manager {
Manager {
context,
servers: Mutex::new(HashMap::new()),
svr_cfg,
connect_opts: ConnectOpts::default(),
accept_opts: AcceptOpts::default(),
udp_expiry_duration: None,
udp_capacity: None,
acl: None,
ipv6_first: false,
security: SecurityConfig::default(),
}
}
/// Set `ConnectOpts`
pub fn set_connect_opts(&mut self, opts: ConnectOpts) {
self.connect_opts = opts;
}
/// Set `AcceptOpts`
pub fn set_accept_opts(&mut self, opts: AcceptOpts) {
self.accept_opts = opts;
}
/// Set UDP association's expiry duration
pub fn set_udp_expiry_duration(&mut self, d: Duration) {
self.udp_expiry_duration = Some(d);
}
/// Set total UDP associations to be kept in one server
pub fn set_udp_capacity(&mut self, c: usize) {
self.udp_capacity = Some(c);
}
/// Get the manager's configuration
pub fn config(&self) -> &ManagerConfig {
&self.svr_cfg
}
/// Get customized DNS resolver
pub fn set_dns_resolver(&mut self, resolver: Arc<DnsResolver>) {
let context = Arc::get_mut(&mut self.context).expect("cannot set DNS resolver on a shared context");
context.set_dns_resolver(resolver)
}
/// Set access control list
pub fn set_acl(&mut self, acl: Arc<AccessControl>) {
self.acl = Some(acl);
}
/// Try to connect IPv6 addresses first if hostname could be resolved to both IPv4 and IPv6
pub fn set_ipv6_first(&mut self, ipv6_first: bool) {
self.ipv6_first = ipv6_first;
}
/// Set security config
pub fn set_security_config(&mut self, security: SecurityConfig) {
self.security = security;
}
/// Start serving
pub async fn run(self) -> io::Result<()> {
let mut listener = ManagerListener::bind(&self.context, &self.svr_cfg.addr).await?;
let local_addr = listener.local_addr()?;
info!("shadowsocks manager server listening on {}", local_addr);
loop {
let (req, peer_addr) = match listener.recv_from().await {
Ok(r) => r,
Err(err) => {
error!("manager recv_from error: {}", err);
continue;
}
};
trace!("received {:?} from {:?}", req, peer_addr);
match req {
ManagerRequest::Add(ref req) => match self.handle_add(req).await {
Ok(rsp) => {
let _ = listener.send_to(&rsp, &peer_addr).await;
}
Err(err) => {
error!("add server_port: {} failed, error: {}", req.server_port, err);
let rsp = ErrorResponse(err);
let _ = listener.send_to(&rsp, &peer_addr).await;
}
},
ManagerRequest::Remove(ref req) => {
let rsp = self.handle_remove(req).await;
let _ = listener.send_to(&rsp, &peer_addr).await;
}
ManagerRequest::List(..) => {
let rsp = self.handle_list().await;
let _ = listener.send_to(&rsp, &peer_addr).await;
}
ManagerRequest::Ping(..) => {
let rsp = self.handle_ping().await;
let _ = listener.send_to(&rsp, &peer_addr).await;
}
ManagerRequest::Stat(ref stat) => self.handle_stat(stat).await,
}
}
}
pub async fn add_server(&self, svr_cfg: ServerConfig) {
match self.svr_cfg.server_mode {
ManagerServerMode::Builtin => self.add_server_builtin(svr_cfg).await,
#[cfg(unix)]
ManagerServerMode::Standalone => self.add_server_standalone(svr_cfg).await,
}
}
async fn add_server_builtin(&self, svr_cfg: ServerConfig) {
// Each server should use a separate Context, but shares
//
// * AccessControlList
// * DNS Resolver
let mut server = Server::new(svr_cfg.clone());
server.set_connect_opts(self.connect_opts.clone());
server.set_accept_opts(self.accept_opts.clone());
server.set_dns_resolver(self.context.dns_resolver().clone());
if let Some(d) = self.udp_expiry_duration {
server.set_udp_expiry_duration(d);
}
if let Some(c) = self.udp_capacity {
server.set_udp_capacity(c);
}
if let Some(ref acl) = self.acl {
server.set_acl(acl.clone());
}
if self.ipv6_first {
server.set_ipv6_first(self.ipv6_first);
}
server.set_security_config(&self.security);
let server_port = server.config().addr().port();
let mut servers = self.servers.lock().await;
// Close existed server
if let Some(v) = servers.remove(&server_port) {
info!(
"closed managed server listening on {}, inbound address {}",
v.svr_cfg.addr(),
v.svr_cfg.external_addr()
);
}
let flow_stat = server.flow_stat();
let abortable = tokio::spawn(async move { server.run().await });
servers.insert(
server_port,
ServerInstance {
mode: ServerInstanceMode::Builtin { flow_stat, abortable },
svr_cfg,
},
);
}
#[cfg(unix)]
fn server_pid_path(&self, port: u16) -> PathBuf {
let pid_file_name = format!("shadowsocks-server-{}.pid", port);
let mut pid_path = self.svr_cfg.server_working_directory.clone();
pid_path.push(&pid_file_name);
pid_path
}
#[cfg(unix)]
fn server_config_path(&self, port: u16) -> PathBuf {
let config_file_name = format!("shadowsocks-server-{}.json", port);
let mut config_file_path = self.svr_cfg.server_working_directory.clone();
config_file_path.push(&config_file_name);
config_file_path
}
#[cfg(unix)]
fn kill_standalone_server(&self, port: u16) {
use log::{debug, warn};
use std::{
fs::{self, File},
io::Read,
};
let pid_path = self.server_pid_path(port);
if pid_path.exists() {
if let Ok(mut pid_file) = File::open(&pid_path) {
let mut pid_content = String::new();
if let Ok(..) = pid_file.read_to_string(&mut pid_content) {
let pid_content = pid_content.trim();
match pid_content.parse::<libc::pid_t>() {
Ok(pid) => {
let _ = unsafe { libc::kill(pid, libc::SIGTERM) };
debug!("killed standalone server port {}, pid: {}", port, pid);
}
Err(..) => {
warn!("failed to read pid from {}", pid_path.display());
}
}
}
}
}
let server_config_path = self.server_config_path(port);
let _ = fs::remove_file(&pid_path);
let _ = fs::remove_file(&server_config_path);
}
#[cfg(unix)]
async fn add_server_standalone(&self, svr_cfg: ServerConfig) {
use std::{
fs::{self, OpenOptions},
io::Write,
};
use tokio::process::Command;
use crate::config::{Config, ConfigType};
// Lock the map first incase there are multiple requests to create one server instance
let mut servers = self.servers.lock().await;
// Check if working_directory exists
if !self.svr_cfg.server_working_directory.exists() {
fs::create_dir_all(&self.svr_cfg.server_working_directory).expect("create working_directory");
}
let port = svr_cfg.addr().port();
// Check if there is already a running process
self.kill_standalone_server(port);
// Create configuration file for server
let config_file_path = self.server_config_path(port);
let pid_path = self.server_pid_path(port);
let mut config = Config::new(ConfigType::Server);
config.server.push(svr_cfg.clone());
trace!("created standalone server with config {:?}", config);
let config_file_content = format!("{}", config);
match OpenOptions::new().write(true).create(true).open(&config_file_path) {
Err(err) => {
error!(
"failed to open {} for writing, error: {}",
config_file_path.display(),
err
);
return;
}
Ok(mut file) => {
if let Err(err) = file.write_all(config_file_content.as_bytes()) {
error!("failed to write {}, error: {}", config_file_path.display(), err);
return;
}
let _ = file.sync_data();
}
}
let manager_addr = self.svr_cfg.addr.to_string();
// Start server process
let child_result = Command::new(&self.svr_cfg.server_program)
.arg("-c")
.arg(&config_file_path)
.arg("--daemonize")
.arg("--daemonize-pid")
.arg(&pid_path)
.arg("--manager-addr")
.arg(&manager_addr)
.kill_on_drop(false)
.spawn();
if let Err(err) = child_result {
error!(
"failed to spawn process of {}, error: {}",
self.svr_cfg.server_program, err
);
return;
}
// Greate. Record into the map
servers.insert(
port,
ServerInstance {
mode: ServerInstanceMode::Standalone { flow_stat: 0 },
svr_cfg,
},
);
}
async fn handle_add(&self, req: &AddRequest) -> io::Result<AddResponse> {
let addr = match self.svr_cfg.server_host {
ManagerServerHost::Domain(ref dname) => ServerAddr::DomainName(dname.clone(), req.server_port),
ManagerServerHost::Ip(ip) => ServerAddr::SocketAddr(SocketAddr::new(ip, req.server_port)),
};
let method = match req.method {
Some(ref m) => match m.parse::<CipherKind>() {
Ok(method) => method,
Err(..) => {
error!("unrecognized method \"{}\", req: {:?}", m, req);
let err = format!("unrecognized method \"{}\"", m);
return Ok(AddResponse(err));
}
},
None => self.svr_cfg.method.unwrap_or(CipherKind::CHACHA20_POLY1305),
};
let mut svr_cfg = ServerConfig::new(addr, req.password.clone(), method);
if let Some(ref plugin) = req.plugin {
let p = PluginConfig {
plugin: plugin.clone(),
plugin_opts: req.plugin_opts.clone(),
plugin_args: Vec::new(),
};
svr_cfg.set_plugin(p);
} else if let Some(ref plugin) = self.svr_cfg.plugin {
svr_cfg.set_plugin(plugin.clone());
}
let mode = match req.mode {
None => None,
Some(ref mode) => match mode.parse::<Mode>() {
Ok(m) => Some(m),
Err(..) => {
error!("unrecognized mode \"{}\", req: {:?}", mode, req);
let err = format!("unrecognized mode \"{}\"", mode);
return Ok(AddResponse(err));
}
},
};
svr_cfg.set_mode(mode.unwrap_or(self.svr_cfg.mode));
self.add_server(svr_cfg).await;
Ok(AddResponse("ok".to_owned()))
}
async fn handle_remove(&self, req: &RemoveRequest) -> RemoveResponse {
let mut servers = self.servers.lock().await;
servers.remove(&req.server_port);
#[cfg(unix)]
if self.svr_cfg.server_mode == ManagerServerMode::Standalone {
self.kill_standalone_server(req.server_port);
}
RemoveResponse("ok".to_owned())
}
async fn handle_list(&self) -> ListResponse {
let instances = self.servers.lock().await;
let mut servers = Vec::new();
for (_, server) in instances.iter() {
let svr_cfg = &server.svr_cfg;
let sc = protocol::ServerConfig {
server_port: svr_cfg.addr().port(),
password: svr_cfg.password().to_owned(),
method: None,
no_delay: None,
plugin: None,
plugin_opts: None,
mode: None,
};
servers.push(sc);
}
ListResponse { servers }
}
async fn handle_ping(&self) -> PingResponse {
let instances = self.servers.lock().await;
let mut stat = HashMap::new();
for (port, server) in instances.iter() {
stat.insert(*port, server.flow_stat());
}
PingResponse { stat }
}
#[cfg(not(unix))]
async fn handle_stat(&self, _: &StatRequest) {}
#[cfg(unix)]
async fn handle_stat(&self, stat: &StatRequest) {
use log::warn;
use std::collections::hash_map::Entry;
use crate::config::{Config, ConfigType};
// `stat` is only supported for Standalone mode
if self.svr_cfg.server_mode != ManagerServerMode::Standalone {
return;
}
let mut instances = self.servers.lock().await;
// Get or create a new instance then record the data statistic numbers
for (port, flow) in stat.stat.iter() {
match instances.entry(*port) {
Entry::Occupied(mut occ) => match occ.get_mut().mode {
ServerInstanceMode::Builtin { .. } => {
error!("received `stat` for port {} that is running a builtin server", *port)
}
ServerInstanceMode::Standalone { ref mut flow_stat } => *flow_stat = *flow,
},
Entry::Vacant(vac) => {
// Read config from file
let server_config_path = self.server_config_path(*port);
if !server_config_path.exists() {
warn!(
"received `stat` for port {} but file {} doesn't exist",
*port,
server_config_path.display()
);
continue;
}
match Config::load_from_file(&server_config_path, ConfigType::Server) {
Err(err) => {
error!(
"failed to load {} for server port {}, error: {}",
server_config_path.display(),
*port,
err
);
continue;
}
Ok(config) => {
trace!(
"loaded {} for server port {}, {:?}",
server_config_path.display(),
*port,
config
);
if config.server.len() != 1 {
error!(
"invalid config {} for server port {}, containing {} servers",
server_config_path.display(),
*port,
config.server.len()
);
continue;
}
let svr_cfg = config.server[0].clone();
vac.insert(ServerInstance {
mode: ServerInstanceMode::Standalone { flow_stat: *flow },
svr_cfg,
});
}
}
}
}
}
}
}