aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
authorMathias Hall-Andersen <mathias@hall-andersen.dk>2019-11-25 13:33:00 +0100
committerMathias Hall-Andersen <mathias@hall-andersen.dk>2019-11-25 13:33:00 +0100
commitf228b6f98b141940a3302d4cd1978f56f5edb13e (patch)
treef0486d26f494ce7f5d507205aa5cd475d05385c1 /src/main.rs
parentMake IO traits suitable for Tun events (up/down) (diff)
downloadwireguard-rs-f228b6f98b141940a3302d4cd1978f56f5edb13e.tar.xz
wireguard-rs-f228b6f98b141940a3302d4cd1978f56f5edb13e.zip
Enable up/down from configuration interface
Diffstat (limited to '')
-rw-r--r--src/main.rs53
1 files changed, 39 insertions, 14 deletions
diff --git a/src/main.rs b/src/main.rs
index c566f81..1a9650b 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -4,6 +4,7 @@
use log;
use daemonize::Daemonize;
+
use std::env;
use std::process::exit;
use std::thread;
@@ -12,7 +13,9 @@ mod configuration;
mod platform;
mod wireguard;
-use platform::tun::PlatformTun;
+use configuration::Configuration;
+
+use platform::tun::{PlatformTun, Status};
use platform::uapi::{BindUAPI, PlatformUAPI};
use platform::*;
@@ -81,34 +84,56 @@ fn main() {
// create WireGuard device
let wg: wireguard::Wireguard<plt::Tun, plt::UDP> = wireguard::Wireguard::new(readers, writer);
- wg.set_mtu(1420);
+ // wrap in configuration interface
+ let cfg = configuration::WireguardConfig::new(wg);
// start Tun event thread
- /*
{
- let wg = wg.clone();
+ let cfg = cfg.clone();
let mut status = status;
thread::spawn(move || loop {
match status.event() {
- Err(_) => break,
+ Err(e) => {
+ log::info!("Tun device error {}", e);
+ exit(0);
+ }
Ok(tun::TunEvent::Up(mtu)) => {
- wg.mtu.store(mtu, Ordering::Relaxed);
+ log::info!("Tun up (mtu = {})", mtu);
+
+ // bring the wireguard device up
+ cfg.up(mtu);
+
+ // start listening on UDP
+ let _ = cfg
+ .start_listener()
+ .map_err(|e| log::info!("Failed to start UDP listener: {}", e));
+ }
+ Ok(tun::TunEvent::Down) => {
+ log::info!("Tun down");
+
+ // set wireguard device down
+ cfg.down();
+
+ // close UDP listener
+ let _ = cfg
+ .stop_listener()
+ .map_err(|e| log::info!("Failed to stop UDP listener {}", e));
}
- Ok(tun::TunEvent::Down) => {}
}
});
}
- */
- // handle TUN updates up/down
-
- // wrap in configuration interface and start UAPI server
- let cfg = configuration::WireguardConfig::new(wg);
+ // start UAPI server
loop {
match uapi.connect() {
- Ok(mut stream) => configuration::uapi::handle(&mut stream, &cfg),
+ Ok(mut stream) => {
+ let cfg = cfg.clone();
+ thread::spawn(move || {
+ configuration::uapi::handle(&mut stream, &cfg);
+ });
+ }
Err(err) => {
- log::info!("UAPI error: {:}", err);
+ log::info!("UAPI error: {}", err);
break;
}
}