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

use log;

use std::env;

mod configuration;
mod platform;
mod wireguard;

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;
    }

    // start logging
    env_logger::builder()
        .try_init()
        .expect("Failed to initialize event logger");

    // 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;
            }
        }
    }
}