New Netcat server script

Posted on October 7th, 2006 by dandavis

In this post, I gave a command to serve up a Mozilla search plugin and icon file so you could use a JavaScript call to add the search plugin without restarting Firefox… that makes more sense if you read the other article…

Well, I decided to put that code into a bash script in case I needed it again… enjoy. Or, not.

#!/bin/bash

FILE=$1
PORT=$2

if [[ "x" == "x${FILE}" || "x" == "x${PORT}" ]]; then
        echo "error: server <filename> <port>"
        exit
fi

if [[ "x" == "x${UID}" ]]; then
        UID=`id -u`
fi

if [[ ${PORT} < 1024 && ${UID} > 0 ]]; then
        echo "error: unable to use privileged port"
        exit
fi

(echo "HTTP/1.1 200 OK" ; \\
 echo "Content-Length: "`wc -c ${FILE} | \\
      awk '{print $1}'` ; \\
 echo "Connection: close" ; \\
 echo ; cat ${FILE} ) | nc -l -p ${PORT}

Firefox addSearchEngine and Netcat

Posted on September 27th, 2006 by dandavis

Today on Lifehacker there was a post with a Mozilla search plugin just for lifehacker.com. Coolness.

But, instead of giving folks a super simple link to auto-install the plugin, they provided the two files (source and icon) in a zip file. So I was kind and offered adivce on creating the super simple auto-install link:

<a href="javascript:window.sidebar.addSearchEngine('http://url/to/lh.src', 'http://url/to/lh.gif', 'Lifehacker', 'Reference');">Install the LH Search Plugin</a>

Cool, huh?

Then I got to thinking about people who downloaded the zip and wanted to install the plugin without having to restart Firefox. And what if they don't run their own webserver… got me to thinking about netcat

A couple of commands at a bash prompt… one for the source and one for the icon… then a javascript call in Firefox…

(echo "HTTP/1.1 200 OK" ; echo "Content-Length: "`wc -c lh.src | awk '{print $1}'` ; echo "Connection: close" ; echo "Content-Type: application/x-wais-source" ; echo ; cat lh.src) | nc -l -p 1500 >/dev/null

(echo "HTTP/1.1 200 OK" ; echo "Content-Length: "`wc -c lh.gif | awk '{print $1}'` ; echo "Connection: close" ; echo "Content-Type: image/gif" ; echo ; cat lh.gif) | nc -l -p 1501 >/dev/null

<a href="javascript:window.sidebar.addSearchEngine('http://localhost:1500/lh.src', 'http://localhost:1501/lh.gif', 'Lifehacker', 'Reference');">Install the LH Search Plugin</a>

And, yes, it would be easier to just install Apache. Update: OK, it would actually be easier to install XAMPP, "an easy to install Apache distribution containing MySQL, PHP and Perl. XAMPP is really very easy to install and to use – just download, extract and start."

Oh, and if you're wondering, I couldn't remember which headers MUST be returned in a web request, so I just included a few that I knew would be a good idea to have in there… works well enough, it seems.