#!/bin/bash
STATUS="200 OK"
CONTENT_TYPE="text/html"
CONTENT=""
LOGS="lakefox"
file_server() {
if [ -d "$1" ]; then
local shopt -s nullglob
CONTENT="
Index of $2
"
CONTENT+=""
for entry in "$1"/[^.]*; do
local filename=$(basename "$entry")
if [ -e "$entry" ]; then
CONTENT+="$filename
"
fi
done
CONTENT+="
"
elif [ -f "$1" ]; then
CONTENT=$(< "$1")
else
STATUS="404 Not Found"
CONTENT_TYPE="text/plain"
fi
}
route_requests() {
read request
host="${request#*Host: }"
host="${host%%$'\r'*}"
path="${request#* }"
path="${path%% *}"
if [[ "$path" == *".."* ]]; then
echo "HTTP/1.1 403 Forbidden\rContent-Type: text/plain\r\rForbidden"
return
fi
logger -t $LOGS "Received: $host:$path"
STATUS="200 OK"
CONTENT_TYPE="text/html"
CONTENT=""
case "$host" in
"lakefox.net")
if [[ "$path" == "/curl" ]]; then
/var/www/lakefox/cgi_bin/curl.sh "$request"
return
else
file_server "/var/www/lakefox/$path" "$path"
fi
;;
"grimui.com")
file_server "/var/www/lakefox/grim/docs/dist/$path" "$path"
;;
"decode.sh")
file_server "/var/www/lakefox/decode/$path" "$path"
;;
*)
logger -t $LOGS "Invalid host: $host"
STATUS="404 Not Found"
CONTENT_TYPE="text/plain"
CONTENT="Domain not found."
;;
esac
echo "HTTP/1.1 $STATUS\r"
echo "Content-Type: $CONTENT_TYPE; charset=utf-8\r"
echo "Content-Length: ${#CONTENT}\r"
echo "Connection: close\r"
echo ""
echo "$CONTENT"
}
# Start the HTTP server on port 80
socat TCP-LISTEN:80,fork EXEC:"bash -c 'route_requests'"