···1717Log notice file $TOR_DIR/tor.log
1818RunAsDaemon 0
19192020-# Performance optimizations
2121-CircuitBuildTimeout 10
2020+# Performance and reliability optimizations
2121+CircuitBuildTimeout 15
2222LearnCircuitBuildTimeout 0
2323-NumEntryGuards 3
2323+NumEntryGuards 8
2424KeepalivePeriod 60
2525-SocksTimeout 60
2525+SocksTimeout 120
26262727-# Don't wait for descriptor downloads
2828-UseMicroDescriptors 0
2727+# Reliability settings
2828+MaxCircuitDirtiness 600
2929+NewCircuitPeriod 30
3030+UseEntryGuards 1
2931EOF
30323131-# Start tor in background
3232-echo "Starting isolated Tor instance on port $SOCKS_PORT..." >&2
3333-tor -f "$TOR_DIR/torrc" &
3333+# Start tor in background - suppress output to avoid protocol interference
3434+tor -f "$TOR_DIR/torrc" >/dev/null 2>&1 &
3435TOR_PID=$!
35363637# Function to cleanup tor process
3738cleanup() {
3839 if kill -0 "$TOR_PID" 2>/dev/null; then
3939- echo "Stopping Tor instance..." >&2
4040 kill "$TOR_PID" 2>/dev/null || true
4141 wait "$TOR_PID" 2>/dev/null || true
4242 fi
···4444}
4545trap cleanup EXIT INT TERM
46464747-# Wait for tor to be ready
4848-echo "Waiting for Tor to establish circuits..." >&2
4949-# Check frequently at first, then slow down
5050-for i in {1..60}; do
5151- if grep -q "Bootstrapped 100%" "$TOR_DIR/tor.log" 2>/dev/null; then
5252- echo "Tor is ready!" >&2
5353- break
4747+# Retry up to 5 times to establish circuits
4848+for attempt in 1 2 3 4 5; do
4949+5050+ if [ $attempt -gt 1 ]; then
5151+ # Kill previous tor instance and restart
5252+ kill "$TOR_PID" 2>/dev/null || true
5353+ wait "$TOR_PID" 2>/dev/null || true
5454+ tor -f "$TOR_DIR/torrc" >/dev/null 2>&1 &
5555+ TOR_PID=$!
5456 fi
5555- # Check every 0.1s for first 2 seconds, then every 0.5s
5656- if [ "$i" -le 20 ]; then
5757- sleep 0.1
5858- else
5959- sleep 0.5
6060- fi
5757+5858+ # Check frequently at first, then slow down - longer timeout per attempt
5959+ for i in {1..120}; do
6060+ if grep -q "Bootstrapped 100%" "$TOR_DIR/tor.log" 2>/dev/null; then
6161+ # Success - exit both loops
6262+ break 2
6363+ fi
6464+ # Check every 0.1s for first 2 seconds, then every 0.5s
6565+ if [ "$i" -le 20 ]; then
6666+ sleep 0.1
6767+ else
6868+ sleep 0.5
6969+ fi
7070+ done
7171+7272+ # This attempt failed, continue to next attempt
6173done
62747575+# Final check - if still not ready after all attempts, exit
6376if ! grep -q "Bootstrapped 100%" "$TOR_DIR/tor.log" 2>/dev/null; then
6464- echo "Error: Tor failed to establish circuits" >&2
6577 exit 1
6678fi
6779