diff options
| -rw-r--r-- | Cargo.lock | 65 | ||||
| -rw-r--r-- | mullvad_daemon/Cargo.toml | 6 | ||||
| -rw-r--r-- | mullvad_daemon/src/cli.rs | 45 | ||||
| -rw-r--r-- | mullvad_daemon/src/main.rs | 46 |
4 files changed, 155 insertions, 7 deletions
diff --git a/Cargo.lock b/Cargo.lock index dd572e10a1..4b51fe51a2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -90,6 +90,15 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] +name = "chrono" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.38 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "clap" version = "2.25.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -158,6 +167,14 @@ dependencies = [ ] [[package]] +name = "fern" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "fnv" version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -377,9 +394,11 @@ name = "mullvad_daemon" version = "0.1.0" dependencies = [ "assert_matches 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "chrono 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "clap 2.25.0 (registry+https://github.com/rust-lang/crates.io-index)", "ctrlc 3.0.2 (registry+https://github.com/rust-lang/crates.io-index)", - "env_logger 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", "error-chain 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fern 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "jsonrpc-core 7.0.1 (git+https://github.com/paritytech/jsonrpc)", "jsonrpc-macros 7.0.1 (git+https://github.com/paritytech/jsonrpc)", "jsonrpc-pubsub 7.0.1 (git+https://github.com/paritytech/jsonrpc)", @@ -427,6 +446,33 @@ dependencies = [ ] [[package]] +name = "num" +version = "0.1.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-integer 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)", + "num-iter 0.1.33 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num-integer" +version = "0.1.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-traits 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num-iter" +version = "0.1.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-integer 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "num-traits" version = "0.1.39" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -706,6 +752,17 @@ dependencies = [ ] [[package]] +name = "time" +version = "0.1.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.24 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.20 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "tokio-core" version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -855,6 +912,7 @@ dependencies = [ "checksum byteorder 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c40977b0ee6b9885c9013cd41d9feffdd22deb3bb4dc3a71d901cc7a77de18c8" "checksum bytes 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8b24f16593f445422331a5eed46b72f7f171f910fead4f2ea8f17e727e9c5c14" "checksum cfg-if 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d4c819a1287eb618df47cc647173c5c4c66ba19d888a6e50d605672aed3140de" +"checksum chrono 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7c20ebe0b2b08b0aeddba49c609fe7957ba2e33449882cb186a180bc60682fa9" "checksum clap 2.25.0 (registry+https://github.com/rust-lang/crates.io-index)" = "867a885995b4184be051b70a592d4d70e32d7a188db6e8dff626af286a962771" "checksum ctrlc 3.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1c663ee353d7e03e4f895af66ff7d3537f94c09deaa341a2f450684a3d45cb46" "checksum dbghelp-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "97590ba53bcb8ac28279161ca943a924d1fd4a8fb3fa63302591647c4fc5b850" @@ -862,6 +920,7 @@ dependencies = [ "checksum duct 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e2f3154a9285e24d7c3aba0dca9a13adf2ba6160cce3490b157c8b37a0f80e85" "checksum env_logger 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3ddf21e73e016298f5cb37d6ef8e8da8e39f91f9ec8b0df44b7deb16a9f8cd5b" "checksum error-chain 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d9435d864e017c3c6afeac1654189b06cdb491cf2ff73dbf0d73b0f292f42ff8" +"checksum fern 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "89273e0d0e210f69600048a209a00e163560b51e3ef51c3942304e9b8aa8b47a" "checksum fnv 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "6cc484842f1e2884faf56f529f960cc12ad8c71ce96cc7abba0a067c98fee344" "checksum futures 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "4b63a4792d4f8f686defe3b39b92127fea6344de5d38202b2ee5a11bbbf29d6a" "checksum gcc 0.3.51 (registry+https://github.com/rust-lang/crates.io-index)" = "120d07f202dcc3f72859422563522b66fe6463a4c513df062874daad05f85f0a" @@ -889,6 +948,9 @@ dependencies = [ "checksum mktemp 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "77001ceb9eed65439f3dc2a2543f9ba1417d912686bf224a7738d0966e6dcd69" "checksum net2 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)" = "bc01404e7568680f1259aa5729539f221cb1e6d047a0d9053cab4be8a73b5d67" "checksum nix 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "47e49f6982987135c5e9620ab317623e723bd06738fd85377e8d55f57c8b6487" +"checksum num 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "2c3a3dc9f30bf824141521b30c908a859ab190b76e20435fcd89f35eb6583887" +"checksum num-integer 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)" = "ef1a4bf6f9174aa5783a9b4cc892cacd11aebad6c69ad027a0b65c6ca5f8aa37" +"checksum num-iter 0.1.33 (registry+https://github.com/rust-lang/crates.io-index)" = "f7d1891bd7b936f12349b7d1403761c8a0b85a18b148e9da4429d5d102c1a41e" "checksum num-traits 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "1708c0628602a98b52fad936cf3edb9a107af06e52e49fdf0707e884456a6af6" "checksum os_pipe 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "998bfbb3042e715190fe2a41abfa047d7e8cb81374d2977d7f100eacd8619cb1" "checksum owning_ref 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "cdf84f41639e037b484f93433aa3897863b561ed65c6e59c7073d7c561710f37" @@ -920,6 +982,7 @@ dependencies = [ "checksum textwrap 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f86300c3e7416ee233abd7cda890c492007a3980f941f79185c753a701257167" "checksum thread-id 3.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2af4d6289a69a35c4d3aea737add39685f2784122c28119a7713165a63d68c9d" "checksum thread_local 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "1697c4b57aeeb7a536b647165a2825faddffb1d3bad386d507709bd51a90bb14" +"checksum time 0.1.38 (registry+https://github.com/rust-lang/crates.io-index)" = "d5d788d3aa77bc0ef3e9621256885555368b47bd495c13dd2e7413c89f845520" "checksum tokio-core 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "6a20ba4738d283cac7495ca36e045c80c2a8df3e05dd0909b17a06646af5a7ed" "checksum tokio-io 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c2c3ce9739f7387a0fa65b5421e81feae92e04d603f008898f4257790ce8c2db" "checksum unicode-bidi 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a6a2c4e3710edd365cd7e78383153ed739fa31af19f9172f72d3575060f5a43a" diff --git a/mullvad_daemon/Cargo.toml b/mullvad_daemon/Cargo.toml index 73c9fe40dd..c5ddc653a2 100644 --- a/mullvad_daemon/Cargo.toml +++ b/mullvad_daemon/Cargo.toml @@ -2,17 +2,19 @@ name = "mullvad_daemon" version = "0.1.0" authors = ["Linus Färnstrand <linus@mullvad.net>", "Erik Larkö <erik@mullvad.net>"] -description = "The meat of Mullvad, the core if you wish" +description = "Mullvad VPN backend daemon. Runs and controls the VPN tunnels." [[bin]] name = "mullvadd" [dependencies] +chrono = "0.4" +clap = "2.25" error-chain = "0.10" +fern = "0.4" serde = "1.0" serde_derive = "1.0" log = "0.3" -env_logger = "0.4" jsonrpc-core = { git = "https://github.com/paritytech/jsonrpc" } jsonrpc-macros = { git = "https://github.com/paritytech/jsonrpc" } jsonrpc-pubsub = { git = "https://github.com/paritytech/jsonrpc" } diff --git a/mullvad_daemon/src/cli.rs b/mullvad_daemon/src/cli.rs new file mode 100644 index 0000000000..670a2bf28b --- /dev/null +++ b/mullvad_daemon/src/cli.rs @@ -0,0 +1,45 @@ +use clap::{App, Arg}; +use log; + +use std::path::PathBuf; + +pub struct Config { + pub log_level: log::LogLevelFilter, + pub log_file: Option<PathBuf>, +} + +pub fn get_config() -> Config { + let app = create_app(); + let matches = app.get_matches(); + + let log_level = match matches.occurrences_of("v") { + 0 => log::LogLevelFilter::Info, + 1 => log::LogLevelFilter::Debug, + _ => log::LogLevelFilter::Trace, + }; + let log_file = matches.value_of_os("log_file").map(PathBuf::from); + + Config { + log_level, + log_file, + } +} + +fn create_app() -> App<'static, 'static> { + App::new(::CRATE_NAME) + .version(crate_version!()) + .author(crate_authors!()) + .about(crate_description!()) + .arg( + Arg::with_name("v") + .short("v") + .multiple(true) + .help("Sets the level of verbosity."), + ) + .arg( + Arg::with_name("log_file") + .long("log") + .takes_value(true) + .help("Activates file logging to the given path"), + ) +} diff --git a/mullvad_daemon/src/main.rs b/mullvad_daemon/src/main.rs index 51bf264465..74d6801cff 100644 --- a/mullvad_daemon/src/main.rs +++ b/mullvad_daemon/src/main.rs @@ -1,8 +1,11 @@ #[macro_use] +extern crate clap; +extern crate chrono; +#[macro_use] extern crate log; -extern crate env_logger; #[macro_use] extern crate error_chain; +extern crate fern; extern crate serde; #[macro_use] @@ -21,6 +24,7 @@ extern crate mullvad_types; extern crate talpid_core; extern crate talpid_ipc; +mod cli; mod management_interface; mod rpc_info; mod shutdown; @@ -29,6 +33,7 @@ use management_interface::{ManagementInterfaceServer, TunnelCommand}; use mullvad_types::states::{DaemonState, SecurityState, TargetState}; use std::io; +use std::path::PathBuf; use std::sync::{Arc, Mutex, mpsc}; use std::thread; @@ -67,6 +72,8 @@ lazy_static! { ]; } +static CRATE_NAME: &str = "mullvadd"; + /// All events that can happen in the daemon. Sent from various threads and exposed interfaces. pub enum DaemonEvent { @@ -452,7 +459,8 @@ fn log_error<E>(error: &E) quick_main!(run); fn run() -> Result<()> { - init_logger()?; + let config = cli::get_config(); + init_logger(config.log_level, config.log_file.as_ref())?; let daemon = Daemon::new().chain_err(|| "Unable to initialize daemon")?; @@ -466,6 +474,36 @@ fn run() -> Result<()> { Ok(()) } -fn init_logger() -> Result<()> { - env_logger::init().chain_err(|| "Failed to bootstrap logging system") +fn init_logger(log_level: log::LogLevelFilter, log_file: Option<&PathBuf>) -> Result<()> { + let silenced_crates = [ + "jsonrpc_core", + "tokio_core", + "jsonrpc_ws_server", + "ws", + "mio", + ]; + let mut config = fern::Dispatch::new() + .format( + |out, message, record| { + out.finish( + format_args!( + "{}[{}][{}] {}", + chrono::Local::now().format("[%Y-%m-%d %H:%M:%S]"), + record.target(), + record.level(), + message + ), + ) + }, + ) + .level(log_level) + .chain(std::io::stdout()); + for silenced_crate in &silenced_crates { + config = config.level_for(*silenced_crate, log::LogLevelFilter::Warn); + } + if let Some(ref log_file) = log_file { + let f = fern::log_file(log_file).chain_err(|| "Failed to open log file for writing")?; + config = config.chain(f); + } + config.apply().chain_err(|| "Failed to bootstrap logging system") } |
