Skip to content

Instantly share code, notes, and snippets.

@fharbe
Forked from ogawa/socks-proxy.sh
Created March 11, 2014 19:18
Show Gist options
  • Save fharbe/9493000 to your computer and use it in GitHub Desktop.
Save fharbe/9493000 to your computer and use it in GitHub Desktop.

Revisions

  1. @ogawa ogawa revised this gist Nov 28, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion socks-proxy.sh
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@
    SSH_HOST=proxy-host
    SOCKS_PROXY_HOST=localhost
    SOCKS_PROXY_PORT=1080
    NW_SERVICE=AirPort # /usr/sbin/networksetup -listallnetworkservices
    NW_SERVICE=Wi-Fi # /usr/sbin/networksetup -listallnetworkservices

    # DO NOT CHANGE
    NWSETUP_CMD="/usr/sbin/networksetup"
  2. @ogawa ogawa revised this gist Aug 31, 2011. 1 changed file with 17 additions and 9 deletions.
    26 changes: 17 additions & 9 deletions socks-proxy.sh
    Original file line number Diff line number Diff line change
    @@ -7,30 +7,32 @@
    # Host proxy-host
    # HostName proxy-host.example.com
    # User a12345
    # DynamicForward 1080
    # DynamicForward localhost:1080
    #
    # Step 3. Configure SSH_HOST and NW_SERVICE variables:
    # Step 3. Configure SSH_HOST, SOCKS_PROXY_PORT, and NW_SERVICE variables.
    #
    SSH_HOST=proxy-host
    SOCKS_PROXY_HOST=localhost
    SOCKS_PROXY_PORT=1080
    NW_SERVICE=AirPort # /usr/sbin/networksetup -listallnetworkservices

    # DO NOT CHANGE
    NWSETUP_CMD="/usr/sbin/networksetup"
    SSH_CMD="/usr/bin/ssh -Nf $SSH_HOST"

    SOCKS_PROXY=
    SOCKS_PROXY_ENABLED=
    if $NWSETUP_CMD -getsocksfirewallproxy $NW_SERVICE | grep -q "^Enabled: Yes"; then
    SOCKS_PROXY=1
    SOCKS_PROXY_ENABLED=1
    fi
    SSH_PID=`pgrep -f "$SSH_CMD"`

    enable_socks_proxy() {
    echo Enable SOCKS Firewall Proxy.
    $NWSETUP_CMD -setsocksfirewallproxystate $NW_SERVICE on
    $NWSETUP_CMD -setsocksfirewallproxy $NW_SERVICE $SOCKS_PROXY_HOST $SOCKS_PROXY_PORT off
    }

    disable_socks_proxy() {
    if [ $SOCKS_PROXY ]; then
    if [ $SOCKS_PROXY_ENABLED ]; then
    echo Disable SOCKS Firewall Proxy.
    $NWSETUP_CMD -setsocksfirewallproxystate $NW_SERVICE off
    fi
    @@ -71,21 +73,27 @@ case $1 in
    else
    echo SSH Connection to $SSH_HOST is dead.
    fi
    if [ $SOCKS_PROXY ]; then
    if [ $SOCKS_PROXY_ENABLED ]; then
    echo SOCKS Firewall Proxy is on.
    else
    echo SOCKS Firewall Proxy is off.
    fi
    ;;
    on)
    enable_socks_proxy
    ;;
    off)
    disable_socks_proxy
    ;;
    toggle)
    if [ $SOCKS_PROXY ]; then
    if [ $SOCKS_PROXY_ENABLED ]; then
    disable_socks_proxy
    else
    enable_socks_proxy
    fi
    ;;
    *)
    echo "Usage: $0 {start|stop|status|toggle|help}"
    echo "Usage: $0 {start|stop|status|on|off|toggle|help}"
    exit 1
    ;;
    esac
  3. @ogawa ogawa revised this gist Aug 1, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion socks-proxy.sh
    Original file line number Diff line number Diff line change
    @@ -38,7 +38,7 @@ disable_socks_proxy() {

    create_ssh_connection() {
    echo Create SSH Connection to $SSH_HOST.
    $SSH_CMD
    $SSH_CMD &> /dev/null
    }

    destroy_ssh_connection() {
  4. @ogawa ogawa created this gist Aug 1, 2011.
    93 changes: 93 additions & 0 deletions socks-proxy.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,93 @@
    #!/bin/sh
    #
    # Step 1. Install proctools to use "pgrep" command.
    #
    # Step 2. Configure your .ssh/config to include:
    #
    # Host proxy-host
    # HostName proxy-host.example.com
    # User a12345
    # DynamicForward 1080
    #
    # Step 3. Configure SSH_HOST and NW_SERVICE variables:
    #
    SSH_HOST=proxy-host
    NW_SERVICE=AirPort # /usr/sbin/networksetup -listallnetworkservices

    # DO NOT CHANGE
    NWSETUP_CMD="/usr/sbin/networksetup"
    SSH_CMD="/usr/bin/ssh -Nf $SSH_HOST"

    SOCKS_PROXY=
    if $NWSETUP_CMD -getsocksfirewallproxy $NW_SERVICE | grep -q "^Enabled: Yes"; then
    SOCKS_PROXY=1
    fi
    SSH_PID=`pgrep -f "$SSH_CMD"`

    enable_socks_proxy() {
    echo Enable SOCKS Firewall Proxy.
    $NWSETUP_CMD -setsocksfirewallproxystate $NW_SERVICE on
    }

    disable_socks_proxy() {
    if [ $SOCKS_PROXY ]; then
    echo Disable SOCKS Firewall Proxy.
    $NWSETUP_CMD -setsocksfirewallproxystate $NW_SERVICE off
    fi
    }

    create_ssh_connection() {
    echo Create SSH Connection to $SSH_HOST.
    $SSH_CMD
    }

    destroy_ssh_connection() {
    if [ $SSH_PID ]; then
    echo Destroy SSH Connection to $SSH_HOST.
    kill -TERM $SSH_PID
    fi
    }

    case $1 in
    start)
    destroy_ssh_connection
    create_ssh_connection
    enable_socks_proxy
    ;;
    stop)
    disable_socks_proxy
    destroy_ssh_connection
    ;;
    pid)
    if [ $SSH_PID ]; then
    echo $SSH_PID
    else
    echo SSH Connection to $SSH_HOST is dead.
    fi
    ;;
    status)
    if [ $SSH_PID ]; then
    echo SSH Connection to $SSH_HOST is alive \(PID:$SSH_PID\).
    else
    echo SSH Connection to $SSH_HOST is dead.
    fi
    if [ $SOCKS_PROXY ]; then
    echo SOCKS Firewall Proxy is on.
    else
    echo SOCKS Firewall Proxy is off.
    fi
    ;;
    toggle)
    if [ $SOCKS_PROXY ]; then
    disable_socks_proxy
    else
    enable_socks_proxy
    fi
    ;;
    *)
    echo "Usage: $0 {start|stop|status|toggle|help}"
    exit 1
    ;;
    esac

    exit 0