added support for different netcat versions

master
KatolaZ 7 years ago
parent 1c23368916
commit 464cedb474
  1. 30
      README.md
  2. 63
      gosher

@ -9,13 +9,41 @@ You start the server using:
$ ./gosher [<PORT> [<GOPHERDIR>] $ ./gosher [<PORT> [<GOPHERDIR>]
If PORT is not specified, it will bind on port 70. If GOPHERDIR is not If PORT is not specified, it will bind on port 70. If GOPHERDIR is not
provided, it defaults to "./". provided, it defaults to "./". Before starting `gosher` you might need
to modify the values of the variables `NETCAT` and `STYLE` in `gosher`
(see "Which netca?" below).
If the selector is a directory, `gosher` will look for a file named If the selector is a directory, `gosher` will look for a file named
`gophermap` to render the submenu. If a `gophermap` does not exist, `gophermap` to render the submenu. If a `gophermap` does not exist,
`gosher` looks for the index.gph gopherfile in the folder and, if it `gosher` looks for the index.gph gopherfile in the folder and, if it
exists, renders it as a gophermap. exists, renders it as a gophermap.
## Which netcat?
There are currently several different implementations of `netcat`, and
each of them works in a slightly different way and/or offers a different
set of options. For the sake of using `gosher`, the main issue is
whether your `netcat` implementation does exit or not when its standard
input gets closed. Notably, the original `netcat` implementation by
hobbit@avian.org does **not** exit, while other common implementations
(OpenBSD `netcat`, `ncat` from the nmap project, and GNU `netcat`). The
current version of `gosher` can work with different implementations of
`netcat`, provided that the variable `NETCAT` points to the `netcat`
version you want to use, and that the variable `STYLE` in `gosher` is set
correctly. Please check below what is the recommended combination for
your version of `netcat`:
+----------------+--------+
| netcat version | STYLE |
+----------------+--------+
| traditional | 'fork' |
+----------------+--------+
| OpenBSD | 'pipe' |
+----------------+--------+
| ncat | 'pipe' |
+----------------+--------+
## Why `gosher`? ## Why `gosher`?
Just for fun. There are only a few TCP/IP application protocols left Just for fun. There are only a few TCP/IP application protocols left

@ -22,20 +22,44 @@
## server... ## server...
## ##
NETCAT=netcat ##
## NETCAT: the netcat command to use, and any additional option
##
### Original netcat
##NETCAT="nc.traditional"
##
### ncat (from nmap)
##NETCAT="ncat"
##
### Openbsd netcat
NETCAT="nc.openbsd"
##
## STYLE: The way in which netcat will talk to gosher_serve
##
### fork with "-c" (Does *not* work with OpenBSD netcat!!!!!)
#STYLE='fork'
##
### use named pipes (Does *not* work with original netcat!!!!!)
STYLE='pipe'
OPREFIX=/tmp/outf_ OPREFIX=/tmp/outf_
IPREFIX=/tmp/inf_ IPREFIX=/tmp/inf_
##DEBUG= DEBUG=
DEBUG=yes #DEBUG=yes
[ -n "$DEBUG" ] && {
set -e
set -x
}
## function ## function
cleanup(){ cleanup(){
[ -p "${OPREFIX}$$" ] && rm -f ${OPREFIX}$$ [ -n "$INF" ] && [ -p "$INF" ] && rm -f ${INF}
[ -p "${IPREFIX}$$" ] && rm -f ${IPREFIX}$$
exit 1 exit 1
} }
MYNAME=$(basename $0) MYNAME=$(basename $0)
@ -48,15 +72,23 @@ if [ -z "${MYNAME#gosher}" ]; then
trap cleanup 0 HUP INT TRAP TERM QUIT trap cleanup 0 HUP INT TRAP TERM QUIT
OUTF=${OPREFIX}$$
INF=${IPREFIX}$$ INF=${IPREFIX}$$
mkfifo -m 600 $OUTF $INF [ "$STYLE" = "pipe" ] && {
mkfifo -m 600 $INF
while [ 1 -eq 1 ]; do
./gosher_serve ${GOPHERDIR} <$INF | ${NETCAT} -vvvvv -l -p ${PORT} >$INF
done
rm -f $INF
exit 0
}
[ "$STYLE" = 'fork' ] && {
while [ 1 -eq 1 ]; do while [ 1 -eq 1 ]; do
./gosher_serve ${GOPHERDIR} <$INF >$OUTF & ${NETCAT} -vv -l -p $PORT -c "~/gosher_serve ${GOPHERDIR}"
${NETCAT} -vv -l -p ${PORT} >$INF <$OUTF
ret=$?
done done
exit 0 exit 0
}
echo "Error!!! wrong STYLE specified!!!" >&2
exit 1
fi fi
@ -71,7 +103,9 @@ fi
invalid_selector(){ invalid_selector(){
sel="$1" sel="$1"
echo "3Error: Invalid selector: \"$sel\"" echo "3Error: Invalid selector: \"$sel\""
echo "." printf ".\r\n"
exec 1>&-
exec 2>&-
exit 1 exit 1
} }
@ -80,6 +114,9 @@ serve_selector(){
sel="$1" sel="$1"
cat "${sel}" cat "${sel}"
echo "$0: selector $sel served -- exiting " >&2
exec 1>&-
exec 2>&-
exit 0 exit 0
} }
@ -104,6 +141,8 @@ serve_index(){
esac esac
done < $IDX done < $IDX
printf ".\r\n" printf ".\r\n"
exec 1>&-
exec 2>&-
exit 0 exit 0
} }

Loading…
Cancel
Save