shadowsocks-rust 1.24.0

shadowsocks is a fast tunnel proxy that helps you bypass firewalls.
Documentation
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
//! Common configuration utilities

use std::{
    env,
    fs::OpenOptions,
    io::{self, Read},
    path::{Path, PathBuf},
};

use clap::ArgMatches;
use directories::ProjectDirs;
use serde::Deserialize;

/// Default configuration file path
pub fn get_default_config_path(config_file: &str) -> Option<PathBuf> {
    // config.json in the current working directory ($PWD)
    let config_files = vec![config_file, "config.json"];
    if let Ok(mut path) = env::current_dir() {
        for filename in &config_files {
            path.push(filename);
            if path.exists() {
                return Some(path);
            }
            path.pop();
        }
    } else {
        // config.json in the current working directory (relative path)
        for filename in &config_files {
            let relative_path = PathBuf::from(filename);
            if relative_path.exists() {
                return Some(relative_path);
            }
        }
    }

    // System standard directories
    if let Some(project_dirs) = ProjectDirs::from("org", "shadowsocks", "shadowsocks-rust") {
        // Linux: $XDG_CONFIG_HOME/shadowsocks-rust/config.json
        //        $HOME/.config/shadowsocks-rust/config.json
        // macOS: $HOME/Library/Application Support/org.shadowsocks.shadowsocks-rust/config.json
        // Windows: {FOLDERID_RoamingAppData}/shadowsocks/shadowsocks-rust/config/config.json

        let mut config_path = project_dirs.config_dir().to_path_buf();
        for filename in &config_files {
            config_path.push(filename);
            if config_path.exists() {
                return Some(config_path);
            }
            config_path.pop();
        }
    }

    // UNIX systems, XDG Base Directory
    // https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
    #[cfg(unix)]
    {
        let base_directories = xdg::BaseDirectories::with_prefix("shadowsocks-rust");
        // $XDG_CONFIG_HOME/shadowsocks-rust/config.json
        // for dir in $XDG_CONFIG_DIRS; $dir/shadowsocks-rust/config.json
        for filename in &config_files {
            if let Some(config_path) = base_directories.find_config_file(filename) {
                return Some(config_path);
            }
        }
    }

    // UNIX global configuration file
    #[cfg(unix)]
    {
        let mut global_config_path = PathBuf::from("/etc/shadowsocks-rust");
        for filename in &config_files {
            global_config_path.push(filename);
            if global_config_path.exists() {
                return Some(global_config_path.to_path_buf());
            }
            global_config_path.pop();
        }
    }

    None
}

/// Error while reading `Config`
#[derive(thiserror::Error, Debug)]
pub enum ConfigError {
    /// Input/Output error
    #[error("{0}")]
    IoError(#[from] io::Error),
    /// JSON parsing error
    #[error("{0}")]
    JsonError(#[from] json5::Error),
    /// Invalid value
    #[error("Invalid value: {0}")]
    InvalidValue(String),
}

/// Configuration Options for shadowsocks service runnables
#[derive(Deserialize, Debug, Clone, Default)]
#[serde(default)]
pub struct Config {
    /// Logger configuration
    #[cfg(feature = "logging")]
    pub log: LogConfig,

    /// Runtime configuration
    pub runtime: RuntimeConfig,
}

impl Config {
    /// Load `Config` from file
    pub fn load_from_file<P: AsRef<Path>>(filename: &P) -> Result<Self, ConfigError> {
        let filename = filename.as_ref();

        let mut reader = OpenOptions::new().read(true).open(filename)?;
        let mut content = String::new();
        reader.read_to_string(&mut content)?;

        Self::load_from_str(&content)
    }

    /// Load `Config` from string
    pub fn load_from_str(s: &str) -> Result<Self, ConfigError> {
        json5::from_str(s).map_err(ConfigError::from)
    }

    /// Set by command line options
    pub fn set_options(&mut self, matches: &ArgMatches) {
        #[cfg(feature = "logging")]
        {
            let debug_level = matches.get_count("VERBOSE");
            if debug_level > 0 {
                self.log.level = debug_level as u32;
            }

            if matches.get_flag("LOG_WITHOUT_TIME") {
                self.log.format.without_time = true;
            }

            if let Some(log_config) = matches.get_one::<PathBuf>("LOG_CONFIG").cloned() {
                self.log.config_path = Some(log_config);
            }
        }

        #[cfg(feature = "multi-threaded")]
        if matches.get_flag("SINGLE_THREADED") {
            self.runtime.mode = RuntimeMode::SingleThread;
        }

        #[cfg(feature = "multi-threaded")]
        if let Some(worker_count) = matches.get_one::<usize>("WORKER_THREADS") {
            self.runtime.worker_count = Some(*worker_count);
        }

        // suppress unused warning
        let _ = matches;
    }
}

/// Logger configuration
#[cfg(feature = "logging")]
#[derive(Deserialize, Debug, Clone)]
#[serde(default)]
pub struct LogConfig {
    /// Default log level for all writers, [0, 3]
    pub level: u32,
    /// Default format configuration for all writers
    pub format: LogFormatConfig,
    /// Log writers configuration
    pub writers: Vec<LogWriterConfig>,
    /// Deprecated: Path to the `log4rs` config file
    pub config_path: Option<PathBuf>,
}

#[cfg(feature = "logging")]
impl Default for LogConfig {
    fn default() -> Self {
        LogConfig {
            level: 0,
            format: LogFormatConfig::default(),
            writers: vec![LogWriterConfig::Console(LogConsoleWriterConfig::default())],
            config_path: None,
        }
    }
}

/// Logger format configuration
#[cfg(feature = "logging")]
#[derive(Deserialize, Debug, Clone, Default, Eq, PartialEq)]
#[serde(default)]
pub struct LogFormatConfig {
    pub without_time: bool,
}

/// Holds writer-specific configuration for logging
#[cfg(feature = "logging")]
#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "snake_case")]
pub enum LogWriterConfig {
    Console(LogConsoleWriterConfig),
    File(LogFileWriterConfig),
    #[cfg(unix)]
    Syslog(LogSyslogWriterConfig),
}

/// Console appender configuration for logging
#[cfg(feature = "logging")]
#[derive(Deserialize, Debug, Clone, Default)]
pub struct LogConsoleWriterConfig {
    /// Level override
    #[serde(default)]
    pub level: Option<u32>,
    /// Format override
    #[serde(default)]
    pub format: LogFormatConfigOverride,
}

/// Logger format override
#[cfg(feature = "logging")]
#[derive(Deserialize, Debug, Clone, Default)]
#[serde(default)]
pub struct LogFormatConfigOverride {
    pub without_time: Option<bool>,
}

/// File appender configuration for logging
#[cfg(feature = "logging")]
#[derive(Deserialize, Debug, Clone)]
pub struct LogFileWriterConfig {
    /// Level override
    #[serde(default)]
    pub level: Option<u32>,
    /// Format override
    #[serde(default)]
    pub format: LogFormatConfigOverride,

    /// Directory to store log files
    pub directory: PathBuf,
    /// Rotation strategy for log files. Default is `Rotation::NEVER`.
    #[serde(default)]
    pub rotation: LogRotation,
    /// Prefix for log file names. Default is the binary name.
    #[serde(default)]
    pub prefix: Option<String>,
    /// Suffix for log file names. Default is "log".
    #[serde(default)]
    pub suffix: Option<String>,
    /// Maximum number of log files to keep. Default is `None`, meaning no limit.
    #[serde(default)]
    pub max_files: Option<usize>,
}

/// Log rotation frequency
#[cfg(feature = "logging")]
#[derive(Deserialize, Debug, Copy, Clone, Default, Eq, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum LogRotation {
    #[default]
    Never,
    Hourly,
    Daily,
}

#[cfg(feature = "logging")]
impl From<LogRotation> for tracing_appender::rolling::Rotation {
    fn from(rotation: LogRotation) -> Self {
        match rotation {
            LogRotation::Never => Self::NEVER,
            LogRotation::Hourly => Self::HOURLY,
            LogRotation::Daily => Self::DAILY,
        }
    }
}

/// File appender configuration for logging
#[cfg(all(feature = "logging", unix))]
#[derive(Deserialize, Debug, Clone)]
pub struct LogSyslogWriterConfig {
    /// Level override
    #[serde(default)]
    pub level: Option<u32>,
    /// Format override
    #[serde(default)]
    pub format: LogFormatConfigOverride,

    /// syslog identity, process name by default
    #[serde(default)]
    pub identity: Option<String>,
    /// Facility, 1 (USER) by default
    #[serde(default)]
    pub facility: Option<i32>,
}

/// Runtime mode (Tokio)
#[derive(Deserialize, Debug, Clone, Copy, Default, Eq, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum RuntimeMode {
    /// Single-Thread Runtime
    #[cfg_attr(not(feature = "multi-threaded"), default)]
    SingleThread,
    /// Multi-Thread Runtime
    #[cfg(feature = "multi-threaded")]
    #[cfg_attr(feature = "multi-threaded", default)]
    MultiThread,
}

/// Runtime configuration
#[derive(Deserialize, Debug, Clone, Default)]
#[serde(default)]
pub struct RuntimeConfig {
    /// Multithread runtime worker count, CPU count if not configured
    #[cfg(feature = "multi-threaded")]
    pub worker_count: Option<usize>,
    /// Runtime Mode, single-thread, multi-thread
    pub mode: RuntimeMode,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_deser_empty() {
        // empty config should load successfully
        let config: Config = Config::load_from_str("{}").unwrap();
        assert_eq!(config.runtime.mode, RuntimeMode::default());
        #[cfg(feature = "multi-threaded")]
        {
            assert!(config.runtime.worker_count.is_none());
        }
        #[cfg(feature = "logging")]
        {
            assert_eq!(config.log.level, 0);
            assert!(!config.log.format.without_time);
            // default writer configuration should contain a stdout writer
            assert_eq!(config.log.writers.len(), 1);
            if let LogWriterConfig::Console(stdout_config) = &config.log.writers[0] {
                assert_eq!(stdout_config.level, None);
                assert_eq!(stdout_config.format.without_time, None);
            } else {
                panic!("Expected a stdout writer configuration");
            }
        }
    }

    #[test]
    fn test_deser_disable_logging() {
        // allow user explicitly disable logging by providing an empty writers array
        let config_str = r#"
            {
                "log": {
                    "writers": []
                }
            }
        "#;
        let config: Config = Config::load_from_str(config_str).unwrap();
        #[cfg(feature = "logging")]
        {
            assert_eq!(config.log.level, 0);
            assert!(!config.log.format.without_time);
            assert!(config.log.writers.is_empty());
        }
    }

    #[test]
    fn test_deser_file_writer_full() {
        let config_str = r#"
            {
                "log": {
                    "writers": [
                        {
                            "file": {
                                "level": 2,
                                "format": {
                                    "without_time": true
                                },
                                "directory": "/var/log/shadowsocks",
                                "rotation": "daily",
                                "prefix": "ss-rust",
                                "suffix": "log",
                                "max_files": 5
                            }
                        }
                    ]
                }
            }
        "#;
        let config: Config = Config::load_from_str(config_str).unwrap();
        #[cfg(feature = "logging")]
        {
            assert_eq!(config.log.writers.len(), 1);
            if let LogWriterConfig::File(file_config) = &config.log.writers[0] {
                assert_eq!(file_config.level, Some(2));
                assert_eq!(file_config.format.without_time, Some(true));
                assert_eq!(file_config.directory, PathBuf::from("/var/log/shadowsocks"));
                assert_eq!(file_config.rotation, LogRotation::Daily);
                assert_eq!(file_config.prefix.as_deref(), Some("ss-rust"));
                assert_eq!(file_config.suffix.as_deref(), Some("log"));
                assert_eq!(file_config.max_files, Some(5));
            } else {
                panic!("Expected a file writer configuration");
            }
        }
    }

    #[test]
    fn test_deser_file_writer_minimal() {
        // Minimal valid file writer configuration
        let config_str = r#"
            {
                "log": {
                    "writers": [
                        {
                            "file": {
                                "directory": "/var/log/shadowsocks"
                            }
                        }
                    ]
                }
            }
        "#;
        let config: Config = Config::load_from_str(config_str).unwrap();
        #[cfg(feature = "logging")]
        {
            assert_eq!(config.log.writers.len(), 1);
            if let LogWriterConfig::File(file_config) = &config.log.writers[0] {
                assert_eq!(file_config.level, None);
                assert_eq!(file_config.format.without_time, None);
                assert_eq!(file_config.directory, PathBuf::from("/var/log/shadowsocks"));
                assert_eq!(file_config.rotation, LogRotation::Never);
                assert!(file_config.prefix.is_none());
                assert!(file_config.suffix.is_none());
                assert!(file_config.max_files.is_none());
            } else {
                panic!("Expected a file writer configuration");
            }
        }
    }
    #[test]
    fn test_deser_console_writer_full() {
        let config_str = r#"
            {
                "log": {
                    "writers": [
                        {
                            "console": {
                                "level": 1,
                                "format": {
                                    "without_time": false
                                }
                            }
                        }
                    ]
                }
            }
        "#;
        let config: Config = Config::load_from_str(config_str).unwrap();
        #[cfg(feature = "logging")]
        {
            assert_eq!(config.log.writers.len(), 1);
            if let LogWriterConfig::Console(stdout_config) = &config.log.writers[0] {
                assert_eq!(stdout_config.level, Some(1));
                assert_eq!(stdout_config.format.without_time, Some(false));
            } else {
                panic!("Expected a console writer configuration");
            }
        }
    }

    #[test]
    fn test_deser_console_writer_minimal() {
        // Minimal valid console writer configuration
        let config_str = r#"
            {
                "log": {
                    "writers": [
                        {
                            "console": {}
                        }
                    ]
                }
            }
        "#;
        let config: Config = Config::load_from_str(config_str).unwrap();
        #[cfg(feature = "logging")]
        {
            assert_eq!(config.log.writers.len(), 1);
            if let LogWriterConfig::Console(stdout_config) = &config.log.writers[0] {
                assert_eq!(stdout_config.level, None);
                assert_eq!(stdout_config.format.without_time, None);
            } else {
                panic!("Expected a console writer configuration");
            }
        }
    }
}