Advertisement
hackbyte

bashttpd.sh v20161225

Dec 27th, 2016
747
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.18 KB | None | 0 0
  1. #!/bin/bash
  2. ####################################################################################################
  3. #
  4. # bashttpd - Bourne-again Shell (based) Hypertext Transfer Protocol Daemon
  5. #
  6. # Crappy stuff to serve just one file like a real httpd would do.
  7. # No user inputs (nothing to sanitize!), file (content) specified and preloaded at launch,
  8. # So, keep it as small as possible. ;)
  9. #
  10. # inspired from:
  11. # httpd as bashism via (german) /
  12. # https://www.heise.de/newsticker/meldung/Webserver-als-Shell-Einzeiler-1936993.html
  13. # logging() based loosely on echoerr() via http://stackoverflow.com/a/2990533
  14. #
  15. # No (cmdline) options, fsckit. ;)
  16. #
  17. # My versioning sheme is _really_ simple. I just use a timestamp according to
  18. # ISO 8601:2004 / EN 28601:1992 using a 24hr day based on
  19. # european central time or european central summer time
  20. # (CET=UTC+1, CEST=UTC+2) because of:
  21. # https://en.wikipedia.org/wiki/ISO_8601#General_principles
  22. #
  23. # take it or i don't care........ ;)
  24. # Ah, version-number detail varies with needs, if i version more than one
  25. # change a day, i may add a counter, or i may even specify versions via
  26. # date down to hours/minutes/seconds/ticks on the host..
  27. #
  28. # but that rarely happens at all....
  29. #
  30. # For a single or just first release in any given time domain,
  31. # only the changed parts may be included!
  32. # Anything behind that will probably be omitted. ;)
  33. #
  34. # I just jerked that into the keyboard, i do not really maintain it, for now.
  35. #
  36. # Anyways, you can reach me at bashttpd at hackbyte dot de if you need to.
  37. #
  38. # CHANGELOG:
  39. # version 20161216T10:30    Coming to existence in this universe.
  40. #                           I serve, therefore i am.
  41. #
  42. # version 20161216T19:23    heh, o.k. it could be better.
  43. #
  44. # version 20161217          what the? it's getting bigger.
  45. #                           but who cares? ;)
  46. #
  47. # version 20151218          o.k. fsckit, we even include a md5
  48. #                           digest for the file we serve. ;)
  49. #
  50. # version 20161218T05:00    Sometimes, writing documentation is
  51. #                           really chilling task...
  52. #
  53. # version 20161218T18:00    Now we even deliver an accurate MD5 digest. 8-)
  54. #
  55. # version 20161225          Fixed Loggingfoo into some 'usable state'
  56. #                           Additionally, changed file to serve vars w/o reflecting changes
  57. #                           to handle them that way.... fixed.
  58. #
  59. ####################################################################################################
  60. #
  61. # 'user' configurable stuff
  62. #
  63. CFG_BASEDIR="/home/hackbyte/wpad/"
  64. CFG_FILETOSERVE=${CFG_BASEDIR}"wpad.dat"
  65. #CFG_FILETOSERVE="/home/hackbyte/bin/bashttpd.sh"
  66. CFG_OURPORT=8080
  67.  
  68. # Please change _only_ these, according to your local machine....
  69. THISHOSTS_NETCAT=$(which netcat)            # Which could/should be 'netcat' or
  70.                                             # just 'nc'
  71. NETCAT_OPTIONS="-c -l -p ${CFG_OURPORT}"    # Well well, you should know what
  72.                                             # _you_ do....
  73. #LOGTOSTDERR=1                               # Log to stderr? yes=1/no=0
  74. ####################################################################################################
  75. #
  76. # PLEASE do not touch anything below this, unless you know what you do...
  77. # if so .. have fun. ;)
  78. #
  79. THISHOSTS_DATE=$(which date)            # Should not change at all
  80. THISHOSTS_LOGGER=$(which logger)        # Should not change at all
  81. THISHOSTS_STAT=$(which stat)            # Should not change at all
  82. THISHOSTS_MD5SUM=$(which md5sum)        # Should not change at all
  83. ####################################################################################################
  84. #
  85. # Stuff i need for convenience or clearness of what we do...... ;)
  86. #
  87. OUR_FSCKING_NAME=$(basename ${0})       # cuz, you're free to name it what the
  88.                                         # heck you want.... even if you have
  89.                                         # an army of me running.... ;)
  90. OUR_PID=${BASHPID}                      # want to be able to tell you my PID
  91. OUR_LINEBREAK="\r\n"                    # do not even ask! ;)
  92. #OUR_LOGCMD="${THISHOSTS_LOGGER} --stderr -t ${OUR_FSCKING_NAME}"
  93. OUR_LOGCMD="${THISHOSTS_LOGGER} -t ${OUR_FSCKING_NAME}"
  94.                                         # And finally, we wanna give you some clue who we are,
  95.                                         # if we write something to the syslog(!).
  96. ####################################################################################################
  97. #
  98. # If you know any way we can handle that with bash _only_, please tell me. ;)
  99. #
  100. OUR_FILELENGTH="$(${THISHOSTS_STAT} -c %s ${CFG_FILETOSERVE})"
  101. ####################################################################################################
  102. #
  103. # we're using netcat, told ya?
  104. #
  105. OUR_NETCAT_CMD="${THISHOSTS_NETCAT} ${NETCAT_OPTIONS}"
  106. #OUR_NETCAT_CMD="cat -"
  107. ####################################################################################################
  108. # datefoo
  109. MADATE() { printf "%s" "$($THISHOSTS_DATE '+%Y%m%dT%T.%N (%Z UTC%:::z)')"; }
  110. MASHORTDATE() { printf "%s" "$($THISHOSTS_DATE '+%Y%m%dT%T (%Z UTC%:::z)')"; }
  111. ####################################################################################################
  112. #
  113. LOGFOO() {
  114.     MAMSG="$(MADATE) pid:${OURPID} $*"
  115.     printf "%s" "${MAMSG}" | $OUR_LOGCMD
  116.     }
  117. LOGSHORTFOO() {
  118.     MAMSG="$(MASHORTDATE) pid:${OURPID} $*"
  119.     printf "%s" "${MAMSG}" | $OUR_LOGCMD
  120.     }
  121. ####################################################################################################
  122. #
  123. MAKE_MD5SUM() {
  124.     MAKE_MD5SUMVAR=$(${THISHOSTS_MD5SUM} ${CFG_FILETOSERVE})
  125.     echo -n "${MAKE_MD5SUMVAR:00:32}"
  126.     }
  127. ####################################################################################################
  128. #
  129. MAKE_HEADER() {
  130.     OUR_HEADERBUILD="" ## always clean up before use ;)
  131.     OUR_HEADERBUILD="${OUR_HEADERBUILD}HTTP/1.1 200 OK${OUR_LINEBREAK}"
  132.     #OUR_HEADERBUILD="${OUR_HEADERBUILD}Content-Type: application/x-ns-proxy-autoconfig${OUR_LINEBREAK}"
  133.     OUR_HEADERBUILD="${OUR_HEADERBUILD}Content-Type: application/x-ns-proxy-autoconfig${OUR_LINEBREAK}"
  134.     OUR_HEADERBUILD="${OUR_HEADERBUILD}Content-Length: ${OUR_FILELENGTH}${OUR_LINEBREAK}"
  135.     OUR_HEADERBUILD="${OUR_HEADERBUILD}Content-MD5: $(MAKE_MD5SUM)${OUR_LINEBREAK}"
  136.     OUR_HEADERBUILD="${OUR_HEADERBUILD}Connection: close${OUR_LINEBREAK}"
  137.     #Content-Type: text/html; charset=ISO-8859-15
  138.     #Cache-Control: no-cache
  139.     OUR_HEADERBUILD="${OUR_HEADERBUILD}${OUR_LINEBREAK}"
  140.     echo -n "${OUR_HEADERBUILD}"
  141.     }
  142. ####################################################################################################
  143. #
  144. PUT_BODY() {
  145.     echo -e "$(<${CFG_FILETOSERVE})"
  146.     }
  147.  
  148. ####################################################################################################
  149. #
  150. MAIN_LOOP() {
  151.     while true ; do {
  152.         #echo -en "${OUR_CONTENT}}" | ${OUR_NETCAT_CMD} 2>&1 >/dev/null
  153.         echo -e "${1}" | ${OUR_NETCAT_CMD} 2>&1 | $OUR_LOGCMD
  154.         LOGFOO "${CFG_FILETOSERVE} queried!"
  155.         } ; done
  156. }
  157.  
  158. ####################################################################################################
  159. #
  160. # Well done, we're going to serve the steak...
  161. #
  162. $(LOGSHORTFOO "${OUR_FSCKING_NAME} started with ${CFG_FILETOSERVE} size ${OUR_FILELENGTH} bytes...")
  163. MAIN_LOOP "$(MAKE_HEADER)$(PUT_BODY)"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement