-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.plugins.bash
executable file
·358 lines (298 loc) · 9.57 KB
/
proxy.plugins.bash
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
cite about-plugin
about-plugin 'Proxy Tools'
PROXY_DIVIDER=$(printf '%.0s-' {1..30}; echo)
disable-proxy () {
about 'Disables proxy settings for Bash, npm and SSH'
group 'Proxy'
unset http_proxy
unset https_proxy
unset HTTP_PROXY
unset HTTPS_PROXY
unset ALL_PROXY
unset no_proxy
unset NO_PROXY
echo "Disabled proxy environment variables"
npm-disable-proxy
ssh-disable-proxy
svn-disable-proxy
}
enable-proxy () {
about 'Enables proxy settings for Bash, npm and SSH'
group 'Proxy'
export http_proxy=$BASH_IT_HTTP_PROXY
export https_proxy=$BASH_IT_HTTPS_PROXY
export HTTP_PROXY=$http_proxy
export HTTPS_PROXY=$https_proxy
export ALL_PROXY=$http_proxy
export no_proxy=$BASH_IT_NO_PROXY
export NO_PROXY=$no_proxy
echo "Enabled proxy environment variables"
npm-enable-proxy
ssh-enable-proxy
svn-enable-proxy
}
enable-proxy-alt () {
about 'Enables alternate proxy settings for Bash, npm and SSH'
group 'Proxy'
export http_proxy=$BASH_IT_HTTP_PROXY_ALT
export https_proxy=$BASH_IT_HTTPS_PROXY_ALT
export HTTP_PROXY=$http_proxy
export HTTPS_PROXY=$https_proxy
export ALL_PROXY=$http_proxy
export no_proxy=$BASH_IT_NO_PROXY
export NO_PROXY=$no_proxy
echo "Enabled alternate proxy environment variables"
npm-enable-proxy $http_proxy $https_proxy
ssh-enable-proxy
svn-enable-proxy $http_proxy
}
show-proxy () {
about 'Shows the proxy settings for Bash, Git, npm and SSH'
group 'Proxy'
printf "\n${GREEN}%s${NC}\n$PROXY_DIVIDER\n" "Environment Variables"
env | grep -i "proxy" | grep -v "BASH_IT"
show-proxy
npm-show-proxy
git-global-show-proxy
svn-show-proxy
ssh-show-proxy
}
proxy-help () {
about 'Provides an overview of the gaudi-bash proxy configuration'
group 'Proxy'
cat << EOF
gaudi-bash provides support for enabling/disabling proxy settings for various shell tools.
The following backends are currently supported (in addition to the shell's environment variables): Git, SVN, npm, ssh
gaudi-bash uses the following variables to set the shell's proxy settings when you call 'enable-proxy'.
These variables are best defined in a custom script in gaudi-bash's custom script folder ('$BASH_IT/custom'),
e.g. '$BASH_IT/custom/proxy.env.bash'
* BASH_IT_HTTP_PROXY and BASH_IT_HTTPS_PROXY: Define the proxy URL to be used, e.g. 'http://localhost:1234'
* BASH_IT_NO_PROXY: A comma-separated list of proxy exclusions, e.g. '127.0.0.1,localhost'
Run 'glossary proxy' to show the available proxy functions with a short description.
EOF
show-proxy
}
show-proxy () {
about 'Shows the gaudi-bash proxy settings'
group 'Proxy'
printf "\n${GREEN}%s${NC}\n%s\n$PROXY_DIVIDER\n" "gaudi-bash Environment Variables" "(These variables will be used to set the proxy when you call 'enable-proxy')"
env | grep -e "BASH_IT.*PROXY"
}
npm-show-proxy () {
about 'Shows the npm proxy settings'
group 'Proxy'
if command -v npm &> /dev/null ; then
printf "\n${GREEN}%s${NC}\n$PROXY_DIVIDER\n" "npm"
echo "npm HTTP proxy: $(npm config get proxy)"
echo "npm HTTPS proxy: $(npm config get https-proxy)"
echo "npm proxy exceptions: $(npm config get noproxy)"
fi
}
npm-disable-proxy () {
about 'Disables npm proxy settings'
group 'Proxy'
if command -v npm &> /dev/null ; then
npm config delete proxy
npm config delete https-proxy
npm config delete noproxy
echo "Disabled npm proxy settings"
fi
}
npm-enable-proxy () {
about 'Enables npm proxy settings'
group 'Proxy'
local my_http_proxy=${1:-$BASH_IT_HTTP_PROXY}
local my_https_proxy=${2:-$BASH_IT_HTTPS_PROXY}
local my_no_proxy=${3:-$BASH_IT_NO_PROXY}
if command -v npm &> /dev/null ; then
npm config set proxy $my_http_proxy
npm config set https-proxy $my_https_proxy
npm config set noproxy $my_no_proxy
echo "Enabled npm proxy settings"
fi
}
git-global-show-proxy () {
about 'Shows global Git proxy settings'
group 'Proxy'
if command -v git &> /dev/null ; then
printf "\n${GREEN}%s${NC}\n$PROXY_DIVIDER\n" "Git (Global Settings)"
echo "Git (Global) HTTP proxy: $(git config --global --get http.proxy)"
echo "Git (Global) HTTPS proxy: $(git config --global --get https.proxy)"
fi
}
git-global-disable-proxy () {
about 'Disables global Git proxy settings'
group 'Proxy'
if command -v git &> /dev/null ; then
git config --global --unset-all http.proxy
git config --global --unset-all https.proxy
echo "Disabled global Git proxy settings"
fi
}
git-global-enable-proxy () {
about 'Enables global Git proxy settings'
group 'Proxy'
if command -v git &> /dev/null ; then
git-global-disable-proxy
git config --global --add http.proxy $BASH_IT_HTTP_PROXY
git config --global --add https.proxy $BASH_IT_HTTPS_PROXY
echo "Enabled global Git proxy settings"
fi
}
git-show-proxy () {
about 'Shows current Git project proxy settings'
group 'Proxy'
if command -v git &> /dev/null ; then
printf "\n${GREEN}%s${NC}\n$PROXY_DIVIDER\n" "Git Project Proxy Settings"
echo "Git HTTP proxy: $(git config --get http.proxy)"
echo "Git HTTPS proxy: $(git config --get https.proxy)"
fi
}
git-disable-proxy () {
about 'Disables current Git project proxy settings'
group 'Proxy'
if command -v git &> /dev/null ; then
git config --unset-all http.proxy
git config --unset-all https.proxy
echo "Disabled Git project proxy settings"
fi
}
git-enable-proxy () {
about 'Enables current Git project proxy settings'
group 'Proxy'
if command -v git &> /dev/null ; then
git-disable-proxy
git config --add http.proxy $BASH_IT_HTTP_PROXY
git config --add https.proxy $BASH_IT_HTTPS_PROXY
echo "Enabled Git project proxy settings"
fi
}
svn-show-proxy () {
about 'Shows SVN proxy settings'
group 'Proxy'
if command -v svn &> /dev/null && command -v python2 &> /dev/null ; then
printf "\n${GREEN}%s${NC}\n$PROXY_DIVIDER\n" "SVN Proxy Settings"
python2 - <<END
import ConfigParser, os
config = ConfigParser.ConfigParser()
config.read(os.path.expanduser('~/.subversion/servers'))
if (config.has_section('global')):
proxy_host = ''
proxy_port = ''
proxy_exceptions = ''
if (config.has_option('global', 'http-proxy-host')):
proxy_host = config.get('global', 'http-proxy-host')
if (config.has_option('global', 'http-proxy-port')):
proxy_port = config.get('global', 'http-proxy-port')
if (config.has_option('global', 'http-proxy-exceptions')):
proxy_exceptions = config.get('global', 'http-proxy-exceptions')
print 'http-proxy-host : ' + proxy_host
print 'http-proxy-port : ' + proxy_port
print 'http-proxy-exceptions: ' + proxy_exceptions
END
fi
}
svn-disable-proxy () {
about 'Disables SVN proxy settings'
group 'Proxy'
if command -v svn &> /dev/null && command -v python2 &> /dev/null ; then
python2 - <<END
import ConfigParser, os
config = ConfigParser.ConfigParser()
config.read(os.path.expanduser('~/.subversion/servers'))
if config.has_section('global'):
changed = False
if config.has_option('global', 'http-proxy-host'):
config.remove_option('global', 'http-proxy-host')
changed = True
if config.has_option('global', 'http-proxy-port'):
config.remove_option('global', 'http-proxy-port')
changed = True
if config.has_option('global', 'http-proxy-exceptions'):
config.remove_option('global', 'http-proxy-exceptions')
changed = True
if changed:
with open(os.path.expanduser('~/.subversion/servers'), 'wb') as configfile:
config.write(configfile)
print 'Disabled SVN proxy settings'
END
fi
}
svn-enable-proxy () {
about 'Enables SVN proxy settings'
group 'Proxy'
if command -v svn &> /dev/null && command -v python2 &> /dev/null ; then
local my_http_proxy=${1:-$BASH_IT_HTTP_PROXY}
python2 - "$my_http_proxy" "$BASH_IT_NO_PROXY" <<END
import ConfigParser, os, sys, urlparse
pieces = urlparse.urlparse(sys.argv[1])
host = pieces.hostname
port = pieces.port
exceptions = sys.argv[2]
config = ConfigParser.ConfigParser()
config.read(os.path.expanduser('~/.subversion/servers'))
if not config.has_section('global'):
config.add_section('global')
if host is not None:
config.set('global', 'http-proxy-host', host)
else:
config.remove_option('global', 'http-proxy-host')
if port is not None:
config.set('global', 'http-proxy-port', port)
else:
config.remove_option('global', 'http-proxy-port')
if exceptions is not None:
config.set('global', 'http-proxy-exceptions', exceptions)
else:
config.remove_option('global', 'http-proxy-exceptions')
with open(os.path.expanduser('~/.subversion/servers'), 'wb') as configfile:
config.write(configfile)
print 'Enabled SVN proxy settings'
END
fi
}
ssh-show-proxy () {
about 'Shows SSH config proxy settings (from ~/.ssh/config)'
group 'Proxy'
if [[ -f ~/.ssh/config ]] ; then
printf "\n${GREEN}%s${NC}\n$PROXY_DIVIDER\n" "SSH Config Enabled in ~/.ssh/config"
awk '
$1 == "Host" {
host = $2;
next;
}
$1 == "ProxyCommand" {
$1 = "";
printf "%s\t%s\n", host, $0
}
' ~/.ssh/config | column -t
printf "\n${GREEN}%s${NC}\n$PROXY_DIVIDER\n" "SSH Config Disabled in ~/.ssh/config"
awk '
$1 == "Host" {
host = $2;
next;
}
$0 ~ "^#.*ProxyCommand.*" {
$1 = "";
$2 = "";
printf "%s\t%s\n", host, $0
}
' ~/.ssh/config | column -t
fi
}
ssh-disable-proxy () {
about 'Disables SSH config proxy settings'
group 'Proxy'
if [[ -f ~/.ssh/config ]] ; then
sed -e's/^.*ProxyCommand/# ProxyCommand/' "${BASH_IT_SED_I_PARAMETERS[@]}" ~/.ssh/config
echo "Disabled SSH config proxy settings"
fi
}
ssh-enable-proxy () {
about 'Enables SSH config proxy settings'
group 'Proxy'
if [[ -f ~/.ssh/config ]] ; then
sed -e's/# ProxyCommand/ ProxyCommand/' "${BASH_IT_SED_I_PARAMETERS[@]}" ~/.ssh/config
echo "Enabled SSH config proxy settings"
fi
}