-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_https_server.sh
More file actions
executable file
·48 lines (40 loc) · 1.16 KB
/
simple_https_server.sh
File metadata and controls
executable file
·48 lines (40 loc) · 1.16 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
#!/bin/bash
#
# simple_https_server.sh
#
# Starts an HTTPS server with a temporary auto-generated cert.
# Uses php and ncat.
#
# kill all processes on exit
trap "trap - SIGTERM && kill -- -$$" SIGINT SIGTERM EXIT
# print help if requested
if [[ "$1" == '-h' || "$1" == '--help' ]]; then
echo 'Usage:' >&2
echo " $0 [port] [web_root]" >&2
echo >&2
echo 'Defaults to TCP 443 in current directory' >&2
exit 1
fi
# override default TLS port if specified
TLS_PORT="$1"
if [[ -z "$TLS_PORT" ]]; then
TLS_PORT=443
fi
# if TLS port is privileged, prompt for sudo password early to
# avoid output stream confusion
if [[ "$TLS_PORT" -lt 1024 ]]; then
sudo echo > /dev/null
SUDO_OR_NOT=sudo
fi
# if web root directory was provided, go there
WEB_ROOT="$2"
if [[ ! -z "$WEB_ROOT" ]]; then
cd "$WEB_ROOT"
fi
# pick high end port for web service
ACTUAL_WEB_PORT=$(shuf -i 49152-65535 -n 1)
# open port forward between TLS port and actual web service
echo Starting TLS forwarder on port "$TLS_PORT"
$SUDO_OR_NOT ncat -k -lnp "$TLS_PORT" --ssl -c "ncat localhost $ACTUAL_WEB_PORT" &
# start web service on high end port
php -S localhost:"$ACTUAL_WEB_PORT"