forked from milos85vasic/Server-Factory-Utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy_install.sh
executable file
·260 lines (183 loc) · 6.26 KB
/
proxy_install.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
#!/bin/sh
here=$(dirname "$0")
working_directory="$1"
config_file_name="proxy.cfg"
set_enforce_script_name="setenforce.sh"
proxy_service_file_name="proxy.service"
proxy_update_execute_script_name="proxy_update_execute.sh"
load_configuration_script_name="proxy_load_configuration.sh"
config_file="$working_directory/$config_file_name"
config_file_source="$here/Proxy/$config_file_name"
set_enforce_script="$here/$set_enforce_script_name"
proxy_service="$working_directory/$proxy_service_file_name"
load_configuration_script="$here/$load_configuration_script_name"
proxy_update_execute_script="$here/$proxy_update_execute_script_name"
if test -e "$working_directory"; then
echo "Working directory is ready"
else
if mkdir -p "$working_directory"; then
echo "$working_directory: working directory has been created"
else
echo "ERROR: $working_directory working directory was not created"
exit 1
fi
fi
if test -e "$config_file"; then
if ! rm -f "$config_file"; then
echo "ERROR: $config_file could not be removed"
exit 1
fi
fi
if ! test -e "$config_file_source"; then
echo "ERROR: $config_file_source does not exist"
exit 1
fi
if mv "$config_file_source" "$config_file" &&
test -e "$config_file" && chmod 640 "$config_file"; then
echo "$config_file: proxy configuration file saved"
else
echo "ERROR: $config_file proxy configuration file not saved"
exit 1
fi
if ! test -e "$load_configuration_script"; then
echo "ERROR: $load_configuration_script does not exist"
exit 1
fi
# shellcheck disable=SC1090
. "$load_configuration_script"
load_configuration "$config_file"
# shellcheck disable=SC2154
if [ -z "$host" ]; then
if [ -z "$hostFallback" ]; then
echo "No Proxy configuration provided"
exit 0
fi
fi
msg1="Initializing Proxy for the first time"
# shellcheck disable=SC2154
msg2="Proxy init. parameters (1): (host=$host, hostFallback=$hostFallback, port=$port, account=$account, password=$password)"
# shellcheck disable=SC2154
msg3="Proxy init. parameters (2): (working_directory=$working_directory, log=$log)"
# shellcheck disable=SC2154
msg4="Proxy init. parameters (3): (certificate_endpoint=$certificate_endpoint, frequency=$frequency)"
# shellcheck disable=SC2154
msg5="Proxy init. parameters (4): (home=$home, behavior_get_ip=$behavior_get_ip, is_factory_service=$FACTORY_SERVICE)"
echo "$msg1"
echo "$msg2"
echo "$msg3"
echo "$msg4"
echo "$msg5"
if test -e "$log"; then
if rm -f "$log"; then
echo "$log: has been removed"
else
echo "WARNING: $log has not been removed"
fi
fi
if ! test -e "$proxy_update_execute_script"; then
echo "ERROR: $proxy_update_execute_script does not exist"
exit 1
fi
validate_ip_script="$here/validate_ip_address.sh"
if ! test -e "$validate_ip_script"; then
echo "ERROR: $validate_ip_script does not exist"
exit 1
fi
if [ -z "$FACTORY_SERVICE" ]; then
echo "Proxy update execution will be logged to: $log"
else
echo "Proxy update execution will be logged to standard output"
log="/dev/stdout"
fi
export PROXY_HOST_FALLBACK="$hostFallback"
# shellcheck disable=SC2154,SC2129
if sh "$proxy_update_execute_script" "$working_directory" "$host" "$port" \
"$account" "$password" "$certificate_endpoint" "$log" "$behavior_get_ip"; then
echo "Proxy has been initialized for the first time"
else
echo "ERROR: Could not initialize proxy for the first time"
if test "$here"/Proxy/"$proxy_service_file_name"; then
rm -f "$here"/Proxy/"$proxy_service_file_name"
fi
exit 1
fi
if test -e "$proxy_service"; then
if ! rm -f "$proxy_service"; then
echo "ERROR: $proxy_service could not be removed"
exit 1
fi
fi
systemd_service="/etc/systemd/system/$proxy_service_file_name"
if test -e systemd_service; then
echo "$systemd_service already exists, cleaning up"
if systemctl stop $proxy_service_file_name &&
systemctl disable $proxy_service_file_name >/dev/null 2>&1; then
echo "$systemd_service stopped and disabled"
fi
if rm -f "$systemd_service"; then
echo "$systemd_service removed"
else
echo "ERROR: $systemd_service could not be removed"
exit 1
fi
fi
if sh "$validate_ip_script" "$host" >/dev/null 2>&1; then
echo "Proxy host is IP address: $host"
else
if [ -z "$FACTORY_SERVICE" ]; then
if mv "$here"/Proxy/"$proxy_service_file_name" "$proxy_service" &&
test -e "$proxy_service" && chmod 640 "$proxy_service"; then
echo "$proxy_service: proxy service file saved"
if ! test -e "$set_enforce_script"; then
echo "ERROR: $set_enforce_script does not exits"
exit 1
fi
if cp "$proxy_service" "$systemd_service"; then
echo "$systemd_service is ready"
else
echo "ERROR: $proxy_service could not be created"
exit 1
fi
if "$set_enforce_script" >/dev/null 2>&1 &&
systemctl enable "$proxy_service_file_name" &&
systemctl start "$proxy_service_file_name"; then
if systemctl status "$proxy_service_file_name" | grep running >/dev/null 2>&1; then
echo "Proxy service started"
else
echo "ERROR: Proxy service is not running"
exit 1
fi
selinux_config_path="/etc/selinux"
selinux_config="$selinux_config_path/config"
selinux_config_backup="$selinux_config_path/config.bak"
if sestatus | grep -i "disabled" >/dev/null 2>&1; then
echo "SELinux is already disabled"
else
if test -e "$selinux_config"; then
if ! test -e "$selinux_config_backup"; then
echo "$selinux_config: backing up"
if ! mv "$selinux_config" "$selinux_config_backup"; then
echo "ERROR: $selinux_config could not backup"
exit 1
fi
fi
if echo "SELINUX=disabled" >"$selinux_config" &&
echo "SELINUXTYPE=targeted" | tee -a "$selinux_config" >/dev/null 2>&1; then
echo "SELinux is disabled"
else
echo "ERROR: Could not disable SELinux"
fi
else
echo "WARNING: $selinux_config not found"
fi
fi
else
echo "ERROR: Could not start proxy service"
exit 1
fi
else
echo "ERROR: $proxy_service proxy service file not saved"
exit 1
fi
fi
fi