Created
January 12, 2026 17:34
-
-
Save lakpahana/53792bd3d8da1fa0bac98b00d0ac0ec3 to your computer and use it in GitHub Desktop.
uni proxy mac
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
| source proxy_zsh.zsh on | |
| source proxy_zsh.zsh off |
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 zsh | |
| # proxy_zsh.zsh - enable/disable corporate proxy across shell + common tools + macOS system proxy | |
| set -euo pipefail | |
| # ------------------------- | |
| # CONFIG | |
| # ------------------------- | |
| PROXY_URL="http://10.50.225.222:3128" | |
| PROXY_HOST="10.50.225.222" | |
| PROXY_PORT="3128" | |
| # If you also use SOCKS: | |
| SOCKS_HOST="$PROXY_HOST" | |
| SOCKS_PORT="$PROXY_PORT" | |
| ENABLE_SOCKS=0 # 1 to enable SOCKS when proxy_on | |
| # If you use PAC (proxy auto-config): | |
| PAC_URL="" # e.g. "http://proxy.corp.local/proxy.pac" | |
| ENABLE_PAC=0 # 1 to enable PAC when proxy_on (overrides HTTP/HTTPS/SOCKS) | |
| # Network service name (Wi-Fi is common). Can auto-detect if empty. | |
| NETWORK_SERVICE="Wi-Fi" | |
| # Flutter/Dart endpoints (default official) | |
| PUB_HOSTED_URL_DEFAULT="https://pub.dev" | |
| FLUTTER_STORAGE_BASE_URL_DEFAULT="https://storage.googleapis.com" | |
| # Gradle proxy | |
| GRADLE_PROPS_DIR="${HOME}/.gradle" | |
| GRADLE_PROPS_FILE="${GRADLE_PROPS_DIR}/gradle.properties" | |
| # Maven proxy | |
| M2_DIR="${HOME}/.m2" | |
| M2_SETTINGS_FILE="${M2_DIR}/settings.xml" | |
| # Managed markers | |
| GRADLE_START="# --- PROXY_ZSH MANAGED START ---" | |
| GRADLE_END="# --- PROXY_ZSH MANAGED END ---" | |
| MAVEN_START="<!-- PROXY_ZSH MANAGED START -->" | |
| MAVEN_END="<!-- PROXY_ZSH MANAGED END -->" | |
| # ------------------------- | |
| # Helpers | |
| # ------------------------- | |
| log() { print -r -- "$@"; } | |
| have() { command -v "$1" >/dev/null 2>&1; } | |
| # Best-effort: run command, if fails and sudo exists, retry with sudo | |
| run_or_sudo() { | |
| local cmd="$*" | |
| if eval "$cmd" >/dev/null 2>&1; then | |
| return 0 | |
| fi | |
| if have sudo; then | |
| sudo sh -c "$cmd" | |
| return $? | |
| fi | |
| return 1 | |
| } | |
| detect_network_service() { | |
| # Tries to find a Wi-Fi-like service name | |
| local svc="" | |
| if have networksetup; then | |
| # networksetup prints services, first line is a header sometimes | |
| # prefer exact "Wi-Fi", else pick first containing "Wi-Fi" | |
| svc="$(networksetup -listallnetworkservices 2>/dev/null | tail -n +2 | grep -x "Wi-Fi" || true)" | |
| if [[ -z "$svc" ]]; then | |
| svc="$(networksetup -listallnetworkservices 2>/dev/null | tail -n +2 | grep -i "wi[- ]*fi" | head -n 1 || true)" | |
| fi | |
| fi | |
| print -r -- "${svc:-Wi-Fi}" | |
| } | |
| remove_managed_block_file() { | |
| # $1 file, $2 start marker regex, $3 end marker regex (perl -0777) | |
| local file="$1" | |
| local start="$2" | |
| local end="$3" | |
| [[ -f "$file" ]] || return 0 | |
| perl -0777 -i -pe "s/\\n?${start}.*?${end}\\n?//sg" "$file" || true | |
| } | |
| # ------------------------- | |
| # macOS System Proxy | |
| # ------------------------- | |
| macos_proxy_on() { | |
| have networksetup || { log "ℹ️ networksetup not found; skipping macOS system proxy"; return 0; } | |
| local svc="${NETWORK_SERVICE}" | |
| [[ -n "$svc" ]] || svc="$(detect_network_service)" | |
| if (( ENABLE_PAC )) && [[ -n "$PAC_URL" ]]; then | |
| run_or_sudo "networksetup -setautoproxyurl \"$svc\" \"$PAC_URL\"" | |
| run_or_sudo "networksetup -setautoproxystate \"$svc\" on" | |
| # turn off manual proxies | |
| run_or_sudo "networksetup -setwebproxystate \"$svc\" off" | |
| run_or_sudo "networksetup -setsecurewebproxystate \"$svc\" off" | |
| run_or_sudo "networksetup -setsocksfirewallproxystate \"$svc\" off" | |
| log "✅ macOS PAC enabled on: $svc ($PAC_URL)" | |
| return 0 | |
| fi | |
| # Manual HTTP/HTTPS | |
| run_or_sudo "networksetup -setwebproxy \"$svc\" \"$PROXY_HOST\" \"$PROXY_PORT\"" | |
| run_or_sudo "networksetup -setwebproxystate \"$svc\" on" | |
| run_or_sudo "networksetup -setsecurewebproxy \"$svc\" \"$PROXY_HOST\" \"$PROXY_PORT\"" | |
| run_or_sudo "networksetup -setsecurewebproxystate \"$svc\" on" | |
| # Optional SOCKS | |
| if (( ENABLE_SOCKS )); then | |
| run_or_sudo "networksetup -setsocksfirewallproxy \"$svc\" \"$SOCKS_HOST\" \"$SOCKS_PORT\"" | |
| run_or_sudo "networksetup -setsocksfirewallproxystate \"$svc\" on" | |
| fi | |
| # Ensure PAC is off if using manual | |
| run_or_sudo "networksetup -setautoproxystate \"$svc\" off" || true | |
| log "✅ macOS system proxy enabled on: $svc (HTTP/HTTPS${ENABLE_SOCKS:+ + SOCKS})" | |
| } | |
| macos_proxy_off() { | |
| have networksetup || { log "ℹ️ networksetup not found; skipping macOS system proxy"; return 0; } | |
| local svc="${NETWORK_SERVICE}" | |
| [[ -n "$svc" ]] || svc="$(detect_network_service)" | |
| run_or_sudo "networksetup -setwebproxystate \"$svc\" off" || true | |
| run_or_sudo "networksetup -setsecurewebproxystate \"$svc\" off" || true | |
| run_or_sudo "networksetup -setsocksfirewallproxystate \"$svc\" off" || true | |
| run_or_sudo "networksetup -setautoproxystate \"$svc\" off" || true | |
| log "✅ macOS system proxy disabled on: $svc" | |
| } | |
| # ------------------------- | |
| # Tool configs | |
| # ------------------------- | |
| gradle_on() { | |
| mkdir -p "$GRADLE_PROPS_DIR" | |
| remove_managed_block_file "$GRADLE_PROPS_FILE" "${GRADLE_START}" "${GRADLE_END}" | |
| cat >> "$GRADLE_PROPS_FILE" <<EOF | |
| ${GRADLE_START} | |
| systemProp.http.proxyHost=${PROXY_HOST} | |
| systemProp.http.proxyPort=${PROXY_PORT} | |
| systemProp.https.proxyHost=${PROXY_HOST} | |
| systemProp.https.proxyPort=${PROXY_PORT} | |
| systemProp.http.nonProxyHosts=localhost|127.0.0.1|::1 | |
| systemProp.https.nonProxyHosts=localhost|127.0.0.1|::1 | |
| ${GRADLE_END} | |
| EOF | |
| } | |
| gradle_off() { | |
| remove_managed_block_file "$GRADLE_PROPS_FILE" "${GRADLE_START}" "${GRADLE_END}" | |
| } | |
| maven_on() { | |
| mkdir -p "$M2_DIR" | |
| # If no settings.xml, create a minimal one | |
| if [[ ! -f "$M2_SETTINGS_FILE" ]]; then | |
| cat > "$M2_SETTINGS_FILE" <<'EOF' | |
| <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" | |
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
| xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd"> | |
| </settings> | |
| EOF | |
| fi | |
| remove_managed_block_file "$M2_SETTINGS_FILE" "${MAVEN_START}" "${MAVEN_END}" | |
| # Insert proxy block before closing </settings> | |
| perl -0777 -i -pe "s#</settings>#${MAVEN_START}\n <proxies>\n <proxy>\n <id>proxy_zsh_http</id>\n <active>true</active>\n <protocol>http</protocol>\n <host>${PROXY_HOST}</host>\n <port>${PROXY_PORT}</port>\n <nonProxyHosts>localhost|127.0.0.1|::1</nonProxyHosts>\n </proxy>\n <proxy>\n <id>proxy_zsh_https</id>\n <active>true</active>\n <protocol>https</protocol>\n <host>${PROXY_HOST}</host>\n <port>${PROXY_PORT}</port>\n <nonProxyHosts>localhost|127.0.0.1|::1</nonProxyHosts>\n </proxy>\n </proxies>\n${MAVEN_END}\n</settings>#s" "$M2_SETTINGS_FILE" | |
| } | |
| maven_off() { | |
| remove_managed_block_file "$M2_SETTINGS_FILE" "${MAVEN_START}" "${MAVEN_END}" | |
| } | |
| pip_on() { | |
| if have pip3; then | |
| pip3 config set global.proxy "$PROXY_URL" >/dev/null 2>&1 || true | |
| elif have pip; then | |
| pip config set global.proxy "$PROXY_URL" >/dev/null 2>&1 || true | |
| fi | |
| } | |
| pip_off() { | |
| if have pip3; then | |
| pip3 config unset global.proxy >/dev/null 2>&1 || true | |
| elif have pip; then | |
| pip config unset global.proxy >/dev/null 2>&1 || true | |
| fi | |
| } | |
| docker_hint() { | |
| # Docker Desktop doesn't automatically use shell env for daemon pulls. | |
| # Many setups rely on Desktop's proxy settings UI; we just print guidance. | |
| log "ℹ️ Docker: shell env set (for docker CLI). Docker Desktop daemon proxy may need Desktop Settings > Resources > Proxies." | |
| } | |
| # ------------------------- | |
| # Main | |
| # ------------------------- | |
| proxy_on() { | |
| # Shell env (covers curl, many CLIs, Dart/Flutter in many cases) | |
| export http_proxy="$PROXY_URL" | |
| export https_proxy="$PROXY_URL" | |
| export ftp_proxy="$PROXY_URL" | |
| export HTTP_PROXY="$PROXY_URL" | |
| export HTTPS_PROXY="$PROXY_URL" | |
| export ALL_PROXY="$PROXY_URL" | |
| export no_proxy="localhost,127.0.0.1,::1" | |
| export NO_PROXY="$no_proxy" | |
| # Flutter/Dart endpoints | |
| export PUB_HOSTED_URL="$PUB_HOSTED_URL_DEFAULT" | |
| export FLUTTER_STORAGE_BASE_URL="$FLUTTER_STORAGE_BASE_URL_DEFAULT" | |
| # npm | |
| if have npm; then | |
| npm config set proxy "$PROXY_URL" >/dev/null 2>&1 || true | |
| npm config set https-proxy "$PROXY_URL" >/dev/null 2>&1 || true | |
| fi | |
| # git | |
| if have git; then | |
| git config --global http.proxy "$PROXY_URL" >/dev/null 2>&1 || true | |
| git config --global https.proxy "$PROXY_URL" >/dev/null 2>&1 || true | |
| fi | |
| # pip | |
| pip_on | |
| # Gradle | |
| gradle_on | |
| # Maven | |
| maven_on | |
| # macOS system proxy | |
| macos_proxy_on | |
| docker_hint | |
| log "✅ Proxy enabled: $PROXY_URL" | |
| log " - Shell env set (http_proxy/https_proxy/ALL_PROXY)" | |
| log " - Flutter env set (PUB_HOSTED_URL, FLUTTER_STORAGE_BASE_URL)" | |
| log " - npm proxy set (if npm exists)" | |
| log " - git proxy set (global) (if git exists)" | |
| log " - pip proxy set (if pip/pip3 exists)" | |
| log " - Gradle proxy set (~/.gradle/gradle.properties)" | |
| log " - Maven proxy set (~/.m2/settings.xml)" | |
| log " - macOS system proxy set (networksetup)" | |
| log "" | |
| log "Tip: Run using: source proxy_zsh.zsh on" | |
| } | |
| proxy_off() { | |
| # Shell env | |
| unset http_proxy https_proxy ftp_proxy ALL_PROXY no_proxy || true | |
| unset HTTP_PROXY HTTPS_PROXY NO_PROXY || true | |
| # Flutter/Dart env | |
| unset PUB_HOSTED_URL FLUTTER_STORAGE_BASE_URL || true | |
| # npm | |
| if have npm; then | |
| npm config delete proxy >/dev/null 2>&1 || true | |
| npm config delete https-proxy >/dev/null 2>&1 || true | |
| fi | |
| # git | |
| if have git; then | |
| git config --global --unset http.proxy >/dev/null 2>&1 || true | |
| git config --global --unset https.proxy >/dev/null 2>&1 || true | |
| fi | |
| # pip | |
| pip_off | |
| # Gradle | |
| gradle_off | |
| # Maven | |
| maven_off | |
| # macOS system proxy | |
| macos_proxy_off | |
| log "❌ Proxy disabled" | |
| log " - Shell env unset" | |
| log " - Flutter env unset" | |
| log " - npm proxy deleted (if npm exists)" | |
| log " - git proxy unset (global) (if git exists)" | |
| log " - pip proxy unset (if pip/pip3 exists)" | |
| log " - Gradle proxy block removed" | |
| log " - Maven proxy block removed" | |
| log " - macOS system proxy disabled" | |
| log "" | |
| log "Tip: Run using: source proxy_zsh.zsh off" | |
| } | |
| case "${1:-}" in | |
| on) proxy_on ;; | |
| off) proxy_off ;; | |
| *) | |
| log "Usage:" | |
| log " source proxy_zsh.zsh on" | |
| log " source proxy_zsh.zsh off" | |
| log "" | |
| log "Notes:" | |
| log " - Enable PAC: set ENABLE_PAC=1 and PAC_URL=\"http://.../proxy.pac\"" | |
| log " - Enable SOCKS: set ENABLE_SOCKS=1 (uses SOCKS_HOST/SOCKS_PORT)" | |
| log " - For macOS service autodetect: set NETWORK_SERVICE=\"\"" | |
| log "" | |
| log "Why source? Because running ./proxy_zsh.zsh can't export env vars into your current shell." | |
| ;; | |
| esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment