This commit is contained in:
Shahrad Elahi 2023-12-19 07:02:10 +03:30
parent 07d24013bb
commit 0706f13216

View File

@ -4,17 +4,46 @@ set -e
TOR_CONFIG="/etc/tor/torrc"
ENV_FILE="/app/.env"
remove_duplicated_lines() {
local file="$1"
local temp_file="/tmp/$(basename "$file")"
awk '!seen[$0]++' "$file" >"$temp_file"
mv "$temp_file" "$file"
}
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_DIR_FILES=$(find /etc/torrc.d -type f -name "*.conf")
if [ -n "$TORRC_DIR_FILES" ]; then
for file in $TORRC_DIR_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}"
}
echo " "
echo " _ ___ ___ __ _ "
echo "| | / (_)_______ / | ____/ /___ ___ (_)___ "
@ -52,38 +81,7 @@ fi
awk -F= '!a[$1]++' "${ENV_FILE}" >"/tmp/$(basename "${ENV_FILE}")" &&
mv "/tmp/$(basename "${ENV_FILE}")" "${ENV_FILE}"
# IP address of the container
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 lines form "${TOR_CONFIG}" file
remove_duplicated_lines "${TOR_CONFIG}"
# Checking if there is /etc/torrc.d folder and if there is
# any file in it, adding them to the torrc file
TORRC_DIR_FILES=$(find /etc/torrc.d -type f -name "*.conf")
if [ -n "$TORRC_DIR_FILES" ]; then
for file in $TORRC_DIR_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}"
generate_tor_config
# Start Tor on the background
screen -L -Logfile /var/vlogs/tor -dmS tor \