diff options
| author | Linus Färnstrand <linus@mullvad.net> | 2016-12-05 15:09:49 +0100 |
|---|---|---|
| committer | Linus Färnstrand <linus@mullvad.net> | 2016-12-05 15:09:49 +0100 |
| commit | 22da2098045bdf69a178eb7e4eaaaf38989235af (patch) | |
| tree | 45207af982855eb3b0762c3382df55e4684779aa /src/process.rs | |
| parent | f0ee88a4c02039ad198e42f78b39905ce7c89436 (diff) | |
| parent | 057a45c28be721d44d76ed64c4dba6f8a0cbffdf (diff) | |
| download | mullvadvpn-22da2098045bdf69a178eb7e4eaaaf38989235af.tar.xz mullvadvpn-22da2098045bdf69a178eb7e4eaaaf38989235af.zip | |
Merge branch 'add-process'
Diffstat (limited to 'src/process.rs')
| -rw-r--r-- | src/process.rs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/process.rs b/src/process.rs new file mode 100644 index 0000000000..7143b85eb3 --- /dev/null +++ b/src/process.rs @@ -0,0 +1,50 @@ +use std::ffi::{OsString, OsStr}; +use std::io; +use std::path::{Path, PathBuf}; +use std::process::{Command, Child, Stdio}; + +/// An OpenVPN process builder, providing control over the different arguments that the OpenVPN +/// binary accepts. +pub struct OpenVpnBuilder { + openvpn_bin: OsString, + config: Option<PathBuf>, +} + +impl OpenVpnBuilder { + /// Constructs a new `OpenVpnBuilder` for launching OpenVPN processes from the binary at + /// `openvpn_bin`. + pub fn new<P: AsRef<OsStr>>(openvpn_bin: P) -> Self { + OpenVpnBuilder { + openvpn_bin: OsString::from(openvpn_bin.as_ref()), + config: None, + } + } + + /// Sets what configuration file will be given to OpenVPN + pub fn config<P: AsRef<Path>>(&mut self, path: P) -> &mut Self { + self.config = Some(path.as_ref().to_path_buf()); + self + } + + /// Executes the OpenVPN process as a child process, returning a handle to it. + pub fn spawn(&mut self) -> io::Result<Child> { + let mut command = self.create_command(); + self.apply_settings(&mut command); + command.spawn() + } + + fn create_command(&mut self) -> Command { + let mut command = Command::new(&self.openvpn_bin); + command.env_clear() + .stdin(Stdio::null()) + .stdout(Stdio::null()) + .stderr(Stdio::null()); + command + } + + fn apply_settings(&self, command: &mut Command) { + if let Some(ref config) = self.config { + command.arg("--config").arg(config); + } + } +} |
