-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathTSsetproxy
More file actions
executable file
·248 lines (222 loc) · 6.26 KB
/
TSsetproxy
File metadata and controls
executable file
·248 lines (222 loc) · 6.26 KB
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
#!/bin/bash
# Copyright (C) 2011 Ion Torrent Systems, Inc. All Rights Reserved
set -e
#--------------------------------------
#--- Include function definition file
#--------------------------------------
TSCONFIG_SRC_DIR='/usr/share/ion-tsconfig'
source $TSCONFIG_SRC_DIR/ts_params
source $TSCONFIG_SRC_DIR/ts_functions
#--------------------------------------
# Functions
#--------------------------------------
function print_help ()
{
echo
echo "Usage: $0 [option]..."
echo -e "\t--address Proxy address (example: 'http://proxy.net')"
echo -e "\t--port Proxy port number (default: 3128)"
echo -e "\t--username Username for authentication"
echo -e "\t--password Password for authentication"
echo -e "\t--remove Removes proxy setting"
echo -e "\t--debug, -d Prints script commands when executing (set -x)"
echo -e "\t--demo Prints what changes would be executed only. No changes are made"
echo -e "\t--help, -h Prints command line args"
echo -e "\t--version, -v Prints version"
echo
echo "Executing the command with no options will force prompting of each parameter"
echo
}
#--------------------------------------
# Trap exit
#--------------------------------------
trap 'error ${LINENO}' ERR
#--------------------------------------
# Default settings
#--------------------------------------
version=`echo '$Revision: 21948 $'|awk '{print $2}'`
DEMO=0
interactive=1
REMOVE=0
environment="/etc/environment"
# Log command line arguments
cmdline=${@}
#--------------------------------------
# command line argument parsing
#--------------------------------------
#--- We convert all arguments to lower case ---#
while [ $# != 0 ]; do
case ${1,,} in
'--help'|'-h')
print_help
exit 0
;;
'--version'|'-v')
echo "`basename $0` version $version"
if [ -f /opt/ion/bin/ion-tsconfig.version.env ]; then
source /opt/ion/bin/ion-tsconfig.version.env
IonVersionGetFullVersion ion-tsconfig
fi
exit 0
;;
'--debug'|'-d')
set -x
;;
'--demo')
DEMO=1
;;
'--address')
shift
proxy_addr=$1
interactive=0
;;
'--port')
shift
proxy_port=$1
interactive=0
;;
'--username')
shift
username=$1
;;
'--password')
shift
password=$1
;;
'--remove')
REMOVE=1
;;
*)
log "Unknown option: $1"
echo "Unknown option: $1. Exiting"
echo "Use TSsetproxy --help for options."
exit 1
;;
esac
shift
done
needs_root
# Log command line arguments
log "$cmdline"
#--------------------------------------
# Handle remove argument and skip the rest
#--------------------------------------
if [ $REMOVE -eq 1 ]; then
if [ $DEMO -eq 1 ]; then
echo
echo "Current $environment file contents are:"
echo
cat $environment
echo
echo "Would be modified to:"
echo
sed "/^http_proxy/d" $environment
echo
else
# Remove existing proxy setting
sed -i "/^http_proxy/d" $environment
unset http_proxy
echo -e "\nRebooting will ensure the http_proxy environment variable is cleared.\n"
fi
log "$0 completed successfully"
exit 0
fi
if [ -z $proxy_addr ]; then
repeat=1
while [ $repeat -eq 1 ]
do
read -p "Enter http proxy address: " answer
case $answer in
"")
echo "No address entered. Ctrl-C to cancel."
repeat=1
;;
*)
proxy_addr=$answer
repeat=0
esac
done
fi
# check for trailing slash
if echo $proxy_addr | grep -E "\/$"; then
proxy_addr=$(echo $proxy_addr | sed -E "s:/$::")
fi
# check for http prefix
if ! echo $proxy_addr | grep -E "^https?:\/\/"; then
proxy_addr="http://"$proxy_addr
fi
if [ -z $proxy_port ]; then
# = get prox port = #
read -p "Enter http proxy port number [3128]: " answer
case $answer in
"")
echo "Use default port 3128"
proxy_port=3128
;;
*)
proxy_port=$answer
esac
fi
#TODO: test for valid port
if [ $interactive -eq 1 ]; then
# Query for the optional parameters: username password for authentication
# = get user name and password = #
read -p "Enter the username for proxy authentication: " answer
case $answer in
"")
echo "no authentication is set"
nouser=1
;;
*)
nouser=0
username=$answer
esac
if [ $nouser -eq 0 ]; then
read -p "Enter the password for proxy authentication: " answer
case $answer in
"")
echo "no password is set. No authentication will be set."
nouser=1
;;
*)
password=$answer
esac
fi
if [ $nouser -eq 0 ]; then
proxy_addr=$(echo $proxy_addr | sed -E "s:^(https?\://):\1$username\:$password@:")
fi
else
# Handle username password command line arguments
if [ ! -z $username ]; then
if [ ! -z $password ]; then
proxy_addr=$(echo $proxy_addr | sed -E "s:^(https?\://):\1$username\:$password@:")
else
echo "ERROR: username specified but no password provided"
exit 1
fi
else
# no username provided; skip authentication
:
fi
fi
if [ $DEMO -eq 1 ]; then
# Print what would happen and exit
echo
echo "Current contents of $environment:"
echo
cat $environment
echo
echo "will be written to $environment:"
echo
echo "http_proxy=$proxy_addr:$proxy_port"
echo
else
# = remove previous proxy information
sed -i '/http_proxy/d' $environment
# = write to $environment file and reload it
echo "http_proxy="$proxy_addr:$proxy_port >> $environment
source $environment
echo "http_proxy is set to "$http_proxy
fi
log "$0 completed successfully"
exit 0