├── LICENSE ├── README.md └── zsh-osx-autoproxy.plugin.zsh /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Sukka 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # zsh-osx-autoproxy 2 | 3 | [![Author](https://img.shields.io/badge/Author-Sukka-b68469.svg?style=flat-square)](https://skk.moe) 4 | [![License](https://img.shields.io/github/license/sukkaw/zsh-osx-autoproxy.svg?style=flat-square)](./LICENSE) 5 | 6 | :nut_and_bolt: An [`oh-my-zsh`](https://ohmyz.sh/) plugin that configures proxy environment variables based on macOS's system preferences automatically. 7 | 8 | ## Installation 9 | 10 | ### oh-my-zsh 11 | 12 | 1. Clone this repository into `$ZSH_CUSTOM/plugins` (by default `~/.oh-my-zsh/custom/plugins`) 13 | 14 | ```bash 15 | git clone https://github.com/sukkaw/zsh-osx-autoproxy ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-osx-autoproxy 16 | ``` 17 | 18 | 2. Add the plugin to the list of plugins for oh-my-zsh to load (inside `~/.zshrc`): 19 | 20 | ``` 21 | plugins=( 22 | [plugins 23 | ...] 24 | zsh-osx-autoproxy 25 | ) 26 | ``` 27 | 28 | 3. Start a new terminal session. 29 | 30 | ### Antigen 31 | 32 | [Antigen](https://github.com/zsh-users/antigen) is a zsh plugin manager, and it support `oh-my-zsh` plugin as well. You only need to add `antigen bundle sukkaw/zsh-osx-autoproxy` to your `.zshrc` with your other bundle commands if you are using Antigen. Antigen will handle cloning the plugin for you automatically the next time you start zsh. You can also add the plugin to a running zsh with `antigen bundle sukkaw/zsh-osx-autoproxy` for testing before adding it to your `.zshrc`. 33 | 34 | ## Usage 35 | 36 | After install the plugin and have proxy configured in `System Prefrences`, start a new terminal session and following environment variables will be set (if applicable): 37 | 38 | - `http_proxy` 39 | - `https_proxy` 40 | - `ftp_proxy` 41 | - `all_proxy` 42 | 43 | ## Uninstallation 44 | 45 | **If you install `zsh-osx-autoproxy` with Antigen**, you need to remove `antigen bundle sukkaw/zsh-osx-autoproxy` to disable the plugin. 46 | **If you install `zsh-osx-autoproxy` with oh-myzsh**, you need to remove `zsh-osx-autoproxy` item from plugin array, then `rm -rf ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-osx-autoproxy` to remove the plugin. 47 | 48 | ## Author 49 | 50 | **zsh-osx-autoproxy** © [Sukka](https://github.com/SukkaW), Released under the [MIT](./LICENSE) License.
51 | Authored and maintained by Sukka with help from contributors ([list](https://github.com/SukkaW/zsh-osx-autoproxy/graphs/contributors)). 52 | 53 | > [Personal Website](https://skk.moe) · [Blog](https://blog.skk.moe) · GitHub [@SukkaW](https://github.com/SukkaW) · Telegram Channel [@SukkaChannel](https://t.me/SukkaChannel) · Twitter [@isukkaw](https://twitter.com/isukkaw) · Keybase [@sukka](https://keybase.io/sukka) 54 | -------------------------------------------------------------------------------- /zsh-osx-autoproxy.plugin.zsh: -------------------------------------------------------------------------------- 1 | #!/bin/zsh 2 | # Auto configure zsh proxy env based on system preferences 3 | # Sukka (https://skk.moe) 4 | 5 | # Cache the output of scutil --proxy 6 | __ZSH_OSX_AUTOPROXY_SCUTIL_PROXY=$(scutil --proxy) 7 | 8 | # Pattern used to match the status 9 | __ZSH_OSX_AUTOPROXY_HTTP_PROXY_ENABLED_PATTERN="HTTPEnable : 1" 10 | __ZSH_OSX_AUTOPROXY_HTTPS_PROXY_ENABLED_PATTERN="HTTPSEnable : 1" 11 | __ZSH_OSX_AUTOPROXY_FTP_PROXY_ENABLED_PATTERN="FTPSEnable : 1" 12 | __ZSH_OSX_AUTOPROXY_SOCKS_PROXY_ENABLED_PATTERN="SOCKSEnable : 1" 13 | 14 | __ZSH_OSX_AUTOPROXY_HTTP_PROXY_ENABLED=$__ZSH_OSX_AUTOPROXY_SCUTIL_PROXY[(I)$__ZSH_OSX_AUTOPROXY_HTTP_PROXY_ENABLED_PATTERN] 15 | __ZSH_OSX_AUTOPROXY_HTTPS_PROXY_ENABLED=$__ZSH_OSX_AUTOPROXY_SCUTIL_PROXY[(I)$__ZSH_OSX_AUTOPROXY_HTTPS_PROXY_ENABLED_PATTERN] 16 | __ZSH_OSX_AUTOPROXY_FTP_PROXY_ENABLED=$__ZSH_OSX_AUTOPROXY_SCUTIL_PROXY[(I)$__ZSH_OSX_AUTOPROXY_FTP_PROXY_ENABLED_PATTERN] 17 | __ZSH_OSX_AUTOPROXY_SOCKS_PROXY_ENABLED=$__ZSH_OSX_AUTOPROXY_SCUTIL_PROXY[(I)$__ZSH_OSX_AUTOPROXY_SOCKS_PROXY_ENABLED_PATTERN] 18 | 19 | # http proxy 20 | if (( $__ZSH_OSX_AUTOPROXY_HTTP_PROXY_ENABLED )); then 21 | __ZSH_OSX_AUTOPROXY_HTTP_PROXY_SERVER=${${__ZSH_OSX_AUTOPROXY_SCUTIL_PROXY#*HTTPProxy : }[(f)1]} 22 | __ZSH_OSX_AUTOPROXY_HTTP_PROXY_PORT=${${__ZSH_OSX_AUTOPROXY_SCUTIL_PROXY#*HTTPPort : }[(f)1]} 23 | export http_proxy="http://${__ZSH_OSX_AUTOPROXY_HTTP_PROXY_SERVER}:${__ZSH_OSX_AUTOPROXY_HTTP_PROXY_PORT}" 24 | export HTTP_PROXY="${http_proxy}" 25 | fi 26 | # https_proxy 27 | if (( $__ZSH_OSX_AUTOPROXY_HTTPS_PROXY_ENABLED )); then 28 | __ZSH_OSX_AUTOPROXY_HTTPS_PROXY_SERVER=${${__ZSH_OSX_AUTOPROXY_SCUTIL_PROXY#*HTTPSProxy : }[(f)1]} 29 | __ZSH_OSX_AUTOPROXY_HTTPS_PROXY_PORT=${${__ZSH_OSX_AUTOPROXY_SCUTIL_PROXY#*HTTPSPort : }[(f)1]} 30 | export https_proxy="http://${__ZSH_OSX_AUTOPROXY_HTTPS_PROXY_SERVER}:${__ZSH_OSX_AUTOPROXY_HTTPS_PROXY_PORT}" 31 | export HTTPS_PROXY="${https_proxy}" 32 | fi 33 | # ftp_proxy 34 | if (( $__ZSH_OSX_AUTOPROXY_FTP_PROXY_ENABLED )); then 35 | __ZSH_OSX_AUTOPROXY_FTP_PROXY_SERVER=${${__ZSH_OSX_AUTOPROXY_SCUTIL_PROXY#*FTPProxy : }[(f)1]} 36 | __ZSH_OSX_AUTOPROXY_FTP_PROXY_PORT=${${__ZSH_OSX_AUTOPROXY_SCUTIL_PROXY#*FTPPort : }[(f)1]} 37 | export ftp_proxy="http://${__ZSH_OSX_AUTOPROXY_FTP_PROXY_SERVER}:${__ZSH_OSX_AUTOPROXY_FTP_PROXY_PORT}" 38 | export FTP_PROXY="${ftp_proxy}" 39 | fi 40 | # all_proxy 41 | if (( $__ZSH_OSX_AUTOPROXY_SOCKS_PROXY_ENABLED )); then 42 | __ZSH_OSX_AUTOPROXY_SOCKS_PROXY_SERVER=${${__ZSH_OSX_AUTOPROXY_SCUTIL_PROXY#*SOCKSProxy : }[(f)1]} 43 | __ZSH_OSX_AUTOPROXY_SOCKS_PROXY_PORT=${${__ZSH_OSX_AUTOPROXY_SCUTIL_PROXY#*SOCKSPort : }[(f)1]} 44 | export all_proxy="http://${__ZSH_OSX_AUTOPROXY_SOCKS_PROXY_SERVER}:${__ZSH_OSX_AUTOPROXY_SOCKS_PROXY_PORT}" 45 | export ALL_PROXY="${all_proxy}" 46 | elif (( $__ZSH_OSX_AUTOPROXY_HTTP_PROXY_ENABLED )); then 47 | export all_proxy="${http_proxy}" 48 | export ALL_PROXY="${all_proxy}" 49 | fi 50 | --------------------------------------------------------------------------------