···11#!/usr/bin/env bash
22set -euo pipefail
33-set -x
4354# Create temporary directory for this tor instance
65TOR_DIR=$(mktemp -d -t tornade.XXXXXX)
77-trap 'rm -rf "$TOR_DIR"' EXIT
8699-# Generate random ports
1010-SOCKS_PORT=$((9050 + RANDOM % 1000))
77+# Generate random ports in ephemeral range (49152-65535) to avoid collisions
88+SOCKS_PORT=$((49152 + RANDOM % 16383))
119CONTROL_PORT=$((SOCKS_PORT + 1))
1010+1111+# Track tor PID for cleanup
1212+TOR_PID=""
1313+1414+# Cleanup function - must be defined before trap
1515+cleanup() {
1616+ if [ -n "$TOR_PID" ] && kill -0 "$TOR_PID" 2>/dev/null; then
1717+ kill "$TOR_PID" 2>/dev/null || true
1818+ wait "$TOR_PID" 2>/dev/null || true
1919+ fi
2020+ rm -rf "$TOR_DIR"
2121+}
2222+trap cleanup EXIT INT TERM
12231324# Create tor configuration
1425cat > "$TOR_DIR/torrc" <<EOF
···1829Log notice file $TOR_DIR/tor.log
1930RunAsDaemon 0
20312121-# Performance and reliability optimizations
2222-CircuitBuildTimeout 15
3232+# Hidden service optimizations - longer timeouts for .onion addresses
3333+CircuitBuildTimeout 60
2334LearnCircuitBuildTimeout 0
2424-NumEntryGuards 8
2525-KeepalivePeriod 60
2626-SocksTimeout 120
3535+SocksTimeout 300
27362828-# Reliability settings
2929-MaxCircuitDirtiness 600
3030-NewCircuitPeriod 30
3131-UseEntryGuards 1
3737+# Keep circuits stable for longer sessions
3838+MaxCircuitDirtiness 900
3939+KeepalivePeriod 60
3240EOF
33413442# Start tor in background - suppress output to avoid protocol interference
3543tor -f "$TOR_DIR/torrc" >/dev/null 2>&1 &
3644TOR_PID=$!
37453838-# Function to cleanup tor process
3939-cleanup() {
4040- if kill -0 "$TOR_PID" 2>/dev/null; then
4141- kill "$TOR_PID" 2>/dev/null || true
4242- wait "$TOR_PID" 2>/dev/null || true
4646+# Wait for Tor to bootstrap (up to 90 seconds)
4747+for i in {1..90}; do
4848+ if [ -f "$TOR_DIR/tor.log" ] && grep -q "Bootstrapped 100%" "$TOR_DIR/tor.log" 2>/dev/null; then
4949+ break
4350 fi
4444- rm -rf "$TOR_DIR"
4545-}
4646-trap cleanup EXIT INT TERM
4747-4848-# Retry up to 5 times to establish circuits
4949-for attempt in 1 2 3 4 5; do
5050-5151- if [ $attempt -gt 1 ]; then
5252- # Kill previous tor instance and restart
5353- kill "$TOR_PID" 2>/dev/null || true
5454- wait "$TOR_PID" 2>/dev/null || true
5555- tor -f "$TOR_DIR/torrc" >/dev/null 2>&1 &
5656- TOR_PID=$!
5151+ # If log file doesn't exist after 5 seconds, tor likely failed to start
5252+ if [ "$i" -eq 5 ] && [ ! -f "$TOR_DIR/tor.log" ]; then
5353+ exit 1
5754 fi
5858-5959- # Check frequently at first, then slow down - longer timeout per attempt
6060- for i in {1..120}; do
6161- # Check if log file exists and contains the success message
6262- if [ -f "$TOR_DIR/tor.log" ] && grep -q "Bootstrapped 100%" "$TOR_DIR/tor.log" 2>/dev/null; then
6363- # Success - exit both loops
6464- break 2
6565- fi
6666- # If log file doesn't exist after 2 seconds, tor likely failed to start
6767- if [ "$i" -eq 20 ] && [ ! -f "$TOR_DIR/tor.log" ]; then
6868- # Log file wasn't created after 2 seconds - tor likely failed completely
6969- # Exit with failure immediately rather than retrying
7070- break
7171- fi
7272- # Check every 0.1s for first 2 seconds, then every 0.5s
7373- if [ "$i" -le 20 ]; then
7474- sleep 0.1
7575- else
7676- sleep 0.5
7777- fi
7878- done
7979-8080- # This attempt failed, continue to next attempt
5555+ sleep 1
8156done
82578358# Final check - if still not ready after all attempts, exit