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
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
//! This module contains code that handles profile switching and automatic restarting.

use std::{
    fmt,
    io::{self, BufRead, BufReader, Read},
    os::unix::net::UnixStream,
    process::ExitStatus,
    sync::{Arc, Mutex, RwLock},
    thread::{self, JoinHandle},
    time::Duration,
};

use bus::{Bus, BusReader};
use crossbeam_channel::{unbounded as unbounded_channel, Receiver, Sender};
use derivative::Derivative;
use duct::{unix::HandleExt, Handle};
use itertools::Itertools;
use log::{debug, error, info, trace, warn};
use nix::sys::signal::Signal;
use shadowsocks_gtk_rs::{
    consts::*,
    util::{
        self,
        leaky_bucket::{NaiveLeakyBucket, NaiveLeakyBucketConfig},
        mutex_lock, rwlock_read, OutputKind,
    },
};

use crate::{
    event::AppEvent,
    io::{
        app_state::AppState,
        profile_loader::{Profile, ProfileFolder},
    },
};

/// Represents a currently running `sslocal` instance, storing the relevant information
/// for its subprocess(es).
///
/// Automatically kills `sslocal` when dropped.
#[derive(Derivative)]
#[derivative(Debug)]
struct ActiveSSInstance {
    /// Ownership instead of reference due to need for restart.
    profile: Profile,
    /// The handle of the subprocess.
    sslocal_process: Arc<Handle>,
    /// Subscribe to me to handle `sslocal`'s `stdout`.
    #[derivative(Debug(format_with = "shadowsocks_gtk_rs::util::hacks::omit_bus"))]
    stdout_brd: Arc<Mutex<Bus<String>>>,
    /// Subscribe to me to handle `sslocal`'s `stderr`.
    #[derivative(Debug(format_with = "shadowsocks_gtk_rs::util::hacks::omit_bus"))]
    stderr_brd: Arc<Mutex<Bus<String>>>,
    /// The daemon threads that need to be cleanup up when deactivating.
    daemon_handles: Vec<JoinHandle<()>>,
}

impl fmt::Display for ActiveSSInstance {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let pids_repr = self.sslocal_process.pids().iter().map(u32::to_string).join(", ");
        write!(
            f,
            "ActiveSSInstance(Profile: {}, PIDs: [{}])",
            self.profile.metadata.display_name, pids_repr
        )
    }
}

impl Drop for ActiveSSInstance {
    /// Kill the `sslocal` child process when going out of scope.
    ///
    /// Also cleans up all daemon threads.
    fn drop(&mut self) {
        let self_name = self.to_string();

        trace!("{} is getting dropped", self_name);

        // send stop signal to `sslocal` process
        if let Err(err) = self.sslocal_process.send_signal(Signal::SIGINT as i32) {
            trace!("{}'s underlying process has already exited: {}", self_name, err);
        }

        // sleep for a short time to allow `sslocal` to exit fully
        thread::sleep(Duration::from_millis(100));

        // make sure all daemon threads finish
        for handle in self.daemon_handles.drain(..) {
            if let Err(err) = handle.join() {
                warn!("A daemon of {} panicked unexpectedly: {:?}", self_name, err);
            };
        }
    }
}

impl ActiveSSInstance {
    /// Start a new instance of `sslocal`.
    fn new(profile: Profile) -> io::Result<Self> {
        let (stdout_stream_tx, stdout_stream_rx) = UnixStream::pair()?;
        let (stderr_stream_tx, stderr_stream_rx) = UnixStream::pair()?;

        // start instance
        let proc = profile.run_sslocal(Some(stdout_stream_tx), Some(stderr_stream_tx))?;
        let mut instance = Self {
            profile,
            sslocal_process: proc.into(),
            stdout_brd: Mutex::new(Bus::new(BUS_BUFFER_SIZE)).into(),
            stderr_brd: Mutex::new(Bus::new(BUS_BUFFER_SIZE)).into(),
            daemon_handles: vec![],
        };

        // pipe output
        instance.pipe_to_broadcast(stdout_stream_rx, OutputKind::Stdout)?;
        instance.pipe_to_broadcast(stderr_stream_rx, OutputKind::Stderr)?;

        Ok(instance)
    }

    /// Start a daemon to pipe output from a readable source to a broadcasting channel.
    fn pipe_to_broadcast<R>(&mut self, source: R, output_kind: OutputKind) -> io::Result<()>
    where
        R: Read + Send + 'static,
    {
        let self_name = self.to_string();
        let source = BufReader::new(source);
        let brd = match output_kind {
            OutputKind::Stdout => Arc::clone(&self.stdout_brd),
            OutputKind::Stderr => Arc::clone(&self.stderr_brd),
        };
        let handle = thread::Builder::new()
            .name(format!("{} piper daemon for {}", output_kind, self_name))
            .spawn(move || {
                trace!("{} piper daemon for {} started", output_kind, self_name);
                for line_res in source.lines() {
                    let line = {
                        let raw = line_res.unwrap_or_else(|err| format!("Error reading {}: {}", &output_kind, err));
                        format!("[{}] {}\n", output_kind, raw)
                    };
                    trace!("Broadcasting: {}", line);
                    // try to send through channel
                    if let Err(_) = mutex_lock(&brd).try_broadcast(line) {
                        warn!(
                            "{} wrote to {}, but the broadcasting channel is full.",
                            self_name, output_kind
                        );
                    }
                }
                // thread exits when the source is closed
            })?;
        self.daemon_handles.push(handle);
        Ok(())
    }

    /// Convenience function to create a new broadcast listener.
    fn new_listener(&self, output_kind: OutputKind) -> BusReader<String> {
        let brd = match output_kind {
            OutputKind::Stdout => &self.stdout_brd,
            OutputKind::Stderr => &self.stderr_brd,
        };
        mutex_lock(brd).add_rx()
    }

    /// Starts a monitoring thread that waits for the underlying `sslocal`
    /// to terminate, when it will emit its `ExitStatus` via the returned channel.
    fn alert_on_exit(&mut self) -> io::Result<Receiver<ExitStatus>> {
        let self_name = self.to_string();
        let proc = Arc::clone(&self.sslocal_process);
        let (exit_tx, exit_rx) = unbounded_channel();
        let handle = thread::Builder::new()
            .name(format!("exit alert daemon for instance {}", self_name))
            .spawn(move || {
                let status = proc
                    .wait()
                    .unwrap() // process already running for sure
                    .status;
                if let Err(err) = exit_tx.send(status) {
                    warn!("{} exit detected: {}, but the receiver has hung up.", self_name, err.0);
                }
            })?;
        self.daemon_handles.push(handle);
        Ok(exit_rx)
    }
}

/// A daemon that manages profile-switching and restarts.
#[derive(Derivative)]
#[derivative(Debug)]
pub struct ProfileManager {
    /// Attempt to restart `sslocal` up to this limit before
    /// setting `ProfileManager` to inactive state.
    /// What to do when a `sslocal` instance fails with a non-0 exit code.
    ///
    /// Scenarios in which a restart will not be attempted:
    /// - Limit reached
    /// - `sslocal` instance terminated by a signal
    /// - Various errors which make it impossible for monitoring to continue
    pub restart_limit: NaiveLeakyBucketConfig,
    events_tx: Sender<AppEvent>,
    /// Inner value of `None` means `Self` is inactive.
    active_instance: Arc<RwLock<Option<ActiveSSInstance>>>,

    /// A string holding the combined backlog history of `stdout` & `stderr`.
    pub backlog: Arc<Mutex<String>>,
    /// A channel that broadcasts the combined logs of `stdout` & `stderr`.
    #[derivative(Debug(format_with = "shadowsocks_gtk_rs::util::hacks::omit_bus"))]
    pub logs_brd: Arc<Mutex<Bus<String>>>,

    /// The daemon threads that need to be cleanup up when deactivating.
    daemon_handles: Vec<JoinHandle<()>>,
}

impl Drop for ProfileManager {
    /// Halts any active `sslocal` instance when going out of scope.
    ///
    /// Also cleans up all daemon threads.
    fn drop(&mut self) {
        trace!("ProfileManager is getting dropped");

        // deactivate `sslocal` instance
        let _ = self.try_stop();

        // make sure all daemon threads finish
        for handle in self.daemon_handles.drain(..) {
            if let Err(err) = handle.join() {
                warn!("A daemon of ProfileManager panicked unexpectedly: {:?}", err);
            };
        }
    }
}

impl ProfileManager {
    pub fn new(restart_limit: NaiveLeakyBucketConfig, events_tx: Sender<AppEvent>) -> Self {
        Self {
            restart_limit,
            events_tx,
            active_instance: RwLock::new(None).into(),
            backlog: Mutex::new(String::new()).into(),
            logs_brd: Mutex::new(Bus::new(BUS_BUFFER_SIZE)).into(),
            daemon_handles: vec![],
        }
    }

    /// Resume from a previously saved state.
    pub fn resume_from(state: &AppState, profiles: &ProfileFolder, events_tx: Sender<AppEvent>) -> Self {
        let mut pm = Self::new(state.restart_limit, events_tx);
        match state.most_recent_profile.as_str() {
            "" => debug!("Most recent profile is none; will not attempt to resume"),
            name => match profiles.lookup(name) {
                Some(p) => match pm.switch_to(p.clone()) {
                    Ok(_) => info!("Successfully resumed with profile \"{}\"", name),
                    Err(err) => error!("Cannot resume - switch to profile \"{}\" failed: {}", name, err),
                },
                None => warn!("Cannot resume - profile \"{}\" not found", name),
            },
        };
        pm
    }

    /// Indicate whether a `sslocal` instance is currently running.
    pub fn is_active(&self) -> bool {
        util::rwlock_read(&self.active_instance).is_some()
    }

    /// Get the profile of the currently active instance.
    pub fn current_profile(&self) -> Option<Profile> {
        util::rwlock_read(&self.active_instance)
            .as_ref()
            .map(|instance| instance.profile.clone())
    }

    /// Start a `sslocal` instance with a new profile, replacing the old one if necessary.
    ///
    /// Returns `Ok(())` if and only if the new instance starts successfully and the old one is cleaned up.
    ///
    /// If the new instance fails to start, this `ProfileManager` will be left in deactivated state.
    pub fn switch_to(&mut self, profile: Profile) -> io::Result<()> {
        // deactivate the old instance
        let _ = self.try_stop();

        // activate the new instance
        let mut new_instance = ActiveSSInstance::new(profile)?;

        // monitor for failure
        let exit_alert_rx = new_instance.alert_on_exit()?;

        // set
        *util::rwlock_write(&self.active_instance) = Some(new_instance);

        // pipe output
        self.log_piping_setup(OutputKind::Stdout)?;
        self.log_piping_setup(OutputKind::Stderr)?;

        // monitor
        self.handle_fail(exit_alert_rx)?;

        Ok(())
    }

    /// Convenience function to create a new broadcast listener.
    pub fn new_listener(&self) -> BusReader<String> {
        mutex_lock(&self.logs_brd).add_rx()
    }

    /// Stop the `sslocal` instance if active.
    ///
    /// Returns `Err(())` if already inactive.
    pub fn try_stop(&mut self) -> Result<(), ()> {
        let instance = util::rwlock_write(&self.active_instance).take();
        instance.map(|_| ()).ok_or(())
        // `sslocal` instance dropped implicitly
    }

    /// Start a daemon that subscribes to an output broadcast of
    /// the underlying `sslocal` instance, then re-broadcasts the logs
    /// and appends them to the backlog.
    fn log_piping_setup(&mut self, output_kind: OutputKind) -> io::Result<()> {
        let instance_opt = rwlock_read(&self.active_instance);
        let instance = instance_opt
            .as_ref()
            .ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, "Not active"))?;
        let re_brd = Arc::clone(&self.logs_brd);
        let backlog = Arc::clone(&self.backlog);

        // create thread
        let handle = log_piping_setup_impl(&instance, output_kind, re_brd, backlog)?;
        self.daemon_handles.push(handle);

        Ok(())
    }

    /// Starts a monitoring thread that waits for the underlying `sslocal` instance
    /// to fail, when it will attempt to perform a restart as specified by
    /// `Self::restart_limit`.
    fn handle_fail(&mut self, listener: Receiver<ExitStatus>) -> io::Result<()> {
        // variables that need to be moved into thread
        let restart_limit = self.restart_limit;
        let events_tx = self.events_tx.clone();
        let instance = Arc::clone(&self.active_instance);
        let profile = self
            .current_profile()
            .ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, "Not active"))?;
        let logs_brd = Arc::clone(&self.logs_brd);
        let backlog = Arc::clone(&self.backlog);

        // create thread
        let handle = thread::Builder::new()
            .name("ProfileManager failure monitor daemon".into())
            .spawn(move || {
                // profile stays the same across restarts, therefore outside of loop
                let profile_name = profile.metadata.display_name.clone();
                let mut exit_listener = listener; // is set to new listener in every iteration
                let mut restart_counter: NaiveLeakyBucket = restart_limit.into();

                // restart loop can exit for a variety of reasons; see code
                loop {
                    let instance_name = match &*util::rwlock_read(&instance) {
                        Some(inst) => inst.to_string(),
                        None => {
                            debug!("ProfileManager has been set to inactive; auto-restart stopped");
                            if let Err(_) = events_tx.send(AppEvent::OkStop { instance_name: None }) {
                                error!("Trying to send OkStop event, but all receivers have hung up.");
                            }
                            break;
                        }
                    };

                    // wait for `sslocal` instance exit signal
                    match exit_listener.recv() {
                        Ok(status) if status.success() => {
                            // most likely because `ActiveInstance` gets dropped
                            // causing `sslocal` to exit gracefully,
                            // or if the user calls `sslocal --version` or something
                            debug!("{} has exited successfully; auto-restart stopped", instance_name);
                            if let Err(_) = events_tx.send(AppEvent::OkStop {
                                instance_name: Some(instance_name),
                            }) {
                                error!("Trying to send OkStop event, but all receivers have hung up.");
                            }
                            break;
                        }
                        Err(err) => {
                            // we no longer know the status of `sslocal`, so fail fast
                            error!(
                                "The exit alert daemon for {} has hung up: {}; auto-restart stopped",
                                instance_name, err
                            );
                            if let Err(_) = events_tx.send(AppEvent::ErrorStop {
                                instance_name: Some(instance_name),
                                err: err.to_string(),
                            }) {
                                error!("Trying to send ErrorStop event, but all receivers have hung up.");
                            }
                            break;
                        }
                        Ok(bad_status) => {
                            // do restart
                            warn!("{} has failed; restarting", instance_name);
                            warn!("Exit status: {}", bad_status);
                        }
                    }

                    // Check if restart counter has overflowed
                    if let Err(err) = restart_counter.push() {
                        error!(
                            "sslocal exits excessively with profile \"{}\"; auto-restart stopped",
                            profile_name
                        );
                        error!("{}", err);
                        if let Err(_) = events_tx.send(AppEvent::ErrorStop {
                            instance_name: Some(instance_name),
                            err: err.to_string(),
                        }) {
                            error!("Trying to send ErrorStop event, but all receivers have hung up.");
                        }
                        break;
                    }

                    // Restart
                    /// Temporary helper builder function to simplify error handling.
                    fn start_pipe_alert(
                        profile: Profile,
                        re_brd: Arc<Mutex<Bus<String>>>,
                        backlog: Arc<Mutex<String>>,
                        exit_listener: &mut Receiver<ExitStatus>,
                    ) -> io::Result<ActiveSSInstance> {
                        let mut instance = ActiveSSInstance::new(profile)?;
                        log_piping_setup_impl(
                            &instance,
                            OutputKind::Stdout,
                            Arc::clone(&re_brd),
                            Arc::clone(&backlog),
                        )?;
                        log_piping_setup_impl(&instance, OutputKind::Stderr, re_brd, backlog)?;
                        *exit_listener = instance.alert_on_exit()?;
                        Ok(instance)
                    }

                    let new_instance = {
                        let start_res = start_pipe_alert(
                            profile.clone(),
                            Arc::clone(&logs_brd),
                            Arc::clone(&backlog),
                            &mut exit_listener,
                        );
                        match start_res {
                            Ok(p) => p,
                            Err(err) => {
                                error!(
                                    "Failed to restart with profile \"{}\": {}. Failure monitor daemon stopping",
                                    profile_name, err
                                );
                                if let Err(_) = events_tx.send(AppEvent::ErrorStop {
                                    instance_name: Some(instance_name),
                                    err: err.to_string(),
                                }) {
                                    error!("Trying to send ErrorStop event, but all receivers have hung up.");
                                }
                                break;
                            }
                        }
                    };

                    // Set new active instance
                    *util::rwlock_write(&instance) = Some(new_instance);
                }
                // loop exit means we should leave ProfileManager inactive
                drop(util::rwlock_write(&instance).take());
            })?;
        self.daemon_handles.push(handle);

        Ok(())
    }
}

/// This is not an associated function because it has to be called by
/// threads created by `ProfileManager::handle_fail`.
fn log_piping_setup_impl(
    instance: &ActiveSSInstance,
    output_kind: OutputKind,
    re_brd: Arc<Mutex<Bus<String>>>,
    backlog: Arc<Mutex<String>>,
) -> io::Result<JoinHandle<()>> {
    // variables that need to be moved into thread
    let instance_name = instance.to_string();
    let mut listener = instance.new_listener(output_kind);
    // create thread
    thread::Builder::new()
        .name(format!("{} log porter daemon for {}", output_kind, instance_name))
        .spawn(move || {
            trace!("{} log porter daemon for {} started", output_kind, instance_name);
            for line in listener.iter() {
                // doing those two in reverse to eliminate `line.clone()` call
                // append to backlog
                mutex_lock(&backlog).push_str(&line);
                // rebroadcast
                mutex_lock(&re_brd).broadcast(line);
            }
            // thread exits when broadcast stops
        })
}

#[cfg(test)]
mod test {
    use std::{
        thread::{self, sleep},
        time::Duration,
    };

    use crossbeam_channel::unbounded as unbounded_channel;
    use log::{debug, LevelFilter};
    use shadowsocks_gtk_rs::util::leaky_bucket::NaiveLeakyBucketConfig;
    use simplelog::{Config, SimpleLogger};

    use super::*;
    use crate::io::profile_loader::ProfileFolder;

    /// This test will always pass. You need to examine the outputs manually.
    ///
    /// `cargo test example_profiles_test_run -- --nocapture`
    #[test]
    fn example_profiles_test_run() {
        SimpleLogger::init(LevelFilter::Trace, Config::default()).unwrap();

        // parse example configs
        let eg_configs = ProfileFolder::from_path_recurse("example-profiles").unwrap();
        let profile_list = eg_configs.get_profiles();
        debug!("Loaded {} profiles.", profile_list.len());

        // setup ProfileManager
        let restart_limit = NaiveLeakyBucketConfig::new(3, Duration::from_secs(10));
        let (events_tx, _) = unbounded_channel();
        let mut mgr = ProfileManager::new(restart_limit, events_tx);

        // run through all example profiles
        for p in profile_list {
            println!();
            mgr.switch_to(p.clone()).unwrap();
            let (mut stdout_listener, mut stderr_listener) = {
                let instance_opt = rwlock_read(&mgr.active_instance);
                let instance = instance_opt.as_ref().unwrap();
                (
                    instance.new_listener(OutputKind::Stdout),
                    instance.new_listener(OutputKind::Stderr),
                )
            };
            thread::spawn(move || stdout_listener.iter().for_each(|s| println!("stdout: {}", s)));
            thread::spawn(move || stderr_listener.iter().for_each(|s| println!("stderr: {}", s)));
            sleep(Duration::from_millis(3000));
        }
        let _ = mgr.try_stop();
    }
}