From 415e56c8252e780d5d206f53745fb27170ce286d Mon Sep 17 00:00:00 2001 From: Bernd Storath <32197462+kaaax0815@users.noreply.github.com> Date: Tue, 3 Sep 2024 15:13:16 +0200 Subject: [PATCH] Feat: Rewrite Wireguard to use Database (#1345) * update wireguard * update * update * remove all config * move all features into one route * improve code * fix some issues add wg_path, update documentation --- .gitignore | 2 - Dockerfile | 1 + Dockerfile.dev | 1 + README.md | 44 +- docker-compose.dev.yml | 2 - docker-compose.yml | 29 +- src/app/app.vue | 7 +- src/app/components/Client/Charts.vue | 20 +- src/app/components/Client/Client.vue | 4 +- src/app/components/Client/ExpireDate.vue | 8 +- src/app/components/Client/LastSeen.vue | 2 +- src/app/components/Client/OneTimeLink.vue | 4 +- src/app/components/Client/OneTimeLinkBtn.vue | 2 +- src/app/components/Clients/CreateDialog.vue | 5 +- src/app/components/Clients/Sort.vue | 2 +- src/app/layouts/Header.vue | 4 +- src/app/pages/login.vue | 2 - src/app/stores/auth.ts | 2 +- src/app/stores/clients.ts | 5 +- src/app/stores/global.ts | 70 +-- src/app/utils/api.ts | 44 +- ...clientOneTimeLink].ts => [oneTimeLink].ts} | 26 +- src/server/api/features.get.ts | 9 + src/server/api/release.get.ts | 6 - src/server/api/remember-me.get.ts | 5 - src/server/api/session.post.ts | 10 +- src/server/api/ui-chart-type.get.ts | 11 - src/server/api/ui-sort-clients.get.ts | 11 - src/server/api/ui-traffic-stats.get.ts | 11 - src/server/api/wg-enable-expire-time.get.ts | 11 - .../api/wg-enable-one-time-links.get.ts | 11 - .../[clientId]/generateOneTimeLink.post.ts | 3 +- src/server/middleware/session.ts | 4 +- src/server/middleware/setup.ts | 3 +- src/server/utils/Database.ts | 1 - src/server/utils/WireGuard.ts | 398 +++++++----------- src/server/utils/config.ts | 64 --- src/server/utils/types.ts | 12 + src/services/database/lowdb.ts | 110 ++++- src/services/database/migrations/1.ts | 64 +-- src/services/database/repositories/client.ts | 43 ++ .../database/repositories/database.ts | 27 +- src/services/database/repositories/system.ts | 4 +- 43 files changed, 477 insertions(+), 627 deletions(-) rename src/server/api/cnf/{[clientOneTimeLink].ts => [oneTimeLink].ts} (60%) create mode 100644 src/server/api/features.get.ts delete mode 100644 src/server/api/remember-me.get.ts delete mode 100644 src/server/api/ui-chart-type.get.ts delete mode 100644 src/server/api/ui-sort-clients.get.ts delete mode 100644 src/server/api/ui-traffic-stats.get.ts delete mode 100644 src/server/api/wg-enable-expire-time.get.ts delete mode 100644 src/server/api/wg-enable-one-time-links.get.ts create mode 100644 src/services/database/repositories/client.ts diff --git a/.gitignore b/.gitignore index ed408701..e6fce2a6 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,3 @@ /src/node_modules .DS_Store *.swp -# lowdb data file -db.json \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index e17dff7e..966632b0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -36,6 +36,7 @@ RUN update-alternatives --install /sbin/iptables iptables /sbin/iptables-legacy # Set Environment ENV DEBUG=Server,WireGuard,LowDB ENV PORT=51821 +ENV HOST=0.0.0.0 # Run Web UI CMD ["/usr/bin/dumb-init", "node", "server/index.mjs"] diff --git a/Dockerfile.dev b/Dockerfile.dev index d464e989..fed4ec47 100644 --- a/Dockerfile.dev +++ b/Dockerfile.dev @@ -26,3 +26,4 @@ RUN update-alternatives --install /sbin/iptables iptables /sbin/iptables-legacy # Set Environment ENV DEBUG=Server,WireGuard,LowDB ENV PORT=51821 +ENV HOST=0.0.0.0 diff --git a/README.md b/README.md index 78e263e5..dba5d9a4 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ You have found the easiest way to install & manage WireGuard on any Linux host! - Traffic Stats (default off) - One Time Links (default off) - Client Expiration (default off) -- Prometheus metrics support +- Prometheus metrics support (default off) ## Requirements @@ -41,7 +41,7 @@ For **stable** versions instead of nightly or development please read **README** | tag | Branch | Example | Description | | ------------- | ------------- | ------------------------------------------------------------- | ------------------------------------------------------------------------------------ | -| `latest` | production | `ghcr.io/wg-easy/wg-easy:latest` or `ghcr.io/wg-easy/wg-easy` | stable as possbile get bug fixes quickly when needed, deployed against `production`. | +| `latest` | production | `ghcr.io/wg-easy/wg-easy:latest` or `ghcr.io/wg-easy/wg-easy` | stable as possible get bug fixes quickly when needed, deployed against `production`. | | `13` | production | `ghcr.io/wg-easy/wg-easy:13` | same as latest, stick to a version tag. | | `nightly` | master | `ghcr.io/wg-easy/wg-easy:nightly` | mostly unstable gets frequent package and code updates, deployed against `master`. | | `development` | pull requests | `ghcr.io/wg-easy/wg-easy:development` | used for development, testing code from PRs before landing into `master`. | @@ -64,13 +64,10 @@ And log in again. To automatically install & run wg-easy, simply run: -``` +```bash docker run -d \ --name=wg-easy \ - -e LANG=de \ - -e WG_HOST=<🚨YOUR_SERVER_IP> \ -e PORT=51821 \ - -e WG_PORT=51820 \ -v ~/.wg-easy:/etc/wireguard \ -p 51820:51820/udp \ -p 51821:51821/tcp \ @@ -102,34 +99,11 @@ Are you enjoying this project? [Buy Emile a beer!](https://github.com/sponsors/W These options can be configured by setting environment variables using `-e KEY="VALUE"` in the `docker run` command. -| Env | Default | Example | Description | -| ----------------------------- | ----------------- | ------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `PORT` | `51821` | `6789` | TCP port for Web UI. | -| `WEBUI_HOST` | `0.0.0.0` | `localhost` | IP address web UI binds to. | -| `WG_HOST` | - | `vpn.myserver.com` | The public hostname of your VPN server. | -| `WG_DEVICE` | `eth0` | `ens6f0` | Ethernet device the wireguard traffic should be forwarded through. | -| `WG_PORT` | `51820` | `12345` | The public UDP port of your VPN server. WireGuard will listen on that (othwise default) inside the Docker container. | -| `WG_CONFIG_PORT` | `51820` | `12345` | The UDP port used on [Home Assistant Plugin](https://github.com/adriy-be/homeassistant-addons-jdeath/tree/main/wgeasy) | -| `WG_MTU` | `null` | `1420` | The MTU the clients will use. Server uses default WG MTU. | -| `WG_PERSISTENT_KEEPALIVE` | `0` | `25` | Value in seconds to keep the "connection" open. If this value is 0, then connections won't be kept alive. | -| `WG_DEFAULT_ADDRESS` | `10.8.0.x` | `10.6.0.x` | Clients IP address range. | -| `WG_DEFAULT_DNS` | `1.1.1.1` | `8.8.8.8, 8.8.4.4` | DNS server clients will use. If set to blank value, clients will not use any DNS. | -| `WG_ALLOWED_IPS` | `0.0.0.0/0, ::/0` | `192.168.15.0/24, 10.0.1.0/24` | Allowed IPs clients will use. | -| `WG_PRE_UP` | `...` | - | See [config.js](https://github.com/wg-easy/wg-easy/blob/master/src/config.js#L19) for the default value. | -| `WG_POST_UP` | `...` | `iptables ...` | See [config.js](https://github.com/wg-easy/wg-easy/blob/master/src/config.js#L20) for the default value. | -| `WG_PRE_DOWN` | `...` | - | See [config.js](https://github.com/wg-easy/wg-easy/blob/master/src/config.js#L27) for the default value. | -| `WG_POST_DOWN` | `...` | `iptables ...` | See [config.js](https://github.com/wg-easy/wg-easy/blob/master/src/config.js#L28) for the default value. | -| `WG_ENABLE_EXPIRES_TIME` | `false` | `true` | Enable expire time for clients | -| `LANG` | `en` | `de` | Web UI language (Supports: en, ua, ru, tr, no, pl, fr, de, ca, es, ko, vi, nl, is, pt, chs, cht, it, th, hi). | -| `UI_TRAFFIC_STATS` | `false` | `true` | Enable detailed RX / TX client stats in Web UI | -| `UI_CHART_TYPE` | `0` | `1` | UI_CHART_TYPE=0 # Charts disabled, UI_CHART_TYPE=1 # Line chart, UI_CHART_TYPE=2 # Area chart, UI_CHART_TYPE=3 # Bar chart | -| `WG_ENABLE_ONE_TIME_LINKS` | `false` | `true` | Enable display and generation of short one time download links (expire after 5 minutes) | -| `MAX_AGE` | `0` | `1440` | The maximum age of Web UI sessions in minutes. `0` means that the session will exist until the browser is closed. | -| `UI_ENABLE_SORT_CLIENTS` | `false` | `true` | Enable UI sort clients by name | -| `ENABLE_PROMETHEUS_METRICS` | `false` | `true` | Enable Prometheus metrics `http://0.0.0.0:51821/metrics` and `http://0.0.0.0:51821/metrics/json` | -| `PROMETHEUS_METRICS_PASSWORD` | - | `$2y$05$Ci...` | If set, Basic Auth is required when requesting metrics. See [How to generate an bcrypt hash.md]("https://github.com/wg-easy/wg-easy/blob/master/How_to_generate_an_bcrypt_hash.md") for know how generate the hash. | - -> If you change `WG_PORT`, make sure to also change the exposed port. +| Env | Default | Example | Description | +| --------- | ----------------- | ------------- | -------------------------------------------- | +| `PORT` | `51821` | `6789` | TCP port for Web UI. | +| `HOST` | `0.0.0.0` | `localhost` | IP address web UI binds to. | +| `WG_PATH` | `/etc/wireguard/` | `/home/user/` | The Path your `wg0.conf` and `db.json` lives | ## Updating @@ -148,7 +122,7 @@ With Docker Compose WireGuard Easy can be updated with a single command: Compose file and it is not `latest`, make sure that it is changed to the desired one; by default it is omitted and [defaults to `latest`](https://docs.docker.com/engine/reference/run/#image-references)). \ -The WireGuared Easy container will be automatically recreated if a newer image +The WireGuard Easy container will be automatically recreated if a newer image was pulled. ## Common Use Cases diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index 8bfbd9d5..7ae32c37 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -14,8 +14,6 @@ services: cap_add: - NET_ADMIN - SYS_MODULE - environment: - - WG_HOST=192.168.1.233 # folders should be generated inside container volumes: diff --git a/docker-compose.yml b/docker-compose.yml index 78a86aba..f607837b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,33 +4,10 @@ volumes: services: wg-easy: environment: - # Change Language: - # (Supports: en, ua, ru, tr, no, pl, fr, de, ca, es, ko, vi, nl, is, pt, chs, cht, it, th, hi) - - LANG=en - # ⚠️ Required: - # Change this to your host's public address - - WG_HOST=raspberrypi.local - + - PORT=51821 # Optional: - # - PORT=51821 - # - WG_PORT=51820 - # - WG_CONFIG_PORT=92820 - # - WG_DEFAULT_ADDRESS=10.8.0.x - # - WG_DEFAULT_DNS=1.1.1.1 - # - WG_MTU=1420 - # - WG_ALLOWED_IPS=192.168.15.0/24, 10.0.1.0/24 - # - WG_PERSISTENT_KEEPALIVE=25 - # - WG_PRE_UP=echo "Pre Up" > /etc/wireguard/pre-up.txt - # - WG_POST_UP=echo "Post Up" > /etc/wireguard/post-up.txt - # - WG_PRE_DOWN=echo "Pre Down" > /etc/wireguard/pre-down.txt - # - WG_POST_DOWN=echo "Post Down" > /etc/wireguard/post-down.txt - # - UI_TRAFFIC_STATS=true - # - UI_CHART_TYPE=0 # (0 Charts disabled, 1 # Line chart, 2 # Area chart, 3 # Bar chart) - # - WG_ENABLE_ONE_TIME_LINKS=true - # - UI_ENABLE_SORT_CLIENTS=true - # - WG_ENABLE_EXPIRES_TIME=true - # - ENABLE_PROMETHEUS_METRICS=false - # - PROMETHEUS_METRICS_PASSWORD=$$2a$$12$$vkvKpeEAHD78gasyawIod.1leBMKg8sBwKW.pQyNsq78bXV3INf2G # (needs double $$, hash of 'prometheus_password'; see "How_to_generate_an_bcrypt_hash.md" for generate the hash) + # - HOST=0.0.0.0 + # - WG_PATH=/etc/wireguard/ image: ghcr.io/wg-easy/wg-easy container_name: wg-easy diff --git a/src/app/app.vue b/src/app/app.vue index 860b7031..a8f0a0f7 100644 --- a/src/app/app.vue +++ b/src/app/app.vue @@ -8,13 +8,8 @@