Last active
February 12, 2023 15:11
-
-
Save programminghoch10/c0f3414032682339181046b1334ffb20 to your computer and use it in GitHub Desktop.
Multi-Core Torsocks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# TorSocksMultiCore | |
# a wrapper for torsocks for multicore tor execution | |
# | |
# This script starts a new tor daemon everytime it is invoked. | |
# This way every command or shell will have it's own tor daemon. | |
# The usage is functionally equivalent to torsocks itself. | |
# | |
# whether to wait for tor start when sourced | |
# true should be the more reliable choice here | |
TSMC_WAIT_WHEN_SOURCED=true | |
(return 0 2>/dev/null) && TSMC_SOURCED=true || TSMC_SOURCED=false | |
for cmd in tor torsocks ss grep shuf nc; do | |
[ -z "$(command -v $cmd)" ] && echo "$cmd is missing!" >&2 && { $TSMC_SOURCED && return 1 || exit 1; } | |
done | |
tsmc_log() { | |
echo "$@" >&2 | |
} | |
tsmc_log_debug() { :; } | |
tsmc_log_info() { tsmc_log "$@"; } | |
tsmc_ensure_script_sourced() { | |
$TSMC_SOURCED || { | |
tsmc_log "This script must be sourced for this command to work!" | |
exit 1 | |
} | |
} | |
TSMC_SOURCE_TORSOCKS=false | |
for arg in "$@"; do | |
case "$arg" in | |
"--port" | "-P") | |
tsmc_log "You're not allowed to set the torsocks port with this script." | |
$TSMC_SOURCED && return 1 || exit 1 | |
;; | |
"off") | |
TSMC_SOURCE_TORSOCKS=true | |
tsmc_ensure_script_sourced | |
source torsocks off | |
tsmc_stop_tor_daemon | |
return | |
;; | |
"on") | |
TSMC_SOURCE_TORSOCKS=true | |
tsmc_ensure_script_sourced | |
;; | |
"-d" | "--debug" | "-v" | "--verbose") | |
tsmc_log_debug() { | |
tsmc_log "$@" | |
} | |
;; | |
"-q" | "--quiet") | |
tsmc_log_info() { :; } | |
;; | |
"--") | |
break | |
;; | |
esac | |
done | |
$TSMC_SOURCED && ! $TSMC_SOURCE_TORSOCKS && tsmc_log "Don't source this script for command calls!" && return 1 | |
[ -n "$TSMC_TOR_PID" ] && tsmc_stop_tor_daemon | |
# find a free local port | |
read TSMC_LOWERPORT TSMC_UPPERPORT < /proc/sys/net/ipv4/ip_local_port_range | |
[ -z "$TSMC_LOWERPORT" ] || [ -z "$TSMC_UPPERPORT" ] && { | |
tsmc_log_debug "Failed to find ports, lower=$TSMC_LOWERPORT upper=$TSMC_UPPERPORT" | |
TSMC_LOWERPORT=32768 | |
TSMC_UPPERPORT=60999 | |
} | |
while true; do | |
TSMC_TOR_PORT=$(shuf -i $TSMC_LOWERPORT-$TSMC_UPPERPORT -n 1) | |
ss -lpn | grep -q "$TSMC_TOR_PORT" || break | |
done | |
unset -v TSMC_LOWERPORT TSMC_UPPERPORT | |
# start the tor daemon | |
TSMC_TOR_DATA_DIR=~/.tor-$TSMC_TOR_PORT | |
mkdir "$TSMC_TOR_DATA_DIR" || { $TSMC_SOURCED && return 1 || exit 1; } | |
LD_PRELOAD="" tor --SOCKSPort "$TSMC_TOR_PORT" --DataDirectory "$TSMC_TOR_DATA_DIR" --RunAsDaemon 0 --User "" --quiet &>/dev/null & | |
TSMC_TOR_PID=$! | |
disown $TSMC_TOR_PID | |
$TSMC_SOURCED && tsmc_log_info "Started seperate Tor Daemon $TSMC_TOR_PID on port $TSMC_TOR_PORT with data directory $TSMC_TOR_DATA_DIR" | |
tsmc_await_tor_started() { | |
tsmc_log_debug "Waiting for Tor $TSMC_TOR_PID on port $TSMC_TOR_PORT to start up..." | |
LD_PRELOAD="" | |
while ! nc -z 127.0.0.1 $TSMC_TOR_PORT; do sleep 1; done | |
tsmc_log_debug "Tor port $TSMC_TOR_PORT is open!" | |
} | |
# setup kill trap to stop tor when shell exits | |
tsmc_stop_tor_daemon() { | |
[ -z "$TSMC_TOR_PID" ] && tsmc_log_debug "Tor daemon already stopped." && return | |
tsmc_log_debug "Stopping tor daemon $TSMC_TOR_PID" | |
kill $TSMC_TOR_PID | |
rm -r "$TSMC_TOR_DATA_DIR" | |
unset -v TSMC_TOR_PID TSMC_TOR_PORT TSMC_TOR_DATA_DIR | |
} | |
trap tsmc_stop_tor_daemon EXIT | |
if $TSMC_SOURCE_TORSOCKS; then | |
$TSMC_WAIT_WHEN_SOURCED && tsmc_await_tor_started | |
tsmc_log_debug "Sourcing torsocks" | |
# source torsocks with new port | |
source torsocks --port $TSMC_TOR_PORT "$@" | |
else | |
tsmc_await_tor_started | |
tsmc_log_debug "Calling torsocks" | |
# call subcommand with new port | |
torsocks --port $TSMC_TOR_PORT "$@" | |
fi | |
unset -v TSMC_SOURCED |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Reserved