summaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: b1762cbf835e724c8d811f2df32a3edb6574a2e0 (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
#![feature(test)]
#![allow(dead_code)]

extern crate jemallocator;

#[global_allocator]
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;

mod configuration;
mod platform;
mod wireguard;

use log;

use std::env;

use platform::tun::PlatformTun;
use platform::uapi::{BindUAPI, PlatformUAPI};
use platform::*;

fn main() {
    let mut name = String::new();
    let mut foreground = false;

    for arg in env::args() {
        if arg == "--foreground" || arg == "-f" {
            foreground = true;
        } else {
            name = arg;
        }
    }

    if name == "" {
        return;
    }

    let _ = env_logger::builder().is_test(true).try_init();

    // create UAPI socket
    let uapi = plt::UAPI::bind(name.as_str()).unwrap();

    // create TUN device
    let (readers, writer, mtu) = plt::Tun::create(name.as_str()).unwrap();

    // create WireGuard device
    let wg: wireguard::Wireguard<plt::Tun, plt::Bind> =
        wireguard::Wireguard::new(readers, writer, mtu);

    // wrap in configuration interface and start UAPI server
    let cfg = configuration::WireguardConfig::new(wg);
    loop {
        match uapi.connect() {
            Ok(mut stream) => configuration::uapi::handle(&mut stream, &cfg),
            Err(err) => {
                log::info!("UAPI error: {:}", err);
                break;
            }
        }
    }
}