100 lines of BASH script that simply rock!

ATTENTION

This software should be quite safe, since this checks for some nasty tricks like:

  • .. in URL
  • / in some places

It binds to port specified from the command line.
It serves only files accessible by the user that runs this script from the current directory (not going up).

Script:

#!/bin/bash

function debug {
    local severity="$1"
    shift
    local message="$@"

    echo -n "`date -u`"    1>&2
    echo -ne '\t'        1>&2
    echo -n "$severity"    1>&2
    echo -ne '\t'        1>&2
    echo "$message"        1>&2
}

function fix_path {
    echo -n "$1" | head -n 1 | sed 's|^[/.-]*||' | sed 's|/\.*|/|g'
}

function serve_dir {
    local dir="`fix_path "$1"`"
    if [ "$dir" = "" ]; then
        dir="./"
    fi
    echo 'HTTP/1.1 200 OK'
    echo 'Content-type: text/html;charset=UTF-8'
    echo
    echo LISTING "$dir"
    echo '<br/>'
    ls -p "$dir" | sed -e 's|^\(.*\)$|<a href="/'"$dir"'\1">\1</a><br/>|'
}

function serve_file {
    echo 'HTTP/1.1 200 OK'
    echo 'Content-type: application/x-download-this'
    echo
    local file="`fix_path "$1"`"
    debug INFO serving file "$file"
    cat "$file"
}

function process {
    local url="`gawk '{print $2}' | head -n 1`"
    case "$url" in
        */)
            debug INFO Processing "$url" as dir
            serve_dir "$url"
            break
            ;;
        *)
            debug INFO Processing "$url" as file
            serve_file "$url"
            ;;
    esac
}

function serve {
    local port="$1"
    local sin="$2"
    local sout="$3"

    while debug INFO Running nc; do

        nc -l -p "$port" < "$sin" > "$sout" &
        pid="$!"

        debug INFO Server PID: "$pid"

        trap cleanup SIGINT
        head -n 1 "$sout" | process > "$sin"
        trap - SIGINT

        debug INFO Killing nc

        kill "$pid"
    done

    debug INFO Quiting server
}

function cleanup {
    debug INFO Caught signal, quitting...
    rm -Rf "$tmp_dir"
    exit
}

tmp_dir="`mktemp -d -t http_server.XXXXXXXXXX`"
sin="$tmp_dir"/in
sout="$tmp_dir"/out
pid=0
port="$1"

mkfifo "$sin"
mkfifo "$sout"

debug INFO Starting server on port "$port"
serve "$port" "$sin" "$sout"
cleanup

Screenshot Tour

STEP 1 choose a port number greater than 1024
flickr:2478716612

STEP 2 running and waiting for clients
flickr:2478716366

STEP 3 browse files with web browser
flickr:2477904925

STEP 4 browse directory structure
flickr:2477904711

STEP 4.5 navigate even deeper
flickr:2477904485

STEP 5 click a file to download it
flickr:2477904225

STEP 6 kill server with Ctrl-C
flickr:2478715162

STEP 7 the server is now down
flickr:2478714870

Dependencies

This script depends on nc (netcat package) being installed in PATH. If you have netcat instead of nc, just change the nc in script to netcat.

Wykop

Jeśli temat Ci się podoba, wykop go!

Responses

Content-type...
Anonymous (82.210.179.x) 1210462958|%e %b %Y, %H:%M %Z|agohover

x-download-this ?

Można lepiej:

function serve_file {
local file="‘fix_path "$1"`"
echo ’HTTP/1.1 200 OK'
printf 'Content-type: %s' "`file -i "$file" | cut -d " " -f 2-`"
echo
debug INFO serving file "$file"
cat "$file"
}

Unfold Content-type... by Anonymous (82.210.179.x), 1210462958|%e %b %Y, %H:%M %Z|agohover
:>
Anonymous (88.199.251.x) 1210487161|%e %b %Y, %H:%M %Z|agohover

A gdzie PHP? :)

Unfold :> by Anonymous (88.199.251.x), 1210487161|%e %b %Y, %H:%M %Z|agohover
php, cgi...
Anonymous (83.13.72.x) 1210490334|%e %b %Y, %H:%M %Z|agohover

CGI czy PHP w trybie CGI mozna do tego typu serwerka dopisac w kolejnych kilku linijkach. serio. :)

Unfold php, cgi... by Anonymous (83.13.72.x), 1210490334|%e %b %Y, %H:%M %Z|agohover
lepsze wykrywanie typu mime
radrad 1218571368|%e %b %Y, %H:%M %Z|agohover

Anonymous (82.210.179.178) trzy posty wyżej zaproponował takie wykrywanie mime

`file -i "$file" | cut -d " " -f 2-`

można znacznie to uprościć i nie bawić się w cut'owanie wyjścia file, wystarczy dodać opcję b ;) nie wyświetli tego co wycinasz
file -i -b $filename
przykład
rad@ubuntu:~/tmp$ file -i -b 11.jpg
image/jpeg

Unfold lepsze wykrywanie typu mime by radrad, 1218571368|%e %b %Y, %H:%M %Z|agohover
bash with its ini to maintain server
namzezamnamzezam 1235390188|%e %b %Y, %H:%M %Z|agohover

i would like to develop a bash for controlling all about a server (nginx,fastsgi, php mysql) including their ini etc, where this bash goes with its ini ( see more here http://ajdiaz.wordpress.com/2008/02/09/bash-ini-parser/ )

i hope to work on it with any one of you in a gpl and/or a comcom mode and about the latter please see my site in comcom.wikidot.com or iswith.info

Unfold bash with its ini to maintain server by namzezamnamzezam, 1235390188|%e %b %Y, %H:%M %Z|agohover
Ubuntu 10.04 patch
Erdem Guven (guest) 1282832984|%e %b %Y, %H:%M %Z|agohover
--- /tmp/sil.sh    2010-08-26 15:26:26.947272063 +0100
+++ /home/erdem/bin/bash_http_server.sh    2010-08-26 15:23:45.663270941 +0100
@@ -60,7 +60,7 @@

     while debug INFO Running nc; do

-        nc -l -p "$port" < "$sin" > "$sout" &
+        nc -l "$port" < "$sin" > "$sout" &
         pid="$!"

         debug INFO Server PID: "$pid"
@@ -79,6 +79,7 @@

 function cleanup {
     debug INFO Caught signal, quitting...
+    kill "$pid"
     rm -Rf "$tmp_dir"
     exit
 }
Unfold Ubuntu 10.04 patch by Erdem Guven (guest), 1282832984|%e %b %Y, %H:%M %Z|agohover
GabrysGabrys 1282833639|%e %b %Y, %H:%M %Z|agohover

Yes, some versions of netcat use "-l" instead of "-l -p". Others just the opposite.


Piotr Gabryjeluk, Wikidot Inc.
visit my blog

Unfold by GabrysGabrys, 1282833639|%e %b %Y, %H:%M %Z|agohover
Add a New Comment

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License