Created
May 4, 2017 12:59
-
-
Save dataday/a2febe21d6da8477be7e7912b8cb4ab6 to your computer and use it in GitHub Desktop.
Creates a bridge between the system network and the local shell session on MAC OS
This file contains 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 bash | |
# | |
# Date: 04 2015 | |
# Author: dataday | |
# | |
# Support: | |
# Mac OS 10.6+, or anything with Mac's 'networksetup' tools installed. | |
# Bash 3.2.48(1)-release | |
# | |
# Links: | |
# http://www.stunnel.org/ | |
# http://subversion.apache.org/ | |
# https://github.com/philchristensen/svn-color | |
# https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man8/networksetup.8.html | |
# | |
# Description: | |
# Creates a bridge between the OS network setup and local shell session. | |
# Detects if the network is BBC / non-BBC and configures the shell as appropriate. | |
# Any specific network information, eg., HTTP proxy, is exported locally to each | |
# shell session created. | |
# | |
# Usage: source ~/.bash_network | |
# stunnel /usr/local/etc/stunnel/stunnel.conf | |
# | |
#set -x | |
shopt -s expand_aliases | |
# set network by type and setup command | |
function get_networks () { | |
networks=( | |
"http -getwebproxy" | |
"https -getsecurewebproxy" | |
"socks -getsocksfirewallproxy" | |
"ftp -getftpproxy" | |
"gopher -getgopherproxy" | |
"no -getproxybypassdomains" | |
"x_host -getwebproxy" | |
"x_port -getwebproxy" | |
) | |
} | |
# get location details, eg., BBC On Network | |
function get_location () { | |
echo "$(networksetup -getcurrentlocation)" | |
} | |
# get service details, eg., Thunderbolt Ethernet or Wi-Fi | |
# http://codesnippets.joyent.com/posts/show/1819 | |
function get_service () { | |
local guid | |
guid="$(printf "open\nget State:/Network/Global/IPv4\nd.show" | \ | |
/usr/sbin/scutil | /usr/bin/awk '/PrimaryService/{print $3}')" | |
service="$(printf "open\nget Setup:/Network/Service/${guid}\nd.show" | \ | |
/usr/sbin/scutil | /usr/bin/awk -F': ' '/UserDefinedName/{print $2}')" | |
echo "$service" | |
} | |
# get network details | |
function get_network () { | |
echo $(networksetup $1 "$2") | |
} | |
# get proxy bypass details | |
function get_proxy_bypass () { | |
echo $(echo "$1") | |
} | |
# trim a string top and tail | |
function trim_str () { | |
echo $(echo "$1" | sed 's/ *$//g' | sed 's/^ *//g') | |
} | |
# toggle set / unset proxy state based on type, eg., http | |
function set_proxy_state () { | |
local cmd="$1" | |
local proxy="$2_proxy" | |
local proxy_flag="$3" | |
local network=$(get_network $proxy_flag "$service") | |
local isEnabled=true | |
local hasHost=true | |
# check network result for enabled and host directives | |
[[ $network =~ "Enabled: No Server: Port: 0"* ]] && unset hasHost | |
[[ $network =~ "Enabled: No"* ]] && isEnabled=true | |
# make sure a network connection can be made to the outside world | |
if [ "$(scutil -r duckduckgo.com)" == "Reachable" ]; then | |
# if network has a host assigned | |
if [[ $hasHost && $isEnabled ]]; then | |
local result | |
local port=$(trim_str $(echo $network | cut -f6 -d ' ' )) | |
local host=$(trim_str $(echo $network | cut -f4 -d ' ' )) | |
local bypass=$(get_proxy_bypass "$network") | |
if [ ! -z "$host" -o ! -z "$bypass" ]; then | |
case "$proxy" in | |
# assign no proxy, | |
no_proxy) | |
isEnabled=true | |
result="$bypass" | |
shift | |
;; | |
# assign proxy host | |
x_host_proxy) | |
result="$host" | |
shift | |
;; | |
# assign proxy port | |
x_port_proxy) | |
result="$port" | |
shift | |
;; | |
# assign proxy host:port by default | |
*) | |
result="http://$host:$port" | |
shift | |
;; | |
esac | |
fi | |
fi | |
[[ $cmd == "set" ]] && export "$proxy=$result" || unset $proxy | |
[[ $cmd == "set" ]] && echo "$location ($service) - $cmd $proxy=$result" || echo "$location ($service) - $cmd $proxy=$result" | |
#unset result | |
else | |
echo "$location ($service) - No Internet" | |
unset $proxy | |
fi | |
unset proxy | |
unset host | |
unset port | |
} | |
function update_ssh_config () { | |
local ssh_file="$HOME/.ssh/config" | |
local ssh_cmd="ProxyCommand nc -x" | |
local add_comment="$1" | |
# add proxies | |
if [ "$add_comment" = true ]; then | |
sed -i '' "/$ssh_cmd/ s/[^#]$ssh_cmd/\ #$ssh_cmd/" $ssh_file | |
# remove proxies | |
else | |
sed -i '' "/$ssh_cmd/ s/#$ssh_cmd/$ssh_cmd/" $ssh_file | |
fi | |
} | |
function init () { | |
local cmd | |
local service=$(get_service) | |
local location=$(get_location) | |
[[ $location =~ "BBC On Network" ]] && cmd="set" || cmd="unset" | |
[[ $service =~ "Thunderbolt Ethernet" ]] && service="Thunderbolt Ethernet" || service="Wi-Fi" | |
get_networks | |
for network in "${networks[@]}" | |
do | |
: | |
# '--' ignore further options if found in results | |
set -- none $network | |
shift | |
# determine state | |
set_proxy_state $cmd $1 $2 | |
done | |
if [[ $x_host_proxy && $x_port_proxy ]]; then | |
git config --global http.proxy "${http_proxy}" | |
git config --global https.proxy "${https_proxy}" | |
git config --global socks.proxy "${socks_proxy}" | |
alias svn="/usr/local/bin/svn --config-option servers:global:http-proxy-host=${x_host_proxy} --config-option servers:global:http-proxy-port=${x_port_proxy}" | |
update_ssh_config false | |
else | |
git config --global --unset-all http.proxy | |
git config --global --unset-all https.proxy | |
git config --global --unset-all socks.proxy | |
alias svn="/usr/local/bin/svn" | |
update_ssh_config true | |
fi | |
unset service | |
unset networks | |
unset location | |
unset bypass | |
unset cmd | |
} | |
init | |
#set +x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment