summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2018-08-29 10:55:18 -0300
committerJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2018-08-29 10:55:18 -0300
commit559a81c6044d620159d8229295bee3b2e909eca4 (patch)
tree92619604cdd321ffcc5730b278f524e3eb5ce6f1
parent59f529d6da092ffa57dd9ca02746938928079525 (diff)
parent0b6a3e38032248d7d33c3ec84b643ba609d7f96f (diff)
downloadmullvadvpn-559a81c6044d620159d8229295bee3b2e909eca4.tar.xz
mullvadvpn-559a81c6044d620159d8229295bee3b2e909eca4.zip
Merge branch 'talpid-tunnel-state-machine'
-rw-r--r--Cargo.lock1
-rw-r--r--mullvad-daemon/src/logging.rs20
-rw-r--r--mullvad-daemon/src/main.rs7
-rw-r--r--talpid-core/Cargo.toml2
-rw-r--r--talpid-core/src/lib.rs8
-rw-r--r--talpid-core/src/logging.rs20
-rw-r--r--talpid-core/src/tunnel/mod.rs10
-rw-r--r--talpid-core/src/tunnel_state_machine/connected_state.rs (renamed from mullvad-daemon/src/tunnel_state_machine/connected_state.rs)4
-rw-r--r--talpid-core/src/tunnel_state_machine/connecting_state.rs (renamed from mullvad-daemon/src/tunnel_state_machine/connecting_state.rs)6
-rw-r--r--talpid-core/src/tunnel_state_machine/disconnected_state.rs (renamed from mullvad-daemon/src/tunnel_state_machine/disconnected_state.rs)3
-rw-r--r--talpid-core/src/tunnel_state_machine/disconnecting_state.rs (renamed from mullvad-daemon/src/tunnel_state_machine/disconnecting_state.rs)3
-rw-r--r--talpid-core/src/tunnel_state_machine/macros.rs (renamed from mullvad-daemon/src/tunnel_state_machine/macros.rs)0
-rw-r--r--talpid-core/src/tunnel_state_machine/mod.rs (renamed from mullvad-daemon/src/tunnel_state_machine/mod.rs)20
13 files changed, 69 insertions, 35 deletions
diff --git a/Cargo.lock b/Cargo.lock
index bb3e67aa73..78db2ce95c 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1502,6 +1502,7 @@ dependencies = [
"duct 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"error-chain 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
"failure 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "futures 0.1.23 (registry+https://github.com/rust-lang/crates.io-index)",
"ipnetwork 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)",
"jsonrpc-core 8.0.1 (git+https://github.com/paritytech/jsonrpc?tag=v8.0.1)",
"jsonrpc-macros 8.0.0 (git+https://github.com/paritytech/jsonrpc?tag=v8.0.1)",
diff --git a/mullvad-daemon/src/logging.rs b/mullvad-daemon/src/logging.rs
index 958d5321e6..9988ea6be2 100644
--- a/mullvad-daemon/src/logging.rs
+++ b/mullvad-daemon/src/logging.rs
@@ -6,9 +6,10 @@ use chrono;
use log;
use std::fmt;
-use std::fs;
use std::io;
-use std::path::{Path, PathBuf};
+use std::path::PathBuf;
+
+use talpid_core::logging::rotate_log;
error_chain! {
errors {
@@ -17,6 +18,9 @@ error_chain! {
display("Unable to open log file for writing: {}", path.display())
}
}
+ links {
+ RotateLog(::talpid_core::logging::Error, ::talpid_core::logging::ErrorKind);
+ }
foreign_links {
SetLoggerError(log::SetLoggerError);
Io(io::Error);
@@ -138,15 +142,3 @@ fn escape_newlines(text: String) -> String {
fn escape_newlines(text: String) -> String {
text.replace("\n", LINE_SEPARATOR)
}
-
-pub fn rotate_log(file: &Path) -> Result<()> {
- let backup = file.with_extension("old.log");
- fs::rename(file, backup).unwrap_or_else(|error| {
- if error.kind() != io::ErrorKind::NotFound {
- warn!("Failed to rotate log file ({})", error);
- }
- });
-
- fs::File::create(file).chain_err(|| "Unable to create new log file")?;
- Ok(())
-}
diff --git a/mullvad-daemon/src/main.rs b/mullvad-daemon/src/main.rs
index 08f8c34146..848207fbb1 100644
--- a/mullvad-daemon/src/main.rs
+++ b/mullvad-daemon/src/main.rs
@@ -55,7 +55,6 @@ mod rpc_uniqueness_check;
mod settings;
mod shutdown;
mod system_service;
-mod tunnel_state_machine;
mod version;
use error_chain::ChainedError;
@@ -65,7 +64,6 @@ use jsonrpc_core::futures::sync::oneshot::Sender as OneshotSender;
use management_interface::{BoxFuture, ManagementCommand, ManagementInterfaceServer};
use mullvad_rpc::{AccountsProxy, AppVersionProxy, HttpHandle};
-use tunnel_state_machine::{TunnelCommand, TunnelParameters, TunnelStateTransition};
use mullvad_types::account::{AccountData, AccountToken};
use mullvad_types::location::GeoIpLocation;
@@ -81,6 +79,9 @@ use std::time::Duration;
use std::{mem, thread};
use talpid_core::mpsc::IntoSender;
+use talpid_core::tunnel_state_machine::{
+ self, TunnelCommand, TunnelParameters, TunnelStateTransition,
+};
use talpid_types::net::TunnelOptions;
@@ -696,7 +697,7 @@ impl Daemon {
options: self.settings.get_tunnel_options().clone(),
log_dir: self.log_dir.clone(),
resource_dir: self.resource_dir.clone(),
- account_token,
+ username: account_token,
allow_lan: self.settings.get_allow_lan(),
})
}
diff --git a/talpid-core/Cargo.toml b/talpid-core/Cargo.toml
index 0f38b657d9..52297f214d 100644
--- a/talpid-core/Cargo.toml
+++ b/talpid-core/Cargo.toml
@@ -9,6 +9,7 @@ license = "GPL-3.0"
atty = "0.2"
duct = "0.11"
error-chain = "0.12"
+futures = "0.1"
jsonrpc-core = { git = "https://github.com/paritytech/jsonrpc", tag = "v8.0.1" }
jsonrpc-macros = { git = "https://github.com/paritytech/jsonrpc", tag = "v8.0.1" }
libc = "0.2.20"
@@ -16,6 +17,7 @@ log = "0.4"
openvpn-plugin = { version = "0.3", features = ["serde"] }
os_pipe = "0.7"
shell-escape = "0.1"
+tokio-core = "0.1"
uuid = { version = "0.6", features = ["v4"] }
talpid-ipc = { path = "../talpid-ipc" }
diff --git a/talpid-core/src/lib.rs b/talpid-core/src/lib.rs
index 1ee4d3b6ad..55df5e135f 100644
--- a/talpid-core/src/lib.rs
+++ b/talpid-core/src/lib.rs
@@ -19,6 +19,7 @@ extern crate log;
extern crate error_chain;
#[cfg(target_os = "linux")]
extern crate failure;
+extern crate futures;
#[cfg(unix)]
extern crate ipnetwork;
extern crate jsonrpc_core;
@@ -29,6 +30,7 @@ extern crate jsonrpc_macros;
extern crate lazy_static;
extern crate libc;
extern crate shell_escape;
+extern crate tokio_core;
extern crate uuid;
#[cfg(target_os = "linux")]
extern crate which;
@@ -47,10 +49,16 @@ pub mod process;
/// Abstracts over different VPN tunnel technologies
pub mod tunnel;
+/// Helper function to preserve previous log files.
+pub mod logging;
+
/// Abstractions and extra features on `std::mpsc`
pub mod mpsc;
/// Abstractions over operating system network security settings.
pub mod security;
+/// State machine to handle tunnel configuration.
+pub mod tunnel_state_machine;
+
mod mktemp;
diff --git a/talpid-core/src/logging.rs b/talpid-core/src/logging.rs
new file mode 100644
index 0000000000..fe77297b84
--- /dev/null
+++ b/talpid-core/src/logging.rs
@@ -0,0 +1,20 @@
+use std::path::Path;
+use std::{fs, io};
+
+error_chain!{}
+
+/// Create a new log file while backing up a previous version of it.
+///
+/// A new log file is created with the given file name, but if a file with that name already exists
+/// it is backed up with the extension changed to `.old.log`.
+pub fn rotate_log(file: &Path) -> Result<()> {
+ let backup = file.with_extension("old.log");
+ fs::rename(file, backup).unwrap_or_else(|error| {
+ if error.kind() != io::ErrorKind::NotFound {
+ warn!("Failed to rotate log file ({})", error);
+ }
+ });
+
+ fs::File::create(file).chain_err(|| "Unable to create new log file")?;
+ Ok(())
+}
diff --git a/talpid-core/src/tunnel/mod.rs b/talpid-core/src/tunnel/mod.rs
index 98d2d0e2d0..c09eaa61bd 100644
--- a/talpid-core/src/tunnel/mod.rs
+++ b/talpid-core/src/tunnel/mod.rs
@@ -144,7 +144,7 @@ impl TunnelMonitor {
tunnel_endpoint: TunnelEndpoint,
tunnel_options: &TunnelOptions,
tunnel_alias: Option<OsString>,
- account_token: &str,
+ username: &str,
log: Option<&Path>,
resource_dir: &Path,
on_event: L,
@@ -156,8 +156,8 @@ impl TunnelMonitor {
TunnelEndpointData::OpenVpn(_) => (),
TunnelEndpointData::Wireguard(_) => bail!(ErrorKind::UnsupportedTunnelProtocol),
}
- let user_pass_file = Self::create_user_pass_file(account_token)
- .chain_err(|| ErrorKind::CredentialsWriteError)?;
+ let user_pass_file =
+ Self::create_user_pass_file(username).chain_err(|| ErrorKind::CredentialsWriteError)?;
let cmd = Self::create_openvpn_cmd(
tunnel_endpoint.to_endpoint(),
tunnel_alias,
@@ -249,7 +249,7 @@ impl TunnelMonitor {
}
}
- fn create_user_pass_file(account_token: &str) -> io::Result<mktemp::TempFile> {
+ fn create_user_pass_file(username: &str) -> io::Result<mktemp::TempFile> {
let temp_file = mktemp::TempFile::new();
debug!(
"Writing user-pass credentials to {}",
@@ -257,7 +257,7 @@ impl TunnelMonitor {
);
let mut file = fs::File::create(&temp_file)?;
Self::set_user_pass_file_permissions(&file)?;
- write!(file, "{}\n-\n", account_token)?;
+ write!(file, "{}\n-\n", username)?;
Ok(temp_file)
}
diff --git a/mullvad-daemon/src/tunnel_state_machine/connected_state.rs b/talpid-core/src/tunnel_state_machine/connected_state.rs
index 88b12e3ec8..1b119eb387 100644
--- a/mullvad-daemon/src/tunnel_state_machine/connected_state.rs
+++ b/talpid-core/src/tunnel_state_machine/connected_state.rs
@@ -1,8 +1,6 @@
use futures::sync::{mpsc, oneshot};
use futures::{Async, Future, Stream};
-use talpid_core::security::{NetworkSecurity, SecurityPolicy};
-use talpid_core::tunnel::{CloseHandle, TunnelEvent, TunnelMetadata};
use talpid_types::net::TunnelEndpoint;
use super::{
@@ -10,6 +8,8 @@ use super::{
SharedTunnelStateValues, StateEntryResult, TunnelCommand, TunnelParameters, TunnelState,
TunnelStateWrapper,
};
+use security::{NetworkSecurity, SecurityPolicy};
+use tunnel::{CloseHandle, TunnelEvent, TunnelMetadata};
pub struct ConnectedStateBootstrap {
pub metadata: TunnelMetadata,
diff --git a/mullvad-daemon/src/tunnel_state_machine/connecting_state.rs b/talpid-core/src/tunnel_state_machine/connecting_state.rs
index c05d85d217..2588da0e77 100644
--- a/mullvad-daemon/src/tunnel_state_machine/connecting_state.rs
+++ b/talpid-core/src/tunnel_state_machine/connecting_state.rs
@@ -8,8 +8,6 @@ use futures::sink::Wait;
use futures::sync::{mpsc, oneshot};
use futures::{Async, Future, Sink, Stream};
-use talpid_core::security::{NetworkSecurity, SecurityPolicy};
-use talpid_core::tunnel::{CloseHandle, TunnelEvent, TunnelMetadata, TunnelMonitor};
use talpid_types::net::{TunnelEndpoint, TunnelEndpointData};
use super::{
@@ -18,6 +16,8 @@ use super::{
StateEntryResult, TunnelCommand, TunnelParameters, TunnelState, TunnelStateWrapper,
};
use logging;
+use security::{NetworkSecurity, SecurityPolicy};
+use tunnel::{CloseHandle, TunnelEvent, TunnelMetadata, TunnelMonitor};
const MIN_TUNNEL_ALIVE_TIME: Duration = Duration::from_millis(1000);
@@ -110,7 +110,7 @@ impl ConnectingState {
parameters.endpoint,
&parameters.options,
TUNNEL_INTERFACE_ALIAS.to_owned().map(OsString::from),
- &parameters.account_token,
+ &parameters.username,
log_file.as_ref().map(PathBuf::as_path),
&parameters.resource_dir,
on_tunnel_event,
diff --git a/mullvad-daemon/src/tunnel_state_machine/disconnected_state.rs b/talpid-core/src/tunnel_state_machine/disconnected_state.rs
index a64f2ca543..694e280ec6 100644
--- a/mullvad-daemon/src/tunnel_state_machine/disconnected_state.rs
+++ b/talpid-core/src/tunnel_state_machine/disconnected_state.rs
@@ -2,12 +2,11 @@ use error_chain::ChainedError;
use futures::sync::mpsc;
use futures::Stream;
-use talpid_core::security::NetworkSecurity;
-
use super::{
ConnectingState, Error, EventConsequence, SharedTunnelStateValues, StateEntryResult,
TunnelCommand, TunnelState, TunnelStateWrapper,
};
+use security::NetworkSecurity;
/// No tunnel is running.
pub struct DisconnectedState;
diff --git a/mullvad-daemon/src/tunnel_state_machine/disconnecting_state.rs b/talpid-core/src/tunnel_state_machine/disconnecting_state.rs
index 9785264da1..5b80ad0598 100644
--- a/mullvad-daemon/src/tunnel_state_machine/disconnecting_state.rs
+++ b/talpid-core/src/tunnel_state_machine/disconnecting_state.rs
@@ -2,12 +2,11 @@ use error_chain::ChainedError;
use futures::sync::{mpsc, oneshot};
use futures::{Async, Future, Stream};
-use talpid_core::tunnel::CloseHandle;
-
use super::{
ConnectingState, DisconnectedState, EventConsequence, ResultExt, SharedTunnelStateValues,
StateEntryResult, TunnelCommand, TunnelParameters, TunnelState, TunnelStateWrapper,
};
+use tunnel::CloseHandle;
/// This state is active from when we manually trigger a tunnel kill until the tunnel wait
/// operation (TunnelExit) returned.
diff --git a/mullvad-daemon/src/tunnel_state_machine/macros.rs b/talpid-core/src/tunnel_state_machine/macros.rs
index 2241c8cb06..2241c8cb06 100644
--- a/mullvad-daemon/src/tunnel_state_machine/macros.rs
+++ b/talpid-core/src/tunnel_state_machine/macros.rs
diff --git a/mullvad-daemon/src/tunnel_state_machine/mod.rs b/talpid-core/src/tunnel_state_machine/mod.rs
index 29e1e5869a..c3d592c952 100644
--- a/mullvad-daemon/src/tunnel_state_machine/mod.rs
+++ b/talpid-core/src/tunnel_state_machine/mod.rs
@@ -16,21 +16,23 @@ use futures::sync::mpsc;
use futures::{Async, Future, Poll, Stream};
use tokio_core::reactor::Core;
-use mullvad_types::account::AccountToken;
-use talpid_core::mpsc::IntoSender;
-use talpid_core::security::{NetworkSecurity, NetworkSecurityImpl};
use talpid_types::net::{TunnelEndpoint, TunnelOptions};
use self::connected_state::{ConnectedState, ConnectedStateBootstrap};
use self::connecting_state::ConnectingState;
use self::disconnected_state::DisconnectedState;
use self::disconnecting_state::{AfterDisconnect, DisconnectingState};
+use super::mpsc::IntoSender;
+use super::security::{NetworkSecurity, NetworkSecurityImpl};
error_chain! {
errors {
+ /// An error occurred while setting up the network security.
NetworkSecurityError {
description("Network security error")
}
+ /// An error occurred while attempting to set up the event loop for the tunnel state
+ /// machine.
ReactorError {
description("Failed to initialize tunnel state machine event loop executor")
}
@@ -110,20 +112,30 @@ pub enum TunnelCommand {
/// Information necessary to open a tunnel.
#[derive(Debug, PartialEq)]
pub struct TunnelParameters {
+ /// Tunnel enpoint to connect to.
pub endpoint: TunnelEndpoint,
+ /// Tunnel connection options.
pub options: TunnelOptions,
+ /// Directory to store tunnel log file.
pub log_dir: Option<PathBuf>,
+ /// Resource directory path.
pub resource_dir: PathBuf,
- pub account_token: AccountToken,
+ /// Username to use for setting up the tunnel.
+ pub username: String,
+ /// Should LAN access be allowed outside the tunnel.
pub allow_lan: bool,
}
/// Event resulting from a transition to a new tunnel state.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum TunnelStateTransition {
+ /// No connection is established and network is unsecured.
Disconnected,
+ /// Network is secured but tunnel is still connecting.
Connecting,
+ /// Tunnel is connected.
Connected,
+ /// Disconnecting tunnel.
Disconnecting,
}