shadowsocks-gtk-rs 0.4.1

A desktop GUI frontend for shadowsocks-rust client implemented with gtk-rs.
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
//! This module contains code that handles profile loading.

use std::{
    collections::HashSet,
    ffi::OsString,
    fmt,
    fs::read_to_string,
    io,
    net::{IpAddr, Ipv6Addr},
    os::unix::prelude::IntoRawFd,
    path::{Path, PathBuf},
};

use derivative::Derivative;
use duct::{cmd, Handle};
use ipnet::IpNet;
use itertools::Itertools;
use log::{error, info, warn};
use serde::{Deserialize, Serialize};
use shadowsocks_gtk_rs::consts::*;
use which::which;

/// Optional fields which allow a config to override its profile's default metadata.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetadataOverride {
    display_name: Option<String>,
    pwd: Option<PathBuf>,
    bin_path: Option<PathBuf>,
}

trait ToLaunchArgs {
    fn to_launch_args(&self) -> Vec<OsString>;
}

/// Fields for a "Config file"-type ProfileConfig.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConfigFileOptions {
    config_path: PathBuf,
}
impl ToLaunchArgs for ConfigFileOptions {
    fn to_launch_args(&self) -> Vec<OsString> {
        vec!["--config".into(), (&self.config_path).into()]
    }
}

/// Common fields for ProfileConfig types that do not use a config file.
#[derive(Derivative, Clone, Serialize, Deserialize)]
#[derivative(Debug)]
pub struct ConnectOptions {
    local_addr: (IpAddr, u16),
    server_addr: (String, u16),
    #[derivative(Debug(format_with = "password_omit"))]
    password: String,
    encrypt_method: String,
}
impl ToLaunchArgs for ConnectOptions {
    fn to_launch_args(&self) -> Vec<OsString> {
        let mut args = vec![];
        // local address
        let local_addr = {
            let (a, p) = self.local_addr;
            match a {
                IpAddr::V4(v4) => format!("{}:{}", v4, p),
                IpAddr::V6(v6) => format!("[{}]:{}", v6, p),
            }
        };
        args.extend_from_slice(&["--local-addr".into(), local_addr.into()]);
        // server address
        let server_addr = {
            let (a, p) = &self.server_addr;
            match a.parse::<Ipv6Addr>() {
                Ok(_) => format!("[{}]:{}", a, p), // IPv6
                Err(_) => format!("{}:{}", a, p),  // Domain or IPv4
            }
        };
        args.extend_from_slice(&["--server-addr".into(), server_addr.into()]);
        // password
        args.extend_from_slice(&["--password".into(), (&self.password).into()]);
        // encrypt_method
        args.extend_from_slice(&["--encrypt-method".into(), (&self.encrypt_method).into()]);
        args
    }
}

/// Helper function for `derivative(Debug)`.
fn password_omit(_: &str, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
    write!(fmt, "*hidden*")
}

/// Fields for a "Proxy"-type ProfileConfig
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProxyOptions {
    // TODO: Add protocol selection
}
impl ToLaunchArgs for ProxyOptions {
    fn to_launch_args(&self) -> Vec<OsString> {
        vec![]
    }
}

/// Fields for a "Tun"-type ProfileConfig.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TunOptions {
    if_name: Option<String>,
    if_addr: Option<IpNet>,
}
impl ToLaunchArgs for TunOptions {
    fn to_launch_args(&self) -> Vec<OsString> {
        // protocol
        let mut args = vec!["--protocol".into(), "tun".into()];
        // interface name
        if let Some(if_name) = &self.if_name {
            args.extend_from_slice(&["--tun-interface-name".into(), if_name.into()]);
        }
        // interface address
        if let Some(if_addr) = &self.if_addr {
            args.extend_from_slice(&["--tun-interface-address".into(), if_addr.to_string().into()]);
        }
        args
    }
}

/// Extra configs for advanced users.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AdvancedOptions {
    // IMPRV: more to come
    extra_args: Option<Vec<String>>,
}
impl ToLaunchArgs for AdvancedOptions {
    fn to_launch_args(&self) -> Vec<OsString> {
        let mut args = vec![];
        // extra args
        if let Some(extra) = &self.extra_args {
            args.append(&mut extra.iter().map_into().collect())
        }
        args
    }
}

/// The static configuration for a profile. Represents the file on disk faithfully.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "mode", rename_all = "kebab-case")] // See https://serde.rs/enum-representations.html#internally-tagged
pub enum ProfileConfig {
    /// Profile launches `sslocal` with arbitrary config file using `sslocal --config <CONFIG>`.
    ConfigFile {
        #[serde(flatten)]
        metadata: MetadataOverride,
        #[serde(flatten)]
        opts: ConfigFileOptions,
        #[serde(flatten)]
        adv_opts: AdvancedOptions,
    },
    /// Profile launches `sslocal` in proxy mode.
    Proxy {
        #[serde(flatten)]
        metadata: MetadataOverride,
        #[serde(flatten)]
        conn_opts: ConnectOptions,
        #[serde(flatten)]
        opts: ProxyOptions,
        #[serde(flatten)]
        adv_opts: AdvancedOptions,
    },
    /// Profile launches `sslocal` in tun mode.
    Tun {
        #[serde(flatten)]
        metadata: MetadataOverride,
        #[serde(flatten)]
        conn_opts: ConnectOptions,
        #[serde(flatten)]
        opts: TunOptions,
        #[serde(flatten)]
        adv_opts: AdvancedOptions,
    },
}

impl ProfileConfig {
    fn get_metadata_override(&self) -> &MetadataOverride {
        use ProfileConfig::*;
        match self {
            ConfigFile { metadata, .. } => metadata,
            Proxy { metadata, .. } => metadata,
            Tun { metadata, .. } => metadata,
        }
    }
    fn to_launch_args(&self) -> Vec<OsString> {
        use ProfileConfig::*;
        match self {
            ConfigFile { opts, adv_opts, .. } => {
                let mut args = opts.to_launch_args();
                args.append(&mut adv_opts.to_launch_args());
                args
            }
            Proxy {
                conn_opts,
                opts,
                adv_opts,
                ..
            } => {
                let mut args = conn_opts.to_launch_args();
                args.append(&mut opts.to_launch_args());
                args.append(&mut adv_opts.to_launch_args());
                args
            }
            Tun {
                conn_opts,
                opts,
                adv_opts,
                ..
            } => {
                let mut args = conn_opts.to_launch_args();
                args.append(&mut opts.to_launch_args());
                args.append(&mut adv_opts.to_launch_args());
                args
            }
        }
    }
}

/// Dynamically generated and patched metadata for a profile.
#[derive(Debug, Clone)]
pub struct ProfileMetadata {
    pub display_name: String,
    pwd: PathBuf,
    bin_path: PathBuf,
}

/// A complete `sslocal` launch profile.
#[derive(Debug, Clone)]
pub struct Profile {
    pub metadata: ProfileMetadata,
    config: ProfileConfig,
}

impl Profile {
    /// Run `sslocal` using the settings specified by this profile.
    ///
    /// If `stdout` or `stderr` is `None`, the corresponding output
    /// is redirected to`/dev/null` (discarded) by default.
    pub fn run_sslocal(&self, stdout: Option<impl IntoRawFd>, stderr: Option<impl IntoRawFd>) -> io::Result<Handle> {
        let ProfileMetadata { pwd, bin_path, .. } = &self.metadata;
        let mut expr = cmd(bin_path, self.config.to_launch_args()).dir(pwd).stdin_null();
        expr = match stdout {
            Some(fd) => expr.stdout_file(fd),
            None => expr.stdout_null(),
        };
        expr = match stderr {
            Some(fd) => expr.stderr_file(fd),
            None => expr.stderr_null(),
        };
        expr.unchecked() // check for abnormal termination elsewhere
            .start()
    }
}

/// A group containing multiple profiles and/or subgroups.
#[derive(Debug, Clone)]
pub struct ProfileGroup {
    pub display_name: String,
    pub content: Vec<ProfileFolder>,
}

#[derive(Debug)]
pub enum ProfileLoadError {
    /// Each profile should be its own directory, which can be placed under other directories to form groups.
    NotDirectory(String),
    /// The profile's config file cannot be parsed.
    ConfigParseError(serde_yaml::Error),
    /// Cannot resolve a binary for this profile.
    BadBinary(which::Error),
    /// At least two profiles share the same name.
    NameConflict(String),
    /// The directory contains files (which means it's considered a profile folder),
    /// but there's no config file.
    NoConfigFile(String),
    /// The directory contains neither files nor other valid profiles.
    EmptyGroup(String),
    /// The filesystem encountered an IOError.
    IOError(io::Error),
}

impl fmt::Display for ProfileLoadError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use ProfileLoadError::*;

        let prefix = "ProfileLoadError";
        match self {
            NotDirectory(s) => write!(f, "{}-NotDirectory: {}", prefix, s),
            ConfigParseError(e) => write!(f, "{}-ConfigParseError: {}", prefix, e),
            BadBinary(e) => write!(f, "{}-BadBinary: {}", prefix, e),
            NameConflict(s) => write!(f, "{}-NameConflict: {}", prefix, s),
            NoConfigFile(s) => write!(f, "{}-NoConfigFile: {}", prefix, s),
            EmptyGroup(s) => write!(f, "{}-EmptyGroup: {}", prefix, s),
            IOError(e) => write!(f, "{}-IOError: {}", prefix, e),
        }
    }
}

impl From<serde_yaml::Error> for ProfileLoadError {
    fn from(err: serde_yaml::Error) -> Self {
        Self::ConfigParseError(err)
    }
}
impl From<which::Error> for ProfileLoadError {
    fn from(err: which::Error) -> Self {
        Self::BadBinary(err)
    }
}
impl From<io::Error> for ProfileLoadError {
    fn from(err: io::Error) -> Self {
        Self::IOError(err)
    }
}

#[derive(Derivative, Clone)]
#[derivative(Debug)]
pub enum ProfileFolder {
    #[derivative(Debug = "transparent")]
    Profile(Profile),
    #[derivative(Debug = "transparent")]
    Group(ProfileGroup),
}

impl ProfileFolder {
    /// Recursively loads all nested profiles within the specified directory.
    ///
    /// **Symlinking is not currently supported.**
    ///
    /// If a call to this function with the user-specified base path fails,
    /// then run the program as if there are no existing configs.
    pub fn from_path_recurse(path: impl AsRef<Path>) -> Result<Self, ProfileLoadError> {
        let mut seen_names = HashSet::new();
        Self::from_path_recurse_impl(path.as_ref(), &mut seen_names)?
            .ok_or(ProfileLoadError::EmptyGroup(path.as_ref().to_string_lossy().into()))
    }

    /// Returns Ok(None) when this directory is ignored.
    fn from_path_recurse_impl(
        path: impl AsRef<Path>,
        seen_names: &mut HashSet<String>,
    ) -> Result<Option<Self>, ProfileLoadError> {
        let path = path.as_ref().canonicalize()?;
        let full_path_str = path.to_string_lossy();

        // make sure path is a directory
        if !path.is_dir() {
            return Err(ProfileLoadError::NotDirectory(full_path_str.into()));
        }
        // make sure directory doesn't contain the ignore file
        if path.join(PROFILE_IGNORE_FILE_NAME).is_file() {
            return Ok(None);
        }

        // use directory name as folder's display name
        let default_display_name = path
            .file_name()
            .unwrap() // path has already been canonicalized
            .to_str()
            .unwrap() // UTF-8 has already been verified
            .to_string();

        // if directory contains the config file, then consider it a profile
        let config_path = path.join(PROFILE_CONFIG_FILE_NAME);
        if config_path.is_file() {
            // config
            let content = read_to_string(config_path)?;
            let config: ProfileConfig = serde_yaml::from_str(&content)?;

            // metadata
            let metadata = {
                let mo = config.get_metadata_override().clone();

                let display_name = mo.display_name.unwrap_or(default_display_name);
                if let Some(_) = seen_names.replace(display_name.clone()) {
                    return Err(ProfileLoadError::NameConflict(display_name));
                }
                let pwd = mo.pwd.unwrap_or(path.clone());
                let bin_path = mo
                    .bin_path
                    // which(&str) & which(Path) works differently
                    .map(|p| which(p))
                    .unwrap_or(which(SSLOCAL_LOOKUP_NAME_DEFAULT))?;

                ProfileMetadata {
                    display_name,
                    pwd,
                    bin_path,
                }
            };

            return Ok(Some(Self::Profile(Profile { metadata, config })));
        }

        // otherwise, check if it contains files at all
        // if so consider it a profile that's missing the config file.
        let has_files = path.read_dir()?.any(|ent_res| match ent_res {
            Ok(ent) => ent.path().is_file(),
            Err(err) => {
                warn!("Cannot open a file or directory: {}", err);
                false
            }
        });
        if has_files {
            return Err(ProfileLoadError::NoConfigFile(full_path_str.into()));
        }

        // otherwise, consider it a group
        let mut subdirs = vec![];
        for ent_res in path.read_dir()? {
            // recursively load all subdirectories
            let subdir_path = ent_res?.path();
            match Self::from_path_recurse_impl(&subdir_path, seen_names) {
                Ok(Some(cf)) => subdirs.push(cf),
                Ok(None) => info!("Ignored a directory and its children: {:?}", subdir_path),
                Err(err) => return Err(err),
            };
        }
        if subdirs.is_empty() {
            error!(
                "The specified profile directory is empty; \
                please read Q&A for a guide on creating a configuration"
            );
            error!("See https://github.com/spyophobia/shadowsocks-gtk-rs/blob/master/res/QnA.md");
            Err(ProfileLoadError::EmptyGroup(full_path_str.into()))
        } else {
            Ok(Some(ProfileFolder::Group(ProfileGroup {
                display_name: default_display_name,
                content: subdirs,
            })))
        }
    }

    /// Recursively count the number of nested profiles within this `ConfigFolder`.
    pub fn profile_count(&self) -> usize {
        use ProfileFolder::*;
        match self {
            Profile(_) => 1,
            Group(g) => g.content.iter().map(|pf| pf.profile_count()).sum(),
        }
    }

    /// Recursively get all the nested profiles within this `ProfileFolder`,
    /// flattened and returned by reference.
    #[allow(dead_code)]
    pub fn get_profiles(&self) -> Vec<&Profile> {
        use ProfileFolder::*;
        match self {
            Profile(p) => vec![p],
            Group(g) => g.content.iter().flat_map(|pf| pf.get_profiles()).collect(),
        }
    }

    /// Recursively searches all the nested profiles within this `ProfileFolder`
    /// for a `Profile` with a matching name.
    pub fn lookup(&self, name: impl AsRef<str>) -> Option<&Profile> {
        use ProfileFolder::*;
        match self {
            Profile(p) if p.metadata.display_name == name.as_ref() => Some(p),
            Profile(_) => None,
            Group(g) => g.content.iter().find_map(|pf| pf.lookup(name.as_ref())),
        }
    }
}