Skip to content

Instantly share code, notes, and snippets.

@funasoul
Created June 2, 2020 20:18
Show Gist options
  • Save funasoul/492a8c999a67cd9a67479b418b9d7f44 to your computer and use it in GitHub Desktop.
Save funasoul/492a8c999a67cd9a67479b418b9d7f44 to your computer and use it in GitHub Desktop.
# A zsh function which will check my proxy configuration.
# Runs only on macOS.
proxycheck() {
local red=`tput setaf 1; tput bold`
local green=`tput setaf 2; tput bold`
local cyan=`tput setaf 6; tput bold`
local reset=`tput sgr0`
local systemflag=0
networksetup -listallnetworkservices | while read line
do
if [[ $line != *"An asterisk"* ]]; then
local systemproxy=$(networksetup -getwebproxy $line | /usr/bin/grep -e '^Enabled:' | awk '{print $2}')
if [ $systemproxy = "Yes" ]; then
echo "System: [${green}ON${reset}] $line"
systemflag=1
fi
fi
done
if [ $systemflag = 0 ]; then
echo "System: [${cyan}No proxy${reset}]"
fi
local count=$(/usr/bin/grep -c 'network.proxy.type' $HOME/Library/Application\ Support/Firefox/Profiles/*/prefs.js)
echo -n "Firefox: "
if [ $count -gt 1 ]; then
echo "Found more than one prefs.js"
return -1
elif [ $count -eq 0 ]; then
if [ $systemflag = 1 ]; then
echo "[${green}Use system proxy settings${reset}]"
return 1
else
echo "[${cyan}Use system proxy settings${reset}]"
return 0
fi
else
local proxytype=$(/usr/bin/grep 'network.proxy.type' $HOME/Library/Application\ Support/Firefox/Profiles/*/prefs.js | sed 's/[^0-9]//g')
# ref. https://developer.mozilla.org/ja/docs/Mozilla_Networking_Preferences
case "$proxytype" in
0) echo "[${cyan}No proxy${reset}]"; return 0 ;;
1) echo "[${green}ON${reset}] Manual proxy configuration" ; return 1 ;;
2) echo "[${green}ON${reset}] Automatic proxy configuration URL (PAC)" ; return 1 ;;
4) echo "[${green}ON${reset}] Auto-detect proxy settings for this network" ; return 1 ;;
*) echo "[${red}UNKNOWN${reset}]" ; return -1 ;;
esac
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment