Skip to content

Instantly share code, notes, and snippets.

@louneskmt
Created February 24, 2021 10:31
Show Gist options
  • Save louneskmt/ad90093eaab6e7861543c51db0342729 to your computer and use it in GitHub Desktop.
Save louneskmt/ad90093eaab6e7861543c51db0342729 to your computer and use it in GitHub Desktop.
Script to install a socat socks proxy service on Linux. Based on systemd.
#!/bin/bash -e
if [ "$EUID" -ne 0 ]
then echo "Please run as root"
exit
fi
check_dependencies () {
for cmd in "$@"; do
if ! command -v $cmd >/dev/null 2>&1; then
echo "This script requires \"${cmd}\" to be installed"
exit 1
fi
done
}
check_dependencies tor socat
usage="Usage: $(basename "$0") [options...] <ONION_URL | ONION_URL:PORT>
Options:
-h, --help show this help text
-p, --port port to bind to localhost
-n, --name proxy name
--socks-port socks proxy port
Example: $(basename "$0") --name umbrel-bitcoin-rpc --port 1000 blablabla.onion:8332"
while [[ "$#" -gt 0 ]]; do
case $1 in
-h|--help)
echo "$usage"
exit 0
;;
-p|--port) port="$2"; shift ;;
-n|--name) name="$2"; shift ;;
--socks-port) socksport="$2"; shift ;;
${@: -1}) url="$1" ;;
*) echo "Unknown parameter passed: $1"; exit 1 ;;
esac
shift
done
if [ -z ${port+x} ] || [ -z ${name+x} ] || [ -z ${url+x} ];
then
echo "$usage"
exit 0
fi;
if [[ "$url" == *".onion" ]];
then
url="$url:80"
fi;
if [[ ! "$url" == *".onion:"* ]];
then
echo "Please use a valid onion hostname."
echo
echo "$usage"
exit 0
fi;
echo "
# Installed at /etc/systemd/system/socat.$name.service
[Unit]
Description=Socat Proxy - $name
Requires=tor.service
After=tor.service
Wants=network-online.target
After=network-online.target
[Service]
Type=exec
ExecStart=/bin/socat TCP4-LISTEN:$port,reuseaddr,fork SOCKS4A:127.0.0.1:$url,socksport=${socksport:-9050}
Restart=always
RestartSec=10
RemainAfterExit=yes
User=root
Group=root
[Install]
WantedBy=multi-user.target
" > /etc/systemd/system/socat.$name.service
systemctl daemon-reload
systemctl enable socat.$name.service
# systemctl start socat.$name
echo "Service ready:"
echo $(sudo systemctl status socat.$name)
echo "-------------------------------------"
echo "You can now start and stop this service with:"
echo " sudo systemctl start socat.$name"
echo " sudo systemctl stop socat.$name"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment