Sunday 31 July 2011

Calibre, and Kindle, and books on the Calibre Webserver

Sad but true - I spent my friday night fiddling with PCs.

I've got a Kindle - an excellent reader - and a bunch of ebooks I've downloaded from Gutenberg. I already use Calibre to load the ebooks onto the kindle, but I liked the idea of accessing the library over my wifi network. Calibre has a built-in web server, on port 8080. (Also delete the calibre web username/password, and convert all your books to MOBI or LIT formats).

The Kindle browser is flawed though - it only connects over port 80 (HTTP) or 443 (S) :-(

I already had apache running on my main server (Giles) on port 80, so time for some tweaking:

I set up an alias for my server (in /etc/hosts/) :
127.0.0.1 localhost
127.0.1.1 giles

192.168.0.11 calibre.giles.local

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
192.168.0.11 is giles's static IP address. calibre.giles.local is my new virtual server name.

Added an apache conf file to use a virtual host (/etc/apache2/conf.d/calibre.conf):

ProxyPreserveHost On
ProxyPass / http://192.168.0.11:8080/
ProxyPassReverse / http://192.168.0.11:8080/
ServerName calibre.giles.local

This tells apache to route any traffic from calibre.giles.local to the calibre webserver (port 8080).

Now, using a browser on giles, I can see the calibre server by visiting http://calibre.giles.local

This works great, but only when I'm on giles - from any other machine, I would have to edit the 'hosts' file to add the DNS entry - and on the kindle that's not possible.

OK, more tweaking:

Installed dnsmasq (sudo apt-get install dnsmasq). This is a local DNS caching server - it caches the DNS records so it speeds up your browsing (fractionally) but more importantly, it also serves the local /etc/hosts file - so my new calibre.giles.local adress will be served to anyone using this DNS server.
Changed the /etc/resolv.conf:
nameserver 192.168.0.11
i.e. - get giles to use it's own dns server.

Changed /etc/dnsmasq.conf to add :
resolv-file=/etc/resolv2.conf
This avoids dnsmasq using itself to look up entries!

server=194.168.4.100
server=194.168.8.100
These are the two Virgin Media cable DNS servers (my ISP): I could also have used any public DNS servers.

Restarting dnsmasq (service dnsmasq restart) to pick up the changes, I can test this :
dig google.com 
gives me 9ms the first time, the 0ms the second - the cache is working.
dig calibre.giles.local 
shows 0ms - it is in the hosts file.

So, to make my entire network pick up the new DNS server: Login to my Apple Airport router, and go to IP (Internet) settings. Change the DNS list to have 192.168.0.11 first in the list.

All my other machines on the network use the airport router as the DHCP server and gateway/DNS proxy.

So:
Kindle connects to the wireless network - gets the DHCP address and a DNS server of 192.168.0.1 (the AirPort router).
Go to menu - experimental - web browser and type in "calibre.giles.local".
Kindle asks the airport router DNS for the address.
Airport router DNS asks Giles DNSmasq for the address. Found in local hosts file.

Kindle now displays the calibre web interface. You can search for books/authors, download and read any book (click on the MOBI file) :-)

No comments: