Skip to content

Instantly share code, notes, and snippets.

@andriitishchenko
Forked from refo/macOS proxy settings from terminal.md
Last active December 12, 2022 21:37
Show Gist options
  • Select an option

  • Save andriitishchenko/2c00435eb9c355fb41bf968deca006a1 to your computer and use it in GitHub Desktop.

Select an option

Save andriitishchenko/2c00435eb9c355fb41bf968deca006a1 to your computer and use it in GitHub Desktop.

Revisions

  1. andriitishchenko revised this gist Dec 12, 2022. 1 changed file with 112 additions and 0 deletions.
    112 changes: 112 additions & 0 deletions .proxy_helper
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,112 @@

    # Macos Proxy Helper (networksetup wraper)
    # add this line to $HOME/.zshrc
    #
    # source "$HOME/.proxy_helper"
    #

    # usage :
    # proxy < | up | down > [-all || -http|-https|-socks|-ftp|-stream ]
    # proxy set <destination> <port> [-http|-https|-socks|-ftp|-stream || -all]'

    if [[ "$(uname -sr)" =~ "Darwin*" ]] ;then

    proxy_read_otps(){
    proxies=()
    # Additional options:
    # [-getgopherproxy networkservice]
    # [-setgopherproxy networkservice domain portnumber authenticated username password]
    # [-setgopherproxystate networkservice on | off]
    # [-getproxybypassdomains networkservice]
    # [-setproxybypassdomains networkservice domain1 [domain2] [...]]
    # [-getpassiveftp networkservice]

    while [[ $# -gt 0 ]]; do
    key="$1"
    case "$key" in
    -https)
    proxies+=("-setsecurewebproxy")
    ;;
    -http)
    proxies+=("-setwebproxy")
    ;;
    -socks)
    proxies+=("-setsocksfirewallproxy")
    ;;
    -ftp)
    proxies+=("-setftpproxy")
    ;;
    -stream)
    proxies+=("-setstreamingproxy")
    ;;
    -all)
    proxies=("-setsecurewebproxy" "-setwebproxy" "-setsocksfirewallproxy" "-setftpproxy" "-setstreamingproxy")
    break
    ;;
    esac
    shift
    done
    echo "${proxies[@]}"
    }

    proxy_set_fn(){
    if [ $# -le "3" ]; then
    echo '** Error: expected arguments: <service_name> <destination> <port> [-http|-https|-socks|-ftp|-stream || -all]' >&2
    return 1
    fi

    service="${1}"
    destination="${2}"
    port="${3}"

    proxies=( $( proxy_read_otps "$@" ) )

    if [ "${#proxies[@]}" -gt 0 ]; then
    for proxy in "${proxies[@]}"; do
    networksetup "$proxy" "$service" "$destination" "$port"
    sleep 1
    done
    # activate
    original_args=("$@") #remove first 3 args
    proxy_service_fn up "${original_args[@]:3}"
    else
    echo '** Error: no proxy type' >&2
    return 1
    fi
    }

    proxy_service_fn(){
    st=""
    service="$(networksetup -listnetworkserviceorder | grep -B1 "$(route get default | awk '/interface/ {print $2}')" | awk -F'\\) ' '/\([0-9]+\)/ {print $2}')"
    if [ $# -le "0" ]; then
    proxies=( $( proxy_read_otps "-all" ) )
    else
    proxies=( $( proxy_read_otps "$@" ) )
    fi
    if [ "up" = "${1}" ]; then
    st="on"
    elif [ "down" = "${1}" ]; then
    st="off"
    elif [ "set" = "${1}" ]; then
    args=("$@") # remove "set"
    proxy_set_fn "$service" "${args[@]:1}"
    return
    fi
    if [ "${st}" ]; then
    if [ "${#proxies[@]}" -gt 0 ]; then
    for proxy in "${proxies[@]}"; do
    networksetup "${proxy}state" "$service" "$st"
    done
    else
    echo '** Error: no proxy type' >&2
    return 1
    fi
    fi

    for proxy in "${proxies[@]}"; do
    echo "* $(echo "$proxy" | sed -E 's/(-set|proxy)//g') $( networksetup $(echo "$proxy" | sed s/set/get/) "$service" | head -n 1 )"
    done
    }

    alias proxy="proxy_service_fn"
    fi
  2. @refo refo revised this gist Feb 16, 2020. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions proxy.sh
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,7 @@ networksetup -setsocksfirewallproxystate Wi-Fi on
    ssh -D 7070 -NCq [email protected]

    # proxy stays open until termination
    echo ""
    echo "Disabling proxy"

    # disable macOS socks proxy upon closing the connection
  3. @refo refo revised this gist Feb 16, 2020. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions proxy.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    echo "Enabling proxy"

    # Set macOS socks proxy to local proxy
    networksetup -setsocksfirewallproxy Wi-Fi 127.0.0.1 7070
    # enable macOS socks proxy
    networksetup -setsocksfirewallproxystate Wi-Fi on
    # create a local socks proxy to remote host
    ssh -D 7070 -NCq [email protected]

    # proxy stays open until termination
    echo "Disabling proxy"

    # disable macOS socks proxy upon closing the connection
    networksetup -setsocksfirewallproxystate Wi-Fi off
  4. @refo refo renamed this gist Feb 16, 2020. 1 changed file with 0 additions and 0 deletions.
  5. @refo refo revised this gist Feb 9, 2020. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions macOS firewall settings from terminal.md
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,11 @@
    ## macOS firewall settings

    ### Set socks proxy
    ```bash
    networksetup -setsocksfirewallproxy Wi-Fi 127.0.0.1 7070
    ```

    ### Get current socks proxy settings and state
    ```bash
    networksetup -getsocksfirewallproxy Wi-Fi
    ```
    @@ -15,6 +17,7 @@ Port: 7070
    Authenticated Proxy Enabled: 0
    ```

    ### Set socks proxy state (enable/disable)
    ```bash
    networksetup -setsocksfirewallproxystate Wi-Fi on
    networksetup -setsocksfirewallproxystate Wi-Fi off
  6. @refo refo created this gist Feb 9, 2020.
    21 changes: 21 additions & 0 deletions macOS firewall settings from terminal.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    ## macOS firewall settings

    ```bash
    networksetup -setsocksfirewallproxy Wi-Fi 127.0.0.1 7070
    ```

    ```bash
    networksetup -getsocksfirewallproxy Wi-Fi
    ```

    ```
    Enabled: Yes
    Server: 127.0.0.1
    Port: 7070
    Authenticated Proxy Enabled: 0
    ```

    ```bash
    networksetup -setsocksfirewallproxystate Wi-Fi on
    networksetup -setsocksfirewallproxystate Wi-Fi off
    ```