Simple script to start a gopher "server"
authorNiki Roo <niki@nikiroo.be>
Sat, 5 Aug 2017 21:55:04 +0000 (23:55 +0200)
committerNiki Roo <niki@nikiroo.be>
Sat, 5 Aug 2017 21:55:04 +0000 (23:55 +0200)
- 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 [new file with mode: 0644]

diff --git a/gopher.sh b/gopher.sh
new file mode 100644 (file)
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"
+