docker-vpn/start.tmpl
2022-03-24 01:32:10 +01:00

41 lines
1.5 KiB
Bash
Executable file

#!/usr/bin/env sh
INSTALLATION_NAME='{INSTALLATION_NAME}'
TAG='{TAG}'
DEPENDENCIES='sed awk shuf dirname docker'
conf_file="/etc/$INSTALLATION_NAME.conf"
for cmd in $DEPENDENCIES; do
if ! command -v "$cmd" >/dev/null 2>&1; then
echo "$INSTALLATION_NAME: no such command '$cmd'" >&2
exit 1
fi
done
## Read the target configuration file name
# Can mention multiple files in one line seperated by space.
# Setting the variable multiple times is the same as
# when all values were set on one line delimited by a space.
# The final configuration file is drawn randomly from the
# resulting set of file.
ovpn_configuration_file="$(sed -n '/^[^#]/ s/OVPN_CONFIGURATION_FILE[^=]*=//p' "$conf_file" | tr ' ' '\n')"
ovpn_configuration_file="$(echo "$ovpn_configuration_file" | shuf -n1)"
ovpn_configuration_file="$(echo "$ovpn_configuration_file" | awk '{if (substr($0, 1, 1) != "/") {print "/etc/openvpn/client/"$0} else {print $0}}')"
ovpn_configuration_file_parent_dir="$(dirname "$ovpn_configuration_file")"
echo "ovpn_configuration_file=$ovpn_configuration_file"
if ! stat "$ovpn_configuration_file"; then
exit 1
fi
docker run -it --cap-add NET_ADMIN \
-v /etc/passwd:/etc/passwd \
-v "$ovpn_configuration_file_parent_dir:$ovpn_configuration_file_parent_dir" \
-v "$HOME:$HOME" \
-v "$PWD:/data/" \
-e OVPN_USER="$USER" \
-e OVPN_HOME="$HOME" \
-e OVPN_WORKDIR="/data/" \
-e OVPN_CONFIGURATION_FILE="$ovpn_configuration_file" \
"$TAG" sh