2023-12-10 23:20:54 +00:00
|
|
|
#!/usr/bin/env bash
|
2023-09-06 10:23:31 +00:00
|
|
|
set -e
|
|
|
|
|
2023-12-10 23:20:54 +00:00
|
|
|
TOR_CONFIG="/etc/tor/torrc"
|
|
|
|
ENV_FILE="/app/.env"
|
2023-09-06 10:23:31 +00:00
|
|
|
|
2024-01-08 08:59:54 +00:00
|
|
|
to_camel_case() {
|
|
|
|
echo "${1}" | awk -F_ '{for(i=1;i<=NF;i++) $i=toupper(substr($i,1,1)) tolower(substr($i,2));}1' OFS=""
|
|
|
|
}
|
|
|
|
|
|
|
|
generate_tor_config() {
|
|
|
|
# IP address of the container
|
|
|
|
local inet_address="$(hostname -i | awk '{print $1}')"
|
|
|
|
|
|
|
|
sed -i "s/{{INET_ADDRESS}}/$inet_address/g" "${TOR_CONFIG}"
|
|
|
|
|
|
|
|
# any other environment variables that start with TOR_ are added to the torrc
|
|
|
|
# file
|
|
|
|
env | grep ^TOR_ | sed -e 's/TOR_//' -e 's/=/ /' | while read -r line; do
|
|
|
|
key=$(echo "$line" | awk '{print $1}')
|
|
|
|
value=$(echo "$line" | awk '{print $2}')
|
|
|
|
key=$(to_camel_case "$key")
|
|
|
|
echo "$key $value" >>"${TOR_CONFIG}"
|
|
|
|
done
|
|
|
|
|
|
|
|
# Removing duplicated tor options
|
|
|
|
awk -F= '!a[tolower($1)]++' "${TOR_CONFIG}" >"/tmp/$(basename "${TOR_CONFIG}")" &&
|
|
|
|
mv "/tmp/$(basename "${TOR_CONFIG}")" "${TOR_CONFIG}"
|
|
|
|
|
|
|
|
# Checking if there is /etc/torrc.d folder and if there is
|
|
|
|
# any file in it, adding them to the torrc file
|
|
|
|
local torrc_files=$(find /etc/torrc.d -type f -name "*.conf")
|
|
|
|
if [ -n "${torrc_files}" ]; then
|
|
|
|
for file in ${torrc_files}; do
|
|
|
|
cat "$file" >>"${TOR_CONFIG}"
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Remove comment line with single Hash
|
|
|
|
sed -i '/^#\([^#]\)/d' "${TOR_CONFIG}"
|
|
|
|
# Remove options with no value. (KEY[:space:]{...VALUE})
|
|
|
|
sed -i '/^[^ ]* $/d' "${TOR_CONFIG}"
|
|
|
|
# Remove double empty lines
|
|
|
|
sed -i '/^$/N;/^\n$/D' "${TOR_CONFIG}"
|
|
|
|
}
|
|
|
|
|
2023-12-11 01:43:05 +00:00
|
|
|
echo " "
|
|
|
|
echo " _ ___ ___ __ _ "
|
|
|
|
echo "| | / (_)_______ / | ____/ /___ ___ (_)___ "
|
|
|
|
echo "| | /| / / / ___/ _ \/ /| |/ __ / __ \`__ \/ / __ \\"
|
|
|
|
echo "| |/ |/ / / / / __/ ___ / /_/ / / / / / / / / / /"
|
|
|
|
echo "|__/|__/_/_/ \___/_/ |_\__,_/_/ /_/ /_/_/_/ /_/ "
|
|
|
|
echo " "
|
2023-12-11 00:36:15 +00:00
|
|
|
|
2023-12-11 01:43:05 +00:00
|
|
|
mkdir -p /var/vlogs
|
2023-09-29 04:02:23 +00:00
|
|
|
|
2023-12-11 01:43:05 +00:00
|
|
|
touch "${ENV_FILE}"
|
2023-12-10 23:20:54 +00:00
|
|
|
chmod 400 "${ENV_FILE}"
|
2023-11-01 16:43:26 +00:00
|
|
|
|
2023-12-10 23:20:54 +00:00
|
|
|
if ! grep -q "AUTH_SECRET" "${ENV_FILE}"; then
|
2023-12-11 01:43:05 +00:00
|
|
|
tee -a "${ENV_FILE}" &>/dev/null <<EOF
|
2023-11-04 05:09:13 +00:00
|
|
|
AUTH_SECRET=$(openssl rand -base64 32)
|
2023-11-01 16:43:26 +00:00
|
|
|
EOF
|
|
|
|
fi
|
2023-09-29 04:02:23 +00:00
|
|
|
|
|
|
|
# Checking if there is `UI_PASSWORD` environment variable
|
|
|
|
# if there was, converting it to hex and storing it to
|
2023-11-05 16:20:21 +00:00
|
|
|
# the .env
|
2023-09-29 04:02:23 +00:00
|
|
|
if [ -n "$UI_PASSWORD" ]; then
|
2023-12-11 01:43:05 +00:00
|
|
|
sed -i '/^HASHED_PASSWORD/d' "${ENV_FILE}"
|
|
|
|
tee -a "${ENV_FILE}" &>/dev/null <<EOF
|
2023-12-19 09:51:23 +00:00
|
|
|
HASHED_PASSWORD=$(printf "%s" "${UI_PASSWORD}" | od -A n -t x1 | tr -d ' \n')
|
2023-11-01 16:43:26 +00:00
|
|
|
EOF
|
2023-09-29 04:02:23 +00:00
|
|
|
unset UI_PASSWORD
|
2023-12-11 01:43:05 +00:00
|
|
|
else
|
|
|
|
echo "[error] no password set for the UI"
|
|
|
|
exit 1
|
2023-09-29 04:02:23 +00:00
|
|
|
fi
|
|
|
|
|
2024-01-08 08:59:54 +00:00
|
|
|
if [ -z "$WG_HOST" ]; then
|
|
|
|
echo "[error] the WG_HOST environment variable is not set"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2023-12-11 01:43:05 +00:00
|
|
|
# Remove duplicated envs
|
|
|
|
awk -F= '!a[$1]++' "${ENV_FILE}" >"/tmp/$(basename "${ENV_FILE}")" &&
|
|
|
|
mv "/tmp/$(basename "${ENV_FILE}")" "${ENV_FILE}"
|
2023-09-29 04:02:23 +00:00
|
|
|
|
2023-12-19 09:51:23 +00:00
|
|
|
# Starting Redis server in detached mode
|
|
|
|
screen -L -Logfile /var/vlogs/redis -dmS "redis" \
|
|
|
|
bash -c "redis-server --port 6479 --daemonize no --dir /data --appendonly yes"
|
|
|
|
|
|
|
|
# Generate Tor configuration
|
2023-12-19 03:32:10 +00:00
|
|
|
generate_tor_config
|
2023-12-11 00:36:15 +00:00
|
|
|
|
2023-09-29 04:02:23 +00:00
|
|
|
# Start Tor on the background
|
2023-12-19 09:51:23 +00:00
|
|
|
screen -L -Logfile /var/vlogs/tor -dmS "tor" tor -f "${TOR_CONFIG}"
|
2023-09-25 11:56:34 +00:00
|
|
|
|
2023-09-29 04:02:23 +00:00
|
|
|
sleep 1
|
2023-09-27 07:01:52 +00:00
|
|
|
echo -e "\n======================== Versions ========================"
|
|
|
|
echo -e "Alpine Version: \c" && cat /etc/alpine-release
|
|
|
|
echo -e "WireGuard Version: \c" && wg -v | head -n 1 | awk '{print $1,$2}'
|
|
|
|
echo -e "Tor Version: \c" && tor --version | head -n 1
|
|
|
|
echo -e "Obfs4proxy Version: \c" && obfs4proxy -version
|
|
|
|
echo -e "\n========================= Torrc ========================="
|
2023-12-10 23:20:54 +00:00
|
|
|
cat "${TOR_CONFIG}"
|
2023-09-27 07:01:52 +00:00
|
|
|
echo -e "========================================================\n"
|
2023-09-29 04:02:23 +00:00
|
|
|
sleep 1
|
2023-09-25 23:31:07 +00:00
|
|
|
|
2023-09-06 10:23:31 +00:00
|
|
|
exec "$@"
|