Skip to content

Instantly share code, notes, and snippets.

@Gcaufy
Last active May 27, 2019 04:02
Show Gist options
  • Save Gcaufy/8f43734e7b34932410029351bb134ba0 to your computer and use it in GitHub Desktop.
Save Gcaufy/8f43734e7b34932410029351bb134ba0 to your computer and use it in GitHub Desktop.
If you changed to another WIFI ssid, you can use this script to automatically switch your proxy setting
#!/bin/bash
##############
##
##
## Switch your proxy setting based on your SSID
##
## Author: Gcuafy <[email protected]>
## Install:
## $ alias ap='source ~/whaterveryourcodeis/auto_switch_proxy.sh'
## $ alias apc='~/whaterveryourcodeis/auto_switch_proxy.sh'
## Usage:
## $ apc -e # Modify the proxy setting
## $ apc -l # List the proxy setting
## $ apc -c # Check the current proxy setting
## $ ap # Auto switch the proxy setting. if it's not set for the SSID, then use the default one
##
##
# proxy setting file
CONFIG_FILE=~/.auto_switch_proxy.ini
CONFIG_ENV_FILE=~/.auto_switch_proxy.env
error() {
printf "\e[31m[✘]\e[0m ${1}\n"
}
success() {
printf "\e[32m[✔]\e[0m ${1}\n"
}
# If the file does not exsit, generate a default file.
check_config() {
if [ ! -f $CONFIG_FILE ]; then
cat > "$CONFIG_FILE" << EOF
[default]
http_proxy=1
https_proxy=2
no_proxy=3
EOF
fi
}
# Get MacOS SSID
get_mac_ssid() {
/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | awk '/ SSID/ {print substr($0, index($0, $2))}'
}
# Parse the .ini file
get_setting() {
cat <<EOF | python
try:
import ConfigParser as cp
except ImportError:
import configparser as cp
config = cp.ConfigParser()
config.read('$CONFIG_FILE')
name = '$1'
if not config.has_section(name):
name = 'default'
print('AUTO_SWITCH_SECTION=%s\nhttp_proxy=%s\nhttps_proxy=%s\nno_proxy=%s'%(name, config.get(name, 'http_proxy') if config.has_option(name, 'http_proxy') else '', config.get(name, 'https_proxy') if config.has_option(name, 'https_proxy') else '', config.get(name, 'no_proxy') if config.has_option(name, 'no_proxy') else ''))
EOF
}
edit_config() {
vi $CONFIG_FILE
}
list_config() {
cat $CONFIG_FILE
}
current_config() {
echo '============CURRENT ENVIRONMENT VARIABLES=========='
cat $CONFIG_ENV_FILE | grep -v AUTO_SWITCH_SECTION
}
main() {
ssid=`get_mac_ssid`
if [ x"$ssid" == "x" ]; then
error "Can not find WIFI SSID, Make sure it is MacOS."
exit
fi
success "SSID: $ssid"
env=`get_setting $ssid`
echo "$env" > $CONFIG_ENV_FILE
source "$CONFIG_ENV_FILE"
if [ x"$AUTO_SWITCH_SECTION" == "x" ]; then
error "Error to load your config file: $CONFIG_FILE, try to remove it and run again."
exit
fi
success "Using: [$AUTO_SWITCH_SECTION]"
echo '============================'
cat "$CONFIG_ENV_FILE" | grep -v AUTO_SWITCH_SECTION
}
# Main
(return 0 2>/dev/null) && sourced=1 || sourced=0
if [ "$sourced" == "0" ] && [ "1" == "$#" ]; then
if [ 'list' == $1 ] || [ '-l' == $1 ]; then
list_config
elif [ 'edit' == $1 ] || [ '-e' == $1 ]; then
edit_config
elif [ 'current' == $1 ] || [ '-c' == $1 ]; then
current_config
fi
else
check_config
main
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment