diff --git a/README.md b/README.md index 02888ed5..bc2267a3 100644 --- a/README.md +++ b/README.md @@ -27,8 +27,6 @@

This project is not affiliate to the official WireGuard Project


- -## [Official Documentation ➡️](https://donaldzou.github.io/WGDashboard-Documentation/) > [!NOTE] > To better manage documentation for this project. I've moved it to its own [repo](https://github.com/donaldzou/WGDashboard-Documentation). I will keep updating over there and leave this README only with important information. diff --git a/docker/Docker-explain.md b/docker/Docker-explain.md deleted file mode 100644 index dd7bfe8a..00000000 --- a/docker/Docker-explain.md +++ /dev/null @@ -1,82 +0,0 @@ -# WG-Dashboard Docker Explanation: - -Author: DaanSelen
- -This document delves into how the WG-Dashboard Docker container has been built.
-Of course there are two stages, one before run-time and one at/after run-time.
-The `Dockerfile` describes how the container image is made, and the `entrypoint.sh` is executed after running the container.
-In this example, WireGuard is integrated into the container itself, so it should be a run-and-go.
-For more details on the source-code specific to this Docker image, refer to the source files, they have lots of comments. - -I have tried to embed some new features such as `isolated_peers` and interface startup on container-start (through `enable_wg0`). - -WG-Dashboard Logo - -## Getting the container running: - -To get the container running you either pull the image from the repository, at the moment: `repo.nerthus.nl/app/wireguard-dashboard:latest`.
-From there either use the environment variables describe below as parameters or use the Docker Compose file: `compose.yaml`. - -An example of a simple command to get the container running is show below:
- -```shell -docker run -d \ - --name wireguard-dashboard \ - --restart unless-stopped \ - -e enable_wg0=true \ - -e isolated_peers=true \ - -p 10086:10086/tcp \ - -p 51820:51820/udp \ - --cap-add NET_ADMIN \ - repo.nerthus.nl/app/wireguard-dashboard:latest -``` -
-If you want to use Compose instead of a raw Docker command, refer to the example in the `compose.yaml` or the one pasted below: -

- -```yaml -services: - wireguard-dashboard: - image: repo.nerthus.nl/app/wireguard-dashboard:latest - restart: unless-stopped - container_name: wire-dash - environment: - #- tz= - #- global_dns= - - enable_wg0=true - - isolated_peers=false - #- public_ip= - ports: - - 10086:10086/tcp - - 51820:51820/udp - volumes: - - conf:/etc/wireguard - - app:/opt/wireguarddashboard/app - cap_add: - - NET_ADMIN - -volumes: - conf: - app: - -``` - -If you want to customize the yaml, make sure the core stays the same, but for example volume PATHs can be freely changed.
-This setup is just generic and will use the Docker volumes. - -## Working with the container and environment variables: - -Once the container is running, the installation process is essentially the same as running it on bare-metal.
-So go to the assign TCP port in this case HTTP, like the default 10086 one in the example and log into the WEB-GUI.
- -| Environment variable | Accepted arguments | Default value | Verbose | -| -------------- | ------- | ------- | ------- | -| tz | Europe/Amsterdam or any confirming timezone notation. | Europe/Amsterdam | Sets the timezone of the Docker container. This is to timesync the container to any other processes which would need it. | -| global_dns | Any IPv4 address, such as my personal recommendation: 9.9.9.9 (QUAD9) | 1.1.1.1 | Set the default DNS given to clients once they connect to the WireGuard tunnel (VPN). -| enable_wg0 | `true` or `false` | `false` | Enables or disables the starting of the WireGuard interface on container 'boot-up'. -| isolated_peers | `true` or `false` | `true` | For security the default is true, and it disables peers to ping or reach eachother, the WireGuard interface IS able to reach the peers (Done through `iptables`). -| public_ip | Any IPv4 (public recommended) address, such as the one returned by default | Default uses the return of `curl ifconfig.me` | To reach your VPN from outside your own network, you need WG-Dashboard to know what your public IP-address is, otherwise it will generate faulty config files for clients. - -## Closing remarks: - -For feedback please submit an issue to the repository. Or message dselen@nerthus.nl. diff --git a/docker/README.md b/docker/README.md new file mode 100644 index 00000000..e31933ea --- /dev/null +++ b/docker/README.md @@ -0,0 +1,108 @@ +# WGDashboard Docker Explanation: + +Author: DaanSelen
+ +This document delves into how the WGDashboard Docker container has been built.
+Of course there are two stages, one before run-time and one at/after run-time.
+The `Dockerfile` describes how the container image is made, and the `entrypoint.sh` is executed after running the container.
+In this example, WireGuard is integrated into the container itself, so it should be a run-and-go/out-of-the-box.
+For more details on the source-code specific to this Docker image, refer to the source files, they have lots of comments. + +I have tried to embed some new features such as `isolate` and interface startup on container-start (through `enable`). I hope you enjoy! + +WG-Dashboard Logo + +## Getting the container running: + +To get the container running you either pull the image from the repository, `dselen/wgdashboard:latest`.
+From there either use the environment variables describe below as parameters or use the Docker Compose file: `compose.yaml`.
+Be careful, the default generated WireGuard configuration file uses port 51820/udp. So use this port if you want to use it out of the box.
+Otherwise edit the configuration file in `/etc/wireguard/wg0.conf`. + +An example of a simple command to get the container running is show below:
+ +```shell +docker run -d \ + --name wireguard-dashboard \ + --restart unless-stopped \ + -e enable=wg0 \ + -e isolate=wg0 \ + -p 10086:10086/tcp \ + -p 51820:51820/udp \ + --cap-add NET_ADMIN \ + dselen/wgdashboard:latest +``` +
+If you want to use Compose instead of a raw Docker command, refer to the example in the `compose.yaml` or the one pasted below: +

+ +```yaml +services: + wireguard-dashboard: + image: dselen/wgdashboard:latest + restart: unless-stopped + container_name: wire-dash + environment: + #- tz= + #- global_dns= + - enable=none + - isolate=wg0 + #- public_ip= + ports: + - 10086:10086/tcp + - 51820:51820/udp + volumes: + - conf:/etc/wireguard + - app:/opt/wireguarddashboard/app + cap_add: + - NET_ADMIN + +volumes: + conf: + app: + +``` + +If you want to customize the yaml, make sure the core stays the same, but for example volume PATHs can be freely changed.
+This setup is just generic and will use the Docker volumes. + +## Working with the container and environment variables: + +Once the container is running, the installation process is essentially the same as running it on bare-metal.
+So go to the assign TCP port in this case HTTP, like the default 10086 one in the example and log into the WEB-GUI.
+ +| Environment variable | Accepted arguments | Default value | Example value | Verbose | +| -------------- | ------- | ------- | ------- | ------- | +| tz | Europe/Amsterdam or any confirming timezone notation. | `Europe/Amsterdam` | `America/New_York` | Sets the timezone of the Docker container. This is to timesync the container to any other processes which would need it. | +| global_dns | Any IPv4 address, such as my personal recommendation: 9.9.9.9 (QUAD9). | `1.1.1.1` | `8.8.8.8` or any IP-Address that resolves DNS-names, and of course is reachable | Set the default DNS given to clients once they connect to the WireGuard tunnel, and for new peers, set to Cloudflare DNS for reliability. +| enable | Anything, preferably an existing WireGuard interface name. | `none` | `wg0,wg2,wg13` | Enables or disables the starting of the WireGuard interface on container 'boot-up'. +| isolate | Anything, preferably an existing WireGuard interface name. | `wg0` | `wg1,wg0` | For security premade `wg0` interface comes with this feature enabled by default. Declaring `isolate=none` in the Docker Compose file will remove this. The WireGuard interface itself IS able to reach the peers (Done through the `iptables` package). +| public_ip | Any IPv4 (public recommended) address, such as the one returned by default | Default uses the return of `curl ifconfig.me` | `23.50.131.156` | To reach your VPN from outside your own network, you need WG-Dashboard to know what your public IP-address is, otherwise it will generate faulty config files for clients. This happends because it is inside a Docker/Kubernetes container. In or outside of NAT is not relevant as long as the given IP-address is reachable from the internet or the target network. + +## Be careful with: + +When you are going to work with multiple WireGuard interfaces, you need to also open them up to the Docker host. This done by either adding the port mappings like: `51821:51821/udp` in the Docker Compose file, or to open a range like: `51820-51830:51820-51830/udp`
+The latter opens up UDP ports from 51820 to 51830, so all ports in between as well! Be careful, it is good security practise to open only needed ports! + +## Building the image yourself: + +To build the image yourself, you need to do a couple things:
+1. Clone the Github repository containing the source code of WGDashboard including the docker directory. For example do: `git clone https://github.com/donaldzou/WGDashboard.git` +1. Navigate into the docker directory. +1. (Make sure you have Docker correctly installed, if not: [Click here](https://docs.docker.com/engine/install/)) and run: `docker build . -t :` as an example: `docker build . -t dselen/wgdashboard:latest`.
This will make Docker compile the image from the resources in the directory you mention, in this case the current one. Let it compile, it takes about a minute or maximally two. +1. If all went well, see your image with `docker images`. Example below: + +```shell +dselen@dev-mach:~/development/WGDashboard/docker$ docker images +REPOSITORY TAG IMAGE ID CREATED SIZE +dselen/wgdashboard latest c96fd96ee3b3 42 minutes ago 314MB +``` + +## Closing remarks: + +Excuse the large image size, whoops! Debian's big... sometimes.
+For feedback please submit an issue to the repository. Or message dselen@nerthus.nl. + +## In Progress: + +Auto-Updating Capabilities, together with Donald I am working on it. \ No newline at end of file diff --git a/docker/alpine/Dockerfile b/docker/alpine/Dockerfile new file mode 100644 index 00000000..73c893ea --- /dev/null +++ b/docker/alpine/Dockerfile @@ -0,0 +1,61 @@ +# Pull from small Debian stable image. +FROM alpine:latest AS build +LABEL maintainer="dselen@nerthus.nl" + +# Declaring environment variables, change Peernet to an address you like, standard is a 24 bit subnet. +ARG Git_Url="https://github.com/DaanSelen/WGDashboard.git" +ARG wg_net="10.0.0.1" +ARG wg_port="51820" + +# Following ENV variables are changable on container runtime because /entrypoint.sh handles that. See compose.yaml for more info. +ENV TZ="Europe/Amsterdam" +ENV global_dns="1.1.1.1" +ENV enable="none" +ENV isolate="wg0" +ENV public_ip="0.0.0.0" + +# Doing package management operations, such as upgrading +RUN apk update \ + && apk add --no-cache bash git tzdata \ + iptables ip6tables curl openrc wireguard-tools \ + sudo py3-psutil py3-bcrypt + +# Using WGDASH -- like wg_net functionally as a ARG command. But it is needed in entrypoint.sh so it needs to be exported as environment variable. +ENV WGDASH=/opt/wireguarddashboard + +# Removing the Linux Image package to preserve space on the image, for this reason also deleting apt lists, to be able to install packages: run apt update. + +# Doing WireGuard Dashboard installation measures. Modify the git clone command to get the preferred version, with a specific branch for example. +RUN mkdir -p /setup/conf && mkdir /setup/app && mkdir ${WGDASH} \ + && git clone ${Git_Url} /setup/app +#COPY src /setup/app/src + +# Set the volume to be used for WireGuard configuration persistency. +VOLUME /etc/wireguard +VOLUME ${WGDASH} + +# Generate basic WireGuard interface. Echoing the WireGuard interface config for readability, adjust if you want it for efficiency. +# Also setting the pipefail option, verbose: https://github.com/hadolint/hadolint/wiki/DL4006. +SHELL ["/bin/bash", "-o", "pipefail", "-c"] +RUN echo "[Interface]" > /setup/conf/wg0.conf \ + && echo "Address = ${wg_net}/24" >> /setup/conf/wg0.conf \ + && echo "PrivateKey =" >> /setup/conf/wg0.conf \ + && echo "PostUp = iptables -t nat -I POSTROUTING 1 -s ${wg_net}/24 -o $(ip -o -4 route show to default | awk '{print $NF}') -j MASQUERADE" >> /setup/conf/wg0.conf \ + && echo "PostUp = iptables -I FORWARD -i wg0 -o wg0 -j DROP" >> /setup/conf/wg0.conf \ + && echo "PreDown = iptables -t nat -D POSTROUTING -s ${wg_net}/24 -o $(ip -o -4 route show to default | awk '{print $NF}') -j MASQUERADE" >> /setup/conf/wg0.conf \ + && echo "PreDown = iptables -D FORWARD -i wg0 -o wg0 -j DROP" >> /setup/conf/wg0.conf \ + && echo "ListenPort = ${wg_port}" >> /setup/conf/wg0.conf \ + && echo "SaveConfig = true" >> /setup/conf/wg0.conf \ + && echo "DNS = ${global_dns}" >> /setup/conf/wg0.conf + +# Defining a way for Docker to check the health of the container. In this case: checking the login URL. +HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \ + CMD sh -c 'pgrep gunicorn > /dev/null && pgrep tail > /dev/null' || exit 1 + + +# Copy the basic entrypoint.sh script. +COPY entrypoint.sh /entrypoint.sh + +# Exposing the default WireGuard Dashboard port for web access. +EXPOSE 10086 +ENTRYPOINT ["/bin/bash", "/entrypoint.sh"] diff --git a/docker/alpine/compose.yaml b/docker/alpine/compose.yaml new file mode 100644 index 00000000..341fa271 --- /dev/null +++ b/docker/alpine/compose.yaml @@ -0,0 +1,23 @@ +services: + wireguard-dashboard: + image: dselen:alpine + restart: unless-stopped + container_name: wgdashboard + environment: + #- tz= # <--- Set container timezone, default: Europe/Amsterdam. + - global_dns=9.9.9.9 # <--- Set global DNS address, default: 1.1.1.1. + - enable=wg0 # <--- Set the interfaces that will be enabled on startup, default: none. The option "off" is also allowed. + - isolate=wg0 # <--- When set to true, it disallows peers to talk to eachother, setting to false, allows it, default: true. + #- public_ip= # <--- Set public IP to ensure the correct one is chosen, defaulting to the IP give by ifconfig.me. + ports: + - 10086:10086/tcp + - 51820:51820/udp + volumes: + - ./app:/opt/wireguarddashboard + - ./conf:/etc/wireguard + cap_add: + - NET_ADMIN + +volumes: + app: + conf: diff --git a/docker/alpine/entrypoint.sh b/docker/alpine/entrypoint.sh new file mode 100644 index 00000000..20c27114 --- /dev/null +++ b/docker/alpine/entrypoint.sh @@ -0,0 +1,225 @@ +#!/bin/bash + +echo "------------------------- START ----------------------------" +echo "Starting the WireGuard Dashboard Docker container." + +ensure_installation() { + # When using a custom directory to store the files, this part moves over and makes sure the installation continues. + echo "Checking if everything is present." + + if [ -z "$(ls -A "${WGDASH}")" ]; then + echo "Detected empty directory, moving over..." + + mv /setup/app/* "${WGDASH}" + mv /setup/app/.* "${WGDASH}" + python3 -m venv "${WGDASH}"/src/venv + . "${WGDASH}/src/venv/bin/activate" + + # Extra step for Alpine + mv /usr/lib/python3.12/site-packages/psutil* "${WGDASH}"/src/venv/lib/python3.12/site-packages + mv /usr/lib/python3.12/site-packages/bcrypt* "${WGDASH}"/src/venv/lib/python3.12/site-packages + + chmod +x "${WGDASH}"/src/wgd.sh + cd "${WGDASH}"/src || exit + ./wgd.sh install + + echo "Looks like the installation succesfully moved over." + else + echo "Looks like everything is present. Or the directory is not empty." + fi + + # This first step is to ensure the wg0.conf file exists, and if not, then its copied over from the ephemeral container storage. + if [ ! -f "/etc/wireguard/wg0.conf" ]; then + echo "Standard wg0 Configuration file not found, grabbing template." + cp "/setup/conf/wg0.conf" "/etc/wireguard/wg0.conf" + + echo "Setting a secure private key." + + local privateKey + privateKey=$(wg genkey) + + sed -i "s|^PrivateKey =$|PrivateKey = ${privateKey}|g" /etc/wireguard/wg0.conf + sed -i "s|^PrivateKey *=.*$|PrivateKey = ${privateKey}|g" /etc/wireguard/wg0.conf + echo "Done setting template." + else + echo "Existing wg0 configuration file found, using that." + fi +} + +# === CLEAN UP === +clean_up() { + printf "\n------------------------ CLEAN UP --------------------------\n" + + # Cleaning out previous data such as the .pid file and starting the WireGuard Dashboard. Making sure to use the python venv. + echo "Looking for remains of previous instances..." + local pid_file="${WGDASH}/src/gunicorn.pid" + if [ -f "$pid_file" ]; then + echo "Found old pid file, removing." + rm $pid_file + else + echo "No pid remains found, continuing." + fi + + # Also check for Python caches (pycache) inspired by https://github.com/shuricksumy + local pycache="${WGDASH}/src/__pycache__" + if [ -d "$pycache" ]; then + local pycache_filecount=$(find "$pycache" -maxdepth 1 -type f | wc -l) + if [ "$pycache_filecount" -gt 0 ]; then + echo "Found old pycaches, removing." + rm -rf "$pycache"/* + else + echo "No pycaches found, continuing." + fi + else + echo "No pycaches found, continuing." + fi + + local logdir="${WGDASH}/src/log" + echo "Cleaning log directory." + find /opt/wireguarddashboard/src/log -name 'access_*.log' -exec rm {} + + find /opt/wireguarddashboard/src/log -name 'error_*.log' -exec rm {} + + echo "Removed unneeded logs!" +} + +# === SET ENV VARS === +set_envvars() { + #printf "\n------------- SETTING ENVIRONMENT VARIABLES ----------------\n" + + # Changing the DNS used for clients and the dashboard itself. + if [ "${global_dns}" != "$(grep "peer_global_dns = " /opt/wireguarddashboard/src/wg-dashboard.ini | awk '{print $NF}')" ]; then + echo "Changing default dns." + + #sed -i "s/^DNS = .*/DNS = ${global_dns}/" /etc/wireguard/wg0.conf # Uncomment if you want to have DNS on server-level. + sed -i "s/^peer_global_dns = .*/peer_global_dns = ${global_dns}/" /opt/wireguarddashboard/src/wg-dashboard.ini + else + echo "DNS is set correctly." + fi + + # Setting the public IP of the WireGuard Dashboard container host. If not defined, it will trying fetching it using a curl to ifconfig.me. + if [ "${public_ip}" = "0.0.0.0" ]; then + default_ip=$(curl -s ifconfig.me) + echo "Trying to fetch the Public-IP using ifconfig.me: ${default_ip}" + + sed -i "s/^remote_endpoint = .*/remote_endpoint = ${default_ip}/" /opt/wireguarddashboard/src/wg-dashboard.ini + elif [ "${public_ip}" != "$(grep "remote_endpoint = " /opt/wireguarddashboard/src/wg-dashboard.ini | awk '{print $NF}')" ]; then + echo "Setting the Public-IP using given variable: ${public_ip}" + + sed -i "s/^remote_endpoint = .*/remote_endpoint = ${public_ip}/" /opt/wireguarddashboard/src/wg-dashboard.ini + fi +} + +# === CORE SERVICES === +start_core() { + printf "\n---------------------- STARTING CORE -----------------------\n" + + echo "Activating Python venv and executing the WireGuard Dashboard service." + . "${WGDASH}"/src/venv/bin/activate + cd "${WGDASH}"/src || return + bash wgd.sh start &>> /dev/null + + # Isolated peers feature, first converting the existing configuration files and the given names to arrays. + local configurations=(/etc/wireguard/*) + IFS=',' read -r -a do_isolate <<< "${isolate}" + non_isolate=() + + # Checking if there are matches between the two arrays. + for config in "${configurations[@]}"; do + local config=$(echo "$config" | sed -e 's|.*/etc/wireguard/||' -e 's|\.conf$||') + found=false + for interface in "${do_isolate[@]}"; do + if [[ "$config" == "$interface" ]]; then + found=true + break + fi + done + if [ "$found" = false ]; then + non_isolate+=("$config") + fi + done + + # Isolating the matches. + for interface in "${do_isolate[@]}"; do + if [ "$interface" = "none" ]; then + echo "Found: $interface, stopping isolation checking." + break + else + if [ -f "/etc/wireguard/${interface}.conf" ]; then + echo "Isolating interface:" $interface + upblocking=$(grep -c "PostUp = iptables -I FORWARD -i ${interface} -o ${interface} -j DROP" /etc/wireguard/${interface}.conf) + downblocking=$(grep -c "PreDown = iptables -D FORWARD -i ${interface} -o ${interface} -j DROP" /etc/wireguard/${interface}.conf) + + if [ "$upblocking" -lt 1 ] && [ "$downblocking" -lt 1 ]; then + sed -i "/PostUp =/a PostUp = iptables -I FORWARD -i ${interface} -o ${interface} -j DROP" /etc/wireguard/${interface}.conf + sed -i "/PreDown =/a PreDown = iptables -D FORWARD -i ${interface} -o ${interface} -j DROP" /etc/wireguard/${interface}.conf + fi + else + echo "Configuration for $interface does not seem to exist, continuing." + fi + fi + done + + # Removing isolation for the configurations that did not match. + for interface in "${non_isolate[@]}"; do + if [ -f "/etc/wireguard/${interface}.conf" ]; then + echo "Removing Isolation if present for:" $interface + sed -i "/PostUp = iptables -I FORWARD -i ${interface} -o ${interface} -j DROP/d" /etc/wireguard/${interface}.conf + sed -i "/PreDown = iptables -D FORWARD -i ${interface} -o ${interface} -j DROP/d" /etc/wireguard/${interface}.conf + else + echo "Configuration for $interface does not seem to exist, continuing." + fi + done + + # The following section takes care of enabling wireguard interfaces on startup. Using arrays and given arguments. + IFS=',' read -r -a enable_array <<< "${enable}" + + for interface in "${enable_array[@]}"; do + if [ "$interface" = "none" ]; then + echo "Found: $interface, stopping enabling checking." + break + else + echo "Enabling interface:" $interface + + local fileperms=$(stat -c "%a" /etc/wireguard/${interface}.conf) + if [ $fileperms -eq 644 ]; then + echo "Configuration is world accessible, adjusting." + chmod 600 "/etc/wireguard/${interface}.conf" + fi + + if [ -f "/etc/wireguard/${interface}.conf" ]; then + wg-quick up $interface + else + echo "No corresponding configuration file found for $interface doing nothing." + fi + fi + done +} + +# === CLEAN UP === +ensure_blocking() { + #printf "\n-------------- ENSURING CONTAINER CONTINUATION -------------\n" + + . "${WGDASH}"/src/venv/bin/activate + cd "${WGDASH}"/src || return + bash wgd.sh restart + + sleep 1s + echo "Ensuring container continuation." + + # This function checks if the latest error log is created and tails it for docker logs uses. + if find "/opt/wireguarddashboard/src/log" -mindepth 1 -maxdepth 1 -type f | read -r; then + latestErrLog=$(find /opt/wireguarddashboard/src/log -name "error_*.log" | head -n 1) + latestAccLog=$(find /opt/wireguarddashboard/src/log -name "access_*.log" | head -n 1) + + tail -f "${latestErrLog}" "${latestAccLog}" + fi + + # Blocking command in case of erroring. So the container does not quit. + sleep infinity +} + +# Execute functions for the WireGuard Dashboard services, then set the environment variables +ensure_installation +clean_up +start_core +set_envvars +ensure_blocking \ No newline at end of file diff --git a/docker/debian/Dockerfile b/docker/debian/Dockerfile new file mode 100644 index 00000000..7e4e46a0 --- /dev/null +++ b/docker/debian/Dockerfile @@ -0,0 +1,69 @@ +# Pull from small Debian stable image. +FROM debian:stable-slim AS build +LABEL maintainer="dselen@nerthus.nl" + +# Declaring environment variables, change Peernet to an address you like, standard is a 24 bit subnet. +ARG Git_Url="https://github.com/donaldzou/WGDashboard.git" +ARG wg_net="10.0.0.1" +ARG wg_port="51820" + +# Following ENV variables are changable on container runtime because /entrypoint.sh handles that. See compose.yaml for more info. +ENV TZ="Europe/Amsterdam" +ENV global_dns="1.1.1.1" +ENV enable="none" +ENV isolate="wg0" +ENV public_ip="0.0.0.0" +ENV update="yes" + +# Doing basic system maintenance. Change the timezone to the desired timezone. +RUN ln -sf /usr/share/zoneinfo/${TZ} /etc/localtime + +# Using WGDASH -- like wg_net functionally as a ARG command. But it is needed in entrypoint.sh so it needs to be exported as environment variable. +ENV WGDASH=/opt/wireguarddashboard + +# Doing package management operations, such as upgrading +RUN apt-get update && apt-get install -y --no-install-recommends \ + curl git \ + iptables python3 \ + wireguard wireguard-tools \ + sudo && \ + apt-get remove -y linux-image-* && \ + apt-get autoremove -y && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* + +# Removing the Linux Image package to preserve space on the image, for this reason also deleting apt lists, to be able to install packages: run apt update. + +# Doing WireGuard Dashboard installation measures. Modify the git clone command to get the preferred version, with a specific branch for example. +RUN mkdir -p /setup/conf && mkdir /setup/app && mkdir ${WGDASH} \ + && git clone ${Git_Url} /setup/app + +# Set the volume to be used for WireGuard configuration persistency. +VOLUME /etc/wireguard +VOLUME ${WGDASH} + +# Generate basic WireGuard interface. Echoing the WireGuard interface config for readability, adjust if you want it for efficiency. +# Also setting the pipefail option, verbose: https://github.com/hadolint/hadolint/wiki/DL4006. +SHELL ["/bin/bash", "-o", "pipefail", "-c"] +RUN echo "[Interface]" > /setup/conf/wg0.conf \ + && echo "Address = ${wg_net}/24" >> /setup/conf/wg0.conf \ + && echo "PrivateKey =" >> /setup/conf/wg0.conf \ + && echo "PostUp = iptables -t nat -I POSTROUTING 1 -s ${wg_net}/24 -o $(ip -o -4 route show to default | awk '{print $NF}') -j MASQUERADE" >> /setup/conf/wg0.conf \ + && echo "PostUp = iptables -I FORWARD -i wg0 -o wg0 -j DROP" >> /setup/conf/wg0.conf \ + && echo "PreDown = iptables -t nat -D POSTROUTING -s ${wg_net}/24 -o $(ip -o -4 route show to default | awk '{print $NF}') -j MASQUERADE" >> /setup/conf/wg0.conf \ + && echo "PreDown = iptables -D FORWARD -i wg0 -o wg0 -j DROP" >> /setup/conf/wg0.conf \ + && echo "ListenPort = ${wg_port}" >> /setup/conf/wg0.conf \ + && echo "SaveConfig = true" >> /setup/conf/wg0.conf \ + && echo "DNS = ${global_dns}" >> /setup/conf/wg0.conf + +# Defining a way for Docker to check the health of the container. In this case: checking the login URL. +HEALTHCHECK --interval=2m --timeout=1m --start-period=20s --retries=3 \ + CMD [ "$(curl -s -o /dev/null -w '%{http_code}' http://localhost:10086/)" -eq "200" ] || exit 1 + + +# Copy the basic entrypoint.sh script. +COPY entrypoint.sh /entrypoint.sh + +# Exposing the default WireGuard Dashboard port for web access. +EXPOSE 10086 +ENTRYPOINT ["/bin/bash", "/entrypoint.sh"] \ No newline at end of file diff --git a/docker/debian/compose.yaml b/docker/debian/compose.yaml new file mode 100644 index 00000000..a921a233 --- /dev/null +++ b/docker/debian/compose.yaml @@ -0,0 +1,23 @@ +services: + wireguard-dashboard: + image: dselen/wgdashboard:dev + restart: unless-stopped + container_name: wgdashboard + environment: + #- tz= # <--- Set container timezone, default: Europe/Amsterdam. + #- global_dns= # <--- Set global DNS address, default: 1.1.1.1. + - enable=wg0 # <--- Set the interfaces that will be enabled on startup, default: none. The option "off" is also allowed. + - isolate=wg0 # <--- When set to true, it disallows peers to talk to eachother, setting to false, allows it, default: true. + #- public_ip= # <--- Set public IP to ensure the correct one is chosen, defaulting to the IP give by ifconfig.me. + ports: + - 10086:10086/tcp + - 51820:51820/udp + volumes: + - app:/opt/wireguarddashboard + - conf:/etc/wireguard + cap_add: + - NET_ADMIN + +volumes: + app: + conf: diff --git a/docker/debian/entrypoint.sh b/docker/debian/entrypoint.sh new file mode 100644 index 00000000..1ece541b --- /dev/null +++ b/docker/debian/entrypoint.sh @@ -0,0 +1,227 @@ +#!/bin/bash + +echo "------------------------- START ----------------------------" +echo "Starting the WireGuard Dashboard Docker container." + +ensure_installation() { + # When using a custom directory to store the files, this part moves over and makes sure the installation continues. + echo "Checking if everything is present." + + if [ -z "$(ls -A "${WGDASH}")" ]; then + echo "Detected empty directory, moving over..." + + mv /setup/app/{.[!.],}* "${WGDASH}" + python3 -m venv "${WGDASH}"/src/venv + . "${WGDASH}/src/venv/bin/activate" + chmod +x "${WGDASH}"/src/wgd.sh + cd "${WGDASH}"/src || exit + ./wgd.sh install + + echo "Looks like the installation succesfully moved over." + else + echo "Looks like everything is present." + fi + + # This first step is to ensure the wg0.conf file exists, and if not, then its copied over from the ephemeral container storage. + if [ ! -f "/etc/wireguard/wg0.conf" ]; then + echo "Standard wg0 Configuration file not found, grabbing template." + cp "/setup/conf/wg0.conf" "/etc/wireguard/wg0.conf" + + echo "Setting a secure private key." + + local privateKey + privateKey=$(wg genkey) + + sed -i "s|^PrivateKey =$|PrivateKey = ${privateKey}|g" /etc/wireguard/wg0.conf + sed -i "s|^PrivateKey *=.*$|PrivateKey = ${privateKey}|g" /etc/wireguard/wg0.conf + echo "Done setting template." + else + echo "Existing wg0 configuration file found, using that." + fi +} + +# === CLEAN UP === +clean_up() { + printf "\n------------------------ CLEAN UP --------------------------\n" + + # Cleaning out previous data such as the .pid file and starting the WireGuard Dashboard. Making sure to use the python venv. + echo "Looking for remains of previous instances..." + local pid_file="${WGDASH}/src/gunicorn.pid" + if [ -f "$pid_file" ]; then + echo "Found old pid file, removing." + rm $pid_file + else + echo "No pid remains found, continuing." + fi + + # Also check for Python caches (pycache) inspired by https://github.com/shuricksumy + local pycache="${WGDASH}/src/__pycache__" + if [ -d "$pycache" ]; then + local pycache_filecount=$(find "$pycache" -maxdepth 1 -type f | wc -l) + if [ "$pycache_filecount" -gt 0 ]; then + echo "Found old pycaches, removing." + rm -rf "$pycache"/* + else + echo "No pycaches found, continuing." + fi + else + echo "No pycaches found, continuing." + fi + + local logdir="${WGDASH}/src/log" + echo "Cleaning log directory." + find /opt/wireguarddashboard/src/log -name 'access_*.log' -exec rm {} + + find /opt/wireguarddashboard/src/log -name 'error_*.log' -exec rm {} + + echo "Removed unneeded logs!" +} + +#update_checker() { + #if [ "$update" = "yes" ]; then + # echo "Activating Python venv and executing the WireGuard Dashboard service." + # . "${WGDASH}/src/venv/bin/activate" + # cd "${WGDASH}"/src || exit + # bash wgd.sh update + #else + # echo "Auto Updater disabled" + #fi +#} + +# === SET ENV VARS === +set_envvars() { + printf "\n------------- SETTING ENVIRONMENT VARIABLES ----------------\n" + + # Changing the DNS used for clients and the dashboard itself. + if [ "${global_dns}" != "$(grep "peer_global_dns = " /opt/wireguarddashboard/src/wg-dashboard.ini | awk '{print $NF}')" ]; then + echo "Changing default dns." + + #sed -i "s/^DNS = .*/DNS = ${global_dns}/" /etc/wireguard/wg0.conf # Uncomment if you want to have DNS on server-level. + sed -i "s/^peer_global_dns = .*/peer_global_dns = ${global_dns}/" /opt/wireguarddashboard/src/wg-dashboard.ini + else + echo "DNS is set correctly." + fi + + # Setting the public IP of the WireGuard Dashboard container host. If not defined, it will trying fetching it using a curl to ifconfig.me. + if [ "${public_ip}" = "0.0.0.0" ]; then + default_ip=$(curl -s ifconfig.me) + echo "Trying to fetch the Public-IP using ifconfig.me: ${default_ip}" + + sed -i "s/^remote_endpoint = .*/remote_endpoint = ${default_ip}/" /opt/wireguarddashboard/src/wg-dashboard.ini + elif [ "${public_ip}" != "$(grep "remote_endpoint = " /opt/wireguarddashboard/src/wg-dashboard.ini | awk '{print $NF}')" ]; then + echo "Setting the Public-IP using given variable: ${public_ip}" + + sed -i "s/^remote_endpoint = .*/remote_endpoint = ${public_ip}/" /opt/wireguarddashboard/src/wg-dashboard.ini + fi +} + +# === CORE SERVICES === +start_core() { + printf "\n---------------------- STARTING CORE -----------------------\n" + + echo "Activating Python venv and executing the WireGuard Dashboard service." + . "${WGDASH}"/src/venv/bin/activate + cd "${WGDASH}"/src || return # If changing the directory fails (permission or presence error), then bash will exist this function, causing the WireGuard Dashboard to not be succesfully launched. + bash wgd.sh start + + # Isolated peers feature, first converting the existing configuration files and the given names to arrays. + local configurations=(/etc/wireguard/*) + IFS=',' read -r -a do_isolate <<< "${isolate}" + non_isolate=() + + # Checking if there are matches between the two arrays. + for config in "${configurations[@]}"; do + local config=$(echo "$config" | sed -e 's|.*/etc/wireguard/||' -e 's|\.conf$||') + found=false + for interface in "${do_isolate[@]}"; do + if [[ "$config" == "$interface" ]]; then + found=true + break + fi + done + if [ "$found" = false ]; then + non_isolate+=("$config") + fi + done + + # Isolating the matches. + for interface in "${do_isolate[@]}"; do + if [ "$interface" = "none" ]; then + echo "Found: $interface, stopping isolation checking." + break + else + if [ -f "/etc/wireguard/${interface}.conf" ]; then + echo "Isolating interface:" $interface + upblocking=$(grep -c "PostUp = iptables -I FORWARD -i ${interface} -o ${interface} -j DROP" /etc/wireguard/${interface}.conf) + downblocking=$(grep -c "PreDown = iptables -D FORWARD -i ${interface} -o ${interface} -j DROP" /etc/wireguard/${interface}.conf) + + if [ "$upblocking" -lt 1 ] && [ "$downblocking" -lt 1 ]; then + sed -i "/PostUp =/a PostUp = iptables -I FORWARD -i ${interface} -o ${interface} -j DROP" /etc/wireguard/${interface}.conf + sed -i "/PreDown =/a PreDown = iptables -D FORWARD -i ${interface} -o ${interface} -j DROP" /etc/wireguard/${interface}.conf + fi + else + echo "Configuration for $interface does not seem to exist, continuing." + fi + fi + done + + # Removing isolation for the configurations that did not match. + for interface in "${non_isolate[@]}"; do + if [ -f "/etc/wireguard/${interface}.conf" ]; then + echo "Removing Isolation if present for:" $interface + sed -i "/PostUp = iptables -I FORWARD -i ${interface} -o ${interface} -j DROP/d" /etc/wireguard/${interface}.conf + sed -i "/PreDown = iptables -D FORWARD -i ${interface} -o ${interface} -j DROP/d" /etc/wireguard/${interface}.conf + else + echo "Configuration for $interface does not seem to exist, continuing." + fi + done + + # The following section takes care of enabling wireguard interfaces on startup. Using arrays and given arguments. + IFS=',' read -r -a enable_array <<< "${enable}" + + for interface in "${enable_array[@]}"; do + if [ "$interface" = "none" ]; then + echo "Found: $interface, stopping enabling checking." + break + else + echo "Enabling interface:" $interface + + local fileperms=$(stat -c "%a" /etc/wireguard/${interface}.conf) + if [ $fileperms -eq 644 ]; then + echo "Configuration is world accessible, adjusting." + chmod 600 "/etc/wireguard/${interface}.conf" + fi + + if [ -f "/etc/wireguard/${interface}.conf" ]; then + wg-quick up $interface + else + echo "No corresponding configuration file found for $interface doing nothing." + fi + fi + done +} + +# === CLEAN UP === +ensure_blocking() { + printf "\n-------------- ENSURING CONTAINER CONTINUATION -------------\n" + + sleep 1s + echo "Ensuring container continuation." + + # This function checks if the latest error log is created and tails it for docker logs uses. + if find "/opt/wireguarddashboard/src/log" -mindepth 1 -maxdepth 1 -type f | read -r; then + latestErrLog=$(find /opt/wireguarddashboard/src/log -name "error_*.log" | head -n 1) + latestAccLog=$(find /opt/wireguarddashboard/src/log -name "access_*.log" | head -n 1) + + tail -f "${latestErrLog}" "${latestAccLog}" + fi + + # Blocking command in case of erroring. So the container does not quit. + sleep infinity +} + +# Execute functions for the WireGuard Dashboard services, then set the environment variables +ensure_installation +clean_up +#update_checker +start_core +set_envvars +ensure_blocking \ No newline at end of file diff --git a/src/wgd.sh b/src/wgd.sh index 6f277a2b..d080e700 100755 --- a/src/wgd.sh +++ b/src/wgd.sh @@ -66,7 +66,7 @@ _determineOS(){ elif [ -f /etc/redhat-release ]; then OS="redhat" else - printf "[WGDashboard] %s Sorry, your OS is not supported. Currently the install script only support Debian-based, Red Hat-based OS." "$heavy_crossmark" + printf "[WGDashboard] %s Sorry, your OS is not supported. Currently the install script only support Debian-based, Red Hat-based OS. With experimental support for Alpine Linux.\n" "$heavy_crossmark" printf "%s\n" "$helpMsg" kill $TOP_PID fi @@ -86,8 +86,8 @@ _installPython(){ fi ;; alpine) - { apk update; apk add python3 net-tools; printf "\n\n"; } &>> ./log/install.txt - ;; + { sudo apk update; sudo apk add python3 net-tools --no-cache; printf "\n\n"; } >> ./log/install.txt + ;; esac if ! python3 --version > /dev/null 2>&1 @@ -114,10 +114,10 @@ _installPythonVenv(){ fi ;; alpine) - { apk add python3 py3-virtualenv; printf "\n\n"; } &>> ./log/install.txt + { sudo apk update; sudo apk add py3-virtualenv ; printf "\n\n"; } >> ./log/install.txt ;; *) - printf "[WGDashboard] %s Sorry, your OS is not supported. Currently the install script only support Debian-based, Red Hat-based OS.\n" "$heavy_crossmark" + printf "[WGDashboard] %s Sorry, your OS is not supported. Currently the install script only support Debian-based, Red Hat-based OS. With experimental support for Alpine Linux.\n" "$heavy_crossmark" printf "%s\n" "$helpMsg" kill $TOP_PID ;; @@ -140,7 +140,6 @@ _installPythonVenv(){ } _installPythonPip(){ - if ! $pythonExecutable -m pip -h > /dev/null 2>&1 then case "$OS" in @@ -159,10 +158,10 @@ _installPythonPip(){ fi ;; alpine) - { apk add py3-pip; printf "\n\n"; } &>> ./log/install.txt + { sudo apk update; sudo apk add py3-pip --no-cache; printf "\n\n"; } >> ./log/install.txt ;; *) - printf "[WGDashboard] %s Sorry, your OS is not supported. Currently the install script only support Debian-based, Red Hat-based OS.\n" "$heavy_crossmark" + printf "[WGDashboard] %s Sorry, your OS is not supported. Currently the install script only support Debian-based, Red Hat-based OS. With experimental support for Alpine Linux.\n" "$heavy_crossmark" printf "%s\n" "$helpMsg" kill $TOP_PID ;; @@ -180,20 +179,41 @@ _installPythonPip(){ } _checkWireguard(){ - if ! wg -h > /dev/null 2>&1 - then - printf "[WGDashboard] %s WireGuard is not installed. Please follow instruction on https://www.wireguard.com/install/ to install. \n" "$heavy_crossmark" - kill $TOP_PID - fi - if ! wg-quick -h > /dev/null 2>&1 - then - printf "[WGDashboard] %s WireGuard is not installed. Please follow instruction on https://www.wireguard.com/install/ to install. \n" "$heavy_crossmark" - kill $TOP_PID - fi + if ! command -v wg > /dev/null 2>&1 || ! command -v wg-quick > /dev/null 2>&1 + then + case "$OS" in + ubuntu|debian) + { + sudo apt update && sudo apt-get install -y wireguard; + printf "\n[WGDashboard] WireGuard installed on %s.\n\n" "$OS"; + } &>> ./log/install.txt + ;; + centos|fedora|redhat|rhel) + { + sudo dnf install -y wireguard-tools; + printf "\n[WGDashboard] WireGuard installed on %s.\n\n" "$OS"; + } &>> ./log/install.txt + ;; + alpine) + { + sudo apk update && sudo apk add wireguard-tools --no-cache; + printf "\n[WGDashboard] WireGuard installed on %s.\n\n" "$OS"; + } &>> ./log/install.txt + ;; + *) + printf "[WGDashboard] %s Sorry, your OS is not supported. Currently, the install script only supports Debian-based, Red Hat-based, and Alpine Linux.\n" "$heavy_crossmark" + printf "%s\n" "$helpMsg" + kill $TOP_PID + ;; + esac + else + printf "[WGDashboard] WireGuard is already installed.\n" + fi } + _checkPythonVersion(){ version_pass=$($pythonExecutable -c 'import sys; print("1") if (sys.version_info.major == 3 and sys.version_info.minor >= 10) else print("0");') version=$($pythonExecutable --version) @@ -221,8 +241,6 @@ _checkPythonVersion(){ install_wgd(){ printf "[WGDashboard] Starting to install WGDashboard\n" - _checkWireguard - sudo chmod -R 755 /etc/wireguard/ if [ ! -d "log" ] then @@ -241,6 +259,8 @@ install_wgd(){ _checkPythonVersion _installPythonVenv _installPythonPip + _checkWireguard + sudo chmod -R 755 /etc/wireguard/ if [ ! -d "db" ] then @@ -252,7 +272,7 @@ install_wgd(){ { date; python3 -m ensurepip --upgrade; printf "\n\n"; } >> ./log/install.txt { date; python3 -m pip install --upgrade pip; printf "\n\n"; } >> ./log/install.txt printf "[WGDashboard] Installing latest Python dependencies\n" - { date; python3 -m pip install -r requirements.txt ; printf "\n\n"; } >> ./log/install.txt + { date; python3 -m pip install -r requirements.txt ; printf "\n\n"; } >> ./log/install.txt #This all works on the default installation. printf "[WGDashboard] WGDashboard installed successfully!\n" printf "[WGDashboard] Enter ./wgd.sh start to start the dashboard\n" } @@ -416,55 +436,51 @@ update_wgd() { } if [ "$#" != 1 ]; - then - help - else - if [ "$1" = "start" ]; then - if check_wgd_status; then - printf "%s\n" "$dashes" - printf "[WGDashboard] WGDashboard is already running.\n" - printf "%s\n" "$dashes" - else - start_wgd - fi - elif [ "$1" = "docker_start" ]; then - printf "%s\n" "$dashes" - startwgd_docker - printf "%s\n" "$dashes" - elif [ "$1" = "stop" ]; then - if check_wgd_status; then - printf "%s\n" "$dashes" - stop_wgd - printf "[WGDashboard] WGDashboard is stopped.\n" - printf "%s\n" "$dashes" - else - printf "%s\n" "$dashes" - printf "[WGDashboard] WGDashboard is not running.\n" - printf "%s\n" "$dashes" - fi - elif [ "$1" = "update" ]; then - update_wgd - elif [ "$1" = "install" ]; then - printf "%s\n" "$dashes" - install_wgd - printf "%s\n" "$dashes" - elif [ "$1" = "restart" ]; then - if check_wgd_status; then - printf "%s\n" "$dashes" - stop_wgd - printf "[WGDashboard] WGDashboard is stopped.\n" - sleep 4 - start_wgd - else - start_wgd - fi - elif [ "$1" = "debug" ]; then - if check_wgd_status; then - printf "[WGDashboard] WGDashboard is already running.\n" - else - start_wgd_debug - fi - else - help - fi -fi \ No newline at end of file + then + help + else + if [ "$1" = "start" ]; then + if check_wgd_status; then + printf "%s\n" "$dashes" + printf "[WGDashboard] WGDashboard is already running.\n" + printf "%s\n" "$dashes" + else + start_wgd + fi + elif [ "$1" = "stop" ]; then + if check_wgd_status; then + printf "%s\n" "$dashes" + stop_wgd + printf "[WGDashboard] WGDashboard is stopped.\n" + printf "%s\n" "$dashes" + else + printf "%s\n" "$dashes" + printf "[WGDashboard] WGDashboard is not running.\n" + printf "%s\n" "$dashes" + fi + elif [ "$1" = "update" ]; then + update_wgd + elif [ "$1" = "install" ]; then + printf "%s\n" "$dashes" + install_wgd + printf "%s\n" "$dashes" + elif [ "$1" = "restart" ]; then + if check_wgd_status; then + printf "%s\n" "$dashes" + stop_wgd + printf "| WGDashboard is stopped. |\n" + sleep 4 + start_wgd + else + start_wgd + fi + elif [ "$1" = "debug" ]; then + if check_wgd_status; then + printf "| WGDashboard is already running. |\n" + else + start_wgd_debug + fi + else + help + fi +fi