shellgopher/shellgopher

76 lines
1.7 KiB
Bash
Executable File

#!/bin/sh
# shellgopher - simple shell script based gopher server
# Copyright (C) 2021 randomuser
# This program is free software: you can redistribute it
# and/or modify it under the terms of the GNU Affero General
# Public License as published by the Free Software Foundation,
# either version 3 of the License, or (at your option) any
# later version.
# This program is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See theGNU General Public License for more details.
# You should have received a copy of the GNU Affero General
# Public License along with this program. If not, see
# <https://www.gnu.org/licenses/>.
set -x
createsrv () {
# $1 - port
# $2 - write location
# $3 - read location
[ -p "$2" ] || mkfifo "$2"
[ -e "$3" ] || touch "$3"
nc -l "$1" <"$2" >>"$3" & exec 3>"$2"
}
die () {
# $1 - error message
printf "%s\n" $1
exit 1
}
checkuid () {
[ $(whoami) = "root" ] && \
die "You must execute shellgopher as root."
}
writefile () {
# $1 - file
foo cat "$1" >&3
}
ioblock () {
# $1 - io to block
while [ "$(wc -l $1 | awk -F' ' '{print $1}')" -eq 0 ]; do
:
done
}
ioflush () {
# $1 - io to flush
rm "$1"
touch "$1"
}
mktmp () {
t=$(mktemp)
rm "$t"
mkdir "$t"
printf "%s\n" "$t"
}
tmplocation=$(mktmp)
writelocation="${tmplocation}/write"
readlocation="${tmplocation}/read"
checkuid
createsrv 70 "${writelocation}" "${readlocation}"
printf "server created\n"
while true; do
ioblock "${readlocation}"
printf "iHello there! This is a test\tfake\t\t0\r\n.\r\n" >&3
printf "file served\n"
ioflush "${readlocation}"
done
exit 1