-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloopTheScript_serveDefaultHtmlViaNc.sh
More file actions
executable file
·49 lines (42 loc) · 2.35 KB
/
loopTheScript_serveDefaultHtmlViaNc.sh
File metadata and controls
executable file
·49 lines (42 loc) · 2.35 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
#!/bin/sh
#
# Clients usually connect within X seconds after the timeout cycle of a choosen worker.
# Therefore they experience an actual delay of (($delay - X)) seconds [maximum: (($delay -2))].
#
# Complication (the last part I'm not sure about):
# The higher the $delay, the more ports ($usedPorts) you might need.
# If there are (at any time) more active connection than #usedPorts, the reverse-proxy will respond with an internal error (502).
# The more ports you use, the higher the probability that the actual waiting time will be lower than ((delay - 2)).
# If a single worker handles all request, any request will likely hit the first few seconds of this worker (spamming behavior).
# If a request is assigned (via random/alternation/..) to a worker, this worker might have been waiting longer than in the previous example.
## Customizeable defaults
delay=60
# The variable $usedPorts may be overwritten by the contents of $nginxFile
usedPorts="8080 8081 8082"
nginxFile="./exemplary_nginx-defaultsite"
# nginxFile="/etc/nginx/sites-enabled/default"
# These HTTP client-headers are uninteresting and should not be included in basic output
uselessHttpRequestHeaders="Connection|Accept|Accept-Language|Accept-Encoding|Upgrade-Insecure-Requests|Sec-Fetch-Dest|Sec-Fetch-Mode|Sec-Fetch-Site|If-Modified-Since|If-None-Match"
## Dependant variables
# Subordinate script, that manages netcat, pv (slow data streaming), and the HTTP-response headers
secondaryScript="./serveDefaultHtmlViaNc.sh"
# Nginx / Reverse-Proxy dependant variable
# If nginx config file exists/is readable:
# first grep: parse the whole 'upstream *name* {*list-of-servers*}' block
# second grep: parse the port numbers (individual lines)
[ -r "$nginxFile" ] && \
usedPorts=$(grep -Po -z "upstream [\w\s\-]+\{[^\}]*\}" "$nginxFile" \
| grep -Po -a "(?<=server localhost:)\d+" \
| tr '\n' ' ')
# create a self-looping worker for each port
for port in $usedPorts ; do
while /bin/true ; do
timeout "$delay" bash "$secondaryScript" "$port" "$((delay - 2))" \
| grep -Ev "($uselessHttpRequestHeaders): "
done &
# create an offset to prohibit simultaneous timeouts
sleep 1
done
# Overwrite the 'exit code' of this script (Neccessary as for systemd)
# 'while' has no exit code and the last statement (string comparisson) will always be false
true