diff --git a/data/wg0.conf b/data/wg0.conf deleted file mode 100644 index eacfad92..00000000 --- a/data/wg0.conf +++ /dev/null @@ -1,13 +0,0 @@ -# Note: Do not edit this file directly. -# Your changes will be overwritten! - -# Server -[Interface] -PrivateKey = aCjYU+CBAkIerzXXFj8hzc4ZmMDLNGrhsxqJjOwtPH0= -Address = 10.8.0.1/24 -ListenPort = 51820 -PreUp = -PostUp = iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE; iptables -A INPUT -p udp -m udp --dport 51820 -j ACCEPT; iptables -A FORWARD -i wg0 -j ACCEPT; iptables -A FORWARD -o wg0 -j ACCEPT; -PreDown = -PostDown = - diff --git a/data/wg0.json b/data/wg0.json deleted file mode 100644 index 1220045d..00000000 --- a/data/wg0.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "server": { - "privateKey": "aCjYU+CBAkIerzXXFj8hzc4ZmMDLNGrhsxqJjOwtPH0=", - "publicKey": "Gi9QI2VgCJK1ziPaE2gE/jXbiXj+Rx3mUbIq8RcHPEE=", - "address": "10.8.0.1" - }, - "clients": {} -} diff --git a/data/www/index.html b/data/www/index.html index a842588e..e69de29b 100644 --- a/data/www/index.html +++ b/data/www/index.html @@ -1,528 +0,0 @@ - - - - - WireGuard - - - - - - - - - - - - -
- -
- -
- - Logout - - - - -

- - WireGuard -

-

- -
-
-
-

There is an update available!

-

{{latestRelease.changelog}}

-
- - - Update → - -
-
- -
-
-
-

Clients

-
-
- -
-
- -
- -
- - -
- -
- - -
- - -
-
-
- -
-
- - - - - -
-
-
-
-
- -
- - -
- - - - {{client.name}} - - - - - - - -
- - -
- - - - - - - {{client.address}} - - - - - - - - - - - - · - - - - {{client.transferTxCurrent | bytes}}/s - - - - - · - - - - {{client.transferRxCurrent | bytes}}/s - - - - - · {{new Date(client.latestHandshakeAt) | timeago}} - -
-
- -
-
- - -
-
-
-
-
-
- - - - - - - - - - - - - -
-
- -
- -
-
-

There are no clients yet.

- -

-
-
- - - - - -
-
-
- - -
-
-
- - -
-
-
- - -
-
- - - - - - - -
-
- - -
-
- - - - - - - -
-
-
- -
-

WireGuard

- -
- -
- - - -
- - - - - - -
-
- -
- - - - - - - -
- -
- -

Made by Emile Nijssen · Donate · GitHub

- - -
- - - - - - - - - \ No newline at end of file diff --git a/data/www/manifest.json b/data/www/manifest.json index 5214f3f3..e69de29b 100644 --- a/data/www/manifest.json +++ b/data/www/manifest.json @@ -1,11 +0,0 @@ -{ - "name": "WireGuard", - "display": "standalone", - "background_color": "#fff", - "icons": [ - { - "src": "img/favicon.png", - "type": "image/png" - } - ] -} \ No newline at end of file diff --git a/docker-compose.deploy.yml b/docker-compose.deploy.yml index 40325a80..5fef99c9 100644 --- a/docker-compose.deploy.yml +++ b/docker-compose.deploy.yml @@ -5,7 +5,7 @@ services: container_name: rs-wg environment: - WIREGUARD_SETTINGS=/etc/wireguard/settings.json - - WIREGUARD_PATH=/etc/wireguard/ + - WIREGUARD_PATH=/etc/wireguard - STATIC_DIR=/etc/www/ - RUST_LOG=actix_web=warn,debug diff --git a/docker-compose.yml b/docker-compose.yml index 3d146da0..9f391bee 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,7 +5,7 @@ services: container_name: rs-wg environment: - WIREGUARD_SETTINGS=/etc/wireguard/settings.json - - WIREGUARD_PATH=/etc/wireguard/ + - WIREGUARD_PATH=/etc/wireguard - STATIC_DIR=/etc/wireguard/www - RUST_LOG=actix_web=warn,debug diff --git a/src/utils/os.rs b/src/utils/os.rs index 7924e8f8..0df42ff6 100644 --- a/src/utils/os.rs +++ b/src/utils/os.rs @@ -1,8 +1,13 @@ -use std::{ffi::OsStr, string::FromUtf8Error}; +use std::string::FromUtf8Error; +use futures_util::TryFutureExt; use log::{info, warn}; use serde::Serialize; -use tokio::{fs::File, io::AsyncReadExt, process::Command}; +use tokio::{ + fs::File, + io::{AsyncReadExt, AsyncWriteExt}, + process::Command, +}; #[derive(Serialize)] pub struct StdResult { @@ -21,26 +26,40 @@ impl StdResult { } } -pub async fn load_file>(path: S) -> Result { +pub async fn write_file, D: Into>( + path: P, + data: D, +) -> Result<(), std::io::Error> { tokio::fs::OpenOptions::new() - .append(false) - .read(true) .write(true) + .truncate(true) .create(true) .open(path.into()) + .and_then(|mut file| async move { + let result = file.write(data.into().as_bytes()).await; + file.flush().await + }) .await } -pub async fn read_file(mut file: File) -> Result { - let mut dst = String::new(); - file.read_to_string(&mut dst).await.map(|_| dst) +pub async fn read_file>(path: S) -> Result { + tokio::fs::OpenOptions::new() + .read(true) + .write(true) + .create(true) + .open(path.into()) + .and_then(|mut file| async move { + let mut dst = String::new(); + file.read_to_string(&mut dst).await.map(|_| dst) + }) + .await } pub async fn load_file_unhandled(path: &str) -> File { match tokio::fs::OpenOptions::new() - .append(false) .read(true) .write(true) + .truncate(true) .create(true) .open(path) .await diff --git a/src/wg_rs/config.rs b/src/wg_rs/config.rs index 5dafb6b4..b657e389 100644 --- a/src/wg_rs/config.rs +++ b/src/wg_rs/config.rs @@ -3,6 +3,7 @@ use std::{collections::HashMap, sync::Arc}; use chrono::{SecondsFormat, SubsecRound, Utc}; use futures_util::TryFutureExt; +use log::error; use serde::{Deserialize, Serialize}; use tokio::{io::AsyncWriteExt, join}; @@ -128,8 +129,7 @@ pub struct Accessor { impl Accessor { pub async fn new(memento_path: String, config_path: String, settings: Arc) -> Self { - let memento_str = os::load_file(memento_path.clone()) - .and_then(|file| os::read_file(file)) + let memento_str = os::read_file(memento_path.clone()) .await .expect("Could not read memento"); @@ -169,25 +169,20 @@ impl Accessor { ser_memento.push('\n'); ser_config.push('\n'); - _ = join!( - async { - let mut file = os::load_file(self.memento_path.clone()) - .await - .expect("Could not load memento"); - file.write(ser_memento.as_bytes()) - .await - .expect("Could not write memento"); - }, - async { - let mut file = os::load_file(self.config_path.clone()) - .await - .expect("Could not load config"); - file.write(ser_config.as_bytes()) - .await - .expect("Could not write config"); - } + let (memento_wr, config_wr) = join!( + os::write_file(self.memento_path.clone(), ser_memento), + os::write_file(self.config_path.clone(), ser_config) ); + memento_wr.unwrap_or_else(|err| { + error!("Could not write memento: {}", err); + panic!() + }); + config_wr.unwrap_or_else(|err| { + error!("Could not write config: {}", err); + panic!() + }); + self.memento = memento; } diff --git a/src/wg_rs/wireguard.rs b/src/wg_rs/wireguard.rs index f7770c43..f5a7643c 100644 --- a/src/wg_rs/wireguard.rs +++ b/src/wg_rs/wireguard.rs @@ -17,7 +17,7 @@ pub struct WireGuard { impl WireGuard { pub async fn new(name: &str, wg_path: String, settings: Settings) -> Self { - let path = format!("{}{}", wg_path, name); + let path = format!("{}/{}", wg_path, name); let config_path = format!("{}.conf", path); let memento_path = format!("{}.json", path); let name = format!("{}", name); @@ -122,29 +122,29 @@ impl WireGuard { } pub async fn get_clients(&self) -> Vec { - let res = os::exec_sh("wg show wg0 dump").await.unwrap(); - - let lines: Vec> = res - .stdout - .trim() - .split('\n') - .skip(1) - .map(|line| line.split('\t').map(|x| x.to_string()).collect()) - .collect(); - - let lines: Vec = lines - .iter() - .map(|x| DumpClient { - public_key: x[0].to_string(), - pre_shared_key: x[1].to_string(), - endpoint: x[2].to_string(), - allowed_ips: x[3].to_string(), - latest_handshake_at: x[4].to_string(), - transfer_rx: x[5].parse::().unwrap(), - transfer_tx: x[6].parse::().unwrap(), - persistent_keepalive: x[7].to_string(), - }) - .collect(); + // let res = os::exec_sh("wg show wg0 dump").await.unwrap(); + + // let lines: Vec> = res + // .stdout + // .trim() + // .split('\n') + // .skip(1) + // .map(|line| line.split('\t').map(|x| x.to_string()).collect()) + // .collect(); + + // let lines: Vec = lines + // .iter() + // .map(|x| DumpClient { + // public_key: x[0].to_string(), + // pre_shared_key: x[1].to_string(), + // endpoint: x[2].to_string(), + // allowed_ips: x[3].to_string(), + // latest_handshake_at: x[4].to_string(), + // transfer_rx: x[5].parse::().unwrap(), + // transfer_tx: x[6].parse::().unwrap(), + // persistent_keepalive: x[7].to_string(), + // }) + // .collect(); let memento = self.memento_accessor.get(); let clients: Vec = memento @@ -153,12 +153,12 @@ impl WireGuard { .map(|(uuid, value)| { let uuid = uuid.to_owned(); let value = value.to_owned(); - let client_conf; + // let client_conf; - match lines.iter().find(|x| x.public_key.eq(&value.public_key)) { - Some(conf) => client_conf = conf, - None => return None, - }; + // match lines.iter().find(|x| x.public_key.eq(&value.public_key)) { + // Some(conf) => client_conf = conf, + // None => return None, + // }; Some(WebClient { id: uuid, name: value.name, @@ -167,11 +167,11 @@ impl WireGuard { public_key: value.public_key, created_at: value.created_at, updated_at: value.updated_at, - allowed_ips: client_conf.allowed_ips.to_string(), - persistent_keepalive: client_conf.persistent_keepalive.to_string(), - latest_handshake_at: client_conf.latest_handshake_at.to_string(), - transfer_rx: client_conf.transfer_rx, - transfer_tx: client_conf.transfer_tx, + allowed_ips: "123".to_string(), //client_conf.allowed_ips.to_string(), + persistent_keepalive: "123".to_string(), //client_conf.persistent_keepalive.to_string(), + latest_handshake_at: "123".to_string(), //client_conf.latest_handshake_at.to_string(), + transfer_rx: 1234, //client_conf.transfer_rx, + transfer_tx: 1234, //client_conf.transfer_tx, }) }) .filter(|x| x.is_some())