Skip to content

Instantly share code, notes, and snippets.

@cmdruid
Created April 18, 2022 07:05
Show Gist options
  • Save cmdruid/2ad71f21c440de3e352013f36ca69f3e to your computer and use it in GitHub Desktop.
Save cmdruid/2ad71f21c440de3e352013f36ca69f3e to your computer and use it in GitHub Desktop.
Forward traffic from a local port to an onion address.
#!/bin/sh
## Bind a local port and forward its traffic to an onion address.
## Requires socat to be installed, and a tor proxy to be running.
###############################################################################
# Environment
###############################################################################
SOCKS_HOST="127.0.0.1"
SOCKS_PORT="9050"
LOG_PATH="/var/log/onionport"
###############################################################################
# Methods
###############################################################################
usage() {
printf "
Bind a local port and forward its traffic to an onion address.
Requires socat to be installed, and a tor proxy to be running.
Usage:
$0 localport onionaddress.onion:port
(start a new forwarding service)
$0 localport stop
(stop an existing service)
Arguments:
localport - The local port that you will be sending traffic to (i.e 127.0.0.1:port).
onionaddress - The onion address (and port) destination to forward traffic.
Default tor proxy configuration is set to $SOCKS_HOST:$SOCKS_PORT.
This must be changed in the script file to match your tor configuration.
You can bring up this help screen by using '$0 --help'
\n"
}
kill_existing_pid() {
EXIST_PID="$(ps aux | grep socat | grep tcp-listen:$1 | awk '{print $2}')"
if [ ! -z $EXIST_PID ]; then
kill -9 $EXIST_PID
echo "Killed existing process for port $1 at pid $EXIST_PID."
else
echo "No existing onionport detected on port $1."
fi
}
###############################################################################
# Script
###############################################################################
set -e
## Check if socat is installed.
if [ -z "$(which socat)" ]; then
echo "Socat is not detected! Make sure socat is installed and visible in your PATH."
fi
## Check if help has been invoked.
if [ "$1" = "--help" ]; then usage && exit 0; fi
## Check if a valid number has been given.
if [ -z "$1" ] || [ -z "$(echo $1 | grep -P '^[0-9]+$')" ]; then
echo "Invalid port number!" && usage && exit 1
fi
## Check if a valid address or option has been given.
if [ "$2" = "stop" ]; then
kill_existing_pid $1 && exit 0
elif [ -z "$2" ] || [ -z "$(echo $2 | grep .onion)" ]; then
echo "Invalid onion address!" && usage && exit 1
fi
## If log path does not exist, create it.
if [ ! -d "$LOG_PATH" ]; then mkdir -p "$LOG_PATH"; fi
## Check if an onionport is already in use.
kill_existing_pid $1
## Start the socat program as a daemon service.
socat tcp-listen:$1,reuseaddr,fork \
socks4a:$SOCKS_HOST:$2,socksport=$SOCKS_PORT > $LOG_PATH/$1.log &
printf "Socat tunnel with pid $! created on port $1 and redirecting to:\nhttp://$2\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment