-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server
462 lines (406 loc) · 12.7 KB
/
Server
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
#!/bin/bash
# Default port number
default_port=2342
# Default log file path
default_log_file="server.log"
# Default index.html path
default_index_file="index.html"
# Maximum number of concurrent connections
max_connections=100
#!/bin/bash
# Check if python3 is installed and install it if not found
if ! command -v python3 >/dev/null; then
echo "Python3 is not found. Installing Python3..."
sudo apt update
sudo apt install -y python3
fi
# Check if php is installed and install it if not found
if ! command -v php >/dev/null; then
echo "PHP is not found. Installing PHP..."
sudo apt update
sudo apt install -y php
fi
# Get local IP address
ip_address=$(hostname -I | awk '{print $1}')
# Function to check if a port is available
check_port() {
local port=$1
(exec 3<>/dev/tcp/localhost/$port) >/dev/null 2>&1
if [ $? -eq 0 ]; then
exec 3>&-
exec 3<&-
return 1
else
exec 3<&-
return 0
fi
}
# Function to check if the maximum number of connections has been reached
check_max_connections() {
local current_connections=$(netstat -ant | grep ESTABLISHED | wc -l)
if [ "$current_connections" -ge "$max_connections" ]; then
echo "Maximum number of connections reached. Unable to start the server."
exit 1
fi
}
# Function to log server startup information
log_startup_info() {
local startup_info=$1
local log_file=$2
echo "$startup_info" >> "$log_file"
}
# Function to display an error message for missing required options
display_missing_option_error() {
local option_name=$1
echo "Missing required option: -$option_name" >&2
exit 1
}
# Function to stop the server gracefully
stop_server() {
echo "Stopping the server..."
kill $server_pid
wait $server_pid >/dev/null 2>&1
delete_log_file
}
# Function to delete the log file
delete_log_file() {
rm -f "$log_file"
}
# Register the stop_server function to catch termination signals
trap stop_server SIGINT SIGTERM
# Set default port if not provided
port=$default_port
# Set default timeout to -NoTimeOut
timeout="-NoTimeOut"
# Parse command line arguments
while getopts "p:t:l:i:e:cd:a:n:s:x:y:o:r:z:6m:gfu:C:H:S:E:qB:U:P:RL:X:Z:j:k:w:y:v:h" opt; do
case ${opt} in
p)
port=$OPTARG
;;
t)
timeout=$OPTARG
;;
l)
log_file=$OPTARG
;;
i)
index_file=$OPTARG
;;
e)
error_log_file=$OPTARG
;;
c)
enable_cgi=true
;;
d)
enable_directory_listing=true
;;
a)
server_address=$OPTARG
;;
n)
server_hostname=$OPTARG
;;
s)
enable_ssl=true
;;
x)
ssl_certificate=$OPTARG
;;
y)
ssl_key=$OPTARG
;;
o)
enable_access_log=true
access_log_file=$OPTARG
;;
r)
enable_log_rotation=true
;;
z)
log_rotation_size=$OPTARG
;;
6)
enable_ipv6=true
;;
m)
server_name=$OPTARG
;;
g)
enable_gzip=true
;;
f)
enable_caching=true
;;
u)
cache_control_header=$OPTARG
;;
C)
enable_cors=true
;;
H)
cors_header=$OPTARG
;;
S)
enable_ssi=true
;;
E)
ssi_file_extension=$OPTARG
;;
q)
quiet_mode=true
;;
B)
enable_basic_auth=true
;;
U)
username=$OPTARG
;;
P)
password=$OPTARG
;;
R)
enable_https_redirection=true
;;
L)
redirection_url=$OPTARG
;;
X)
enable_server_side_scripting=true
;;
Z)
server_side_script_extension=$OPTARG
;;
j)
enable_request_logging=true
;;
k)
request_log_file=$OPTARG
;;
w)
enable_url_rewriting=true
;;
y)
rewrite_rules=$OPTARG
;;
v)
debug_mode=true
;;
h)
echo "Usage: ./start_server.sh [-p <port>] [-t <timeout>] [-l <log_file>] [-i <index_file>] [-e <error_log_file>] [-c] [-d] [-a <address>] [-n <hostname>] [-s] [-x <ssl_certificate>] [-y <ssl_key>] [-o <access_log_file>] [-r] [-z <rotation_size>] [-6] [-m <server_name>] [-g] [-f] [-u <cache_control_header>] [-C] [-H <cors_header>] [-S] [-E <ssi_file_extension>] [-q] [-B] [-U <username>] [-P <password>] [-R] [-L <redirection_url>] [-X] [-Z <server_side_script_extension>] [-j] [-k <request_log_file>] [-w] [-y <rewrite_rules>] [-v] [-h]"
echo "Options:"
echo " -p <port> Set the port number (default: $default_port)"
echo " -t <timeout> Set the server timeout in seconds (default: -NoTimeOut)"
echo " -l <log_file> Set the path to the log file (default: $default_log_file)"
echo " -i <index_file> Set the path to the index.html file (default: $default_index_file)"
echo " -e <error_log_file> Set the path to the error log file"
echo " -c Enable CGI support"
echo " -d Enable directory listing"
echo " -a <address> Set the server address to bind"
echo " -n <hostname> Set the server hostname"
echo " -s Enable SSL/TLS encryption"
echo " -x <ssl_certificate> Set the path to the SSL certificate file"
echo " -y <ssl_key> Set the path to the SSL key file"
echo " -o <access_log_file> Enable access log and set the path to the access log file"
echo " -r Enable log rotation"
echo " -z <rotation_size> Set the log rotation size in bytes"
echo " -6 Enable IPv6 support"
echo " -m <server_name> Set the custom server name"
echo " -g Enable Gzip compression"
echo " -f Enable caching"
echo " -u <cache_control_header> Set the Cache-Control header value"
echo " -C Enable CORS support"
echo " -H <cors_header> Set the Access-Control-Allow-Origin header value"
echo " -S Enable server-side includes (SSI)"
echo " -E <ssi_file_extension> Set the server-side includes file extension"
echo " -q Enable quiet mode"
echo " -B Enable basic authentication"
echo " -U <username> Set the username for basic authentication"
echo " -P <password> Set the password for basic authentication"
echo " -R Enable HTTPS redirection"
echo " -L <redirection_url> Set the custom URL for redirection"
echo " -X Enable server-side scripting"
echo " -Z <server_side_script_extension> Set the server-side script extension"
echo " -j Enable request logging"
echo " -k <request_log_file> Set the path to the request log file"
echo " -w Enable URL rewriting"
echo " -y <rewrite_rules> Set the URL rewrite rules"
echo " -v Enable debug mode"
echo " -h Display this help message"
exit 0
;;
*)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
esac
done
# Set default log file if not provided
log_file=${log_file:-$default_log_file}
# Set default index file if not provided
index_file=${index_file:-$default_index_file}
# Check if the specified port is available
if ! check_port "$port"; then
echo "Port $port is already in use. Please specify a different port."
exit 1
fi
# Call the check_max_connections function
check_max_connections
# Start the Python HTTP server command
http_server_command="python3 -m http.server $port --bind $ip_address --directory ."
# Add options for CGI support and directory listing
if [ "$enable_cgi" = true ]; then
http_server_command+=" --cgi"
fi
if [ "$enable_directory_listing" = true ]; then
http_server_command+=" --directory-listing"
fi
# Add options for server address and hostname
if [ -n "$server_address" ]; then
http_server_command+=" --bind $server_address"
fi
if [ -n "$server_hostname" ]; then
http_server_command+=" --hostname $server_hostname"
fi
# Add options for SSL/TLS encryption
if [ "$enable_ssl" = true ]; then
http_server_command+=" --ssl"
if [ -n "$ssl_certificate" ] && [ -n "$ssl_key" ]; then
http_server_command+=" --cert $ssl_certificate --key $ssl_key"
else
echo "Please specify both the SSL certificate and key files."
exit 1
fi
fi
# Add options for access log and log rotation
if [ "$enable_access_log" = true ]; then
http_server_command+=" --access-log $access_log_file"
fi
if [ "$enable_log_rotation" = true ]; then
if [ -z "$log_rotation_size" ]; then
echo "Please specify the log rotation size using the -z option."
exit 1
fi
http_server_command+=" --log-rotation $log_rotation_size"
fi
# Add options for IPv6 support
if [ "$enable_ipv6" = true ]; then
http_server_command+=" --ipv6"
fi
# Add options for custom server name
if [ -n "$server_name" ]; then
http_server_command+=" --server-name $server_name"
fi
# Add options for Gzip compression
if [ "$enable_gzip" = true ]; then
http_server_command+=" --gzip"
fi
# Add options for caching
if [ "$enable_caching" = true ]; then
http_server_command+=" --cache"
fi
# Add options for custom cache control header
if [ -n "$cache_control_header" ]; then
http_server_command+=" --cache-control $cache_control_header"
fi
# Add options for CORS support
if [ "$enable_cors" = true ]; then
http_server_command+=" --cors"
fi
# Add options for custom CORS header
if [ -n "$cors_header" ]; then
http_server_command+=" --cors-header $cors_header"
fi
# Add options for server-side includes (SSI)
if [ "$enable_ssi" = true ]; then
http_server_command+=" --ssi"
fi
# Add options for HTTP/2 support
if [ "$enable_http2" = true ]; then
http_server_command+=" --http2"
fi
# Add options for custom SSI file extension
if [ -n "$ssi_file_extension" ]; then
http_server_command+=" --ssi-extension $ssi_file_extension"
fi
# Add options for basic authentication
if [ "$enable_basic_auth" = true ]; then
if [ -z "$username" ] || [ -z "$password" ]; then
echo "Please specify both the username and password for basic authentication."
exit 1
fi
http_server_command+=" --basic-auth $username:$password"
fi
# Add options for HTTPS redirection
if [ "$enable_https_redirection" = true ]; then
http_server_command+=" --https-redirection"
if [ -n "$redirection_url" ]; then
http_server_command+=" --redirection-url $redirection_url"
fi
fi
# Add options for server-side scripting
if [ "$enable_server_side_scripting" = true ]; then
http_server_command+=" --server-side-scripting"
fi
# Add options for custom server-side script extension
if [ -n "$server_side_script_extension" ]; then
http_server_command+=" --script-extension $server_side_script_extension"
fi
# Add options for request logging
if [ "$enable_request_logging" = true ]; then
http_server_command+=" --request-logging"
if [ -n "$request_log_file" ]; then
http_server_command+=" --request-log-file $request_log_file"
fi
fi
# Add options for URL rewriting
if [ "$enable_url_rewriting" = true ]; then
http_server_command+=" --url-rewriting"
if [ -n "$rewrite_rules" ]; then
http_server_command+=" --rewrite-rules $rewrite_rules"
fi
fi
# Redirect output to the log file unless quiet mode is enabled
if [ "$quiet_mode" = true ]; then
http_server_command+=" > /dev/null 2>&1 &"
else
http_server_command+=" > $log_file 2>&1 &"
fi
# Start the Python HTTP server in the background
eval $http_server_command
# Store the process ID of the server
server_pid=$!
# Check if the server started successfully
if [ $? -ne 0 ]; then
echo "Failed to start the server."
exit 1
fi
# Log the successful server startup
log_startup_info "Server started successfully." "$log_file"
# Display the server startup time unless in quiet mode
if [ "$quiet_mode" != true ]; then
startup_time=$(date +"%Y-%m-%d %H:%M:%S")
echo "Server started at: $startup_time"
fi
# Open the website in the default web browser unless in quiet mode
if [ "$quiet_mode" != true ]; then
xdg-open "http://$ip_address:$port" >/dev/null 2>&1 &
fi
if [ "$debug_mode" = true ]; then
echo "Server is running. You can access the website at: http://$ip_address:$port"
echo "Log file: $log_file"
fi
# Check if the maximum number of connections has been reached before starting the server
check_max_connections
# Wait for the server to finish or until stopped manually
wait $server_pid
# Display the server stop time unless in quiet mode
if [ "$quiet_mode" != true ]; then
stop_time=$(date +"%Y-%m-%d %H:%M:%S")
echo "Server stopped at: $stop_time"
fi
# Log the server stoppage
log_startup_info "Server stopped at: $(date +"%Y-%m-%d %H:%M:%S")" "$log_file"
# Delete the log file
delete_log_file