From c8b05198d89f026d13a577f57d2dda8cb608a3be Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Sat, 5 Aug 2017 23:55:04 +0200 Subject: [PATCH] Simple script to start a gopher "server" - it will listen on a port, then call another program to handle the connection - can be used to test some things or for a simple 7 (search) service in Bash --- gopher.sh | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 gopher.sh diff --git a/gopher.sh b/gopher.sh new file mode 100644 index 0000000..d0c9ea5 --- /dev/null +++ b/gopher.sh @@ -0,0 +1,47 @@ +#!/bin/sh + +# $0 [prog ($1 will be the query)] ([port]) + +[ "$ADDR" = "" ] && ADDR=127.0.0.1 +[ "$LOG" = "" ] && LOG=/tmp/log.err +[ "$PIDF" = "" ] && PIDF="`mktemp`" +[ "$TIMEOUT" = "" ] && TIMEOUT=10 +[ "$MAXCON" = "" ] && MAXCON=1024 +prog="$1" +port="$2" +[ "$port" = "" ] && port=70 + +if [ "$prog" = "" ]; then + rm "$PIDF" + echo "Syntax: $0 [prog (\$1 will be the query)] ([port])" >&2 + exit 1 +fi + +tmpd=`mktemp -t -d .gopher-service.XXXXXX` +if [ ! -d "$tmpd" ]; then + rm "$PIDF" + echo "Cannot create temporary directory, aborting..." >&2 + exit 2 +fi + +touch "$PIDF" +echo "$PIDF" + +i=0 +while [ -e "$PIDF" ]; do + i=`expr $i + 1` + [ $i -gt "$MAXCON" ] && i=1 + fifo="$tmpd/fifo.$i" + rm -f "$fifo" + mkfifo "$fifo" + < "$fifo" nc -l -q0 -w"$TIMEOUT" "$ADDR" -p "$port" | ( + read -r query + query="`echo "$query" | cut -f2 -d' '`" + ( + "$prog" "$query" 2>> "$LOG" + rm "$fifo" + )& + ) > "$fifo" +done +rm -rf "$tmpd" + -- 2.27.0