11#! /usr/bin/env bash
22set -euo pipefail
3- set -x
43
54# Create temporary directory for this tor instance
65TOR_DIR=$( mktemp -d -t tornade.XXXXXX)
7- trap ' rm -rf "$TOR_DIR"' EXIT
86
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 ))
119CONTROL_PORT=$(( SOCKS_PORT + 1 ))
1210
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
23+
1324# Create tor configuration
1425cat > " $TOR_DIR /torrc" << EOF
1526DataDirectory $TOR_DIR /data
@@ -18,66 +29,30 @@ ControlPort 127.0.0.1:$CONTROL_PORT
1829Log notice file $TOR_DIR /tor.log
1930RunAsDaemon 0
2031
21- # Performance and reliability optimizations
22- CircuitBuildTimeout 15
32+ # Hidden service optimizations - longer timeouts for .onion addresses
33+ CircuitBuildTimeout 60
2334LearnCircuitBuildTimeout 0
24- NumEntryGuards 8
25- KeepalivePeriod 60
26- SocksTimeout 120
35+ SocksTimeout 300
2736
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
3240EOF
3341
3442# Start tor in background - suppress output to avoid protocol interference
3543tor -f " $TOR_DIR /torrc" > /dev/null 2>&1 &
3644TOR_PID=$!
3745
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
4350 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
5754 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
8156done
8257
8358# Final check - if still not ready after all attempts, exit
0 commit comments