From fbe463d10389127a2799590f50c42452d9715ffd Mon Sep 17 00:00:00 2001 From: Lisias T Date: Wed, 12 Oct 2016 11:55:14 -0300 Subject: [PATCH 1/5] Enhancing file serving by using NMAP's NCAT: Multiple connections are possible now. If the NCAT exits in error, the While still restart it. --- README.txt | 9 +++++++-- httpd.bash | 11 ++++++----- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/README.txt b/README.txt index 04b596a..50b9a77 100644 --- a/README.txt +++ b/README.txt @@ -5,21 +5,26 @@ Simple HTTP server written in bash script == Requirements * bash -* nc (netcat) +* ncat (nmap) * file * wc * cat +* upnpc (MiniUPnP) == Usage -./httpd.bash [port] +./httpd.bash
[port] +
is the adress of the eth that will receive requests. Needed to configure port foward under NAT. +Use ipconfig to check it out or use a dash '-' to ignore UPnP. (port is default to 3000) == Warning This isn't secure. Don't export to internet. +Note from Lisias: I did it, since the UPnP port forward setup. But I used an expandable Raspberry PI box, without any usefull or sensitive data. + == License The MIT License diff --git a/httpd.bash b/httpd.bash index 2db451a..6e6da05 100755 --- a/httpd.bash +++ b/httpd.bash @@ -4,12 +4,12 @@ # ./httpd.bash [port] # (port is default to 3000) # requires: -# bash, nc (netcat), file, wc, cat +# bash, ncat (nmap), file, wc, cat, upnpc # warning: # This isn't secure. Don't export to internet. readonly CRLF=$'\r\n' -readonly SERVERNAME='httpd.bash/0.01' +readonly SERVERNAME='httpd.bash/0.2-LST' function content_type() { local file=$1 @@ -49,11 +49,13 @@ function not_found_404() { function dispatch() { local method=$1 path=$2 + echo $method $path >&2 if [[ $method == GET ]]; then [[ $path == /* ]] && path=".$path" path=${path#../} path=${path//\/..\//} [ -d "$path" ] && path="$path/index.html" + echo $path >&2 if [ -f "$path" ]; then ok_200 "$path" else @@ -71,12 +73,11 @@ function run() { dispatch "$method" "$path" } -export -f content_type content_length ok_200 not_implemented_501 \ - not_found_404 dispatch run +export -f content_type content_length ok_200 not_implemented_501 not_found_404 dispatch run export CRLF SERVERNAME trap 'echo shutdown.; exit' INT echo 'Ctrl-C to shutdown server' while :; do - nc -l -p ${1:-3000} -c 'bash -c run' + ncat -v -lk -p ${2:-3000} -c 'bash -c run' done From 36a618750af41befc7e97325218b3c5efbe96078 Mon Sep 17 00:00:00 2001 From: Lisias T Date: Wed, 12 Oct 2016 11:56:36 -0300 Subject: [PATCH 2/5] A somewhat temerarious change - punching a hole on NAT to allow serving to Internet from inside a intranet. --- httpd.bash | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/httpd.bash b/httpd.bash index 6e6da05..23e60dd 100755 --- a/httpd.bash +++ b/httpd.bash @@ -1,12 +1,19 @@ #!/bin/bash # httpd.bash: simple HTTP server written in bash script +# # usage: -# ./httpd.bash [port] +# ./httpd.bash
[port] +# +#
is the adress of the eth that will receive requests. Needed to configure port foward under NAT. +# Use ipconfig to check it out or use a dash '-' to ignore UPnP. # (port is default to 3000) +# # requires: # bash, ncat (nmap), file, wc, cat, upnpc +# # warning: # This isn't secure. Don't export to internet. +# I enjoy livin' La Vida Loca!! - Lisias :-) readonly CRLF=$'\r\n' readonly SERVERNAME='httpd.bash/0.2-LST' @@ -76,6 +83,10 @@ function run() { export -f content_type content_length ok_200 not_implemented_501 not_found_404 dispatch run export CRLF SERVERNAME +if [ "${1}" != "-" -a "${1}" != "" ]; then + upnpc -a ${1} 8080 ${2:-3000} tcp 14400 +fi + trap 'echo shutdown.; exit' INT echo 'Ctrl-C to shutdown server' while :; do From 991af3dec0dd04a6580302794035fca95e8370ed Mon Sep 17 00:00:00 2001 From: Lisias T Date: Thu, 13 Oct 2016 20:34:57 -0300 Subject: [PATCH 3/5] Setting up the external port on upnp Deleting port forward on exit. --- httpd.bash | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/httpd.bash b/httpd.bash index 23e60dd..3ffe93c 100755 --- a/httpd.bash +++ b/httpd.bash @@ -2,11 +2,12 @@ # httpd.bash: simple HTTP server written in bash script # # usage: -# ./httpd.bash
[port] +# ./httpd.bash
[port] [external port] # #
is the adress of the eth that will receive requests. Needed to configure port foward under NAT. # Use ipconfig to check it out or use a dash '-' to ignore UPnP. # (port is default to 3000) +# (external port is default to 8080) # # requires: # bash, ncat (nmap), file, wc, cat, upnpc @@ -83,11 +84,23 @@ function run() { export -f content_type content_length ok_200 not_implemented_501 not_found_404 dispatch run export CRLF SERVERNAME +FINISHCMD="" if [ "${1}" != "-" -a "${1}" != "" ]; then - upnpc -a ${1} 8080 ${2:-3000} tcp 14400 + upnpc -a ${1} ${3:8080} ${2:-3000} tcp 14400 + FINISHCMD="upnpc -d ${3:-8080} tcp " fi +finish() { + echo "" + if [ "$FINISHCMD" != "" ]; then + echo "Shutting down..." + eval $FINISHCMD + fi + echo "Shutdown complete." +} +export -f finish +export FINISHCMD -trap 'echo shutdown.; exit' INT +trap 'finish; exit' INT echo 'Ctrl-C to shutdown server' while :; do ncat -v -lk -p ${2:-3000} -c 'bash -c run' From dfef90f282e2dc113ac34cbc0e437ee7fcd64e3c Mon Sep 17 00:00:00 2001 From: Lisias Date: Thu, 13 Oct 2016 20:41:53 -0300 Subject: [PATCH 4/5] Update README.txt typo --- README.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.txt b/README.txt index 50b9a77..c039498 100644 --- a/README.txt +++ b/README.txt @@ -23,7 +23,7 @@ Use ipconfig to check it out or use a dash '-' to ignore UPnP. This isn't secure. Don't export to internet. -Note from Lisias: I did it, since the UPnP port forward setup. But I used an expandable Raspberry PI box, without any usefull or sensitive data. +Note from Lisias: I did it, since the UPnP port forward setup. But I used an expendable Raspberry PI box, without any usefull or sensitive data. == License From afef360d25c78e298621ea0e8f846d9a12b50b1a Mon Sep 17 00:00:00 2001 From: Lisias T Date: Thu, 13 Oct 2016 21:00:16 -0300 Subject: [PATCH 5/5] Sanitizing the command line option. (there was no need to change the local port position!) --- README.txt | 7 ++++--- httpd.bash | 14 +++++++------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/README.txt b/README.txt index c039498..620f9f8 100644 --- a/README.txt +++ b/README.txt @@ -13,11 +13,12 @@ Simple HTTP server written in bash script == Usage -./httpd.bash
[port] +./httpd.bash [port]
[external port] -
is the adress of the eth that will receive requests. Needed to configure port foward under NAT. -Use ipconfig to check it out or use a dash '-' to ignore UPnP. (port is default to 3000) +
is the adress of the eth that will receive requests. Needed to configure port foward under NAT. + Use ipconfig to check it out or use a dash '-' to ignore UPnP. +(external port is default to 8080) == Warning diff --git a/httpd.bash b/httpd.bash index 3ffe93c..71eda2e 100755 --- a/httpd.bash +++ b/httpd.bash @@ -2,11 +2,11 @@ # httpd.bash: simple HTTP server written in bash script # # usage: -# ./httpd.bash
[port] [external port] +# ./httpd.bash [port]
[external port] # -#
is the adress of the eth that will receive requests. Needed to configure port foward under NAT. -# Use ipconfig to check it out or use a dash '-' to ignore UPnP. # (port is default to 3000) +#
is the adress of the eth that will receive requests. Needed to configure port foward under NAT. +# Use ipconfig to check it out or use a dash '-' to ignore UPnP. # (external port is default to 8080) # # requires: @@ -85,9 +85,9 @@ export -f content_type content_length ok_200 not_implemented_501 not_found_404 d export CRLF SERVERNAME FINISHCMD="" -if [ "${1}" != "-" -a "${1}" != "" ]; then - upnpc -a ${1} ${3:8080} ${2:-3000} tcp 14400 - FINISHCMD="upnpc -d ${3:-8080} tcp " +if [ "${2}" != "-" -a "${2}" != "" ]; then + upnpc -a ${2} ${1:-3000} ${3:-8080} tcp 14400 + FINISHCMD="upnpc -d ${3:-8080} tcp" fi finish() { echo "" @@ -103,5 +103,5 @@ export FINISHCMD trap 'finish; exit' INT echo 'Ctrl-C to shutdown server' while :; do - ncat -v -lk -p ${2:-3000} -c 'bash -c run' + ncat -v -lk -p ${1:-3000} -c 'bash -c run' done