blob: a4c9e16008a6846a93bcee88b984dce5354e03c4 (
plain) (
blame)
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
|
//! The main executable for `WireGuard`
#[macro_use]
extern crate clap;
extern crate daemonize;
extern crate libc;
#[macro_use]
extern crate log;
extern crate mowl;
extern crate nix;
#[macro_use]
extern crate wireguard;
use clap::App;
use daemonize::Daemonize;
use log::LogLevel;
use wireguard::{WireGuard, WgResult, WgError, error};
use std::process::exit;
fn main() {
if let Err(error) = run() {
error!("{}", error);
exit(1);
}
}
fn run() -> WgResult<()> {
// Load the CLI parameters from the yaml file
let yaml = load_yaml!("cli.yaml");
let app = App::from_yaml(yaml).version(crate_version!());
let matches = app.get_matches();
// Set the verbosity level
let log_level = match matches.occurrences_of("verbose") {
0 => LogLevel::Info, // Default value
1 => LogLevel::Debug,
_ => LogLevel::Trace,
};
// Init the logging
match mowl::init_with_level(log_level) {
Err(_) => warn!("Log level already set"),
Ok(_) => info!("Log level set to: {}", log_level),
}
// Get the CLI matches
let interface_name = matches.value_of("interface_name")
.ok_or_else(|| WgError::new("No 'interface_name' provided"))?;
// Create a `WireGuard` instance
let wireguard = WireGuard::new(interface_name)?;
// Run the instance in foreground if needed
if !matches.is_present("foreground") {
// Check if we are the root user
if nix::unistd::getuid() != 0 {
bail!("You are not the root user which can spawn the daemon.");
}
debug!("Starting daemon.");
// Daemonize the process
let daemonize = Daemonize::new()
.pid_file("/tmp/wireguard.pid")
.chown_pid_file(true)
.working_directory("/tmp")
.user("nobody")
.group("daemon")
.umask(0o077);
daemonize.start()?;
}
// Run the instance
wireguard.run()?;
Ok(())
}
|