Skip to content

Instantly share code, notes, and snippets.

@nauhygon
Last active April 11, 2021 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nauhygon/f87ebf4e1d74691c7730bab4edeedd92 to your computer and use it in GitHub Desktop.
Save nauhygon/f87ebf4e1d74691c7730bab4edeedd92 to your computer and use it in GitHub Desktop.
# Func: Start an SSH tunnel (autossh) to a server $1 and map it to a
# local port $2 (default to 8888), which can be used as a SOCK. Start
# a HTTP proxy server (polipo) with proxy port $3 (default to 8123)
# and point it to the SSH tunnel.
#
# See also:
# autossh (http://www.harding.motd.ca/autossh/)
# polipo (https://www.irif.univ-paris-diderot.fr/~jch/software/polipo/)
#
# Usages:
# stun john@awesome.org
# stun john@awesome.org 8080 7777
#
# Note:
# You must make sure you can ssh to the remote host passwordlessly
# first. To make this happen, simply do this on your local host.
# ssh-keygen # No passphrase
# ssh-copy-id john@awesome.org
# And now you should ssh to the host without being asked to enter a
# password.
# ssh john@awesome.org
#
function stun() {
if [ -z $1 ]; then
echo "${FUNCNAME}() requires at least one parameter, `server`."
fi
server=$1
tunnel_port=$2
if [ -z $tunnel_port ]; then
tunnel_port=8888
fi
proxy_port=$3
if [ -z $proxy_port ]; then
proxy_port=8123
fi
# Restart ssh & autossh
killall -q ssh
killall -q autossh
#ssh -o TCPKeepAlive=yes -o ServerAliveInterval=50 -f -C -D 127.0.0.1:${tunnel_port} ${server} -N
autossh -M 0 -f -o TCPKeepAlive=yes -o ServerAliveInterval=50 -f -C -D 127.0.0.1:${tunnel_port} ${server} -N
# Restart polipo
killall -q polipo
polipo daemonise=true socksParentProxy=127.0.0.1:${tunnel_port} proxyPort=${proxy_port}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment