fix(chore): code split

This commit is contained in:
Shahrad Elahi 2023-12-19 13:21:23 +03:30
parent 8488858c46
commit 3fdb21d8d7
5 changed files with 74 additions and 55 deletions

View File

@ -8,8 +8,6 @@ RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
COPY --from=chriswayg/tor-alpine:latest /usr/local/bin/obfs4proxy /usr/local/bin/obfs4proxy
COPY --from=chriswayg/tor-alpine:latest /usr/local/bin/meek-server /usr/local/bin/meek-server
COPY /config/torrc /etc/tor/torrc
# Update and upgrade packages
RUN apk update && apk upgrade \
# Install required packages
@ -23,6 +21,14 @@ RUN apk update && apk upgrade \
# Clear APK cache
&& rm -rf /var/cache/apk/*
COPY /config/torrc /etc/tor/torrc
COPY /scripts /scripts
RUN chmod -R +x /scripts
COPY /bin /usr/local/bin
RUN chmod -R +x /usr/local/bin
FROM base AS deps
@ -53,6 +59,7 @@ COPY --from=build /app/build build
COPY --from=build /app/package.json .
ENV NODE_ENV=production
ENV LOG_LEVEL=error
COPY docker-entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
@ -63,7 +70,7 @@ HEALTHCHECK --interval=60s --timeout=3s --start-period=20s --retries=3 \
RUN mkdir -p /data && chmod 700 /data
RUN mkdir -p /etc/torrc.d && chmod -R 400 /etc/torrc.d
RUN mkdir -p /var/vlogs && chmod -R 600 /var/vlogs && touch /var/vlogs/web.log
RUN mkdir -p /var/vlogs && chmod -R 600 /var/vlogs && touch /var/vlogs/web
VOLUME ["/etc/torrc.d", "/data", "/var/vlogs"]

View File

@ -8,9 +8,6 @@ RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
COPY --from=chriswayg/tor-alpine:latest /usr/local/bin/obfs4proxy /usr/local/bin/obfs4proxy
COPY --from=chriswayg/tor-alpine:latest /usr/local/bin/meek-server /usr/local/bin/meek-server
COPY /config/torrc /etc/tor/torrc
COPY /config/obfs4-bridges.conf /etc/torrc.d/obfs4-bridges.conf
# Update and upgrade packages
RUN apk update && apk upgrade \
# Install required packages
@ -24,6 +21,15 @@ RUN apk update && apk upgrade \
# Clear APK cache
&& rm -rf /var/cache/apk/*
COPY /config/torrc /etc/tor/torrc
COPY /config/obfs4-bridges.conf /etc/torrc.d/obfs4-bridges.conf
COPY /scripts /scripts
RUN chmod -R +x /scripts
COPY /bin /usr/local/bin
RUN chmod -R +x /usr/local/bin
FROM base AS deps
@ -47,7 +53,7 @@ ENTRYPOINT ["/entrypoint.sh"]
RUN mkdir -p /data && chmod 700 /data
RUN mkdir -p /etc/torrc.d && chmod -R 400 /etc/torrc.d
RUN mkdir -p /var/vlogs && chmod -R 600 /var/vlogs && touch /var/vlogs/web.log
RUN mkdir -p /var/vlogs && chmod -R 600 /var/vlogs && touch /var/vlogs/web
VOLUME ["/etc/torrc.d", "/data", "/var/vlogs"]

View File

@ -9,6 +9,6 @@ services:
environment:
- UI_PASSWORD=password
- WG_HOST=192.168.1.102
- TOR_SOCKS5_PROXY=host.docker.internal:1080
- TOR_SOCKS5_PROXY=host.docker.internal:8080
extra_hosts:
- "host.docker.internal:host-gateway"

View File

@ -4,46 +4,6 @@ set -e
TOR_CONFIG="/etc/tor/torrc"
ENV_FILE="/app/.env"
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 "| | / (_)_______ / | ____/ /___ ___ (_)___ "
@ -69,7 +29,7 @@ fi
if [ -n "$UI_PASSWORD" ]; then
sed -i '/^HASHED_PASSWORD/d' "${ENV_FILE}"
tee -a "${ENV_FILE}" &>/dev/null <<EOF
HASHED_PASSWORD=$(echo -n "$UI_PASSWORD" | xxd -ps -u)
HASHED_PASSWORD=$(printf "%s" "${UI_PASSWORD}" | od -A n -t x1 | tr -d ' \n')
EOF
unset UI_PASSWORD
else
@ -81,15 +41,18 @@ fi
awk -F= '!a[$1]++' "${ENV_FILE}" >"/tmp/$(basename "${ENV_FILE}")" &&
mv "/tmp/$(basename "${ENV_FILE}")" "${ENV_FILE}"
# 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"
# Starting Tor
source /scripts/tord.sh
# Generate Tor configuration
generate_tor_config
# Start Tor on the background
screen -L -Logfile /var/vlogs/tor -dmS tor \
bash -c "tor -f ${TOR_CONFIG}"
# 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"
screen -L -Logfile /var/vlogs/tor -dmS "tor" tor -f "${TOR_CONFIG}"
sleep 1
echo -e "\n======================== Versions ========================"

43
scripts/tord.sh Normal file
View File

@ -0,0 +1,43 @@
#!/usr/bin/env bash
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}"
}