2021-02-18 01:08:58 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
2021-02-18 02:21:13 +00:00
|
|
|
# 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/>.
|
|
|
|
|
2021-02-18 01:08:58 +00:00
|
|
|
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"
|
|
|
|
}
|
2021-02-18 01:42:23 +00:00
|
|
|
die () {
|
|
|
|
# $1 - error message
|
|
|
|
printf "%s\n" $1
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
checkuid () {
|
|
|
|
[ $(whoami) = "root" ] && \
|
|
|
|
die "You must execute shellgopher as root."
|
|
|
|
}
|
2021-02-18 01:08:58 +00:00
|
|
|
writefile () {
|
|
|
|
# $1 - file
|
|
|
|
foo cat "$1" >&3
|
|
|
|
}
|
|
|
|
ioblock () {
|
|
|
|
# $1 - io to block
|
2021-02-18 02:21:13 +00:00
|
|
|
while [ "$(wc -l $1 | awk -F' ' '{print $1}')" -eq 0 ]; do
|
|
|
|
:
|
|
|
|
done
|
2021-02-18 01:08:58 +00:00
|
|
|
}
|
|
|
|
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"
|
|
|
|
|
2021-02-18 01:42:23 +00:00
|
|
|
checkuid
|
2021-02-18 01:08:58 +00:00
|
|
|
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
|