-
Notifications
You must be signed in to change notification settings - Fork 4
/
set-http-proxy
executable file
·155 lines (128 loc) · 4 KB
/
set-http-proxy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/bin/bash
# make sure, script is sourced
# -------------------------------------------------------------------------------------------------
SOURCED=0
if [[ "$BASH" = "" ]]; then
# not bash
if [[ "$ZSH_VERSION" != "" ]]; then
# zsh
SOURCED=1
SOURCE_DIR="${0%/*}"
else
echo "Unsupported shell. Exiting"
read
exit 2
fi
else
# bash
COMMAND=`awk -F/ '{print $NF}' <<< $0`
if [ "x$COMMAND" = "xbash" -o "x$COMMAND" = "x-bash" ]; then
SOURCED=1
fi
SOURCE_DIR="${BASH_SOURCE[0]}"
fi
if [ $SOURCED = 0 ]; then
echo "script must be sourced. Exiting ..."
exit 1
fi
log() {
if [[ $VERBOSE == 1 ]]; then
echo $*
fi
}
logvar() {
local var=$1
if [[ "$ZSH_VERSION" != "" ]]; then
log "${var}: ${(P)var}"
else # bash
log "${var}: ${!var}"
fi
}
for i in "$@"; do
case $i in
-v|--verbose) VERBOSE=1
echo VERBOSE
;;
*) INTERFACE=$i ;;
esac
done
logvar INTERFACE
# -------------------------------------------------------------------------------------------------
# make sure, script runs on OS X
if [ "Darwin" != `uname` ]; then
echo "This script is only tested on OSX. Aborting"
return 1
fi
# make sure, pacparser is installed
type pacparser >/dev/null 2>&1 || { echo >&2 "'pacparser' required. Aborting."; return 2; }
function enabled() {
echo "$1" | grep "^Enabled" | sed "s/[a-zA-Z]*: \(.*\)/\1/"
}
#--------------------------------------------------------------------------------------------------
# MAIN
#--------------------------------------------------------------------------------------------------
# determine the network service name for the default route
if [[ -z "$INTERFACE" ]]; then
INTERFACE=`route -n get default | grep interface | sed "s/.*interface: \(.*\)/\1/"`
fi
if [[ "$INTERFACE" == "" ]]; then
echo could not determine the interface
return 2
fi
logvar INTERFACE
NETWORK=`networksetup -listnetworkserviceorder | grep -B1 "$INTERFACE" | head -n 1 | sed "s/([0-9]*) \(.*\)/\1/"`
logvar NETWORK
echo "using '$NETWORK'"
# currently two proxy configurations are supported:
# a proxy that is explicitely set with host and port and
# an Automatic Proxy Configuration with .pac file.
# Auto-Discovery is not (yet) supported
# proxy explicitely set?
PROXY_INFO=`networksetup -getwebproxy "$NETWORK"`
PROXY_ENABLED=`enabled "$PROXY_INFO"`
if [ "$PROXY_ENABLED" = "Yes" ]; then
export PROXY_HOST=`echo "$PROXY_INFO" | grep Server | sed "s/[a-zA-Z]*: \(.*\)/\1/"`
export PROXY_PORT=`echo "$PROXY_INFO" | grep Port | sed "s/[a-zA-Z]*: \(.*\)/\1/"`
PROXY="$PROXY_HOST:$PROXY_PORT"
else
# proxy not explicitely set, try proxy.pac
PROXY_INFO=`networksetup -getautoproxyurl $NETWORK`
PROXY_ENABLED=`enabled "$PROXY_INFO"`
if [ "$PROXY_ENABLED" = "Yes" ]; then
PROXY_PAC=`echo "$PROXY_INFO" | grep "^URL:" | sed "s/URL: //"`
echo "proxy.pac: $PROXY_PAC"
if [ -e /tmp/proxy.pac ]; then
if test "`find /tmp/proxy.pac -mmin +1440`"; then
curl -sL $PROXY_PAC > /tmp/proxy.pac
fi
else
curl -sL $PROXY_PAC > /tmp/proxy.pac
fi
# use the pacparser to get the proxy
PROXY=`pacparser | sed "s/PROXY //"`
export PROXY_HOST=`echo "$PROXY" | sed 's/\(.*\):.*/\1/'`
export PROXY_PORT=`echo "$PROXY" | sed 's/.*:\(.*\)/\1/'`
# not supported on bash 3.x:
# PROXY_PARTS=( `echo "$PROXY" | tr -s ':' ' '` )
# export PROXY_HOST="$PROXY_PARTS[1]"
# export PROXY_PORT="$PROXY_PARTS[2]"
fi
fi
# set/unset environment variables, git and Maven settings
if [ "$PROXY_ENABLED" = "Yes" ]; then
echo " setting http_proxy: http://$PROXY"
PROXY_URL=`echo "http://$PROXY"`
export http_proxy="$PROXY_URL"
export https_proxy="$PROXY_URL"
git config --global http.proxy "$PROXY_URL"
echo " settings.xml:"
# uses $PROXY_HOST and $PROXY_PORT
mvn-proxy-patch true
else
echo " unset http_proxy"
unset http_proxy
unset https_proxy
git config --global --unset http.proxy
echo " settings.xml:"
mvn-proxy-patch false
fi