Initial commit.

This commit is contained in:
Emilis Dambauskas 2020-11-29 17:27:03 +02:00
commit a6bcfca0d8
7 changed files with 155 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
certs/

54
Makefile Normal file
View File

@ -0,0 +1,54 @@
CERT_DAYS=10000
COMMAND="./server-main.sh"
COUNTRY=LT
DOMAIN=localhost
.PHONY: default
default: server
### Server ---------------------------------------------------------------------
.PHONY: server
server: socat-server
.PHONY: ncat-server
ncat-server: certs
ncat -v \
--listen --keep-open -p 1965 \
--ssl --ssl-cert "certs/${DOMAIN}.crt" --ssl-key "certs/${DOMAIN}.key" \
-c "${COMMAND}"
.PHONY: socat-server
socat-server: certs
socat -d \
OPENSSL-LISTEN:1965,cert=certs/${DOMAIN}.crt,key=certs/${DOMAIN}.key,verify=0,reuseaddr,fork \
EXEC:${COMMAND}
### Certificates ---------------------------------------------------------------
.PHONY: show-cert
show-cert:
openssl x509 -in "certs/${DOMAIN}.crt" -text
.PHONY: clean-certs
clean-certs:
rm "certs/${DOMAIN}"*
.PHONY: certs
certs: \
certs/${DOMAIN}.crt
certs/${DOMAIN}.crt: certs/${DOMAIN}.key
echo -en ".\n.\n.\n.\n.\n${DOMAIN}\n.\n" | \
openssl req -x509 -key "certs/${DOMAIN}.key" -out "$@" -days ${CERT_DAYS} -nodes
certs/${DOMAIN}.key:
openssl genrsa -out "$@" 4096

17
server-main.sh Executable file
View File

@ -0,0 +1,17 @@
#!/usr/bin/sh
read -r URL GARBAGE;
REQUEST_URL=${URL%?}; # Remove "\r" at the end
URL_NO_PROTOCOL=${REQUEST_URL#*://}
REQUEST_PATH=/${URL_NO_PROTOCOL#*/}
REQUEST_QUERY=${URL#*\?}
echo "$REQUEST_PATH" | awk \
-v REQUEST_PATH="$REQUEST_PATH" \
-v REQUEST_QUERY="$REQUEST_QUERY" \
-v REQUEST_URL="$REQUEST_URL" \
-f srv/exec.awk \
-f srv/response.awk \
-f srv/tags.awk \
-f srv/main.awk

5
srv/exec.awk Normal file
View File

@ -0,0 +1,5 @@
function exec_to_string( cmd ) {
cmd | getline getline_output;
close( cmd );
return getline_output;
}

25
srv/main.awk Normal file
View File

@ -0,0 +1,25 @@
REQUEST_PATH ~ /^\/test/ {
response_reset();
h1( "Test path" );
println( "^/test matched" );
pre( REQUEST_URL );
link( "/", "Back to home" );
response_ok();
}
REQUEST_PATH == "/" {
response_reset();
h1( "Hello World from Awk!" );
println( "This is a test." );
pre( REQUEST_URL );
link( "/test123", "A test page" );
println( "Updated " \
exec_to_string( "date --iso-8601=seconds" ) \
);
response_ok();
}

22
srv/response.awk Normal file
View File

@ -0,0 +1,22 @@
BEGIN {
response_code = "51"
response_meta = "text/gemini"
response_body = ""
}
END {
printf "%s %s\r\n", response_code, response_meta;
print response_body
}
function response_ok() {
response_code = "20"
}
function response_reset() {
response_body = ""
}
function println( arg ) {
response_body = response_body "\n" arg;
}

31
srv/tags.awk Normal file
View File

@ -0,0 +1,31 @@
function h1( text ) {
println( "#" text );
}
function h2( text ) {
println( "##" text );
}
function h3( text ) {
println( "###" text );
}
function li( text ) {
println( "* " text );
}
function link( url, text ) {
println( "=> " url " " text );
}
function pre( text ) {
println( "```" );
println( text );
println( "```" );
}