This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

tools.tornade: fix orphaned processes breaking, tune timeouts

+29 -54
+29 -54
tools/tornade/tornade.sh
··· 1 1 #!/usr/bin/env bash 2 2 set -euo pipefail 3 - set -x 4 3 5 4 # Create temporary directory for this tor instance 6 5 TOR_DIR=$(mktemp -d -t tornade.XXXXXX) 7 - trap 'rm -rf "$TOR_DIR"' EXIT 8 6 9 - # Generate random ports 10 - SOCKS_PORT=$((9050 + RANDOM % 1000)) 7 + # Generate random ports in ephemeral range (49152-65535) to avoid collisions 8 + SOCKS_PORT=$((49152 + RANDOM % 16383)) 11 9 CONTROL_PORT=$((SOCKS_PORT + 1)) 10 + 11 + # Track tor PID for cleanup 12 + TOR_PID="" 13 + 14 + # Cleanup function - must be defined before trap 15 + cleanup() { 16 + if [ -n "$TOR_PID" ] && kill -0 "$TOR_PID" 2>/dev/null; then 17 + kill "$TOR_PID" 2>/dev/null || true 18 + wait "$TOR_PID" 2>/dev/null || true 19 + fi 20 + rm -rf "$TOR_DIR" 21 + } 22 + trap cleanup EXIT INT TERM 12 23 13 24 # Create tor configuration 14 25 cat > "$TOR_DIR/torrc" <<EOF ··· 18 29 Log notice file $TOR_DIR/tor.log 19 30 RunAsDaemon 0 20 31 21 - # Performance and reliability optimizations 22 - CircuitBuildTimeout 15 32 + # Hidden service optimizations - longer timeouts for .onion addresses 33 + CircuitBuildTimeout 60 23 34 LearnCircuitBuildTimeout 0 24 - NumEntryGuards 8 25 - KeepalivePeriod 60 26 - SocksTimeout 120 35 + SocksTimeout 300 27 36 28 - # Reliability settings 29 - MaxCircuitDirtiness 600 30 - NewCircuitPeriod 30 31 - UseEntryGuards 1 37 + # Keep circuits stable for longer sessions 38 + MaxCircuitDirtiness 900 39 + KeepalivePeriod 60 32 40 EOF 33 41 34 42 # Start tor in background - suppress output to avoid protocol interference 35 43 tor -f "$TOR_DIR/torrc" >/dev/null 2>&1 & 36 44 TOR_PID=$! 37 45 38 - # Function to cleanup tor process 39 - cleanup() { 40 - if kill -0 "$TOR_PID" 2>/dev/null; then 41 - kill "$TOR_PID" 2>/dev/null || true 42 - wait "$TOR_PID" 2>/dev/null || true 46 + # Wait for Tor to bootstrap (up to 90 seconds) 47 + for i in {1..90}; do 48 + if [ -f "$TOR_DIR/tor.log" ] && grep -q "Bootstrapped 100%" "$TOR_DIR/tor.log" 2>/dev/null; then 49 + break 43 50 fi 44 - rm -rf "$TOR_DIR" 45 - } 46 - trap cleanup EXIT INT TERM 47 - 48 - # Retry up to 5 times to establish circuits 49 - for attempt in 1 2 3 4 5; do 50 - 51 - if [ $attempt -gt 1 ]; then 52 - # Kill previous tor instance and restart 53 - kill "$TOR_PID" 2>/dev/null || true 54 - wait "$TOR_PID" 2>/dev/null || true 55 - tor -f "$TOR_DIR/torrc" >/dev/null 2>&1 & 56 - TOR_PID=$! 51 + # If log file doesn't exist after 5 seconds, tor likely failed to start 52 + if [ "$i" -eq 5 ] && [ ! -f "$TOR_DIR/tor.log" ]; then 53 + exit 1 57 54 fi 58 - 59 - # Check frequently at first, then slow down - longer timeout per attempt 60 - for i in {1..120}; do 61 - # Check if log file exists and contains the success message 62 - if [ -f "$TOR_DIR/tor.log" ] && grep -q "Bootstrapped 100%" "$TOR_DIR/tor.log" 2>/dev/null; then 63 - # Success - exit both loops 64 - break 2 65 - fi 66 - # If log file doesn't exist after 2 seconds, tor likely failed to start 67 - if [ "$i" -eq 20 ] && [ ! -f "$TOR_DIR/tor.log" ]; then 68 - # Log file wasn't created after 2 seconds - tor likely failed completely 69 - # Exit with failure immediately rather than retrying 70 - break 71 - fi 72 - # Check every 0.1s for first 2 seconds, then every 0.5s 73 - if [ "$i" -le 20 ]; then 74 - sleep 0.1 75 - else 76 - sleep 0.5 77 - fi 78 - done 79 - 80 - # This attempt failed, continue to next attempt 55 + sleep 1 81 56 done 82 57 83 58 # Final check - if still not ready after all attempts, exit