From c15a524ced17da029abaa09bdb025487a3b27551 Mon Sep 17 00:00:00 2001 From: KatolaZ Date: Sun, 15 Jul 2018 11:27:09 +0100 Subject: name changed to gosher --- README.md | 31 ++++++++++++--- TODO | 5 +++ gosher | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ gosher_serve | 1 + gprsh | 121 ----------------------------------------------------------- gprsh_serve | 1 - 6 files changed, 152 insertions(+), 128 deletions(-) create mode 100644 TODO create mode 100755 gosher create mode 120000 gosher_serve delete mode 100755 gprsh delete mode 120000 gprsh_serve diff --git a/README.md b/README.md index d1b278a..15ac4f1 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,34 @@ -## gprsh -- A simple Gopher server in a POSIX shell script +## gosher -- A simple Gopher server in a POSIX shell script -`gprsh` is a Gopher server in a shell script. It requires only a -standard `netcat`. +`gosher` is a Gopher server in a POSIX shell script. It requires only a +standard `netcat(1)`. You start the server using: - $ ./gprsh [ [] + $ ./gosher [ [] If PORT is not specified, it will bind on port 70. If GOPHERDIR is not provided, it defaults to "./". -`gprsh` reads the index.gph gopherfile in a folder and renders it as a -gophermap. +If the selector is a directory, `gosher` will look for a file named +`gophermap` to render the submenu. If a `gophermap` does not exist, +`gosher` looks for the index.gph gopherfile in the folder and, if it +exists, renders it as a gophermap. + +## Why `gosher`? + +Just for fun. There are only a few TCP/IP application protocols left +that can be implemented in a few dozen lines of POSIX shell script, and +Gopher (RFC 1436) is one of those. + +## Why calling it `gosher`? + +Well, `gosher` is just `gopher` where the `p` is replaced with an `s`, +and `sh(1)` is all that you need to run `gosher` (if we leave +`netcat(1)` out...). + +Also, `gopher` is probably one of the few `kosher` protocols out there. + +According to the Urban Dictionary, `gosher` is "an insult that can mean +anything", so choose your own. diff --git a/TODO b/TODO new file mode 100644 index 0000000..3d53072 --- /dev/null +++ b/TODO @@ -0,0 +1,5 @@ +- add chroot and privilege drop +- allow on-the-fly creation of gopherfle from a folder (set it active + through an option) + + diff --git a/gosher b/gosher new file mode 100755 index 0000000..23319b8 --- /dev/null +++ b/gosher @@ -0,0 +1,121 @@ +#!/bin/sh + +## +## === gosher === +## +## gosher is a simple gopher server in a POSIX shell script: +## +## $ ./gosher [ [] +## +## If PORT is not specified, the default is 70. If GOPHERDIR is not +## specified, "./" is assumed +## +## +## (c) 2018 Vincenzo 'KatolaZ' Nicosia +## +## + +###################### + +## +## If the script is called with basename "gosher", launch the netcat +## server... +## + +MYNAME=$(basename $0) + +if [ -z "${MYNAME#gosher}" ]; then + ## we are called as gosher -- launch the server + + PORT=${1:-70} + GOPHERDIR=${2:-"./"} + + while [ 1 -eq 1 ]; do + netcat -vv -k -l -p ${PORT} -c "./gosher_serve ${GOPHERDIR}" + ret=$? + done + exit 0 +fi + + +###################### + +## +## ...otherwise, serve a request +## + + +## function +invalid_selector(){ + sel="$1" + echo "iInvalid selector: \"$sel\"" + echo "." + exit 1 +} + +## function +serve_selector(){ + sel="$1" + + cat "${sel}" + echo "." + exit 0 +} + + +### transform a .gph file into a gophermap +## function +serve_index(){ + IDX=$1 + IFS=' +' + while read line; do + rline=$(echo $line | sed -r -e 's/\r//g') + case $rline in + '['*) + echo $rline | sed -r -e 's/\[//g;s/\]//g;s/\|/\t/g;s/\t//;s/$/\r/g' + ;; + *) + echo $line + esac + done < $IDX + exit 0 +} + + + +GOPHERDIR=${1:-"./"} + +read selector + +selector=$(echo $selector | sed -r 's:\r::g' ) + +echo "iGOPHERDIR: ${GOPHERDIR}" +echo "iselector: \"${selector}\"" + +case $selector in + /*|"") + RP1=$(realpath "${GOPHERDIR}"/"${selector}" || "") + RP2=$(realpath "${GOPHERDIR}")"${selector}" + echo "iRP1: ${RP1}" + echo "iRP2: ${RP2}" + #echo "." + + if [ "${RP1}" = "${RP2}" ]; then + if [ -f "${RP1}" ]; then + serve_selector "${RP1}" + elif [ -d "${RP1}" ]; then + [ -f "${RP1}/gophermap" ] && serve_selector "${RP1}/gophermap" + [ -f "${RP1}/index.gph" ] && serve_index "${RP1}/index.gph" + else + echo "3Err Unable to find file ${selector}" + fi + fi + invalid_selector "$selector" + ;; + *) + [ -f "${GOPHERDIR}/gophermap" ] && cat "${GOPHERDIR}/gophermap" && echo "." && exit 0 + invalid_selector "/" + ;; +esac + diff --git a/gosher_serve b/gosher_serve new file mode 120000 index 0000000..400ae39 --- /dev/null +++ b/gosher_serve @@ -0,0 +1 @@ +gosher \ No newline at end of file diff --git a/gprsh b/gprsh deleted file mode 100755 index f93cf29..0000000 --- a/gprsh +++ /dev/null @@ -1,121 +0,0 @@ -#!/bin/sh - -## -## === gprsh === -## -## gprsh is a simple gopher server in a POSIX shell script: -## -## $ ./gprsh [ [] -## -## If PORT is not specified, the default is 70. If GOPHERDIR is not -## specified, "./" is assumed -## -## -## (c) 2018 Vincenzo 'KatolaZ' Nicosia -## -## - -###################### - -## -## If the script is called with basename "gprsh", launch the netcat -## server... -## - -MYNAME=$(basename $0) - -if [ -z "${MYNAME#gprsh}" ]; then - ## we are called as gprsh -- launch the server - - PORT=${1:-70} - GOPHERDIR=${2:-"./"} - - while [ 1 -eq 1 ]; do - netcat -vv -k -l -p ${PORT} -c "./gprsh_serve ${GOPHERDIR}" - ret=$? - done - exit 0 -fi - - -###################### - -## -## ...otherwise, serve a request -## - - -## function -invalid_selector(){ - sel="$1" - echo "iInvalid selector: \"$sel\"" - echo "." - exit 1 -} - -## function -serve_selector(){ - sel="$1" - - cat "${sel}" - echo "." - exit 0 -} - - -### transform a .gph file into a gophermap -## function -serve_index(){ - IDX=$1 - IFS=' -' - while read line; do - rline=$(echo $line | sed -r -e 's/\r//g') - case $rline in - '['*) - echo $rline | sed -r -e 's/\[//g;s/\]//g;s/\|/\t/g;s/\t//;s/$/\r/g' - ;; - *) - echo $line - esac - done < $IDX - exit 0 -} - - - -GOPHERDIR=${1:-"./"} - -read selector - -selector=$(echo $selector | sed -r 's:\r::g' ) - -echo "iGOPHERDIR: ${GOPHERDIR}" -echo "iselector: \"${selector}\"" - -case $selector in - /*|"") - RP1=$(realpath "${GOPHERDIR}"/"${selector}" || "") - RP2=$(realpath "${GOPHERDIR}")"${selector}" - echo "iRP1: ${RP1}" - echo "iRP2: ${RP2}" - #echo "." - - if [ "${RP1}" = "${RP2}" ]; then - if [ -f "${RP1}" ]; then - serve_selector "${RP1}" - elif [ -d "${RP1}" ]; then - [ -f "${RP1}/gophermap" ] && serve_selector "${RP1}/gophermap" - [ -f "${RP1}/index.gph" ] && serve_index "${RP1}/index.gph" - else - echo "3Err Unable to find file ${selector}" - fi - fi - invalid_selector "$selector" - ;; - *) - [ -f "${GOPHERDIR}/gophermap" ] && cat "${GOPHERDIR}/gophermap" && echo "." && exit 0 - invalid_selector "/" - ;; -esac - diff --git a/gprsh_serve b/gprsh_serve deleted file mode 120000 index c280e5b..0000000 --- a/gprsh_serve +++ /dev/null @@ -1 +0,0 @@ -gprsh \ No newline at end of file -- cgit v1.2.3