Skip to content

Instantly share code, notes, and snippets.

@benfasoli
Last active April 6, 2021 21:57
Show Gist options
  • Save benfasoli/543c14c90a4909e0305180bd8423969e to your computer and use it in GitHub Desktop.
Save benfasoli/543c14c90a4909e0305180bd8423969e to your computer and use it in GitHub Desktop.
MacOS SOCKS tunnel
#!/bin/bash
# SOCKS proxy via ssh tunnel for MacOS
# Install to /usr/local/bin/tunnel with:
# curl https://gist.githubusercontent.com/benfasoli/543c14c90a4909e0305180bd8423969e/raw/ > /usr/local/bin/tunnel && chmod +x /usr/local/bin/tunnel
TARGET=$1
if [ -z "$TARGET" ]; then
echo -n "SSH target (e.g. name@server.com): "
read TARGET
fi
DEFAULT_PORT=8080
echo "Enter local port for outgoing connections"
echo "Leave this as is unless you have a need to change it"
echo "System ports (e.g. 80) requires running this script with the sudo command"
echo -n "Local proxy port [$DEFAULT_PORT]: "
read PORT
PORT=${PORT:-$DEFAULT_PORT}
echo "Opening a SOCKS tunnel to: $TARGET"
echo "Tunnel will be enabled after server password is entered..."
echo
echo "To end proxy session, ctrl-C to stop this script"
echo
function enable_proxy() {
networksetup -setsocksfirewallproxy Wi-Fi 0.0.0.0 $PORT
}
function disable_proxy() {
networksetup -setsocksfirewallproxy Wi-Fi "" ""
networksetup -setsocksfirewallproxystate Wi-Fi off
}
trap disable_proxy EXIT
enable_proxy
ssh -D *:$PORT -C -N $TARGET
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment