1
0
mirror of https://github.com/donaldzou/WGDashboard.git synced 2024-11-22 07:10:09 +01:00

Merge pull request #447 from DaanSelen/workflow-change

This commit is contained in:
Donald Zou 2024-11-01 16:11:26 +08:00 committed by GitHub
commit efb9a34cb8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 231 additions and 208 deletions

View File

@ -1,8 +1,10 @@
name: Docker-Analyze name: Docker-Analyze
on: on:
schedule: #schedule:
- cron: "0 0 * * *" # Daily at midnight UTC # - cron: "0 0 * * *" # Daily at midnight UTC
push:
branches: [ main ]
workflow_dispatch: workflow_dispatch:
inputs: inputs:
trigger-build: trigger-build:
@ -10,7 +12,7 @@ on:
default: 'true' default: 'true'
env: env:
DOCKER_IMAGE: dselen/wgdashboard DOCKER_IMAGE: donaldzou/wgdashboard
jobs: jobs:
docker_analyze: docker_analyze:

View File

@ -1,4 +1,4 @@
FROM alpine:latest AS build FROM alpine:latest
LABEL maintainer="dselen@nerthus.nl" LABEL maintainer="dselen@nerthus.nl"
# Declaring environment variables, change Peernet to an address you like, standard is a 24 bit subnet. # Declaring environment variables, change Peernet to an address you like, standard is a 24 bit subnet.
@ -9,14 +9,15 @@ ARG wg_port="51820"
ENV TZ="Europe/Amsterdam" ENV TZ="Europe/Amsterdam"
ENV global_dns="1.1.1.1" ENV global_dns="1.1.1.1"
ENV enable="none" ENV enable="none"
ENV isolate="wg0" ENV isolate="none"
ENV public_ip="0.0.0.0" ENV public_ip="0.0.0.0"
# Doing package management operations, such as upgrading # Doing package management operations, such as upgrading
RUN apk update \ RUN apk update \
&& apk add --no-cache bash git tzdata \ && apk add --no-cache bash git tzdata \
iptables ip6tables openrc curl wireguard-tools \ iptables ip6tables openrc curl wireguard-tools \
sudo py3-psutil py3-bcrypt sudo py3-psutil py3-bcrypt \
&& apk upgrade
# 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. # 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 ENV WGDASH=/opt/wireguarddashboard
@ -24,12 +25,10 @@ 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. # 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. # 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} RUN mkdir /data \
COPY ./src /setup/app/src && mkdir /configs \
&& mkdir -p ${WGDASH}/src
# Set the volume to be used for WireGuard configuration persistency. COPY ./src ${WGDASH}/src
VOLUME /etc/wireguard
VOLUME ${WGDASH}
# Generate basic WireGuard interface. Echoing the WireGuard interface config for readability, adjust if you want it for efficiency. # 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. # Also setting the pipefail option, verbose: https://github.com/hadolint/hadolint/wiki/DL4006.
@ -44,10 +43,10 @@ PreDown = iptables -t nat -D POSTROUTING -s ${wg_net}/24 -o ${out_adapt} -j MASQ
PreDown = iptables -D FORWARD -i wg0 -o wg0 -j DROP\n\ PreDown = iptables -D FORWARD -i wg0 -o wg0 -j DROP\n\
ListenPort = ${wg_port}\n\ ListenPort = ${wg_port}\n\
SaveConfig = true\n\ SaveConfig = true\n\
DNS = ${global_dns}" > /setup/conf/wg0.conf \ DNS = ${global_dns}" > /configs/wg0.conf.template \
&& chmod 600 /setup/conf/wg0.conf && chmod 600 /configs/wg0.conf.template
# Defining a way for Docker to check the health of the container. In this case: checking the login URL. # Defining a way for Docker to check the health of the container. In this case: checking the gunicorn process.
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \ HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD sh -c 'pgrep gunicorn > /dev/null && pgrep tail > /dev/null' || exit 1 CMD sh -c 'pgrep gunicorn > /dev/null && pgrep tail > /dev/null' || exit 1
@ -56,4 +55,4 @@ COPY entrypoint.sh /entrypoint.sh
# Exposing the default WireGuard Dashboard port for web access. # Exposing the default WireGuard Dashboard port for web access.
EXPOSE 10086 EXPOSE 10086
ENTRYPOINT ["/bin/bash", "/entrypoint.sh"] ENTRYPOINT ["/bin/bash", "/entrypoint.sh"]

View File

@ -13,7 +13,7 @@ I have tried to embed some new features such as `isolate` and interface startup
## Getting the container running: ## Getting the container running:
To get the container running you either pull the image from the repository, `dselen/wgdashboard:latest`.<br> To get the container running you either pull the image from the repository, `donaldzou/wgdashboard:latest`.<br>
From there either use the environment variables describe below as parameters or use the Docker Compose file: `compose.yaml`.<br> From there either use the environment variables describe below as parameters or use the Docker Compose file: `compose.yaml`.<br>
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.<br> 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.<br>
Otherwise edit the configuration file in `/etc/wireguard/wg0.conf`. Otherwise edit the configuration file in `/etc/wireguard/wg0.conf`.
@ -22,14 +22,14 @@ An example of a simple command to get the container running is show below:<br>
```shell ```shell
docker run -d \ docker run -d \
--name wireguard-dashboard \ --name wgdashboard \
--restart unless-stopped \ --restart unless-stopped \
-e enable=wg0 \ -e enable=wg0 \
-e isolate=wg0 \ -e isolate=wg0 \
-p 10086:10086/tcp \ -p 10086:10086/tcp \
-p 51820:51820/udp \ -p 51820:51820/udp \
--cap-add NET_ADMIN \ --cap-add NET_ADMIN \
dselen/wgdashboard:latest donaldzou/wgdashboard:latest
``` ```
<br> <br>
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: 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:
@ -37,34 +37,38 @@ If you want to use Compose instead of a raw Docker command, refer to the example
```yaml ```yaml
services: services:
wireguard-dashboard: wgdashboard:
image: dselen/wgdashboard:latest image: donaldzou/wgdashboard:latest
restart: unless-stopped restart: unless-stopped
container_name: wire-dash container_name: wgdashboard
environment: environment:
#- tz= #- tz=
#- global_dns= #- global_dns=
- enable=none #- enable=
- isolate=wg0 #- isolate=
#- public_ip= #- public_ip=
ports: ports:
- 10086:10086/tcp - 10086:10086/tcp
- 51820:51820/udp - 51820:51820/udp
volumes: volumes:
- conf:/etc/wireguard - conf:/etc/wireguard
- app:/opt/wireguarddashboard/app - data:/data
cap_add: cap_add:
- NET_ADMIN - NET_ADMIN
volumes: volumes:
conf: conf:
app: data:
``` ```
If you want to customize the yaml, make sure the core stays the same, but for example volume PATHs can be freely changed.<br> If you want to customize the yaml, make sure the core stays the same, but for example volume PATHs (ON THE HOST) can be freely changed.<br>
This setup is just generic and will use the Docker volumes. This setup is just generic and will use the Docker volumes.
## Updating the container:
Updating is right now in Alpha stage. I have got it to work, testing methods.
## Working with the container and environment variables: ## 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.<br> Once the container is running, the installation process is essentially the same as running it on bare-metal.<br>
@ -75,8 +79,8 @@ So go to the assign TCP port in this case HTTP, like the default 10086 one in th
| 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. | | 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. | 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'. | 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). | isolate | Anything, preferably an existing WireGuard interface name. | `none` | `wg1,wg0` | 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. | public_ip | Any IPv4 (public recommended) address, such as the one returned by default | Default uses the return of `curl ifconfig.me` | `89.20.83.118` | 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: ## Be careful with:
@ -87,8 +91,9 @@ The latter opens up UDP ports from 51820 to 51830, so all ports in between as we
To build the image yourself, you need to do a couple things:<br> To build the image yourself, you need to do a couple things:<br>
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. 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. Navigate into the cloned repository.
1. (Make sure you have Docker correctly installed, if not: [Click here](https://docs.docker.com/engine/install/)) and run: `docker build . -t <Image name>:<Image tag>` as an example: `docker build . -t dselen/wgdashboard:latest`.<br>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. (Make sure you have Docker correctly installed, if not: [Click here](https://docs.docker.com/engine/install/)) and run: `docker build . -t <Image name>:<Image tag>` as an example: `docker build . -t dselen/wgdashboard:latest`.<br>
This will make Docker compile the image from the resources in the directory you mention, in this case the source/root one. Let it compile, it takes only a couple seconds with a minute at most.
1. If all went well, see your image with `docker images`. Example below: 1. If all went well, see your image with `docker images`. Example below:
```shell ```shell
@ -100,7 +105,3 @@ dselen/wgdashboard latest c96fd96ee3b3 42 minutes ago 314MB
## Closing remarks: ## Closing remarks:
For feedback please submit an issue to the repository. Or message dselen@nerthus.nl. 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.

View File

@ -1,23 +1,23 @@
services: services:
wireguard-dashboard: wireguard-dashboard:
image: test:latest image: donaldzou/wgdashboard:latest
restart: unless-stopped restart: unless-stopped
container_name: wgdashboard container_name: wgdashboard
environment: environment:
#- tz= # <--- Set container timezone, default: Europe/Amsterdam. #- tz= # <--- Set container timezone, default: Europe/Amsterdam.
- global_dns=9.9.9.9 # <--- Set global DNS address, default: 1.1.1.1. #- 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. #- enable= # <--- Set the interfaces that will be enabled on startup, default: 'none'.
- isolate=wg0 # <--- When set to true, it disallows peers to talk to eachother, setting to false, allows it, default: true. #- isolate= # <--- Set the interfaces that will disallow peer communication, default: 'none'.
#- public_ip= # <--- Set public IP to ensure the correct one is chosen, defaulting to the IP give by ifconfig.me. #- public_ip= # <--- Set public IP to ensure the correct one is chosen, defaulting to the IP give by ifconfig.me.
ports: ports:
- 10086:10086/tcp - 10086:10086/tcp
- 51820:51820/udp - 51820:51820/udp
volumes: # Can be customized to only the /opt/wireguarddashboard/src/db folder with the /opt/wireguarddashboard/src/wg-dashboard.ini file. volumes:
- ./app:/opt/wireguarddashboard - conf:/etc/wireguard
- ./conf:/etc/wireguard - data:/data
cap_add: cap_add:
- NET_ADMIN - NET_ADMIN
volumes: volumes:
app:
conf: conf:
data:

View File

@ -5,107 +5,91 @@ echo "Starting the WireGuard Dashboard Docker container."
ensure_installation() { ensure_installation() {
# When using a custom directory to store the files, this part moves over and makes sure the installation continues. # 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." echo "Quick-installing..."
if [ -z "$(ls -A "${WGDASH}")" ]; then [ ! -d "/data/db" ] && echo "Creating database dir" && mkdir /data/db
echo "Detected empty directory, moving over..." ln -s /data/db "${WGDASH}/src/db"
mv /setup/app/* "${WGDASH}" [ ! -f "/data/wg-dashboard.ini" ] && echo "Creating wg-dashboard.ini file" && touch /data/wg-dashboard.ini
#mv /setup/app/.* "${WGDASH}" ln -s /data/wg-dashboard.ini "${WGDASH}/src/wg-dashboard.ini"
python3 -m venv "${WGDASH}"/src/venv python3 -m venv "${WGDASH}"/src/venv
. "${WGDASH}/src/venv/bin/activate" . "${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/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
mv /usr/lib/python3.12/site-packages/bcrypt* "${WGDASH}"/src/venv/lib/python3.12/site-packages
chmod +x "${WGDASH}"/src/wgd.sh chmod +x "${WGDASH}"/src/wgd.sh
cd "${WGDASH}"/src || exit cd "${WGDASH}"/src || exit
./wgd.sh install ./wgd.sh install
echo "Looks like the installation succesfully moved over." echo "Looks like the installation succeeded."
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. # This first step is to ensure the wg0.conf file exists, and if not, then its copied over from the ephemeral container storage.
# This is done so WGDashboard it works out of the box
if [ ! -f "/etc/wireguard/wg0.conf" ]; then if [ ! -f "/etc/wireguard/wg0.conf" ]; then
echo "Standard wg0 Configuration file not found, grabbing template." echo "Standard wg0 Configuration file not found, grabbing template."
cp -a "/setup/conf/wg0.conf" "/etc/wireguard/wg0.conf" cp -a "/configs/wg0.conf.template" "/etc/wireguard/wg0.conf"
echo "Setting a secure private key." echo "Setting a secure private key." # SORRY 4 BE4 - Daan
local privateKey local privateKey
privateKey=$(wg genkey) 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 sed -i "s|^PrivateKey *=.*$|PrivateKey = ${privateKey}|g" /etc/wireguard/wg0.conf
echo "Done setting template." echo "Done setting template."
else else
echo "Existing wg0 configuration file found, using that." echo "Existing wg0 configuration file found, using that."
fi 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() { set_envvars() {
#printf "\n------------- SETTING ENVIRONMENT VARIABLES ----------------\n" printf "\n------------- SETTING ENVIRONMENT VARIABLES ----------------\n"
# Changing the DNS used for clients and the dashboard itself. # Path to the configuration file (exists because of previous function).
if [ "${global_dns}" != "$(grep "peer_global_dns = " /opt/wireguarddashboard/src/wg-dashboard.ini | awk '{print $NF}')" ]; then local config_file="/opt/wireguarddashboard/src/wg-dashboard.ini"
echo "Changing default dns."
# Check if the file is empty
if [ ! -s "$config_file" ]; then
echo "Config file is empty. Creating [Peers] section."
# Create [Peers] section with initial values
{
echo "[Peers]"
echo "remote_endpoint = ${public_ip}"
echo "peer_global_dns = ${global_dns}"
} > "$config_file"
#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 else
echo "DNS is set correctly." echo "Config file is not empty, enforcing environment variables."
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. # Check and update the DNS if it has changed
if [ "${public_ip}" = "0.0.0.0" ]; then current_dns=$(grep "peer_global_dns = " "$config_file" | awk '{print $NF}')
default_ip=$(curl -s ifconfig.me) if [ "${global_dns}" != "$current_dns" ]; then
echo "Trying to fetch the Public-IP using ifconfig.me: ${default_ip}" echo "Changing default DNS."
sed -i "s/^peer_global_dns = .*/peer_global_dns = ${global_dns}/" "$config_file"
else
echo "DNS is set correctly."
fi
sed -i "s/^remote_endpoint = .*/remote_endpoint = ${default_ip}/" /opt/wireguarddashboard/src/wg-dashboard.ini # Determine the public IP and update if necessary
elif [ "${public_ip}" != "$(grep "remote_endpoint = " /opt/wireguarddashboard/src/wg-dashboard.ini | awk '{print $NF}')" ]; then if [ "${public_ip}" = "0.0.0.0" ]; then
echo "Setting the Public-IP using given variable: ${public_ip}" 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}/" "$config_file"
else
current_ip=$(grep "remote_endpoint = " "$config_file" | awk '{print $NF}')
if [ "${public_ip}" != "$current_ip" ]; then
echo "Setting the Public-IP using given variable: ${public_ip}"
sed -i "s/^remote_endpoint = .*/remote_endpoint = ${public_ip}/" "$config_file"
fi
fi
sed -i "s/^remote_endpoint = .*/remote_endpoint = ${public_ip}/" /opt/wireguarddashboard/src/wg-dashboard.ini
fi fi
} }
@ -114,113 +98,138 @@ start_core() {
printf "\n---------------------- STARTING CORE -----------------------\n" printf "\n---------------------- STARTING CORE -----------------------\n"
echo "Activating Python venv and executing the WireGuard Dashboard service." echo "Activating Python venv and executing the WireGuard Dashboard service."
. "${WGDASH}"/src/venv/bin/activate . "${WGDASH}"/src/venv/bin/activate
cd "${WGDASH}"/src || return cd "${WGDASH}"/src || return
bash wgd.sh start &>> /dev/null bash wgd.sh start
# Isolated peers feature, first converting the existing configuration files and the given names to arrays. # Isolated peers feature, first converting the existing configuration files and the given names to arrays.
#
# WILL BE REMOVED IN FUTURE WHEN WGDASHBOARD ITSELF SUPPORTS THIS!!
#
local configurations=(/etc/wireguard/*) local configurations=(/etc/wireguard/*)
IFS=',' read -r -a do_isolate <<< "${isolate}" IFS=',' read -r -a do_isolate <<< "${isolate}"
non_isolate=() non_isolate=()
# Checking if there are matches between the two arrays. # Checking if there are matches between the two arrays.
for config in "${configurations[@]}"; do for config in "${configurations[@]}"; do
local config=$(echo "$config" | sed -e 's|.*/etc/wireguard/||' -e 's|\.conf$||') config=$(echo "$config" | sed -e 's|.*/etc/wireguard/||' -e 's|\.conf$||')
local found
found=false found=false
for interface in "${do_isolate[@]}"; do for interface in "${do_isolate[@]}"; do
if [[ "$config" == "$interface" ]]; then if [[ "$config" == "$interface" ]]; then
found=true found=true
break break
fi fi
done done
if [ "$found" = false ]; then if [ "$found" = false ]; then
non_isolate+=("$config") non_isolate+=("$config")
fi fi
done done
# Isolating the matches. # Isolating the matches.
for interface in "${do_isolate[@]}"; do for interface in "${do_isolate[@]}"; do
if [ "$interface" = "none" ]; then
if [ "$interface" = "none" ] || [ "$interface" = "" ]; then
echo "Found: $interface, stopping isolation checking." echo "Found: $interface, stopping isolation checking."
break break
else else
if [ -f "/etc/wireguard/${interface}.conf" ]; then if [ -f "/etc/wireguard/${interface}.conf" ]; then
echo "Isolating interface:" $interface 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) 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 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 "/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 sed -i "/PreDown =/a PreDown = iptables -D FORWARD -i ${interface} -o ${interface} -j DROP" /etc/wireguard/"${interface}".conf
fi fi
else else
echo "Configuration for $interface does not seem to exist, continuing." echo "Configuration for $interface in enforce isolation does not seem to exist, continuing."
fi fi
fi fi
done done
# Removing isolation for the configurations that did not match. # Removing isolation for the configurations that did not match.
for interface in "${non_isolate[@]}"; do for interface in "${non_isolate[@]}"; do
if [ -f "/etc/wireguard/${interface}.conf" ]; then if [ -f "/etc/wireguard/${interface}.conf" ]; then
echo "Removing Isolation if present for:" $interface echo "Removing isolation, if isolation is 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 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 else
echo "Configuration for $interface does not seem to exist, continuing." echo "Configuration for $interface in removing isolation does not seem to exist, continuing."
fi fi
done done
# The following section takes care of enabling wireguard interfaces on startup. Using arrays and given arguments. # The following section takes care of enabling wireguard interfaces on startup. Using arrays and given arguments.
#
# WILL BE REMOVED IN FUTURE WHEN WGDASHBOARD ITSELF SUPPORTS THIS!!
#
IFS=',' read -r -a enable_array <<< "${enable}" IFS=',' read -r -a enable_array <<< "${enable}"
for interface in "${enable_array[@]}"; do for interface in "${enable_array[@]}"; do
if [ "$interface" = "none" ]; then if [ "$interface" = "none" ]; then
echo "Found: $interface, stopping enabling checking." echo "Found: $interface, stopping enabling checking."
break break
else else
echo "Enabling interface:" $interface echo "Enabling interface:" "$interface"
local fileperms=$(stat -c "%a" /etc/wireguard/${interface}.conf) local fileperms
if [ $fileperms -eq 644 ]; then fileperms=$(stat -c "%a" /etc/wireguard/"${interface}".conf)
if [ "$fileperms" -eq 644 ]; then
echo "Configuration is world accessible, adjusting." echo "Configuration is world accessible, adjusting."
chmod 600 "/etc/wireguard/${interface}.conf" chmod 600 "/etc/wireguard/${interface}.conf"
fi fi
if [ -f "/etc/wireguard/${interface}.conf" ]; then if [ -f "/etc/wireguard/${interface}.conf" ]; then
wg-quick up $interface wg-quick up "$interface"
else else
echo "No corresponding configuration file found for $interface doing nothing." echo "No corresponding configuration file found for $interface doing nothing."
fi fi
fi fi
done done
} }
# === CLEAN UP ===
ensure_blocking() { ensure_blocking() {
#printf "\n-------------- ENSURING CONTAINER CONTINUATION -------------\n"
. "${WGDASH}"/src/venv/bin/activate
cd "${WGDASH}"/src || return
bash wgd.sh restart
sleep 1s sleep 1s
echo "Ensuring container continuation." echo -e "\nEnsuring container continuation."
# This function checks if the latest error log is created and tails it for docker logs uses. # Find and tail the latest error and access logs if they exist
if find "/opt/wireguarddashboard/src/log" -mindepth 1 -maxdepth 1 -type f | read -r; then local logdir="/opt/wireguarddashboard/src/log"
latestErrLog=$(find /opt/wireguarddashboard/src/log -name "error_*.log" | head -n 1)
latestAccLog=$(find /opt/wireguarddashboard/src/log -name "access_*.log" | head -n 1) latestErrLog=$(find "$logdir" -name "error_*.log" -type f -print | sort -r | head -n 1)
latestAccLog=$(find "$logdir" -name "access_*.log" -type f -print | sort -r | head -n 1)
tail -f "${latestErrLog}" "${latestAccLog}" # Only tail the logs if they are found
if [ -n "$latestErrLog" ] || [ -n "$latestAccLog" ]; then
tail -f "$latestErrLog" "$latestAccLog"
else
echo "No log files found to tail."
fi fi
# Blocking command in case of erroring. So the container does not quit. # Blocking command to keep the container running as a last resort.
sleep infinity sleep infinity
} }
# Execute functions for the WireGuard Dashboard services, then set the environment variables # Execute functions for the WireGuard Dashboard services, then set the environment variables
ensure_installation ensure_installation
clean_up
start_core
set_envvars set_envvars
start_core
ensure_blocking ensure_blocking

View File

@ -380,20 +380,29 @@ update_wgd() {
new_ver=$($venv_python -c "import json; import urllib.request; data = urllib.request.urlopen('https://api.github.com/repos/donaldzou/WGDashboard/releases/latest').read(); output = json.loads(data);print(output['tag_name'])") new_ver=$($venv_python -c "import json; import urllib.request; data = urllib.request.urlopen('https://api.github.com/repos/donaldzou/WGDashboard/releases/latest').read(); output = json.loads(data);print(output['tag_name'])")
printf "%s\n" "$dashes" printf "%s\n" "$dashes"
printf "[WGDashboard] Are you sure you want to update to the %s? (Y/N): " "$new_ver"
read up if [ "$commandConfirmed" = "true" ]; then
printf "[WGDashboard] Confirmation granted.\n"
up="Y"
else
printf "[WGDashboard] Are you sure you want to update to the %s? (Y/N): " "$new_ver"
read up
fi
if [ "$up" = "Y" ] || [ "$up" = "y" ]; then if [ "$up" = "Y" ] || [ "$up" = "y" ]; then
printf "[WGDashboard] Shutting down WGDashboard\n" printf "[WGDashboard] Shutting down WGDashboard\n"
if check_wgd_status; then if check_wgd_status; then
stop_wgd stop_wgd
fi fi
mv wgd.sh wgd.sh.old
printf "[WGDashboard] Downloading %s from GitHub..." "$new_ver" mv wgd.sh wgd.sh.old && \
{ date; git stash; git pull https://github.com/donaldzou/WGDashboard.git $new_ver --force; } >> ./log/update.txt printf "[WGDashboard] Downloading %s from GitHub..." "$new_ver" && \
chmod +x ./wgd.sh { date; git stash; git pull https://github.com/donaldzou/WGDashboard.git $new_ver --force; } >> ./log/update.txt && \
sudo ./wgd.sh install chmod +x ./wgd.sh && \
printf "[WGDashboard] Update completed!\n" sudo ./wgd.sh install && \
printf "%s\n" "$dashes" printf "[WGDashboard] Update completed!\n" && \
printf "%s\n" "$dashes"; \
rm wgd.sh.old rm wgd.sh.old
else else
printf "%s\n" "$dashes" printf "%s\n" "$dashes"
@ -402,52 +411,55 @@ update_wgd() {
fi fi
} }
if [ "$#" != 1 ]; if [ "$#" -lt 1 ]; then
then help
help else
else if [ "$2" = "-y" ] || [ "$2" = "-Y" ]; then
if [ "$1" = "start" ]; then commandConfirmed="true"
if check_wgd_status; then fi
printf "%s\n" "$dashes"
printf "[WGDashboard] WGDashboard is already running.\n" if [ "$1" = "start" ]; then
printf "%s\n" "$dashes" if check_wgd_status; then
else printf "%s\n" "$dashes"
start_wgd printf "[WGDashboard] WGDashboard is already running.\n"
fi printf "%s\n" "$dashes"
elif [ "$1" = "stop" ]; then else
if check_wgd_status; then start_wgd
printf "%s\n" "$dashes" fi
stop_wgd elif [ "$1" = "stop" ]; then
printf "[WGDashboard] WGDashboard is stopped.\n" if check_wgd_status; then
printf "%s\n" "$dashes" printf "%s\n" "$dashes"
else stop_wgd
printf "%s\n" "$dashes" printf "[WGDashboard] WGDashboard is stopped.\n"
printf "[WGDashboard] WGDashboard is not running.\n" printf "%s\n" "$dashes"
printf "%s\n" "$dashes" else
fi printf "%s\n" "$dashes"
elif [ "$1" = "update" ]; then printf "[WGDashboard] WGDashboard is not running.\n"
update_wgd printf "%s\n" "$dashes"
elif [ "$1" = "install" ]; then fi
printf "%s\n" "$dashes" elif [ "$1" = "update" ]; then
install_wgd update_wgd
printf "%s\n" "$dashes" elif [ "$1" = "install" ]; then
elif [ "$1" = "restart" ]; then printf "%s\n" "$dashes"
if check_wgd_status; then install_wgd
printf "%s\n" "$dashes" printf "%s\n" "$dashes"
stop_wgd elif [ "$1" = "restart" ]; then
printf "| WGDashboard is stopped. |\n" if check_wgd_status; then
sleep 4 printf "%s\n" "$dashes"
start_wgd stop_wgd
else printf "| WGDashboard is stopped. |\n"
start_wgd sleep 4
fi start_wgd
elif [ "$1" = "debug" ]; then else
if check_wgd_status; then start_wgd
printf "| WGDashboard is already running. |\n" fi
else elif [ "$1" = "debug" ]; then
start_wgd_debug if check_wgd_status; then
fi printf "| WGDashboard is already running. |\n"
else else
help start_wgd_debug
fi fi
else
help
fi
fi fi