Saturday, June 17, 2017

implementing a simple language switch using rocket in rust

Let's look how easy it is to implement a simple cookie based language switch in the rocket web framework for the rust programming language. Defining the language type:

In this case there will be support for Dutch and English. PartialEq is derived to be able to compare Lang items with ==.

The Default trait is implemented to define the default language:

The FromStr trait is implemented to allow creating a Lang item from a string.

The Into<&'static str> trait is added to allow the conversion in the other direction.

Finally the FromRequest trait is implemented to allow extracting the "lang" cookie from the request.

It always succeeds and falls back to the default when no cookie or an unknown language is is found. How to use the Lang constraint on a request:

And the language switch page:

And as a cherry on the pie, let's have the language switch page automatically redirect to the referrer. First let's implement a FromRequest trait for our own Referer type:

When it finds a Referer header it uses the content, else the request is forwarded to the next handler. This means that if the request has no Referer header it is not handled, and a 404 will be returned.
Finally let's update the language switch request handler:

Pretty elegant. A recap with all code combined and adding the missing glue:

Sunday, January 29, 2017

making a HP Color LaserJet printer wireless with a Raspberry Pi 3

My old HP Color LaserJet 2820 is still working great, however it only supports being connected via ethernet or USB. As a workaround I've been using "Power Line Communication" to bridge directly from my router to the printer over the electrical wires.
Recently I've been revamping the home network and wanted to get rid of these last power line connections. Ideally I'd want the printer to join the wireless network.

The arrival of the Raspberry Pi 3 with builtin WiFi seems like a good opportunity to implement this.
The raspberry Pi is connected with it's Ethernet port to the printer. The WiFi is connected to the local LAN where I configured the router to give it a fixed IP address.

Approach 1: Cups printer server

I install the cupsys printing system on the Raspberry Pi, and configure it to properly support the printer (using HPLIP). Besides that, I install a DHCP server to serve the printer an IP address on it's own private network: The cable between Raspberry Pi and printer.
I change the configuration of the printer in Cups on the Raspberry Pi to make the printer shared on the LAN.
Now all other computers can just use that shared printer to print.
This works really good for printing, however it doesn't work at all for scanning.
When scanning the scanning software (SANE) seems to want to connect directly to the printer and this of course isn't possible when the printer is on a different network and there is no routing.

Approach 2: Routing

I updated the Raspberry Pi to do routing, by enabling IP forwarding.
(uncomment the net.ipv4.ip_forward=1 line in /etc/sysctl.conf )
I also configure my router with an extra route to have it know about the network that is now reachable via the Raspberry Pi.
Now the printer can be directly communicated with. This allows me to configure the printer directly again on my Linux laptop.
Printing still works fine. However scanning still doesn't work.
When I tell the scanner software the IP address of the printer explicitly, it sometimes works, but most of the time it doesn't work at all.
My guess is that the scanner software relies on some communication that doesn't work outside it's own network.

Approach 3: Proxy ARP

To learn more about the technicalities of proxy ARP, read here on Wikipedia, and here on In summary it is a trick that makes it seem to the rest of the network that the Raspberry Pi and the printer are the same.
In order for this to work I want the printer to have an address in the same network as the local net.
First remove the DHCP server installed for the previous approaches, then install a DHCP proxy. This is easily installed (apt-get install dhcp-helper) and configured (DHCPHELPER_OPTS="-b wlan0" in /etc/default/dhcp-helper).
There are multiple ways to enable Proxy ARP. The easiest is to install (apt-get install parprouted) and configure it.
My /etc/network/interfaces now looks like this:

auto lo
iface lo inet loopback
auto eth0
allow-hotplug eth0
iface eth0 inet manual

allow-hotplug wlan0
iface wlan0 inet dhcp
  wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
  post-up /usr/sbin/parprouted eth0 wlan0
  post-down /usr/bin/killall /usr/sbin/parprouted
  post-up /sbin/ip addr add $(/sbin/ip addr show wlan0 | perl -wne 'm|^\s+inet (.*)/| && print $1')/32 dev eth0
  post-down /sbin/ifdown eth0

With proxy ARP in place the printer still works great, and finally the scanner also works in a stable way.

Alternative approaches

I could try hooking the printer to the Raspberry Pi via USB. As I've never used the printer in this way, I've not attempted this.

Tuesday, January 10, 2017

stable wifi on the shuttle xs36v with Debian

The Wifi connection of this machine is very slow, especially noticeable when logging in to the machine via ssh.
The solution is to follow the notes on , copied here:

Devices supported by this driver may suffer from intermittent connection loss. If so, it can be made more reliable by disabling power management via module options ipsand fwlps. These can be supplied by creating a new file:

  • options rtl8192ce ips=0 fwlps=0

Thursday, January 5, 2017

Dirt Rally Timing Log

In order to keep track of my skill evolution playing the excellent rally sim game "Dirt Rally" I've set up a google sheet.

This google sheet could be interesting for other people as well so I'm sharing it here. Note that this one has some example content only not my actual full content.

To use it make a copy of it on your own drive.

Have fun driving!