diff --git a/README.md b/README.md index 11ea5db3..b4a79e3d 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ These options can be configured by setting environment variables using `-e KEY=" | - | - | - | - | | `PASSWORD` | - | `foobar123` | When set, requires a password when logging in to the Web UI. | | `HOST` | - | `vpn.myserver.com` | The public hostname of your VPN server. | -| `WG_PORT` | `51820` | `12345` | The public UDP port of your VPN server. WireGuard will always listen on `51820` inside the Docker container. | +| `WG_PORT` | `51820` | `12345` | The public UDP port of your VPN server. WireGuard will always listen on this port inside the Docker container. | | `MTU` | `null` | `1420` | The MTU the clients will use. Server uses default WG MTU. | | `PERSISTENT_KEEPALIVE` | `0` | `25` | Value in seconds to keep the "connection" open. If this value is 0, then connections won't be kept alive. | | `DEFAULT_ADDRESS` | `10.8.0.1` | `10.6.0.1` | Clients IP address range. | diff --git a/data/wg0.conf b/data/wg0.conf deleted file mode 100644 index 2d546ed4..00000000 --- a/data/wg0.conf +++ /dev/null @@ -1,19 +0,0 @@ -# Note: Do not edit this file directly. -# Your changes will be overwritten! - -# Server -[Interface] -PrivateKey = 7djcxyZBiUQ6OrB36p1cKLGHl1zg5MqQShN9Udl41Hw= -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 = - -# Client: 321 (184a9b70-03b3-4bfb-ae89-ce463634f394) -[Peer] -PublicKey = ej10pb+XPRVuotGrkz+423Lku6Jyf5OHqt0ccjx6KU8= -PresharedKey = Y/5LvXKegqRoeugphZZOLS1/Kl+wN+HdJoCdulxzrEs= -AllowedIPs = 10.8.0.2/32 - diff --git a/data/wg0.json b/data/wg0.json deleted file mode 100644 index ca7bbf7f..00000000 --- a/data/wg0.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "server": { - "privateKey": "7djcxyZBiUQ6OrB36p1cKLGHl1zg5MqQShN9Udl41Hw=", - "publicKey": "kAW7Jefvgqs5H6hyuF05a1vSZJJdAiedg3AnVdgjFVk=", - "address": "10.8.0.1" - }, - "clients": { - "184a9b70-03b3-4bfb-ae89-ce463634f394": { - "uuid": "184a9b70-03b3-4bfb-ae89-ce463634f394", - "name": "321", - "address": "10.8.0.2", - "privateKey": "+K2Bi7AYB1pUCWqIPdRIpqnGFOjG0PE7W8pOBLsatXo=", - "publicKey": "ej10pb+XPRVuotGrkz+423Lku6Jyf5OHqt0ccjx6KU8=", - "preSharedKey": "Y/5LvXKegqRoeugphZZOLS1/Kl+wN+HdJoCdulxzrEs=", - "createdAt": "2022-12-18T15:55:49Z", - "updatedAt": "2022-12-18T15:55:49Z", - "enabled": true - } - } -} diff --git a/data/www/index.html b/data/www/index.html index e69de29b..7d5da28d 100644 --- a/data/www/index.html +++ b/data/www/index.html @@ -0,0 +1,528 @@ + + + + + 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/index__.html b/data/www/index__.html deleted file mode 100644 index deed7168..00000000 --- a/data/www/index__.html +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - Document - - -

Hello!

- - \ No newline at end of file diff --git a/data/www/manifest.json b/data/www/manifest.json index e69de29b..fdf1f9e1 100644 --- a/data/www/manifest.json +++ b/data/www/manifest.json @@ -0,0 +1,11 @@ +{ + "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 5fef99c9..fc01db35 100644 --- a/docker-compose.deploy.yml +++ b/docker-compose.deploy.yml @@ -1,6 +1,6 @@ version: "3.8" services: - rs-wg: + rs-wg: image: yamzeg/rs-wg container_name: rs-wg environment: @@ -15,7 +15,7 @@ services: # - API_PORT=8080 # - PASSWORD=123321 - HOST=localhost - # - WG_PORT=51820 + - WG_PORT=51820 # - MTU=0 # - PERSISTENT_KEEPALIVE=25 # - DEFAULT_ADDRESS=10.8.0.1 @@ -37,4 +37,4 @@ services: sysctls: - net.ipv4.ip_forward=1 - net.ipv4.conf.all.src_valid_mark=1 - entrypoint: dumb-init /usr/local/cargo/bin/rs-wg \ No newline at end of file + entrypoint: dumb-init /usr/local/cargo/bin/rs-wg diff --git a/docker-compose.yml b/docker-compose.yml index 9f391bee..6c89c003 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -15,7 +15,7 @@ services: # - API_PORT=8080 # - PASSWORD=123321 - HOST=localhost - # - WG_PORT=51825 + - WG_PORT=51820 # - MTU=0 # - PERSISTENT_KEEPALIVE=25 # - DEFAULT_ADDRESS=10.8.0.1 @@ -31,7 +31,7 @@ services: - ./data:/etc/wireguard - ./application/bin:/usr/local/cargo/bin ports: - - "8081:51820/udp" + - "51820:51820/udp" - "8080:8080/tcp" cap_add: - NET_ADMIN diff --git a/src/main.rs b/src/main.rs index a826a856..6030afe7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -221,13 +221,18 @@ async fn main() -> std::io::Result<()> { let password = get_env("PASSWORD", "123321"); let settings = get_wireguard_settings(); - let mut wireguard = WireGuard::new("wg0", wireguard_path, settings).await; + let mut wireguard = WireGuard::new(wireguard_path, settings).await; wireguard.start().await; let index_path = format!("{}/index.html", static_dir); let mainfest_path = format!("{}/manifest.json", static_dir); - let (_, index_page) = os::load_and_read_file_unhandled(index_path.as_str()).await; - let (_, manifest_file) = os::load_and_read_file_unhandled(mainfest_path.as_str()).await; + + let index_page = os::read_file(index_path) + .await + .expect("Could not read index"); + let manifest_file = os::read_file(mainfest_path) + .await + .expect("Could not read manifest"); let security = web::Data::new(Security { password }); let web_wireguard = web::Data::new(RwLock::new(wireguard)); @@ -281,7 +286,7 @@ fn get_wireguard_settings() -> Settings { let api_port = get_env("API_PORT", "8080"); let password = get_env("PASSWORD", "123321"); let host = std::env::var("HOST").expect("Must set HOST env var"); - let wg_port = get_env("WG_PORT", "51820"); + let wg_port = std::env::var("WG_PORT").expect("Must set WG_PORT env var"); let mtu = get_env("MTU", "0"); let persistent_keepalive = get_env("PERSISTENT_KEEPALIVE", "25"); let default_address = get_env("DEFAULT_ADDRESS", "10.8.0.1"); diff --git a/src/utils/os.rs b/src/utils/os.rs index ac119057..4450e11b 100644 --- a/src/utils/os.rs +++ b/src/utils/os.rs @@ -67,35 +67,6 @@ pub async fn read_file>(path: S) -> Result File { - match tokio::fs::OpenOptions::new() - .read(true) - .write(true) - .truncate(true) - .create(true) - .open(path) - .await - { - Ok(file) => file, - Err(error) => { - log::error!("Could not load file {}: {}", path, error); - panic!() - } - } -} - -pub async fn load_and_read_file_unhandled(path: &str) -> (File, String) { - let mut buffer = String::new(); - let mut file = load_file_unhandled(path).await; - match file.read_to_string(&mut buffer).await { - Ok(_) => (file, buffer), - Err(error) => { - log::error!("Could not read file data: {}", error); - panic!() - } - } -} - pub async fn exec_sh(command: &S) -> Result> { let command = command.to_string(); info!("$ {}", command.clone()); diff --git a/src/wg_rs/config.rs b/src/wg_rs/config.rs index b57c74a2..f96ee07f 100644 --- a/src/wg_rs/config.rs +++ b/src/wg_rs/config.rs @@ -140,13 +140,13 @@ impl Accessor { if memento_str.is_empty() { log::info!("Creating new WireGuard configuration..."); os::exec_sh(&"wg genkey") - .and_then(|res_public| async move { - os::exec_sh(&format!("echo {} | wg pubkey", &res_public.stdout)) + .and_then(|res_private| async move { + os::exec_sh(&format!("echo {} | wg pubkey", &res_private.stdout)) .await - .map(|res_private| (res_public.stdout, res_private.stdout)) + .map(|res_public| (res_private.stdout, res_public.stdout)) }) .await - .and_then(|(public_key, private_key)| { + .and_then(|(private_key, public_key)| { let address = settings.default_address.clone(); let memento = Memento::new(Server { private_key, @@ -207,7 +207,7 @@ impl Accessor { format!("[Interface]\n"), format!("PrivateKey = {}\n", config.server.private_key), format!("Address = {}/24\n", config.server.address), - format!("ListenPort = 51820\n"), + format!("ListenPort = {}\n", self.settings.wg_port), format!("PreUp = {}\n", self.settings.pre_up), format!("PostUp = {}\n", self.settings.post_up), format!("PreDown = {}\n", self.settings.pre_down), diff --git a/src/wg_rs/wireguard.rs b/src/wg_rs/wireguard.rs index eac22f42..0b557876 100644 --- a/src/wg_rs/wireguard.rs +++ b/src/wg_rs/wireguard.rs @@ -17,11 +17,11 @@ pub struct WireGuard { } impl WireGuard { - pub async fn new(name: &str, wg_path: String, settings: Settings) -> Self { - let path = format!("{}/{}", wg_path, name); + pub async fn new(wg_path: String, settings: Settings) -> Self { + let path = format!("{}/{}", wg_path, settings.interface_name); let config_path = format!("{}.conf", path); let memento_path = format!("{}.json", path); - let name = format!("{}", name); + let name = format!("{}", settings.interface_name); let counter = Arc::new(settings);