aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 5f75d04ae4c67cbf7c58f8898b1898953786aa3b (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#![allow(unused_imports)]

#[macro_use] extern crate error_chain;
#[macro_use] extern crate futures;
#[macro_use] extern crate log;
#[macro_use] extern crate structopt_derive;

extern crate base64;
extern crate byteorder;
extern crate bytes;
extern crate crypto;
extern crate daemonize;
extern crate env_logger;
extern crate hex;
extern crate nix;
extern crate pnet;
extern crate rand;
extern crate snow;
extern crate structopt;
extern crate time;
extern crate tokio_core;
extern crate tokio_io;
extern crate tokio_uds;
extern crate tokio_utun;
extern crate tokio_timer;
extern crate treebitmap;

mod consts;
mod error;
mod interface;
mod protocol;
mod types;

use std::path::PathBuf;

use daemonize::Daemonize;
use error::{ErrorKind, Error, Result};
use interface::Interface;
use structopt::StructOpt;

#[derive(StructOpt, Debug)]
#[structopt(name = "wgrs", about = "WireGuard - a network tunnel")]
struct Opt {
    /// A flag, true if used in the command line.
    #[structopt(short = "d", long = "debug", help = "Activate debug mode")]
    debug: bool,

    /// An argument of type float, with a default value.
    #[structopt(short = "f", long = "foreground", help = "Run in the foreground")]
    foreground: bool,

    /// Needed parameter, the first on the command line.
    #[structopt(help = "WireGuard interface name", default_value = "utun4")]
    interface: String,

    /// An optional parameter, will be `None` if not present on the
    /// command line.
    #[structopt(help = "Output file, stdout if not present")]
    output: Option<String>,
}

fn main() {
    env_logger::init().unwrap();
    let opt = Opt::from_args();

//    if !opt.foreground {
//        daemonize().expect("failed to daemonize");
//    }

    Interface::new(&opt.interface).start();
}

fn daemonize() -> Result<()> {
    if !nix::unistd::getuid().is_root() {
        bail!("You are not the root user which can spawn the daemon.");
    }

    debug!("Starting daemon.");

    let pid_path = PathBuf::new(); // TODO temporary

//    let pid_path = WireGuard::get_run_path();

    let daemonize = Daemonize::new()
        .pid_file(pid_path.join("wireguard.pid"))
        .chown_pid_file(true)
        .working_directory(pid_path)
        .user("nobody")
        .group("daemon")
        .umask(0o077);

    daemonize.start()?;
    Ok(())
}