-
Notifications
You must be signed in to change notification settings - Fork 0
/
poc-httpd.zsh
executable file
·116 lines (101 loc) · 3.18 KB
/
poc-httpd.zsh
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
#!/bin/zsh
#
# start with: $0 start
#
# POC "built-in" httpd for bp-letsenc.
# start before any "renew"
#
# Requirements:
# * socat
# * zsh (replacing '(P)' with '!' in trim() schould make it work with bash)
#
# License:
## Copyright (c) 2017-2019 brainpower <brainpower at mailbox dot org>
##
## Permission is hereby granted, free of charge, to any person obtaining a copy
## of this software and associated documentation files (the "Software"), to deal
## in the Software without restriction, including without limitation the rights
## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
## copies of the Software, and to permit persons to whom the Software is
## furnished to do so, subject to the following conditions:
##
## The above copyright notice and this permission notice shall be included in
## all copies or substantial portions of the Software.
##
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
## AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
## THE SOFTWARE.
script_dir="$(dirname $(readlink -f "${0}"))"
# default
acmedir="/srv/acme-challenges/"
# read configuration from config.zsh if readable
if [[ -r "${script_dir}/config.zsh" ]]; then
source "${script_dir}/config.zsh"
fi
function error404() {
printf "HTTP/1.1 404 Not Found\nContent-Length: 0\n\n"
exit 0
}
function trim() {
read -rd '' $1 <<<"${(P)1}"
}
function do_reply(){
request="$(head -1)"
method="${request%% *}"
url="${request#* }"
url="${url/ HTTP\/*/}"
trim url
# debug:
#printf "Request for '%s' using '%s'\n" "$url" "$method" >&2
if [[ $method != "GET" ]]; then
error404 # this "server" only answers to GET requests
elif [[ ! $url =~ ^/.well-known/acme-challenge/.* ]]; then
error404 # this server only answers to acme challenges
else
file="${url#\/.well-known\/acme-challenge\/}"
#printf "Parsed filename: '%s'\n" "$file" >&2
if ! [[ -r "${acmedir}/${file}" ]]; then
error404
fi
size="$(stat --format '%s' "${acmedir}/${file}")"
# http headers
printf "HTTP/1.1 200 OK\n"
printf "Server: poc-httpd.zsh\n"
printf "Content-Type: text/plain\n"
if [[ -n "$size" ]]; then
printf "Content-Length: %s\n" "$size"
fi
printf "\n"
# http body
cat "${acmedir}/${file}"
fi
}
# main
case $1 in
reply)
do_reply
;;
start)
if ! [[ -r "${pidfile}" ]]; then
socat TCP-LISTEN:80,crlf,reuseaddr,fork SYSTEM:"$0 reply" &
printf "%s" "$!" > ${pidfile}
else
echo "Pidfile exists. Already started?"
exit 1
fi
;;
stop)
kill -15 "$(cat ${pidfile})"
rm -rf "${pidfile}"
;;
*)
printf "Usage: $0 (start|stop|reply)\n"
printf " reply for internal use\n"
printf " start starts socat listening to port 80 and calling $0 reply for every request\n"
printf " stop kills the started socat\n\n"
;;
esac