forked from himanshub16/ProxyMan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.sh
executable file
·505 lines (471 loc) · 16.9 KB
/
main.sh
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
#!/bin/bash
# ProxyMan v1.8
# Author : Himanshu Shekhar < https://github.com/himanshub16/ProxyMan >
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
#
# Tools used/required for implementation : bash, sed, grep, regex support, gsettings, apt
# and of-course privilege for setting up apt.
# The user should be in the sudoers to set up apt, or modify the script as required.
#
# Addition made on 11 Aug, 2016
# If users is not in sudoers or sudo is not available, check the username.
# If not root, then exit the script with a message about starting the script as root.
#
#
# Credits : https://wiki.archlinux.org/index.php/Proxy_settings
# https://wiki.archlinux.org/index.php/Environment_variables
# https://developer.gnome.org/ProxyConfiguration/
# Alan Pope : https://wiki.ubuntu.com/AlanPope
# The Linux Command Line, William E. Shotts
# Ubuntu Forums
# Stack Exchange
# Anurag Sharma (for demanding npm proxy configuration)
# and many more Google search links.
#
# This script sets the proxy configuration for a general system from the Debian family.
# Configures apt, environment variables and gsettings
# The Network Manager settings (native to Gnome), sets system-wide proxy settings
# but does not set up authentication
# which is not an issue except the case of apt, which does not work
# for authenticated proxy in this manner.
# Also, this script rules out the need of configuring proxy for apt separately.
# Also, as tested, it does not work for bash/Terminal.
# Thus, we need to configure not one but three things:
# gsettings, apt, and environment variables.
#
# ASSUMPTIONS :
# toggle checks current mode taking gsettings as default
#
# So, lets proceed.
# function to configure environment variables
configure_environment() {
# configure_environment $http_host $http_port $use_auth $use_same $username $password $https_host $https_port $ftp_host $ftp_port $socks_host $socks_port
if [[ $4 == 'y' ]]; then
if [[ $3 == 'y' ]]; then
sed -i "s/<PROXY>/$5\:$6\@$1\:$2/g" bash_set.conf
else
sed -i "s/<PROXY>/$1:$2/g" bash_set.conf
fi
elif [[ $4 == 'n' ]]; then
if [[ $3 == 'y' ]]; then
auth=$5:$6@
else
auth=''
fi
sed -i '/proxy\|proxy\|PROXY/d' bash_set.conf
echo "http_proxy=$auth$1:$2" > bash_set.conf
echo "HTTP_PROXY=$auth$1:$2" >> bash_set.conf
echo "https_proxy=$auth$7:$8" >> bash_set.conf
echo "HTTPS_PROXY=$auth$7:$8" >> bash_set.conf
echo "ftp_proxy=$auth$9:$10" >> bash_set.conf
echo "FTP_PROXY=$auth$9:$10" >> bash_set.conf
echo "socks_proxy=$auth$11:$12" >> bash_set.conf
echo "SOCKS_PROXY=$auth$11:$12" >> bash_set.conf
read -e -p "Enter proxy settings for rsync in format host:port " -i $1:$2 rsync
echo "rsync_proxy=$auth$rsync" >> bash_set.conf
echo "RSYNC_PROXY=$auth$rsync" >> bash_set.conf
fi
if [[ $3 == "y" ]]; then # require authentication
sed -i "s/<PROXY>/$4:$5@$1:$2/g" bash_set.conf
else
sed -i "s/<PROXY>/$1:$2/g" bash_set.conf
fi
if [[ -e "$HOME/.bashrc" ]]; then
cat bash_set.conf >> $HOME/.bashrc
fi
if [[ -e "$HOME/.bash_profile" ]]; then
cat bash_set.conf >> $HOME/.bash_profile
fi
read -p "Configure Terminal proxy for root user? (at /etc/environment) ? (y/n) " -e
if [[ $REPLY = 'y' ]]; then
case "$superusermethod" in
0) echo "You need sudo/root privileges"
break
;;
1) if [[ -e "/etc/environment" ]]; then
cat bash_set.conf | sudo tee -a "/etc/environment" > /dev/null
else
cat bash_set.conf | sudo tee -a "/etc/environment" > /dev/null
fi
;;
2) if [[ -e "/etc/environment" ]]; then
cat bash_set.conf | tee -a "/etc/environment" > /dev/null
else
cat bash_set.conf | tee -a "/etc/environment" > /dev/null
fi
;;
esac
fi
}
configure_apt() {
#configure_apt $http_host $http_port $use_auth $use_same $username $password $https_host $https_port $ftp_host $ftp_port $socks_host $socks_port
if [[ ! -e "/etc/apt" ]]; then
echo "/etc/apt/ does not exist. Make sure apt is configured properly on this system."
return 1
fi
if [[ $3 == "n" ]]; then
if [[ $4 == "y" ]]; then
sed -i "s/<HOST>/$http_host/g" apt_config.conf
sed -i "s/<PORT>/$http_port/g" apt_config.conf
sed -i "s/<USERID>\:<PASSWORD>\@//g" apt_config.conf
elif [[ $4 == "n" ]]; then
echo Acquire\:\:Http\:\:Proxy\ \"http\:\/\/$1\:$2\/\"\; > apt_config.conf
echo Acquire\:\:Https\:\:Proxy\ \"https\:\/\/$7\:$8\/\"\; >> apt_config.conf
echo Acquire\:\:Ftp\:\:Proxy\ \"Ftp\:\/\/$9\:$10\/\"\; >> apt_config.conf
echo Acquire\:\:Socks\:\:Proxy\ \"socks\:\/\/$11\:$12\/\"\; >> apt_config.conf
else
echo "Error!"
exit 1
fi
elif [[ $3 == "y" ]]; then
if [[ $4 == "y" ]]; then
sed -i "s/<USERID>/$5/g" apt_config.conf
sed -i "s/<PASSWORD>/$6/g" apt_config.conf
sed -i "s/<HOST>/$1/g" apt_config.conf
sed -i "s/<PORT>/$2/g" apt_config.conf
else
echo Acquire\:\:Http\:\:Proxy\ \"http\:\/\/$5\:$6\@$1\:$2\/\"\; > apt_config.conf
echo Acquire\:\:Https\:\:Proxy\ \"https\:\/\/$5\:$6\@$7\:$8\/\"\; >> apt_config.conf
echo Acquire\:\:Ftp\:\:Proxy\ \"ftp\:\/\/$5\:$6\@$9\:$10\/\"\; >> apt_config.conf
echo Acquire\:\:Socks\:\:Proxy\ \"socks\:\/\/$5\:$6\@$11\:$12\/\"\; >> apt_config.conf
fi
else
echo "Error encountered!"
exit 1
fi
case "$superusermethod" in
0) echo "You need sudo/root privileges"
return
;;
1) if [[ -e "/etc/apt/apt.conf" ]]; then
sudo touch "/etc/apt/apt.conf"
fi
cat apt_config.conf | sudo tee -a /etc/apt/apt.conf > /dev/null
;;
2) if [[ -e "/etc/apt/apt.conf" ]]; then
touch "/etc/apt/apt.conf"
fi
cat apt_config.conf | tee -a /etc/apt/apt.conf > /dev/null
;;
esac
}
configure_gsettings() {
# configure_gsettings $http_host $http_port $use_auth $use_same $username $password $https_host $https_port $ftp_host $ftp_port $socks_host $socks_port
if [[ "$gsettingsavailable" = '' ]];then
return
fi
gsettings set org.gnome.system.proxy mode 'manual'
if [[ $4 == "y" ]]; then
gsettings set org.gnome.system.proxy use-same-proxy true
gsettings set org.gnome.system.proxy.http enabled true
gsettings set org.gnome.system.proxy.http host "'$1'";
gsettings set org.gnome.system.proxy.http port "$2";
gsettings set org.gnome.system.proxy.https host "'$1'"
gsettings set org.gnome.system.proxy.https port "$2";
# gsettings set org.gnome.system.proxy.socks host "'$1'"
# gsettings set org.gnome.system.proxy.socks port "$2";
gsettings set org.gnome.system.proxy.ftp host "'$1'"
gsettings set org.gnome.system.proxy.ftp port "$2";
else
gsettings set org.gnome.system.proxy use-same-proxy false
gsettings set org.gnome.system.proxy.http enabled true
gsettings set org.gnome.system.proxy.http host "'$1'"
gsettings set org.gnome.system.proxy.http port "$2"
gsettings set org.gnome.system.proxy.https host "'$7'"
gsettings set org.gnome.system.proxy.https port "$8"
# gsettings set org.gnome.system.proxy.socks host "'$11'"
# gsettings set org.gnome.system.proxy.socks port "$12"
gsettings set org.gnome.system.proxy.ftp host "'$9'"
gsettings set org.gnome.system.proxy.ftp port "$10"
fi
if [[ $3 == "y" ]]; then
gsettings set org.gnome.system.proxy.http use-authentication true
gsettings set org.gnome.system.proxy.http authentication-user "'$5'"
gsettings set org.gnome.system.proxy.http authentication-password "'$6'"
else
gsettings set org.gnome.system.proxy.http use-authentication false
gsettings set org.gnome.system.proxy.http authentication-user "''"
gsettings set org.gnome.system.proxy.http authentication-password "''"
fi
}
configure_npm() {
# configure_npm $http_host $http_port $use_auth $use_same $username $password $https_host $https_post
echo "1: $1, 2: $2, 3: $3, 4: $4, 5: $5, 6: $6, 7: $7, 8: $8"
if [ "$4" = "y" ]; then
if [ "$3" = "y" ]; then
npm config set proxy "http://$5:$6@$1:$2/"
npm config set https-proxy "https://$5:$6@$1:$2/"
else
npm config set proxy "http://$1:$2/"
npm config set proxy "https://$1:$2/"
fi
else
if [ "$3" = "y" ]; then
npm config set proxy "http://$5:$6@$1:$2/"
npm config set https-proxy "https://$5:$6@$7:$8/"
else
npm config set proxy "http://$1:$2/"
npm config set proxy "https://$7:$8/"
fi
fi
}
unset_environment() {
# unset all environment variables from bash_profile and bashrc
if [[ -e "$HOME/.bash_profile" ]]; then
sed -i '/proxy\|PROXY\|Proxy/d' ~/.bash_profile
sed -i '/ProxyMan/d' ~/.bash_profile
sed -i '/github\.com/d' ~/.bash_profile
sed -i '/Alan\ Pope/d' ~/.bash_profile
sed -i '/end\ of\ proxy\ settings/d' ~/.bash_profile
fi
if [[ -e "$HOME/.bashrc" ]]; then
sed -i '/proxy\|PROXY\|Proxy/d' ~/.bashrc
sed -i '/ProxyMan/d' ~/.bashrc
sed -i '/github\.com/d' ~/.bashrc
sed -i '/Alan\ Pope/d' ~/.bashrc
sed -i '/end\ of\ proxy\ settings/d' ~/.bashrc
fi
read -p "modify root user settings (/etc/environment) ? (y/n) " -e
if [[ $REPLY = 'y' ]]; then
# adding settings for /etc/environment
case "$superusermethod" in
0) echo "You need sudo/root privileges"
return
;;
1) sudo sed -i '/proxy\|PROXY\|Proxy/d' /etc/environment
sudo sed -i '/ProxyMan/d' /etc/environment
sudo sed -i '/github\.com/d' /etc/environment
sudo sed -i '/Alan\ Pope/d' /etc/environment
sudo sed -i '/end\ of\ proxy\ settings/d' /etc/environment
;;
2) sed -i '/proxy\|PROXY\|Proxy/d' /etc/environment
sed -i '/ProxyMan/d' /etc/environment
sed -i '/github\.com/d' /etc/environment
sed -i '/Alan\ Pope/d' /etc/environment
sed -i '/end\ of\ proxy\ settings/d' /etc/environment
;;
esac
fi
}
unset_gsettings() {
if [[ "$gsettingsavailable" = '' ]];then
return
fi
gsettings set org.gnome.system.proxy mode 'none'
gsettings set org.gnome.system.proxy.http use-authentication false
gsettings set org.gnome.system.proxy.http authentication-user "''"
gsettings set org.gnome.system.proxy.http authentication-password "''"
}
unset_apt() {
if [[ -e "/etc/apt/apt.conf" ]]; then
case "$superusermethod" in
0) echo "You need sudo/root privileges"
return
;;
1) sudo sed -i '/Proxy/d' /etc/apt/apt.conf
;;
2) sed -i '/Proxy/d' /etc/apt/apt.conf
;;
esac
fi
}
unset_npm() {
npm config rm proxy
npm config rm https-proxy
}
set_parameters() {
# echo "Set parameters received" $1
echo "HTTP parameters : "
read -p "Enter Host IP : " http_host
read -p "Enter Host Port : " http_port
read -e -p "Enable authentication (y/n) : " use_auth
if [[ $use_auth == 'y' ]]; then
read -p "Enter Proxy Username : " username
echo -n "Enter password (%40 for @) : "
read -s password
echo
fi
read -e -p "Use the same values for all http, https, ftp, socks (y/n) : " use_same
if [[ $use_same == 'n' ]]; then
echo
echo "FTP parameters : "
read -p "Enter Host IP : " ftp_host
read -p "Enter Host Port : " ftp_port
echo "HTTPS parameters : "
read -p "Enter Host IP : " https_host
read -p "Enter Host Port : " https_port
echo "SOCKS parameters : "
read -p "Enter Host IP : " socks_host
read -p "Enter Host Port : " socks_port
fi
if [[ $1 == "ALL" ]]; then
configure_apt $http_host $http_port $use_auth $use_same $username $password $https_host $https_port $ftp_host $ftp_port $socks_host $socks_port
configure_gsettings $http_host $http_port $use_auth $use_same $username $password $https_host $https_port $ftp_host $ftp_port $socks_host $socks_port
configure_environment $http_host $http_port $use_auth $use_same $username $password $https_host $https_port $ftp_host $ftp_port $socks_host $socks_port
configure_npm $http_host $http_port $use_auth $use_same $username $password $https_host $https_port
elif [[ $1 -eq 1 ]]; then
configure_environment $http_host $http_port $use_auth $use_same $username $password $https_host $https_port $ftp_host $ftp_port $socks_host $socks_port
elif [[ $1 -eq 2 ]]; then
configure_gsettings $http_host $http_port $use_auth $use_same $username $password $https_host $https_port $ftp_host $ftp_port $socks_host $socks_port
elif [[ $1 -eq 3 ]]; then
configure_apt $http_host $http_port $use_auth $use_same $username $password $https_host $https_port $ftp_host $ftp_port $socks_host $socks_port
elif [[ $1 -eq 4 ]]; then
configure_npm $http_host $http_port $use_auth $use_same $username $password $https_host $https_port
else
echo "Invalid arguments! ERROR encountered."
exit 1
fi
}
# Here is where the script starts
# Above were the functions to be used
clear
echo "
MESSAGE : In case of options, one value is displayed as the default value.
Do erase it to use other value.
ProxyMan v1.8
This script is documented in README.md file.
There are the following options for this script
TASK : DESCRIPTION
toggle : Toggle current mode ( none <--> manual ) for Desktop-environment,
with old configuration
set : Configure all settings, recommended (to configure)
unset : Remove proxy settings from all places
sfew : Apply proxy settings from selective locations
ufew : Unset proxy settings from selective locations
q : Quit this program
"
read -p "Enter your choice : " choice
if [[ choice == 'q' ]]; then
exit
fi
echo "Enter your system password (if asked)..."
# create temporary files with extension .conf to be configured
if [[ -e "apt_config.config" && -e "bash_set.config" ]]; then
cp apt_config.config apt_config.conf
cp bash_set.config bash_set.conf
else
echo "Required files are missing. Please check for files apt_config.bak and bash_set.bak" >&2
exit 1
fi
# check for possibilities if sudo is not available or sudo is not configured
sudoperms=false
sudoavailable="$(which sudo)"
superusermethod=0
# 1 means sudo, 2 means is root user, 0 means can't be super user... a lame one
if [[ "$sudoavailable" = '' ]]; then
sudoavailable=false
if [[ "$USER" = 'root' ]]; then
superusermethod=2
else
superusermethod=0
fi
else
sudoavailable=true
sudo -v &> /dev/null && sudoperms="true" || sudoperms="false"
if [[ "$sudoperms" = "true" ]]; then
superusermethod=1
else
superusermethod=0
fi
fi
gsettingsavailable="$(which gsettings)"
npmavailable="$(which npm)"
# take inputs and perform as necessary
case "$choice" in
toggle) if [[ "$gsettingsavailable" = '' ]]; then
echo "Desktop environment proxy settings are available for GNOME based environments."
echo "Please refer to the conventional GUI method for your DE."
break
fi
mode=$(gsettings get org.gnome.system.proxy mode)
if [ $mode == "'none'" ]; then
gsettings set org.gnome.system.proxy mode 'manual'
elif [ $mode == "'manual'" ]; then
gsettings set org.gnome.system.proxy mode 'none'
else
echo "Invalid values found! Please recheck gsettings."
exit
fi
auth=$(gsettings get org.gnome.system.proxy.http use-authentication)
if [[ $auth = 'true' && $mode = "'none'" ]]; then
read -p "Remove authentication credentials (id/password) saved on this system : " -i "y"
if [[ $REPLY = "y" ]]; then
gsettings set org.gnome.system.proxy.http use-authentication false
gsettings set org.gnome.system.proxy.http authentication-user "''"
gsettings set org.gnome.system.proxy.http authentication-password "''"
else
echo "Your login credentials still exist on this system"
echo "Take care! :)"
fi
fi
echo "Operation completed successfully."
;;
set) if [[ "$gsettingsavailable" != '' ]]; then
unset_gsettings
fi
unset_environment
unset_apt
set_parameters ALL
;;
unset) if [[ "$gsettingsavailable" != '' ]]; then
unset_gsettings
fi
unset_apt
unset_environment
if [[ "$npmavailable" != '' ]]; then
unset_npm
fi
;;
sfew) echo
echo "Where do you want to set proxy?"
echo "1 : Terminal only."
echo "2 : Desktop-environment/GUI and apps"
echo "3 : APT/Software Center only"
echo "4 : npm proxy settings only"
read response
if [[ $response -gt 3 ]]; then
echo "Invalid option."
elif [[ $response -eq 1 ]]; then
unset_environment
fi
set_parameters $response
;;
ufew) echo
echo "Where do you want to unset proxy?"
echo "1 : Terminal only."
echo "2 : Desktop-environment/GUI and apps"
echo "3 : APT/Software Center only"
echo "4 : npm proxy settings only"
read
case $REPLY in
1) unset_environment
;;
2) unset_gsettings
;;
3) unset_apt
;;
4) unset_npm
;;
*) echo "Invalid arguments! Please retry."
exit 1
;;
esac
;;
q) ;;
*) exit 1
;;
esac
rm apt_config.conf bash_set.conf
echo "Job done!"
echo "Thanks for using. :-)"