Last active
September 29, 2021 12:23
-
-
Save finagin/7a18e927c4faf4bd153d2383ead25f06 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env bash | |
SUDO='' | |
if [ "$(id -u)" != "0" ]; then | |
SUDO='sudo' | |
echo "This script requires superuser access." | |
echo "You will be prompted for your password by sudo." | |
# clear any previous sudo permission | |
sudo -k | |
fi | |
function usage() { | |
echo "Usage: $0 -u <host_user> -h <host_ip> [-p <port>] [-i <identity_file>]"; | |
exit 1; | |
} | |
while getopts :u:h:p:i: flag; do | |
case "${flag}" in | |
u) username=${OPTARG} ;; | |
h) hostname=${OPTARG} ;; | |
p) port=${OPTARG} ;; | |
i) filename=${OPTARG} ;; | |
?) usage ;; | |
esac | |
done | |
if [[ -z $username ]] || [[ -z $hostname ]]; then | |
usage; | |
fi | |
if [[ -z $port ]]; then | |
port=3128; | |
fi | |
if [[ ! -z $filename ]]; then | |
if [[ $filename =~ ^[^\/~] ]]; then | |
filename="~/.ssh/$filename"; | |
fi | |
filename="-i $filename"; | |
fi | |
list=($(networksetup -listnetworkserviceorder \ | |
| grep -B1 'Hardware Port: Wi-Fi' \ | |
| sed -n -e 's/([0-9]\{1,\}) \(.*\)/\1/p' \ | |
)); | |
disable_proxy() | |
{ | |
if [[ -z $disabled ]]; then | |
for i in "${list[@]}"; do | |
networksetup -setsocksfirewallproxystate "${i}" off; | |
done | |
echo "\n\nSOCKS proxy disabled."; | |
disabled=true; | |
fi; | |
exit; | |
} | |
trap disable_proxy INT QUIT EXIT KILL; | |
for i in "${list[@]}"; do | |
networksetup -setsocksfirewallproxy "${i}" localhost ${port}; | |
${SUDO} networksetup -setsocksfirewallproxystate "${i}" on; | |
done | |
echo "SOCKS proxy enabled."; | |
echo "Tunneling..."; | |
ssh -ND $port "$username@$hostname" $filename; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment