diff --git a/.gitea/workflows/dfi.bash b/.gitea/workflows/dfi.bash index ecb8a0e..1ec90b6 100755 --- a/.gitea/workflows/dfi.bash +++ b/.gitea/workflows/dfi.bash @@ -219,24 +219,10 @@ function client::finance::plugins::__tor() [ -z "$_plugins" ] && exit 1 client::finance::up - local -r _congrats="| grep Congratulations" - - # start - local _tries=0 - while [ $_tries -lt 3 ]; do - "${ci_shell[@]}" "$_plugins repo/tor.bash start" $_congrats - [ $? -eq 0 ] && break || ((_tries++)) + local -r _tor=("start" "restart" "stop") + for _arg in "${_tor[@]}"; do + "${ci_shell[@]}" "$_plugins repo/tor.bash $_arg" 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 } diff --git a/client/plugins/docker/tor.bash b/client/plugins/docker/tor.bash index b2c4645..d272ecf 100755 --- a/client/plugins/docker/tor.bash +++ b/client/plugins/docker/tor.bash @@ -44,17 +44,17 @@ function tor::start() # 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. - # Ergo, a already-running container will be needed (sorry, lib_docker::run()) - if ! docker container inspect -f '{{.State.Running}}' "$global_container" &>/dev/null; then - lib_utils::die_fatal "docker-finance not running! Bring \`up\` container and try again." - fi + # Ergo, an already-running container will be needed (sorry, lib_docker::run()) + lib_docker::exec "" \ + || lib_utils::die_fatal "docker-finance not running! Bring \`up\` a \`dfi\` instance and try again." 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\`)" return 1 else + docker pull alpine:latest || lib_utils::die_fatal docker run -it --rm --detach \ --network "$global_network" \ --name="${tor_container}" \ @@ -71,7 +71,7 @@ function tor::start() lib_utils::print_info "${tor_container}: container IP '${_ip}'" # 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 sleep 1s 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}" 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" lib_utils::print_info "${global_container}: updating $_proxychains" docker exec --user root "$global_container" \ - /bin/bash -i -c " + /bin/bash -c " sed -i \ -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:' \ @@ -100,14 +100,27 @@ function tor::start() # Test Tor connection 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 - # TODO: run timer to verify response (and fail if N times if no response) - docker exec "$global_container" \ - /bin/bash -i -c " - proxychains curl -s https://check.torproject.org 2>/dev/null \ - | 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 + + local _tries=1 + while [ $_tries -ne 3 ]; do + lib_docker::exec "proxychains curl -s https://check.torproject.org 2>/dev/null \ + | grep -B3 'Your IP address appears to be' \ + | 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() @@ -116,8 +129,8 @@ function tor::stop() lib_utils::print_error "${tor_container}: container not running" return 1 fi - lib_utils::print_warning "${tor_container}: stopping container" - docker container stop "$tor_container" &>/dev/null \ + lib_utils::print_info "${tor_container}: stopping container" + docker container stop -t 3 "$tor_container" &>/dev/null \ && lib_utils::print_info "${tor_container}: container stopped" }