Merge pull request #292 into master

0871934f repo: gitea: workflows: dfi: plugins: tor: refactor (Aaron Fiore)
aebe9c66 client: plugins: tor: add timeout when `stop`ing tor container (Aaron Fiore)
f302908b client: plugins: tor: implement retries when bootstrapping (Aaron Fiore)
e0c66e74 client: plugins: tor: don't use interactive shell when updating proxychains (Aaron Fiore)
19c2d477 client: plugins: tor: print info instead of warnings (when info) (Aaron Fiore)
890c0631 client: plugins: tor: pull `alpine:latest` when `start`ing (Aaron Fiore)
56ba75e7 client: plugins: tor: fix inspection for running tor container (Aaron Fiore)
810e21d7 client: plugins: tor: refactor check for running `dfi` instance (Aaron Fiore)
This commit is contained in:
2026-02-05 11:26:35 -08:00
2 changed files with 33 additions and 34 deletions

View File

@@ -219,24 +219,10 @@ function client::finance::plugins::__tor()
[ -z "$_plugins" ] && exit 1 [ -z "$_plugins" ] && exit 1
client::finance::up client::finance::up
local -r _congrats="| grep Congratulations" local -r _tor=("start" "restart" "stop")
for _arg in "${_tor[@]}"; do
# start "${ci_shell[@]}" "$_plugins repo/tor.bash $_arg"
local _tries=0
while [ $_tries -lt 3 ]; do
"${ci_shell[@]}" "$_plugins repo/tor.bash start" $_congrats
[ $? -eq 0 ] && break || ((_tries++))
done done
# restart
local _tries=0
while [ $_tries -lt 3 ]; do
"${ci_shell[@]}" "$_plugins repo/tor.bash restart" $_congrats
[ $? -eq 0 ] && break || ((_tries++))
done
# stop
"${ci_shell[@]}" "$_plugins repo/tor.bash stop"
client::finance::down client::finance::down
} }

View File

@@ -44,17 +44,17 @@ function tor::start()
# NOTE: proxychains.conf's [ProxyList] won't allow hostnames (or Docker network container name). # NOTE: proxychains.conf's [ProxyList] won't allow hostnames (or Docker network container name).
# So, to avoid conflicting IP address spaces, docker-finance will not hardcode address space. # So, to avoid conflicting IP address spaces, docker-finance will not hardcode address space.
# Ergo, a already-running container will be needed (sorry, lib_docker::run()) # Ergo, an already-running container will be needed (sorry, lib_docker::run())
if ! docker container inspect -f '{{.State.Running}}' "$global_container" &>/dev/null; then lib_docker::exec "" \
lib_utils::die_fatal "docker-finance not running! Bring \`up\` container and try again." || lib_utils::die_fatal "docker-finance not running! Bring \`up\` a \`dfi\` instance and try again."
fi
local -r _torrc="/etc/tor/torrc" local -r _torrc="/etc/tor/torrc"
if docker container inspect -f '{{.State.Running}}' "$tor_container" &>/dev/null; then if [[ $(docker container inspect -f '{{.State.Running}}' "$tor_container" 2>/dev/null) == "true" ]]; then
lib_utils::print_error "${tor_container}: instance already running (consider \`restart\`)" lib_utils::print_error "${tor_container}: instance already running (consider \`restart\`)"
return 1 return 1
else else
docker pull alpine:latest || lib_utils::die_fatal
docker run -it --rm --detach \ docker run -it --rm --detach \
--network "$global_network" \ --network "$global_network" \
--name="${tor_container}" \ --name="${tor_container}" \
@@ -71,7 +71,7 @@ function tor::start()
lib_utils::print_info "${tor_container}: container IP '${_ip}'" lib_utils::print_info "${tor_container}: container IP '${_ip}'"
# Need to wait for a working installation # Need to wait for a working installation
lib_utils::print_warning "${tor_container}: waiting for Tor installation" lib_utils::print_info "${tor_container}: waiting for Tor installation"
while ! docker exec "$tor_container" /bin/sh -c 'apk info -e tor' 1>/dev/null; do while ! docker exec "$tor_container" /bin/sh -c 'apk info -e tor' 1>/dev/null; do
sleep 1s sleep 1s
done && lib_utils::print_info "${tor_container}: Tor installation ready" done && lib_utils::print_info "${tor_container}: Tor installation ready"
@@ -87,11 +87,11 @@ function tor::start()
lib_utils::print_info "${tor_container}: restarting Tor with updated ${_torrc}" lib_utils::print_info "${tor_container}: restarting Tor with updated ${_torrc}"
docker exec "$tor_container" /bin/sh -c "pkill -HUP tor" || lib_utils::die_fatal docker exec "$tor_container" /bin/sh -c "pkill -HUP tor" || lib_utils::die_fatal
# Set docker-finance's proxychains to point to Tor instance # Set `dfi`'s proxychains instance to point to Tor instance
local -r _proxychains="/etc/proxychains.conf" local -r _proxychains="/etc/proxychains.conf"
lib_utils::print_info "${global_container}: updating $_proxychains" lib_utils::print_info "${global_container}: updating $_proxychains"
docker exec --user root "$global_container" \ docker exec --user root "$global_container" \
/bin/bash -i -c " /bin/bash -c "
sed -i \ sed -i \
-e 's:^#quiet_mode:quiet_mode:' \ -e 's:^#quiet_mode:quiet_mode:' \
-e 's:^# localnet 127.0.0.0/255.0.0.0:localnet 127.0.0.0/255.0.0.0:' \ -e 's:^# localnet 127.0.0.0/255.0.0.0:localnet 127.0.0.0/255.0.0.0:' \
@@ -100,14 +100,27 @@ function tor::start()
# Test Tor connection # Test Tor connection
local -r _sleep="30s" local -r _sleep="30s"
lib_utils::print_warning "${global_container}: testing connection (bootstrapping ~${_sleep})" lib_utils::print_info "${global_container}: testing connection (bootstrapping ~${_sleep})"
sleep "$_sleep" # Give time to bootstrap sleep "$_sleep" # Give time to bootstrap
# TODO: run timer to verify response (and fail if N times if no response)
docker exec "$global_container" \ local _tries=1
/bin/bash -i -c " while [ $_tries -ne 3 ]; do
proxychains curl -s https://check.torproject.org 2>/dev/null \ lib_docker::exec "proxychains curl -s https://check.torproject.org 2>/dev/null \
| grep -B3 'Your IP address appears to be' \ | grep -B3 'Your IP address appears to be' \
| sed -e 's/^ //g' -e '\$ s/[^\\.0-9]//g' -e '/^\$/d' -e '2,3d'" || lib_utils::die_fatal | sed -e 's/^ //g' -e '\$ s/[^\\.0-9]//g' -e '/^\$/d' -e '2,3d' \
| grep -A2 --color=never Congratulations || exit 1 2>/dev/null" 2>/dev/null
if [ $? -ne 0 ]; then
lib_utils::print_warning "Could not bootstrap, trying again (${_tries}/3)"
docker exec "$tor_container" /bin/sh -c "pkill -HUP tor" || lib_utils::die_fatal
sleep "$_sleep"
((_tries++))
else
break
fi
done
if [ $_tries -eq 3 ]; then
lib_utils::die_fatal "Could not successfully bootstrap! \`restart\` this instance"
fi
} }
function tor::stop() function tor::stop()
@@ -116,8 +129,8 @@ function tor::stop()
lib_utils::print_error "${tor_container}: container not running" lib_utils::print_error "${tor_container}: container not running"
return 1 return 1
fi fi
lib_utils::print_warning "${tor_container}: stopping container" lib_utils::print_info "${tor_container}: stopping container"
docker container stop "$tor_container" &>/dev/null \ docker container stop -t 3 "$tor_container" &>/dev/null \
&& lib_utils::print_info "${tor_container}: container stopped" && lib_utils::print_info "${tor_container}: container stopped"
} }