Add httpd-utils

This commit is contained in:
Lucas 2020-12-28 23:24:06 +00:00
parent 174cc1338b
commit 2c86e788e9
5 changed files with 357 additions and 0 deletions

58
utils/httpd/Makefile Normal file
View File

@ -0,0 +1,58 @@
# httpd-utils
# Written in 2020 by Lucas
# CC0 1.0 Universal/Public domain - No rights reserved
#
# To the extent possible under law, the author(s) have dedicated all
# copyright and related and neighboring rights to this software to the
# public domain worldwide. This software is distributed without any
# warranty. You should have received a copy of the CC0 Public Domain
# Dedication along with this software. If not, see
# <http://creativecommons.org/publicdomain/zero/1.0/>.
.POSIX:
.SUFFIXES:
.SUFFIXES: .c
P = httpd-utils
V = 1
DOMAIN = example.net
DOMAIN_V4 = 4.${DOMAIN}
DOMAIN_V6 = 6.${DOMAIN}
IPV4 = 192.0.2.1
IPV6 = 2001:db8::2
PREFIX = /var/www/htdocs/${DOMAIN}
CGI = ip
SRC = ip.c gen-config.sh gen-index.sh
TXT = codes.txt
.c:
${CC} ${CFLAGS} ${LDFLAGS} -static -o $@ $<
all: ${DOMAIN}.httpd.conf index.html ${CGI}
${DOMAIN}.httpd.conf: gen-config.sh codes.txt
sh gen-config.sh ${DOMAIN} ${DOMAIN_V4} ${IPV4} ${DOMAIN_V6} ${IPV6} >$@
index.html: gen-index.sh codes.txt
sh gen-index.sh ${DOMAIN} ${DOMAIN_V4} ${DOMAIN_V6} >$@
clean:
rm -f ${DOMAIN}.httpd.conf index.html ${CGI} ${P}-${V}.tgz
dist: clean
pax -ws ',^,${P}-${V}/,' Makefile ${SRC} ${TXT} | gzip >${P}-${V}.tgz
install: all
cp -f ${DOMAIN}.httpd.conf /etc
mkdir -p ${PREFIX}
cp -f ${CGI} ${PREFIX}
cd ${PREFIX} && chmod 555 ${CGI}
cd ${PREFIX} && chown www:www ${CGI}
cp -f index.html ${PREFIX}
cd ${PREFIX} && chmod 444 index.html
cd ${PREFIX} && chown www:www index.html
uninstall:
rm -f /etc/${DOMAIN}.httpd.conf
cd ${PREFIX} && rm -f ${CGI} index.html

70
utils/httpd/codes.txt Normal file
View File

@ -0,0 +1,70 @@
# Informational 1xx
100
101
102
# Successful 2xx
200
201
202
203
204
205
206
207
208
226
# Redirection 3xx
300
301
302
303
304
305
306
307
308
# Client Error 4xx
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
420
422
423
424
426
428
429
431
451
# Server Error 5xx
500
501
502
503
504
505
506
507
508
510
511

84
utils/httpd/gen-config.sh Normal file
View File

@ -0,0 +1,84 @@
#!/bin/sh
# httpd-utils
# Written in 2020 by Lucas
# CC0 1.0 Universal/Public domain - No rights reserved
#
# To the extent possible under law, the author(s) have dedicated all
# copyright and related and neighboring rights to this software to the
# public domain worldwide. This software is distributed without any
# warranty. You should have received a copy of the CC0 Public Domain
# Dedication along with this software. If not, see
# <http://creativecommons.org/publicdomain/zero/1.0/>.
usage()
{
printf "Usage: %s domain domain_v4 ipv4 domain_v6 ipv6\n" "${0##*/}" >&2
exit 1
}
list_codes()
{
case $1 in
1xx) q='1[0-9][0-9]' ;;
2xx) q='2[0-9][0-9]' ;;
3xx) q='3[0-9][0-9]' ;;
4xx) q='4[0-9][0-9]' ;;
5xx) q='5[0-9][0-9]' ;;
*) q=X ;;
esac
grep -x -- "$q" codes.txt
}
print_block_return()
{
_c=$1 _l=${2:-}
_fmt='\tlocation "/c/%u" { block return %u'${_l:+' "%s"'}' }\n'
printf "$_fmt" $_c $_c $_l
}
[ $# -eq 5 ] || usage
domain=$1 domain_v4=$2 ipv4=$3 domain_v6=$4 ipv6=$5
cat - <<EOF
server "$domain" {
listen on $ipv4 port http
listen on $ipv6 port http
listen on $ipv4 tls port https
listen on $ipv6 tls port https
no log
alias "$domain_v4"
alias "$domain_v6"
tls {
certificate "/etc/ssl/$domain.fullchain.pem"
key "/etc/ssl/private/$domain.key"
}
location "/.well-known/acme-challenge/*" {
root "/acme"
request strip 2
}
root "/htdocs/$domain"
location "/ip" { fastcgi }
EOF
for _c in $(list_codes 1xx); do
print_block_return "$_c"
done
for _c in $(list_codes 2xx); do
print_block_return "$_c"
done
for _c in $(list_codes 3xx); do
print_block_return "$_c" "/"
done
for _c in $(list_codes 4xx); do
print_block_return "$_c"
done
for _c in $(list_codes 5xx); do
print_block_return "$_c"
done
printf "}\n"

114
utils/httpd/gen-index.sh Normal file
View File

@ -0,0 +1,114 @@
#!/bin/sh
# httpd-utils
# Written in 2020 by Lucas
# CC0 1.0 Universal/Public domain - No rights reserved
#
# To the extent possible under law, the author(s) have dedicated all
# copyright and related and neighboring rights to this software to the
# public domain worldwide. This software is distributed without any
# warranty. You should have received a copy of the CC0 Public Domain
# Dedication along with this software. If not, see
# <http://creativecommons.org/publicdomain/zero/1.0/>.
usage()
{
printf "Usage: %s domain domain_v4 domain_v6\n" "${0##*/}" >&2
exit 1
}
list_codes()
{
case $1 in
1xx) q='1[0-9][0-9]' ;;
2xx) q='2[0-9][0-9]' ;;
3xx) q='3[0-9][0-9]' ;;
4xx) q='4[0-9][0-9]' ;;
5xx) q='5[0-9][0-9]' ;;
*) q=X ;;
esac
grep -x -- "$q" codes.txt
}
show_category()
{
_t=$1 _cc=$2
cat - <<EOF
<dl>
<dt>$_t $_cc</dt>
EOF
for _c in $(list_codes "$_cc"); do
printf '\t\t\t<dd><a href="/c/%u">%u</a></dd>\n' "$_c" "$_c"
done
printf "\t\t</dl>\n"
}
[ $# -eq 3 ] || usage
domain=$1 domain_v4=$2 domain_v6=$3
cat - <<EOF
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>$domain</title>
<style>
* {
margin: 0;
padding: 0;
}
body {
width: 800px;
margin: 20px auto;
font-size: 16px;
line-height: 20px;
}
h1,
h2,
dl {
margin-bottom: 20px;
}
dd {
margin-left: 20px;
}
h1 {
font-size: 32px;
line-height: 40px;
}
h2 {
font-size: 24px;
line-height: 40px;
}
</style>
</head>
<body>
<h1>$domain</h1>
<h2>Utilities</h2>
<dl>
<dt><a href="/ip">/ip</a></dt>
<dd>shows client IP.</dd>
<dt><a href="//$domain_v4/ip">$domain_v4/ip</a></dt>
<dd>shows client IP, only listens on a IPv4 address.</dd>
<dt><a href="//$domain_v6/ip">$domain_v6/ip</a></dt>
<dd>shows client IP, only listens on a IPv6 address.</dd>
</dl>
<h2>HTTP status codes</h2>
EOF
show_category "Informational" 1xx
show_category "Successful" 2xx
show_category "Redirection" 3xx
show_category "Client Error" 4xx
show_category "Server Error" 5xx
cat - <<EOF
</body>
</html>
EOF

31
utils/httpd/ip.c Normal file
View File

@ -0,0 +1,31 @@
/*
* httpd-utils
* Written in 2020 by Lucas
* CC0 1.0 Universal/Public domain - No rights reserved
*
* To the extent possible under law, the author(s) have dedicated all
* copyright and related and neighboring rights to this software to the
* public domain worldwide. This software is distributed without any
* warranty. You should have received a copy of the CC0 Public Domain
* Dedication along with this software. If not, see
* <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#include <stdio.h>
#include <stdlib.h>
int
main(void)
{
char *ip;
ip = getenv("HTTP_X_FORWARDED_FOR");
if (ip == NULL)
ip = getenv("REMOTE_ADDR");
if (ip == NULL)
ip = "unknown";
printf("Content-type: text/plain\n\n%s\n", ip);
return 0;
}