commit a6bcfca0d8de4ecfe00edc40189d266f8f5f30c6 Author: Emilis Dambauskas Date: Sun Nov 29 17:27:03 2020 +0200 Initial commit. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..df91287 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +certs/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ed0598f --- /dev/null +++ b/Makefile @@ -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 diff --git a/server-main.sh b/server-main.sh new file mode 100755 index 0000000..81bed76 --- /dev/null +++ b/server-main.sh @@ -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 diff --git a/srv/exec.awk b/srv/exec.awk new file mode 100644 index 0000000..99e4956 --- /dev/null +++ b/srv/exec.awk @@ -0,0 +1,5 @@ +function exec_to_string( cmd ) { + cmd | getline getline_output; + close( cmd ); + return getline_output; +} diff --git a/srv/main.awk b/srv/main.awk new file mode 100644 index 0000000..3d67571 --- /dev/null +++ b/srv/main.awk @@ -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(); +} diff --git a/srv/response.awk b/srv/response.awk new file mode 100644 index 0000000..240defb --- /dev/null +++ b/srv/response.awk @@ -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; +} diff --git a/srv/tags.awk b/srv/tags.awk new file mode 100644 index 0000000..261efa6 --- /dev/null +++ b/srv/tags.awk @@ -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( "```" ); +}