<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Smörgåsbord &#187; Code</title>
	<atom:link href="http://smorgasbord.gavagai.nl/topics/tech/code-tech/feed/" rel="self" type="application/rss+xml" />
	<link>http://smorgasbord.gavagai.nl</link>
	<description>Ambachtelijk bereide beschouwingen.</description>
	<lastBuildDate>Thu, 02 Sep 2010 08:17:45 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Social social networking with Facemix</title>
		<link>http://smorgasbord.gavagai.nl/2010/04/social-social-networking-with-facemix/</link>
		<comments>http://smorgasbord.gavagai.nl/2010/04/social-social-networking-with-facemix/#comments</comments>
		<pubDate>Fri, 16 Apr 2010 16:50:52 +0000</pubDate>
		<dc:creator>Wicher</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[WWW]]></category>
		<category><![CDATA[aprilfools]]></category>
		<category><![CDATA[en_GB]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[squid]]></category>
		<category><![CDATA[upsidedownternet]]></category>
		<category><![CDATA[url_rewrite_program]]></category>

		<guid isPermaLink="false">http://smorgasbord.gavagai.nl/?p=1007</guid>
		<description><![CDATA[It&#8217;s not too late for posts about April Fool&#8217;s Day pranks I hope?
In the tradition of the Upsidedownternet this April 1st I had some fun with Facebook addicts.
You may not be aware of the fact that any picture on facebook is publicly accessible. Yes, it is. There&#8217;s no authentication &#038; authorisation whatsoever. Handling those in [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s not too late for posts about April Fool&#8217;s Day pranks I hope?<br />
In the tradition of <a href="http://www.ex-parrot.com/pete/upside-down-ternet.html">the Upsidedownternet</a> this April 1st I had some fun with Facebook addicts.</p>
<p>You may not be aware of the fact that any picture on facebook is publicly accessible. Yes, it is. There&#8217;s no authentication &#038; authorisation whatsoever. Handling those in a scalable way would ramp up costs. Your privacy is not worth those costs. Contrary to the impression you are trying to deliver through your profile, you are not important. Happy shareholders are important!</p>
<p>Due to this fact I just need to know the URLs of your pictures. From the URL I can determine whether it&#8217;s a profile picture, profile picture thumbnail, photo, photo thumbnail, etc.</p>
<p>Wouldn&#8217;t it be fun to mix the pictures of the facebook page you are currently viewing with those from facebook pages others are viewing? So when you&#8217;re browsing your friend&#8217;s albums, you not only see his pictures but pictures from other peoples&#8217; albums too, and vice versa?<br />
The pictures may be requested by the guy across the bar, or by the girl one floor down in the library, or by anyone on the same network as you are — all of you are browsing together with the people in your physical vicinity, sharing whatever pictures you encounter! It&#8217;s beyond Facebook. It&#8217;s crowdbrowsing. It&#8217;s Megafacebook.<br />
While you may not know these newly inserted friends, you might get to. Maybe you bump into one another at the toilets, or at the counter.<br />
<i>&#8220;Why is everyone staring at me like that?&#8221;</i> you naively wonder. (They&#8217;ve seen those pictures).<br />
<i>&#8220;Does she know that I know about those pictures of her and her friends? But wait&#8230; what might she know about me?&#8221;</i>, your paranoid mind ponders.<br />
It&#8217;s all about what you think of others and what others think of you. Total absorption. Now that&#8217;s what I call social networking. All hail Facebook <b><i>Social!</i></b></p>
<p><a href="http://smorgasbord.gavagai.nl/wp-content/uploads/2010/04/FacebookSocial3.png"><img src="http://smorgasbord.gavagai.nl/wp-content/uploads/2010/04/FacebookSocial3.png" alt="Facebook Social" title="Facebook Social" width="90" height="20" class="aligncenter size-full wp-image-1009" /></a></p>
<p>Give the wifi crowd at your local coffeeshop the pleasure of learning a little bit more about eachothers lives and friends.</p>
<h3>Get to work</h3>
<p>You need:</p>
<ul>
<li>one network vulnerable to ARP poison routing (that&#8217;s most of them) or one network which you already control anyway. Make everyone route their traffic through your machine.
</li>
<li>one installment of the Nginx web server, configured with <code>--with-http_random_index_module</code>. I use the 0.8.3x series.
</li>
<li>one installment of the Squid http proxy server. I use the 3.1 series.
</li>
<li>Perl and LWP::Simple.
</li>
</ul>
<h4>Set up Nginx</h4>
<p>Create some directories to hold the images:<br />
<code><br />
mkdir /var/www/facemix/{albums,photos,photosthumb,smoelen,smoelenthumb}<br />
</code><br />
Tell Nginx to respond to requests for those directories by randomly serving one of the files in them:<br />
<code><br />
        location ~ ^/facemix/([^/]+)(/?.*)$ {<br />
                alias /var/www/facemix/$1/$2;<br />
                random_index on;<br />
                expires -1;<br />
                }<br />
</code><br />
You need the &#8216;expires -1&#8242; to avoid caching. If proxies or user agents were to cache the results, they wouldn&#8217;t be very random anymore now would they.</p>
<p>Stick some files in there and test your installation.</p>
<h4>Set up Squid</h4>
<p>Set up squid in interception mode. If you&#8217;re not NATting the routed traffic, set it to run on port 80. If Nginx is already listening on that socket, make Nginx listen on some other port, or localhost only, while running squid on port 80 but only on the external interface.</p>
<h4>Set up networking</h4>
<p>This is for iptables.</p>
<ul>
<li>You&#8217;re NATting the pwned hosts. Run something along the lines of<br />
<code>iptables -t nat -A PREROUTING -i $INTERFACE -p tcp --dport 80 -j REDIRECT --to-port 8080</code><br />
to redirect all traffic incoming on $INTERFACE and destined for port 80 to port 8080, which is where you need squid to listen on.</li>
<li>You&#8217;re doing 2-way ARP poisoning (cheers!). Run something along the lines of<br />
<code>iptables -t nat -A PREROUTING -p tcp -m tcp --dport 80 -j DNAT --to-destination $YOURIP</code><br />
Squid needs to run on port 80 on interface with IP $YOURIP.
</li>
</ul>
<p>Check Squid&#8217;s logs to verify that requests are intercepted successfully.</p>
<h4>Run the redirection script</h4>
<p>I don&#8217;t touch Perl very often, and cobbling together this script made me remember why that is. It&#8217;s very usable as a means of frightening little kids.<br />
In a nutshell, what my redirector script does is</p>
<ol>
<li>determine whether the URL fed to it by Squid is a facebook picture url;
</li>
<li>if so, and if we don&#8217;t have that picture yet, fork off to download it;
</li>
<li>point Squid to a random picture of the same type (served by Nginx).
</li>
</ol>
<p>I like the forking. I dislike the iffed regexes which could probably be condensed into one but then it wouldn&#8217;t be &#8216;cobbling together&#8217; anymore. </p>
<p>Adjust the variables for your setup and tell Squid about the script (eg <code>url_rewrite_program /usr/local/lib/facemix-squidredir.pl</code>).</p>
<p>The Facebook logo will change to reflect the fact that the users are now browsing facebook in <b><i>Social!</i></b> mode.</p>
<p>One further note: This is privacy-invasive. I brush away my moral doubts by stating that anyone who signed away their privacy rights when joining facebook AND AT THE SAME TIME entertains any expectations with respect to privacy,<br />
« inhale »<br />
&#8230; is utterly mental and has completely lost any and all sense of proportionality. If you care about privacy, why use a service which lets you view <b>any</b> picture of <b>any</b> user <i>regardless</i> of who you are? Who are you kidding?</p>
<p>If you&#8217;re still reading, here&#8217;s the script:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl -w</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> LWP<span style="color: #339933;">::</span><span style="color: #006600;">Simple</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #0000ff;">$WEBROOT</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'http://localhost/facemix/'</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$WEBDIR</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'/var/www/facemix/'</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$CHANCE</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">#One in X requests gets mixed</span>
<span style="color: #0000ff;">$SIG</span><span style="color: #009900;">&#123;</span>CHLD<span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'IGNORE'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #0000ff;">$|</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">&lt;&gt;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">local</span> <span style="color: #0000ff;">@reqfrags</span> <span style="color: #339933;">=</span> <span style="color: #000066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/ /</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$_</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">local</span> <span style="color: #0000ff;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@reqfrags</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span>    <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$url</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/(^http:\/\/.*.fbcdn.net\/rsrc.php\/z7VU4\/hash\/66ad7upf.png$)/</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;http://smormedia.gavagai.nl/2010/04/FacebookSocial2.png<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">elsif</span>    <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$url</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/(^http:\/\/photos-.*.fbcdn.net\/.*\/.*_n.jpg$)/</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$url</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/(^http:\/\/photos-.*.fbcdn.net\/.*\/n.*.jpg$)/</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">&amp;mixurl</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'photos/'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">elsif</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$url</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/(^http:\/\/photos-.*.fbcdn.net\/.*\/.*_s.jpg$)/</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$url</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/(^http:\/\/photos-.*.fbcdn.net\/.*\/s.*.jpg$)/</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">&amp;mixurl</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'photosthumb/'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">elsif</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$url</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/(^http:\/\/profile.*.fbcdn.net\/.*\/.*_n.jpg$)/</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$url</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/(^http:\/\/profile.*.fbcdn.net\/.*\/n.*.jpg$)/</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">&amp;mixurl</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'smoelen/'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">elsif</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$url</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/(^http:\/\/profile.*.fbcdn.net\/.*\/.*_q.jpg$)/</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$url</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/(^http:\/\/profile.*.fbcdn.net\/.*\/q.*.jpg$)/</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">&amp;mixurl</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'smoelenthumb/'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">elsif</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$url</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/(^http:\/\/photos-.*.fbcdn.net\/.*\/.*_a.jpg$)/</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$url</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/(^http:\/\/photos-.*.fbcdn.net\/.*\/a.*.jpg$)/</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">&amp;mixurl</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'albums/'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">else</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">print</span> <span style="color: #0000ff;">$url</span><span style="color: #339933;">.</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> mixurl <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">#args: subdir, url</span>
    <span style="color: #000066;">local</span> <span style="color: #0000ff;">$vork</span> <span style="color: #339933;">=</span> <span style="color: #000066;">fork</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$vork</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&amp;getit</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">int</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$CHANCE</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066;">print</span> <span style="color: #0000ff;">$WEBROOT</span><span style="color: #339933;">.</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066;">print</span> <span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> getit <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">#args: subdir, url</span>
    <span style="color: #000066;">local</span> <span style="color: #0000ff;">$storedir</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$WEBDIR</span><span style="color: #339933;">.</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">local</span> <span style="color: #0000ff;">@urlfrags</span> <span style="color: #339933;">=</span> <span style="color: #000066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\//</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">local</span> <span style="color: #0000ff;">$fname</span> <span style="color: #339933;">=</span> <span style="color: #000066;">pop</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@urlfrags</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000066;">stat</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$storedir</span><span style="color: #339933;">.</span><span style="color: #0000ff;">$fname</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      getstore<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$storedir</span><span style="color: #339933;">.</span><span style="color: #ff0000;">'._tmp-'</span><span style="color: #339933;">.</span><span style="color: #0000ff;">$fname</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000066;">rename</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$storedir</span><span style="color: #339933;">.</span><span style="color: #ff0000;">'._tmp-'</span><span style="color: #339933;">.</span><span style="color: #0000ff;">$fname</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$storedir</span><span style="color: #339933;">.</span><span style="color: #0000ff;">$fname</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066;">exit</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://smorgasbord.gavagai.nl/2010/04/social-social-networking-with-facemix/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Network security 101: &#8216;Stealth mode&#8217; + ARP cache inoculation</title>
		<link>http://smorgasbord.gavagai.nl/2010/01/network-security-101-stealth-mode-arp-cache-inoculation/</link>
		<comments>http://smorgasbord.gavagai.nl/2010/01/network-security-101-stealth-mode-arp-cache-inoculation/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 19:28:48 +0000</pubDate>
		<dc:creator>Wicher</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Howto]]></category>
		<category><![CDATA[arp spoofing]]></category>
		<category><![CDATA[en_GB]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[wifi]]></category>

		<guid isPermaLink="false">http://smorgasbord.gavagai.nl/?p=820</guid>
		<description><![CDATA[There are times you need to connect to &#8216;dirty&#8217; networks such as public WiFi hotspots. Hopefully you&#8217;re ensuring that sensitive information is encapsulated in transport layer security enabled protocols such as SSL, because anyone on the same link (in the case of WiFi, that&#8217;s the air surrounding you. A vacuum will do, too, but that&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>There are times you need to connect to &#8216;dirty&#8217; networks such as public WiFi hotspots. Hopefully you&#8217;re ensuring that sensitive information is encapsulated in transport layer security enabled protocols such as SSL, because anyone on the same link (in the case of WiFi, that&#8217;s the air surrounding you. A vacuum will do, too, but that&#8217;s less common) can listen in on the traffic you&#8217;re sending. With SSL encapsulation such as HTTP over SSL (https://), your traffic can still be read — but for those who do it&#8217;s an extremely boring read because they don&#8217;t know the session key, only you and the other endpoint do. Hopefully.</p>
<p>One particularly nasty thing that can happen to you is when your machine is subverted into using the attacker&#8217;s machine as the router.  That is known as <a href="http://en.wikipedia.org/wiki/Arp_poisoning">ARP poison routing</a>. The attacker can proceed to not only read the traffic coming from your machine (which, on a shared medium, could be done anyway), or read the traffic going into your machine (again: on a shared medium, that could be done anyway), but the attacker can now also <em><strong>modify</strong></em> the traffic between you and the rest of the non-local network, e.g., the internet, in both directions. And that&#8217;s when he can really go to town with your traffic. Injecting a <a href="http://code.google.com/p/middler/">javascript keylogger</a> into all the webpages you visit. &#8216;<a href="http://hamster.erratasec.com/help/index.html">Sidejacking</a>&#8216; your sessions, so he does not even need to know your passwords, just your session cookies — which you happen to transmit with every page request.</p>
<p>All possible unless you use transport layer security, which is tamper-proof once properly set up. <em><strong>Once properly set up</strong></em>. But setting up can have problems of itself — there are ways of preventing you ever going from HTTP to HTTPS. If you know a thing or two about HTTP and SSL you&#8217;ll be <a href="http://securitytube.net/Defeating-SSL-using-SSLStrip-%28Marlinspike-Blackhat%29-video.aspx">delighted</a> to learn about</a> Moxie&#8217;s <a href="http://www.thoughtcrime.org/software/sslstrip/">very evil but very clever ways</a> of doing so.</p>
<p>Anyway, some level of security can be achieved if you tell your machine to ignore any messages sent to you from the other machines on the local network. That includes messages that will make your machine believe that the router has suddenly changed its physical address — which is quite unlikely to happen, but those messages are exactly the type of message an impersonator would send you. Of course we&#8217;d need to whitelist the routers of the network, otherwise we can&#8217;t get traffic out of it and onto other networks. DNS resolvers will need whitelisting too, unless you&#8217;re running one on your own machine (probably not).<br />
Not openly announcing your presence may also be something you wish for. If you have ever been on a network with a Mac user you have probably seen them popping up in your Zeroconf service browser as &#8220;Firstname Lastname&#8217;s iSomething&#8221;. Let&#8217;s cut down on that kind of promiscuity, too. But you should understand now that you can not actually hide unless you turn off your WiFi. Shared medium, remember?</p>
<p>I prepared a simple script to accomplish the above. I&#8217;ve used <code>ip</code> from the iproute2 package instead of sticking to old-school <code>route</code>, <code>ifconfig</code>, <code>arp</code> &#038; co. And I must say <code>ip neigh flush nud stale</code> has a poetic ring to it, <a href="http://ars.userfriendly.org/cartoons/?id=20100124">wouldn&#8217;t you agree?</a></p>
<p>Take note: this will only protect you from some kind of attacks, and only partially. An attacker has a window of opportunity between your machine getting assigned a DHCP lease and you running this script, for instance. Or maybe the access point <a href="http://www.viddler.com/explore/hak5/videos/56/">is rigged</a>. Actually all protection other than end-to-end encryption combined with mutual authentication is pretty useless on shared networks ;-)</p>
<p>Here&#8217;s the script. Linux-only. If you want to use it, get the latest version from <a href="http://smormedia.gavagai.nl/dist">my public repository</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># arpshield 0.2</span>
<span style="color: #666666; font-style: italic;"># Protects against ARP poisoning and cloaks your machine for all </span>
<span style="color: #666666; font-style: italic;"># local link devices but the router(s) and the DNS server(s).</span>
<span style="color: #666666; font-style: italic;"># Whitelisting DHCP servers also works if you use the dhcpcd program</span>
<span style="color: #666666; font-style: italic;"># to obtain DHCP leases.</span>
<span style="color: #666666; font-style: italic;"># This program is of no help if your setup is already poisoned.</span>
<span style="color: #666666; font-style: italic;"># Have a look at ArpON (http://arpon.sourceforge.net/manpage.html) if</span>
<span style="color: #666666; font-style: italic;"># you need more extensive protection.</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># Needs 'ip', 'awk', 'sed', 'arptables', and 'arping' and expects</span>
<span style="color: #666666; font-style: italic;"># them on $PATH. Needs appropriate privileges (so use sudo).</span>
<span style="color: #666666; font-style: italic;"># Takes a network interface as an argument. The network interface</span>
<span style="color: #666666; font-style: italic;"># should be up and configured. If no argument is given, clear all</span>
<span style="color: #666666; font-style: italic;"># rules. Obviously you should do that before connecting to a new</span>
<span style="color: #666666; font-style: italic;"># network.</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># Copyright 2010 Wicher Minnaard (wicher@gavagai.eu)</span>
<span style="color: #666666; font-style: italic;"># License: Creative Commons Attribution-Share Alike 3.0</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Do you use dhcpcd for aquiring DHCP leases? And is it running?</span>
<span style="color: #007800;">dhcpcdLEASEFILE</span>=<span style="color: #ff0000;">&quot;/var/lib/dhcpcd-<span style="color: #007800;">${1}</span>.info&quot;</span>
<span style="color: #007800;">dhcpcdPIDFILE</span>=<span style="color: #ff0000;">&quot;/var/run/dhcpcd-<span style="color: #007800;">${1}</span>.pid&quot;</span>
<span style="color: #7a0874; font-weight: bold;">test</span> <span style="color: #660033;">-f</span> <span style="color: #800000;">${dhcpcdLEASEFILE}</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #7a0874; font-weight: bold;">test</span> <span style="color: #660033;">-f</span> <span style="color: #800000;">${dhcpcdPIDFILE}</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #7a0874; font-weight: bold;">source</span> <span style="color: #800000;">${dhcpcdLEASEFILE}</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># In case you lack the luxury of dhcpcd, where is your resolv.conf?</span>
<span style="color: #007800;">RESOLV</span>=<span style="color: #ff0000;">&quot;/etc/resolv.conf&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># No user-servicable parts below this line.</span>
<span style="color: #007800;">DEV</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">${1}</span>&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># I know, I know. But if your routing table contains 0.333.456.789 you have bigger problems ;-)</span>
<span style="color: #007800;">IPREGEX</span>=<span style="color: #ff0000;">&quot;\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Register</span>
<span style="color: #007800;">MACreg</span>=<span style="color: #ff0000;">&quot;&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># If not run as root, bail</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$(id -u)</span>&quot;</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;You need root privileges to modify networking parameters. Exiting.&quot;</span> <span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">2</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">2</span>
&nbsp;
getmac<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>
<span style="color: #666666; font-style: italic;"># sets MAC register by IP. Sets to nil, if the MAC is not on the local link. </span>
  <span style="color: #007800;">getMAC</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span>ip neigh show <span style="color: #800000;">${1}</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print $5}'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-z</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${getMAC}</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    arping <span style="color: #660033;">-c1</span> <span style="color: #660033;">-I</span> <span style="color: #800000;">${DEV}</span> <span style="color: #800000;">${1}</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">1</span>
    <span style="color: #007800;">getMAC</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span>ip neigh show <span style="color: #800000;">${1}</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print $5}'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">fi</span>
  <span style="color: #007800;">MACreg</span>=<span style="color: #800000;">${getMAC}</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
allow<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>
  <span style="color: #666666; font-style: italic;"># Whitelists traffic to and from particular IP+MAC pairings and</span>
  <span style="color: #666666; font-style: italic;"># adds them to static ARP.</span>
  <span style="color: #007800;">IP</span>=<span style="color: #800000;">${1}</span>
  <span style="color: #007800;">MAC</span>=<span style="color: #800000;">${2}</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${IP}</span>&quot;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${MAC}</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    arptables <span style="color: #660033;">-A</span> INPUT  <span style="color: #660033;">-s</span> <span style="color: #800000;">${IP}</span> <span style="color: #660033;">--source-mac</span>      <span style="color: #800000;">${MAC}</span> <span style="color: #660033;">-j</span> ACCEPT
    arptables <span style="color: #660033;">-A</span> OUTPUT <span style="color: #660033;">-d</span> <span style="color: #800000;">${IP}</span> <span style="color: #660033;">--destination-mac</span> <span style="color: #800000;">${MAC}</span> <span style="color: #660033;">-j</span> ACCEPT
    ip neigh replace <span style="color: #800000;">${IP}</span> lladdr <span style="color: #800000;">${MAC}</span> nud permanent dev <span style="color: #800000;">${DEV}</span>
  <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${DEV}</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #666666; font-style: italic;"># whitelist the routers</span>
  <span style="color: #7a0874; font-weight: bold;">test</span> <span style="color: #660033;">-z</span> <span style="color: #800000;">${GATEWAYS}</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #007800;">GATEWAYS</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span>ip route show dev <span style="color: #800000;">${DEV}</span><span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;s:.* via \(<span style="color: #007800;">${IPREGEX}</span>\).*:\1:p&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">for</span> GWIP <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #800000;">${GATEWAYS}</span>; <span style="color: #000000; font-weight: bold;">do</span>
    <span style="color: #007800;">MACreg</span>=<span style="color: #ff0000;">&quot;&quot;</span>
    getmac <span style="color: #800000;">${GWIP}</span>
    allow <span style="color: #800000;">${GWIP}</span> <span style="color: #800000;">${MACreg}</span>
  <span style="color: #000000; font-weight: bold;">done</span>
  <span style="color: #666666; font-style: italic;"># whitelist the DNS servers</span>
  <span style="color: #7a0874; font-weight: bold;">test</span> <span style="color: #660033;">-z</span> <span style="color: #800000;">${DNSSERVERS}</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #007800;">DNSSERVERS</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;s:^nameserver \(<span style="color: #007800;">${IPREGEX}</span>\):\1:p&quot;</span> <span style="color: #800000;">${RESOLV}</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">for</span> DNS <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #800000;">${DNSSERVERS}</span>; <span style="color: #000000; font-weight: bold;">do</span>
    <span style="color: #007800;">MACreg</span>=<span style="color: #ff0000;">&quot;&quot;</span>
    getmac <span style="color: #800000;">${DNS}</span>
    allow <span style="color: #800000;">${DNS}</span> <span style="color: #800000;">${MACreg}</span>
  <span style="color: #000000; font-weight: bold;">done</span>
  <span style="color: #666666; font-style: italic;"># if using dhcpcd, we can whitelist the DHCP server too</span>
  <span style="color: #7a0874; font-weight: bold;">test</span> <span style="color: #660033;">-n</span> <span style="color: #800000;">${DHCPSID}</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> getmac <span style="color: #800000;">${DHCPSID}</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> allow <span style="color: #800000;">${DHCPSID}</span> <span style="color: #800000;">${MACreg}</span>
  <span style="color: #666666; font-style: italic;"># set default policy to DROP    </span>
  arptables <span style="color: #660033;">-P</span> INPUT DROP
  arptables <span style="color: #660033;">-P</span> OUTPUT DROP
  <span style="color: #666666; font-style: italic;"># clear out non-hardcoded ARP cache entries</span>
  ip neigh flush nud reachable
  ip neigh flush nud stale
<span style="color: #000000; font-weight: bold;">else</span>
  <span style="color: #666666; font-style: italic;"># No argument given, so clean up.</span>
  arptables <span style="color: #660033;">-F</span>
  arptables <span style="color: #660033;">-P</span> INPUT ACCEPT
  arptables <span style="color: #660033;">-P</span> OUTPUT ACCEPT
  ip neigh flush nud permanent
<span style="color: #000000; font-weight: bold;">fi</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://smorgasbord.gavagai.nl/2010/01/network-security-101-stealth-mode-arp-cache-inoculation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>LIRC plugin for Exaile</title>
		<link>http://smorgasbord.gavagai.nl/2009/12/lirc-plugin-for-exaile/</link>
		<comments>http://smorgasbord.gavagai.nl/2009/12/lirc-plugin-for-exaile/#comments</comments>
		<pubDate>Wed, 09 Dec 2009 01:44:58 +0000</pubDate>
		<dc:creator>Wicher</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[en_GB]]></category>
		<category><![CDATA[exaile]]></category>
		<category><![CDATA[lirc]]></category>
		<category><![CDATA[lircaile]]></category>
		<category><![CDATA[remote]]></category>

		<guid isPermaLink="false">http://smorgasbord.gavagai.nl/?p=718</guid>
		<description><![CDATA[Just finished up a 0.1 version of a LIRC (Linux Infrared Control) plugin for the Exaile media player. Now you can use your remote with Exaile efficiently. The plugin is in the public repository and is called Lircaile.
I haven&#8217;t touched Python much as of yet, but I&#8217;m pleased with it: it appears to be a [...]]]></description>
			<content:encoded><![CDATA[<p>Just finished up a 0.1 version of a <a href="http://lirc.org">LIRC</a> (Linux Infrared Control) plugin for the <a href="http://exaile.org/">Exaile media player</a>. Now you can use your remote with Exaile efficiently. The plugin is in <a href="http://smormedia.gavagai.nl/dist/">the public repository</a> and is called Lircaile.<br />
I haven&#8217;t touched Python much as of yet, but I&#8217;m pleased with it: it appears to be a consistent language. Well, here&#8217;s my 0.1 effort. I desperately wanted to have some fun with introspection, but I have the feeling the nested exception logic is a bit&#8230; unusual.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># A LIRC plugin for Exaile. Depends on pylirc from http://sourceforge.net/projects/pylirc/</span>
<span style="color: #808080; font-style: italic;"># Copyright (C) 2009 Wicher Minnaard, http://smorgasbord.gavagai.nl / wicher@gavagai.eu</span>
<span style="color: #808080; font-style: italic;">#</span>
<span style="color: #808080; font-style: italic;"># This program is free software: you can redistribute it and/or modify</span>
<span style="color: #808080; font-style: italic;"># it under the terms of the GNU General Public License as published by</span>
<span style="color: #808080; font-style: italic;"># the Free Software Foundation, either version 3 of the License, or</span>
<span style="color: #808080; font-style: italic;"># (at your option) any later version.</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> xl <span style="color: #ff7700;font-weight:bold;">import</span> playlist, player, event
<span style="color: #ff7700;font-weight:bold;">import</span> pylirc, <span style="color: #dc143c;">logging</span>
&nbsp;
LIRCAILE = <span style="color: #008000;">None</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> enable<span style="color: black;">&#40;</span>exaile<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>exaile.<span style="color: black;">loading</span><span style="color: black;">&#41;</span>:
        event.<span style="color: black;">add_callback</span><span style="color: black;">&#40;</span>_enable, <span style="color: #483d8b;">'exaile_loaded'</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        _enable<span style="color: black;">&#40;</span><span style="color: #008000;">None</span>, exaile, <span style="color: #008000;">None</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> _enable<span style="color: black;">&#40;</span>eventname, exaile, nothing<span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">global</span> LIRCAILE
  LIRCAILE = Lircaile<span style="color: black;">&#40;</span>exaile<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> disable<span style="color: black;">&#40;</span>exaile<span style="color: black;">&#41;</span>:
  pylirc.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Lircaile<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">def</span> polLirc<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Pops all queued signals off of the LIRC queue and hands them to
    handleCode() for further processing.&quot;&quot;&quot;</span>
    gopoll = <span style="color: #008000;">True</span>
    <span style="color: #ff7700;font-weight:bold;">while</span><span style="color: black;">&#40;</span>gopoll<span style="color: black;">&#41;</span>:
      <span style="color: #dc143c;">code</span> = <span style="color: black;">&#40;</span>pylirc.<span style="color: black;">nextcode</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
      <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span><span style="color: #dc143c;">code</span><span style="color: black;">&#41;</span>:
        comval = <span style="color: #dc143c;">code</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>comval<span style="color: black;">&#41;</span> == <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
          <span style="color: #008000;">self</span>.<span style="color: black;">handleCode</span><span style="color: black;">&#40;</span>comval<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>          
        <span style="color: #ff7700;font-weight:bold;">else</span>:
          <span style="color: #008000;">self</span>.<span style="color: black;">handleCode</span><span style="color: black;">&#40;</span>comval<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, comval<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
      <span style="color: #ff7700;font-weight:bold;">else</span>:
        <span style="color: #808080; font-style: italic;"># We're done, the queue is empty.</span>
        gopoll = <span style="color: #008000;">False</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">True</span>
&nbsp;
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, exaile<span style="color: black;">&#41;</span>:
    <span style="color: #008000;">self</span>.<span style="color: black;">exaile</span> = exaile
    <span style="color: #008000;">self</span>.<span style="color: black;">logger</span> = <span style="color: #dc143c;">logging</span>.<span style="color: black;">getLogger</span><span style="color: black;">&#40;</span>__name__<span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">socket</span> = pylirc.<span style="color: black;">init</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'lircaile'</span><span style="color: black;">&#41;</span>
    event.<span style="color: black;">EventTimer</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0.05</span>, <span style="color: #008000;">self</span>.<span style="color: black;">polLirc</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">def</span> handleCode<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, command, <span style="color: #66cc66;">*</span>arg<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Takes LIRC signals and uses introspection to try to find appropriate 
    exaile functions to call based on the name of the signal. &quot;&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>command == <span style="color: #483d8b;">'chvol'</span><span style="color: black;">&#41;</span>:
      <span style="color: #008000;">self</span>.<span style="color: black;">exaile</span>.<span style="color: black;">player</span>.<span style="color: black;">set_volume</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">exaile</span>.<span style="color: black;">player</span>.<span style="color: black;">get_volume</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> + <span style="color: #008000;">float</span><span style="color: black;">&#40;</span>arg<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
      func = <span style="color: #008000;">None</span>
      <span style="color: #808080; font-style: italic;"># Look for a matching playlist function</span>
      <span style="color: #ff7700;font-weight:bold;">try</span>:
        func = <span style="color: #008000;">getattr</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">exaile</span>.<span style="color: black;">queue</span>, command<span style="color: black;">&#41;</span>
      <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">AttributeError</span>:
        <span style="color: #808080; font-style: italic;"># No? Then look for a matching player function</span>
        <span style="color: #ff7700;font-weight:bold;">try</span>:
          func = <span style="color: #008000;">getattr</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">exaile</span>.<span style="color: black;">player</span>, command<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">AttributeError</span>:
          <span style="color: #808080; font-style: italic;"># No? Then we're out of options</span>
          <span style="color: #008000;">self</span>.<span style="color: black;">logger</span>.<span style="color: black;">warning</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'No function to handle &quot;'</span>+ command +<span style="color: #483d8b;">'&quot; LIRC event'</span><span style="color: black;">&#41;</span>
      <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">callable</span><span style="color: black;">&#40;</span>func<span style="color: black;">&#41;</span>:
        func<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://smorgasbord.gavagai.nl/2009/12/lirc-plugin-for-exaile/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>how I fixed my Firefox hiccups</title>
		<link>http://smorgasbord.gavagai.nl/2009/05/how-i-fixed-my-firefox-hiccups/</link>
		<comments>http://smorgasbord.gavagai.nl/2009/05/how-i-fixed-my-firefox-hiccups/#comments</comments>
		<pubDate>Thu, 07 May 2009 17:59:39 +0000</pubDate>
		<dc:creator>Wicher</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Howto]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[en_GB]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[inotify]]></category>
		<category><![CDATA[tmpfs]]></category>

		<guid isPermaLink="false">http://smorgasbord.gavagai.nl/?p=373</guid>
		<description><![CDATA[The other day I compiled Firefox 3.5-beta4, and, apart from many improvements, I noticed that I am now affected by the infamous &#8216;hiccups&#8217;. Firefox will stall for seconds at a time on my poor netbook. Details on how this relates to the many fsync() calls made by the persistance layer (SQLite) can be found all [...]]]></description>
			<content:encoded><![CDATA[<p>The other day I compiled Firefox 3.5-beta4, and, apart from many improvements, I noticed that I am now affected by the infamous &#8216;hiccups&#8217;. Firefox will stall for seconds at a time on my poor netbook. Details on how this relates to the many fsync() calls made by the persistance layer (SQLite) can be found all over the net.<br />
But I don&#8217;t want Firefox to stall and I don&#8217;t want to keep my harddisk awake with all these writes when I&#8217;m on battery power.<br />
Luckily, both these problems go away if you put your Firefox profile on a ramdisk. There are numerous guides out there that save you from working out the details; I used <a href="http://forums.gentoo.org/viewtopic-t-717117.html">this one from the Gentoo forums</a>.<br />
The guide uses cron to sync the (presumably) modified contents of the ramdisk &#8211; bookmarks, cookies, whatever &#8211; back to permanent storage (harddisk). You and I both know that while it&#8217;s often convenient to use cron, it&#8217;s not always the Right Way of Doing Things®. Why sync if nothing&#8217;s changed? I wrote a script that employs the inotify system to do the syncing only when necessary. You&#8217;ll find it in the forum thread I linked to earlier, but I post it here &#8220;ter lering ende vermaeck&#8221;. Depending on your browser there might be a vertical scrollbar at the bottom which lets you read up to the EOL&#8217;s ;-)<br />
You can find the latest version at <a href="http://smormedia.gavagai.nl/dist/">my public repo</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Packfox, a tool to facilitate running Firefox with its profile stored</span>
<span style="color: #666666; font-style: italic;"># in RAM (tmpfs). Copyright 2008-2009 Wicher Minnaard, wicher@gavagai.eu .</span>
<span style="color: #666666; font-style: italic;"># Distributed under the WTFPL, http://smormedia.gavagai.nl/dist/packfox/COPYING</span>
<span style="color: #666666; font-style: italic;"># Latest version available at http://smormedia.gavagai.nl/dist/packfox/</span>
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;"># Change this to match your profile</span>
<span style="color: #007800;">PROFILE</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">hostname</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #007800;">PFDIR</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">${HOME}</span>/.mozilla/firefox&quot;</span>
<span style="color: #666666; font-style: italic;"># Tar every .. seconds (regardless of changes)</span>
<span style="color: #007800;">TMOUT</span>=<span style="color: #ff0000;">&quot;1800&quot;</span>
<span style="color: #666666; font-style: italic;"># But not more often than every .. seconds (regardless of changes)</span>
<span style="color: #007800;">TMMIN</span>=<span style="color: #ff0000;">&quot;60&quot;</span>
<span style="color: #666666; font-style: italic;"># Regex for which files not to act on when they're changed.</span>
<span style="color: #666666; font-style: italic;"># Use inotifywait -m -e modify -e move -e create -e delete --exclude '(/Cache/)' -r your_profile_dir</span>
<span style="color: #666666; font-style: italic;"># and watch the output while browsing to determine which regex will be right for YOU.</span>
<span style="color: #007800;">IEXCL</span>=<span style="color: #ff0000;">&quot;(.sqlite-journal$)|(\-log.txt$)|(cookies.sqlite$)|(sessionstore\-[0-9].js$)|(/weave/)|(/Cache/)&quot;</span>
<span style="color: #666666; font-style: italic;"># Have you read everything and have you made the necessary adjustments? Then remove the line below ;-)</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;I should read the README and adjust the script variables before running this.&quot;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">2</span>
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;"># No user servicable parts below this line.</span>
<span style="color: #007800;">TGT</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">${PFDIR}</span>/<span style="color: #007800;">${PROFILE}</span>&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Global vars</span>
<span style="color: #007800;">INOTYPID</span>=<span style="color: #ff0000;">&quot;&quot;</span>
<span style="color: #007800;">SLEEPPID</span>=<span style="color: #ff0000;">&quot;&quot;</span>
<span style="color: #007800;">PACKLOCK</span>=<span style="color: #ff0000;">&quot;&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Cleanup function</span>
terminate<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>
  <span style="color: #666666; font-style: italic;"># If we are the daemon and we get SIGINTed/SIGTERMed, kill our children</span>
  <span style="color: #666666; font-style: italic;"># and if not already packing, do one last round of packing.</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$(basename ${0})</span>&quot;</span> == <span style="color: #ff0000;">&quot;packfox-daemon&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
  <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${INOTYPID}</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span> <span style="color: #c20cb9; font-weight: bold;">kill</span> <span style="color: #800000;">${INOTYPID}</span>; <span style="color: #000000; font-weight: bold;">fi</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${SLEEPPID}</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span> <span style="color: #c20cb9; font-weight: bold;">kill</span> <span style="color: #800000;">${SLEEPPID}</span>; <span style="color: #000000; font-weight: bold;">fi</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-z</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${PACKLOCK}</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;<span style="color: #000000; font-weight: bold;">then</span> packup; <span style="color: #000000; font-weight: bold;">fi</span>
    <span style="color: #7a0874; font-weight: bold;">exit</span>
  <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># For cleaning up </span>
<span style="color: #7a0874; font-weight: bold;">trap</span> terminate SIGINT SIGTERM
&nbsp;
<span style="color: #666666; font-style: italic;"># Suicide with goodbye note. If gxmessage is installed, use that.</span>
seppuku<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${1}</span>&quot;</span> <span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">2</span>
  <span style="color: #c20cb9; font-weight: bold;">which</span> gxmessage <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> gxmessage <span style="color: #660033;">-nofocus</span> <span style="color: #660033;">-title</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$(basename ${0})</span>&quot;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${1}</span>&quot;</span> <span style="color: #000000; font-weight: bold;">||</span> xmessage <span style="color: #ff0000;">&quot;<span style="color: #007800;">${1}</span>&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">2</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Checks and setup</span>
<span style="color: #7a0874; font-weight: bold;">test</span> <span style="color: #660033;">-d</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${PFDIR}</span>&quot;</span> <span style="color: #000000; font-weight: bold;">||</span> seppuku <span style="color: #ff0000;">&quot;Profile dir doesn't exist&quot;</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-z</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$(mount -t tmpfs | grep -F &quot;${TGT}&quot; )</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #c20cb9; font-weight: bold;">mount</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${PFDIR}</span>/<span style="color: #007800;">${PROFILE}</span>&quot;</span> <span style="color: #000000; font-weight: bold;">||</span> seppuku <span style="color: #ff0000;">&quot;Mounting of profile's tmpfs failed. Check /etc/fstab and the output of 'dmesg'.&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">test</span> <span style="color: #660033;">-f</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${TGT}</span>/.unpacked&quot;</span> <span style="color: #000000; font-weight: bold;">||</span> <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-xpf</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${PFDIR}</span>/<span style="color: #007800;">${PROFILE}</span>.packed.tar&quot;</span> <span style="color: #660033;">-C</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${PFDIR}</span>&quot;</span> \
<span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">touch</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${TGT}</span>/.unpacked&quot;</span> <span style="color: #000000; font-weight: bold;">||</span> seppuku <span style="color: #ff0000;">&quot;Error unpacking the profile tarball. You might want to use the backup tarball located in <span style="color: #007800;">${PFDIR}</span>.&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># This tars up the profile</span>
packup<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>
  <span style="color: #007800;">PACKLOCK</span>=<span style="color: #ff0000;">&quot;locked&quot;</span>  
  <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${PFDIR}</span>&quot;</span>
  <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">--exclude</span> <span style="color: #ff0000;">'.unpacked'</span> <span style="color: #660033;">-cpf</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${PFDIR}</span>/<span style="color: #007800;">${PROFILE}</span>.packed.tmp.tar&quot;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${PROFILE}</span>&quot;</span>
  <span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${PFDIR}</span>/<span style="color: #007800;">${PROFILE}</span>.packed.tar&quot;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${PFDIR}</span>/<span style="color: #007800;">${PROFILE}</span>.packed.tar.old&quot;</span>
  <span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${PFDIR}</span>/<span style="color: #007800;">${PROFILE}</span>.packed.tmp.tar&quot;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${PFDIR}</span>/<span style="color: #007800;">${PROFILE}</span>.packed.tar&quot;</span>
  <span style="color: #007800;">PACKLOCK</span>=<span style="color: #ff0000;">&quot;&quot;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># No daemon, just packing</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$(basename ${0})</span>&quot;</span> == <span style="color: #ff0000;">&quot;packfox&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span> packup; <span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># The daemon loop</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$(basename ${0})</span>&quot;</span> == <span style="color: #ff0000;">&quot;packfox-daemon&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #c20cb9; font-weight: bold;">which</span> inotifywait <span style="color: #000000; font-weight: bold;">&gt;/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">||</span> seppuku <span style="color: #ff0000;">&quot; You'll need the 'inotify-tools' package for this script. Get it at http://inotify-tools.sourceforge.net or from your distro's repos&quot;</span>.
  <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #c20cb9; font-weight: bold;">true</span>
    <span style="color: #000000; font-weight: bold;">do</span> inotifywait <span style="color: #660033;">-q</span> <span style="color: #660033;">-q</span> <span style="color: #660033;">-t</span> <span style="color: #800000;">${TMOUT}</span> <span style="color: #660033;">-e</span> modify <span style="color: #660033;">-e</span> move <span style="color: #660033;">-e</span> create <span style="color: #660033;">-e</span> delete <span style="color: #660033;">--exclude</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${IEXCL}</span>&quot;</span> \
    <span style="color: #660033;">-r</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${PFDIR}</span>/<span style="color: #007800;">${PROFILE}</span>&quot;</span> <span style="color: #000000; font-weight: bold;">&amp;</span>
    <span style="color: #007800;">INOTYPID</span>=<span style="color: #800000;">${!}</span>
    <span style="color: #7a0874; font-weight: bold;">wait</span> <span style="color: #800000;">${INOTYPID}</span>; <span style="color: #007800;">INOTIFYPID</span>=<span style="color: #ff0000;">&quot;&quot;</span>
&nbsp;
    packup
&nbsp;
    <span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #800000;">${TMMIN}</span> <span style="color: #000000; font-weight: bold;">&amp;</span>
    <span style="color: #007800;">SLEEPPID</span>=<span style="color: #800000;">${!}</span>
    <span style="color: #7a0874; font-weight: bold;">wait</span> <span style="color: #800000;">${SLEEPPID}</span>; <span style="color: #007800;">SLEEPPID</span>=<span style="color: #ff0000;">&quot;&quot;</span>
    <span style="color: #000000; font-weight: bold;">done</span>
  <span style="color: #7a0874; font-weight: bold;">exit</span>
<span style="color: #000000; font-weight: bold;">fi</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://smorgasbord.gavagai.nl/2009/05/how-i-fixed-my-firefox-hiccups/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>OmroepStreamDump</title>
		<link>http://smorgasbord.gavagai.nl/2009/04/omroepstreamdump-001/</link>
		<comments>http://smorgasbord.gavagai.nl/2009/04/omroepstreamdump-001/#comments</comments>
		<pubDate>Mon, 27 Apr 2009 21:28:18 +0000</pubDate>
		<dc:creator>Wicher</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[OmroepStreamDump]]></category>

		<guid isPermaLink="false">http://smorgasbord.gavagai.nl/?p=359</guid>
		<description><![CDATA[[Update @20090607: De code moet regelmatig bijgewerkt worden om veranderingen op player.omroep.nl te reflecteren. De laatste versie kun je altijd downloaden van de officiële pagina, daar kun je ook pingen (via een issue) als het script aangepast moet worden aan een nieuwe versie van player.omroep.nl (m.a.w., als het niet meer werkt).]
Ik heb een Greasemonkey-scriptje geschreven [...]]]></description>
			<content:encoded><![CDATA[<p><span style="font-size:x-small; color:orange;">[Update @20090607: De code moet regelmatig bijgewerkt worden om veranderingen op player.omroep.nl te reflecteren. De laatste versie kun je altijd downloaden van <a href="http://userscripts.org/scripts/show/47693">de officiële pagina</a>, daar kun je ook pingen (via een issue) als het script aangepast moet worden aan een nieuwe versie van player.omroep.nl (m.a.w., als het niet meer werkt).]</span></p>
<p>Ik heb een <a href="https://addons.mozilla.org/en-US/firefox/addon/748">Greasemonkey</a>-scriptje geschreven dat op player.omroep.nl een dialoogje geeft waarvan je de inhoud direct in een shell (Bash-shell, wellicht ook de standaard Bourne-shell) kan pasten. Je krijgt iets dergelijks voorgeschoteld:<br />
<code><br />
export TIEFDIR="${PWD}"; export TEMPDIR=$(mktemp -d); cd ${TEMPDIR} &#038;&#038; mplayer -dumpstream -user-agent 'Windows-Media-Player/11.0.6001.7000' 'http://cgi.omroep.nl/legacy/player?/ceres/vara/rest/2009/VARA_101192917/bb.20090424.asf' &#038;&#038; mv stream.dump ${TIEFDIR}/'De Wereld Draait Door - 25-04-2009.wmv' &#038;&#038; cd - &#038;&#038; rmdir ${TEMPDIR}<br />
</code><br />
Dat maakt het downloaden van videomateriaal van uitzendinggemist.nl een stukje makkelijker. Natuurlijk moet je wel Firefox + Greasemonkey-extensie, een shell en MPlayer hebben, maar wie heeft dat nou niet ;-)<br />
<a href="http://userscripts.org/scripts/review/47693">Hier kun je de source lezen</a> en als je <a href="https://addons.mozilla.org/en-US/firefox/addon/748">Greasemonkey</a> hebt geïnstalleerd kun je <a href="http://userscripts.org/scripts/show/47693">hier op &#8220;Install&#8221; klikken</a>.<br />
Surf vervolgens naar <a href="http://www.uitzendinggemist.nl/">uitzendinggemist.nl</a> en fair-use er op los.<br />
En hier natuurlijk het leukste stuk van de source. Ik schrijf haast nooit Javascript dus opmerkingen/aanvullingen zijn zeer welkom.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> playert <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;player&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// Listener for node insertions (generated by omroep.nl's javascript)</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>playert<span style="color: #009900;">&#41;</span> playert.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;DOMNodeInserted&quot;</span><span style="color: #339933;">,</span> genAlert<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> genAlert<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> playert <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;player&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> embedelement <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'MediaPlayer'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>embedelement<span style="color: #009900;">&#41;</span>
  <span style="color: #006600; font-style: italic;">//The embed element got inserted - now we have all required parameters for dumping</span>
    <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> pastetext<span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> source <span style="color: #339933;">=</span> embedelement.<span style="color: #660066;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;src&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;'&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//These URL's shouldn't have &quot;'&quot; in them but we escape them just to be sure.</span>
    <span style="color: #006600; font-style: italic;">//Create a copy of the title &amp; date info so we can use DOM functions to separate the two</span>
    <span style="color: #003366; font-weight: bold;">var</span> worktitle <span style="color: #339933;">=</span> document.<span style="color: #660066;">importNode</span><span style="color: #009900;">&#40;</span>playert.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;h3&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> worktitledatespan <span style="color: #339933;">=</span> worktitle.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;span&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> datum <span style="color: #339933;">=</span> worktitledatespan.<span style="color: #660066;">innerHTML</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;'&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//Escape ' to make shell-safe</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>datum<span style="color: #009900;">&#41;</span> datum <span style="color: #339933;">=</span> <span style="color: #3366CC;">'nodate'</span><span style="color: #339933;">;</span>
    worktitle.<span style="color: #660066;">removeChild</span><span style="color: #009900;">&#40;</span>worktitledatespan<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> titel <span style="color: #339933;">=</span> worktitle.<span style="color: #660066;">innerHTML</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;'&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//Escape ' to make shell-safe</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>titel <span style="color: #339933;">&amp;&amp;</span> source<span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
      playert.<span style="color: #660066;">removeEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;DOMNodeInserted&quot;</span><span style="color: #339933;">,</span> genAlert<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      pastetext <span style="color: #339933;">=</span> <span style="color: #3366CC;">'export TIEFDIR=&quot;${PWD}&quot;; export TEMPDIR=$(mktemp -d); cd ${TEMPDIR} &amp;&amp; '</span>
      pastetext<span style="color: #339933;">+=</span> <span style="color: #3366CC;">&quot;mplayer -dumpstream -user-agent 'Windows-Media-Player/11.0.6001.7000' '&quot;</span><span style="color: #339933;">+</span>source<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;' &amp;&amp; &quot;</span><span style="color: #339933;">;</span>
      pastetext<span style="color: #339933;">+=</span> <span style="color: #3366CC;">'mv stream.dump ${TIEFDIR}/<span style="color: #000099; font-weight: bold;">\'</span>'</span><span style="color: #339933;">+</span>titel<span style="color: #339933;">+</span><span style="color: #3366CC;">' - '</span><span style="color: #339933;">+</span>datum<span style="color: #339933;">+</span><span style="color: #3366CC;">'.wmv<span style="color: #000099; font-weight: bold;">\'</span> &amp;&amp; cd - &amp;&amp; rmdir ${TEMPDIR}'</span><span style="color: #339933;">;</span>
      <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>pastetext<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://smorgasbord.gavagai.nl/2009/04/omroepstreamdump-001/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://cgi.omroep.nl/legacy/player?/ceres/vara/rest/2009/VARA_101192917/bb.20090424.asf" length="168" type="video/x-ms-wmv" />
		</item>
	</channel>
</rss>
