You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
217 lines
5.6 KiB
217 lines
5.6 KiB
#!/bin/bash |
|
# tor-dev-setup.sh - Development Tor hidden service setup for ORLY relay |
|
# |
|
# This script sets up a user-space Tor hidden service for local development. |
|
# No root privileges required (except for initial Tor installation). |
|
# |
|
# Usage: ./scripts/tor-dev-setup.sh [port] |
|
# port: internal port ORLY listens on for Tor traffic (default: 3336) |
|
# |
|
# After running this script: |
|
# 1. Start ORLY with: ORLY_TOR_ENABLED=true ORLY_TOR_HS_DIR=~/.tor/orly-dev ./orly |
|
# 2. Connect via Tor Browser to the .onion address |
|
|
|
set -e |
|
|
|
# Configuration |
|
TOR_PORT="${1:-3336}" |
|
TOR_DATA_DIR="${HOME}/.tor/orly-dev" |
|
TOR_CONFIG="${TOR_DATA_DIR}/torrc" |
|
TOR_PID_FILE="${TOR_DATA_DIR}/tor.pid" |
|
|
|
# Colors for output |
|
RED='\033[0;31m' |
|
GREEN='\033[0;32m' |
|
YELLOW='\033[1;33m' |
|
BLUE='\033[0;34m' |
|
NC='\033[0m' # No Color |
|
|
|
info() { echo -e "${GREEN}[INFO]${NC} $1"; } |
|
warn() { echo -e "${YELLOW}[WARN]${NC} $1"; } |
|
error() { echo -e "${RED}[ERROR]${NC} $1"; exit 1; } |
|
debug() { echo -e "${BLUE}[DEBUG]${NC} $1"; } |
|
|
|
# Check if Tor is installed |
|
check_tor() { |
|
if ! command -v tor &> /dev/null; then |
|
error "Tor is not installed. Please install it first: |
|
Debian/Ubuntu: sudo apt install tor |
|
Arch: sudo pacman -S tor |
|
macOS: brew install tor |
|
Fedora: sudo dnf install tor" |
|
fi |
|
info "Tor is installed: $(tor --version | head -1)" |
|
} |
|
|
|
# Create directory structure |
|
setup_dirs() { |
|
info "Creating directory structure..." |
|
|
|
mkdir -p "${TOR_DATA_DIR}" |
|
mkdir -p "${TOR_DATA_DIR}/hidden_service" |
|
chmod 700 "${TOR_DATA_DIR}" |
|
chmod 700 "${TOR_DATA_DIR}/hidden_service" |
|
|
|
info "Directory created: ${TOR_DATA_DIR}" |
|
} |
|
|
|
# Create Tor configuration |
|
create_config() { |
|
info "Creating Tor configuration..." |
|
|
|
cat > "$TOR_CONFIG" << EOF |
|
# ORLY Development Tor Configuration |
|
# Generated by tor-dev-setup.sh on $(date) |
|
|
|
# Data directory |
|
DataDirectory ${TOR_DATA_DIR}/data |
|
|
|
# Run in background |
|
RunAsDaemon 1 |
|
PidFile ${TOR_PID_FILE} |
|
|
|
# SOCKS proxy for outgoing connections (optional, for testing) |
|
SocksPort 9150 |
|
|
|
# Hidden service for ORLY relay |
|
HiddenServiceDir ${TOR_DATA_DIR}/hidden_service/ |
|
HiddenServicePort 80 127.0.0.1:${TOR_PORT} |
|
|
|
# Logging |
|
Log notice file ${TOR_DATA_DIR}/tor.log |
|
EOF |
|
|
|
chmod 600 "$TOR_CONFIG" |
|
info "Configuration created: ${TOR_CONFIG}" |
|
} |
|
|
|
# Stop existing Tor instance |
|
stop_tor() { |
|
if [ -f "$TOR_PID_FILE" ]; then |
|
PID=$(cat "$TOR_PID_FILE" 2>/dev/null) |
|
if [ -n "$PID" ] && kill -0 "$PID" 2>/dev/null; then |
|
info "Stopping existing Tor instance (PID: $PID)..." |
|
kill "$PID" 2>/dev/null || true |
|
sleep 2 |
|
fi |
|
rm -f "$TOR_PID_FILE" |
|
fi |
|
} |
|
|
|
# Start Tor |
|
start_tor() { |
|
info "Starting Tor..." |
|
|
|
# Ensure data directory exists |
|
mkdir -p "${TOR_DATA_DIR}/data" |
|
|
|
# Start Tor with our config |
|
tor -f "$TOR_CONFIG" 2>&1 | head -20 & |
|
|
|
# Wait for Tor to bootstrap |
|
info "Waiting for Tor to connect to the network..." |
|
|
|
for i in {1..60}; do |
|
if [ -f "${TOR_DATA_DIR}/hidden_service/hostname" ]; then |
|
ONION_ADDR=$(cat "${TOR_DATA_DIR}/hidden_service/hostname") |
|
if [ -n "$ONION_ADDR" ]; then |
|
break |
|
fi |
|
fi |
|
|
|
# Check if Tor is still running |
|
if [ -f "$TOR_PID_FILE" ]; then |
|
PID=$(cat "$TOR_PID_FILE") |
|
if ! kill -0 "$PID" 2>/dev/null; then |
|
error "Tor process died. Check ${TOR_DATA_DIR}/tor.log" |
|
fi |
|
fi |
|
|
|
sleep 1 |
|
echo -n "." |
|
done |
|
echo "" |
|
|
|
if [ -f "${TOR_DATA_DIR}/hidden_service/hostname" ]; then |
|
ONION_ADDR=$(cat "${TOR_DATA_DIR}/hidden_service/hostname") |
|
info "Tor started successfully" |
|
echo "" |
|
echo -e "${GREEN}======================================${NC}" |
|
echo -e "${GREEN}Hidden Service Address:${NC}" |
|
echo -e "${YELLOW}${ONION_ADDR}${NC}" |
|
echo -e "${GREEN}======================================${NC}" |
|
echo "" |
|
else |
|
warn "Tor started but hidden service not ready yet" |
|
warn "Check: tail -f ${TOR_DATA_DIR}/tor.log" |
|
fi |
|
} |
|
|
|
# Print usage instructions |
|
print_instructions() { |
|
echo "" |
|
info "Development Tor setup complete!" |
|
echo "" |
|
echo " To start ORLY with Tor:" |
|
echo -e " ${BLUE}ORLY_TOR_ENABLED=true ORLY_TOR_HS_DIR=${TOR_DATA_DIR}/hidden_service ./orly${NC}" |
|
echo "" |
|
echo " To view the .onion address:" |
|
echo -e " ${BLUE}cat ${TOR_DATA_DIR}/hidden_service/hostname${NC}" |
|
echo "" |
|
echo " To view Tor logs:" |
|
echo -e " ${BLUE}tail -f ${TOR_DATA_DIR}/tor.log${NC}" |
|
echo "" |
|
echo " To stop Tor:" |
|
echo -e " ${BLUE}kill \$(cat ${TOR_PID_FILE})${NC}" |
|
echo "" |
|
echo " To restart Tor:" |
|
echo -e " ${BLUE}./scripts/tor-dev-setup.sh${NC}" |
|
echo "" |
|
} |
|
|
|
# Status command |
|
status() { |
|
if [ -f "$TOR_PID_FILE" ]; then |
|
PID=$(cat "$TOR_PID_FILE") |
|
if kill -0 "$PID" 2>/dev/null; then |
|
info "Tor is running (PID: $PID)" |
|
if [ -f "${TOR_DATA_DIR}/hidden_service/hostname" ]; then |
|
ONION_ADDR=$(cat "${TOR_DATA_DIR}/hidden_service/hostname") |
|
echo -e " Address: ${YELLOW}${ONION_ADDR}${NC}" |
|
fi |
|
return 0 |
|
fi |
|
fi |
|
warn "Tor is not running" |
|
return 1 |
|
} |
|
|
|
# Main |
|
main() { |
|
case "${1:-}" in |
|
status) |
|
status |
|
exit $? |
|
;; |
|
stop) |
|
stop_tor |
|
info "Tor stopped" |
|
exit 0 |
|
;; |
|
*) |
|
;; |
|
esac |
|
|
|
info "ORLY Development Tor Setup" |
|
info "Internal port: ${TOR_PORT}" |
|
echo "" |
|
|
|
check_tor |
|
setup_dirs |
|
stop_tor |
|
create_config |
|
start_tor |
|
print_instructions |
|
} |
|
|
|
main "$@"
|
|
|