Hello!

Hi, I’m Seb :) I’m a web and software developer (and now a game programmer) from Brighton. You can read a bit more about me and what I do in the “about me” page.

Anyway, feel free to have a read of some of my (relatively infrequent) posts – mostly geeky musings, but you never know…

Free cross-platform dynamic DNS with wildcards

This will roughly explain how to get wildcard DNS working for free (any-words-here.yoursubdomain.yourdomain.com) on a connection which gets assigned dynamic IPs.

Goal

To have subdomain.example.com always up to date with your home IP, and be able to request subdomains (like someword.subdomain.example.com) resolve to the same IP. These are useful if you want to use multiple virtual hosts in apache hosted from a machine on your home network, accessible to the outside world using a fixed hostname.

Requirements

Your own domain (example.com)
Your have a server on the outside world, running PHP (for this example anyway)
A way of requesting a remote web page automatically

Introduction

Our previous internet provider gave me a static IP (2 actually!) on a standard ADSL broadband connection. I also own a domain (for this, I’ll say example.com) – so I setup a zone specifically for my house (subdomain.example.com) – this was nice and easy using any DNS provider as it never changed. I also created NS records pointing subdomain.example.com at my home IP, so that I could create requests for blah.subdomain.example.com

We’ve recently moved house. We’re now with Virgin, and they don’t provide static IPs – so I thought I could use something like dyndns.com or no-ip.com (which runs a small daemon which updates your IP to point to something like yourname.no-ip.com). However, these don’t support recursive wildcard DNS requests without paying for a subscription (i.e. something.yourname.no-ip.com) so I’d only be able to have a single domain name for my home, which makes using Virtual Hosts in apache not really possible…

Solution!

Find a free DNS host which support an API, and supports wildcard DNS records for a fixed zone. I’ve done the hard work for you, and found Zerigo

Setup a free account on Zerigo and creat a new zone for subdomain.example.com – my primary DNS is hosted elsewhere for example.com, so I just created an NS record for subdomain.example.com on my main DNS to point at a.ns.zerigo.com. You could probably host both on Zerigo if you wanted.

Then, create 2 hosts on this new zone – “” (empty hostname) and “*” (the wildcard host) – give these any old IP; they’ll be updated in a minute!

So, now if you do something like (from your machine):

nslookup banana.subdomain.example.com a.ns.zerigo.com

you should get the IP you’ve given both these hosts. If you do, everything’s working fine.

Now, the best bit about Zerigo is that it has a rather nifty REST api for updating DNS zones and hosts, and they provide a PHP client library along with some fairly simple examples. So I created a small PHP script (using their provided API – thank you!) which just updates your zone to point to the client IP requesting the script, provided it’s not the same as it used to be (with very simple authentication):

The PHP script to do it for you

<?php
$WILDCARD_ZONE="subdomain.example.com"

// rather nasty "authentication" - does the job though... (and it's fine provided the logs on your server are private!)
// just check the MD5 of a GET param is the same as one we're expecting
if (isset($_GET["key"]) && "abc123abc123abc123abc123abc123ab"===md5($_GET["key"])) {
        // get the last IP we updated it to - if it's not the same, write the new one into the file
        // for use next time.
        $oldIP=file_get_contents("oldIP");
        $remoteIP=$_SERVER['REMOTE_ADDR'];
        if ($oldIP != $remoteIP) {
                file_put_contents("oldIP", $remoteIP);
        }
        else {
                // IP hasn't changed, so nothing to do - save superfluous calls to the Zerigo API
                die("No change: $oldIP, $remoteIP");
        }
        // include the zerigo API
        require_once("zerigo-dns_api_php/zerigo_ns.php");

        ZerigoAPI::$api_user = 'you@zerigouser.com';
        ZerigoAPI::$api_key  = 'abc123abc123abc123abc123abc123ab';

        // get all zones on your account (up to 50 of them anyway)
        $zones = NSZone::find_all(array('per_page'=>50, 'page'=>1));

        // Now get the zone we're trying to update
        $homeZone = null;
        foreach ($zones as $zone) {
                if ($zone->domain == $WILDCARD_ZONE) {
                        $homeZone = $zone;
                }
        }
        // found the zone we want?
        if ($homeZone) {
                // update all the hosts that match what we're fudging (i.e. the empty host and the wildcard)
                foreach ($homeZone->hosts as $host) {
                        if ($host->hostname == "" || $host->hostname == "*") {
                                // bit of debug to go in my cron logs
                                echo "Updating " . $host->hostname . "." . $homeZone->domain;
                                echo " from " . $host->data . " to " . $remoteIP . "<br/>\n";
                                $host->data = $remoteIP;
                                // call the Zerigo API to update the remote IP for this host
                                if ($host->save()) {
                                        echo "Updated.<br/>\n<br/>\n";
                                }
                                else echo "Failed.<br/>\n<br/>\n";
                        }
                }
        }

        exit();
}
header("Status: 404 Not Found");

(You’ll also need the Zerigo PHP api stuff too, which can be found here: Zerigo PHP Libs)

Getting it to automatically update

I just set a box on my home network to fetch this every now and again – at the moment, I’ve just got a cron job that does it hourly and it keeps my remote IP up to date  (with a low (1 hour) TTL) on Zerigo’s DNS servers. I presume there’s a way in Windows to create a scheduled job that does it too, so this method is pretty cross-platform (and because PHP can run on pretty much anything, it works on all client/server setups).

Hooray!

VIM quick fix (for arrow keys in insert mode)

When using VIM in insert mode, when I try and move around with the arrow keys, it inserts A, B, C and D instead. Not completely sure why, but typing:

:set nocompatible

seems to fix the problem. You can make this permanent by sticking

set nocompatible

in .vimrc in your home directory. Anyone know why?!

Adventures in WordPress

I’ve spent a bit of time building wordpress themes for clients, and usually they all need fairly simple page layouts, probably having commenting turned off, and nothing particularly complicated; occasionally there’ll be the odd nested page, and some funky javascript, but all fairly basic stuff (see lets-skinnydip for my last WordPress site).

For this site, as it’s my own site, I feel a bit more relaxed to play around and (possibly) break things. So my theme here is built from scratch, with empty files everywhere, and I’ve gradually been learning more and more from the WordPress codex filling in the blanks. Yesterday I worked out how to get comments working (I know it sounds obvious, but like I say, I started from scratch with the theme rather than from someone else’s and changing what it looked like) and got the navigation buttons under posts working too.

I’ve just discovered custom page templates, and how to create pages in WordPress that reference these – I’m really starting to see the power WordPress has!

The only other problem I had is that I used a subfolder to house WordPress on my server, and don’t really fancy moving it out (I like having it all self-contained) but wanted to reference a particular wordpress page as my “home” page (i.e. actually have the content served from http://5eb.me/ rather than redirecting to http://5eb.me/wp/ and displaying a particular page) – turns out that this is possible too, but doing something along the lines of:

<?php include("wp/wp-load.php"); // include the wordpress engine?>
<?php get_header(); // header file ?>
<?php get_sidebar("barebones"); // custom sidebar template file - hides the menu etc ?>
<div id="content" style="margin-top: 60px;">
	<?php $page=get_page_by_title("Seb Maynard"); // get the page we'd like to display ?>
	<h2><?php echo $page->post_title; ?></h2>
	<div>
		<?php
			// echo the content, as if we were inside the wordpress loop
			echo apply_filters("the_content", $page->post_content);
		?>
	</div>
</div>
<?php get_footer(); ?>

After working all this out, I decided it was actually better to have a normal wordpress page, with a different “subtheme”, and just redirect to that… So I’m not actually using any of the above for this site after all! However, I’m sure I (or someone else) may find it useful in the future…

It’s quite incredible that a piece of so well-maintained and powerful software is free.

Ubuntu tips and tricks

Here’s a small selection of tips and tricks I’ve collected in Ubuntu linux – and probably might help in other Linuses (is that the plural of Linux?!) too.


X11 forwarding problems

When setting up an SSH connection to forward X11 applications back to your client, you can connect with:
ssh -X user@host

This will use standard X11 forwarding; you can also
ssh -Y user@host

to get authenticated forwarding. Once this is done, if everything is working, doing:
echo $DISPLAY

on the host should print something like :0.0

How to fix it if it doesn’t work…

Sometimes, $DISPLAY doesn’t get set. There are a couple of reasons.

  1. Make sure /etc/ssh/sshd_config contains X11Forwarding yes
  2. Make sure you haven’t got any scripts overwriting the $DISPLAY variable
  3. (this is the one that always catches me out) Make sure you have xauth package installed on the target host!

Back to top


Window button order in Ubuntu

Or otherwise know as How to put the window close/maximize/minimize buttons back on the right. An infuriating change if you ask me…

In gconf-editor find
/apps/metacity/general/button_layout
The : is where the large space will be, and the buttons are quite self-explanatory :)

Back to top


Customizable menu shortcuts in Ubuntu

How to bind a keyboard shortcut to almost anything in almost any Gnome app

Old versions of Ubuntu

In older versions of Ubuntu, on the main menu, under preferences, on the Appearance dialog, there was an “Interface” tab with various tweaks and things… one of which was customisable keyboard shortcuts for menu items. Just tick the appropriate one, and away you go…

Ubuntu 10 and on

In Ubuntu 10, the team removed the “interface” tab from the Gnome Appearance preferences dialog, so the option to enable “custom keyboard shortcuts” (which let you hover over any menu item, press a new keyboard shortcut and bind it immediately) was hidden away…

You can turn this behaviour on by opening up gconf-editor (either in run (alt+f2) or in a command line) and finding and ticking the key: /desktop/gnome/interface/can_change_accels

Back to top


How to fix “Setting locale failed” in Ubuntu

For when you see errors like…
perl: warning: Falling back to the standard locale ("C").
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings

Just reinstall the packages by:
apt-get install --reinstall language-pack-en
(Stolen shamelessly from http://bit.ly/nlug9c)

Back to top

Let Firefox read local files during development

Use with caution!!

By default, Firefox doesn’t allow resources on the web to access local files. However, when debugging a web application, this is sometimes useful (for example, if you have a remote web system that takes a long time to rebuild/update, and want to make a lot of small trial-and-error changes to javascript/css). This is potentially very dangerous, so only enable whilst you’re debugging, and turn off when you’re done :)

Just visit about:config in the address bar, and search for
security.fileuri.strict_origin_policy

set it to false, restart Firefox and you’re done :)

p.s. for older Firefox versions, look for security.checkloaduri instead and change that

Random dev doodling – JavaScript 3D cube

Being a developer, I spend quite a bit of time playing around with programming bits and bobs. A little while ago, I had a relatively old Nokia that supported J2ME (without floating point) so I decided to try and write a simple 3d renderer for it – having no FPU (floating point unit) I had to write an integer math library, and sine/cosine lookup tables against the math library. This was all good stuff, and reminded me some of the maths I learnt at uni, and some good optimization tricks.

A couple of months after that, bored one afternoon, I decided to write something similar in JavaScript – modern browsers have decent JavaScript engines with good floating point support, so it was a lot easier… after proving that it could be done, I lost interest, but here’s where it go to:

Just a (very) simple 3D wireframe cube renderer, but done using a <canvas> tag, a fairly standard 3D matrix, and some javascript.

You can see it in action here – not sure if it works in every browser (more than likely doesn’t) but it definitely works in FireFox and Chrome.

Quick howto: init.d script for glassfish 3

Glassfish is a reference implentation of the Java EE 6 application server – it’s fairly easy to download and get running, but I wanted to get it running automatically on system startup on my linux server (which at the moment is running on coLinux – probably post about separately that at some point).

I’m assuming you’ve got the glassfish .zip, and extracted it to /opt/glassfishv3… and you’re running a debian-based OS (like Debian Lenny, or Ubuntu). These commands assume you’re either running as root, or prepend all of them with sudo…

Create a file:

/etc/init.d/glassfish.sh

and put the following in it:

GLASSFISH_HOME=${GLASSFISH_HOME:-"/opt/glassfishv3/glassfish"}
case "$1" in
start)
    $GLASSFISH_HOME/bin/asadmin start-domain | tee -a /var/log/glassfish.log
    ;;
stop)
    $GLASSFISH_HOME/bin/asadmin stop-domain | tee -a /var/log/glassfish.log
    ;;
restart)
    $GLASSFISH_HOME/bin/asadmin restart-domain | tee -a /var/log/glassfish.log
    ;;
*)
echo "usage: $0 (start|stop|restart|help)"
esac

Make sure it’s executable:

chmod +x /etc/init.d/glassfish.sh

Make it start by default on system boot:
update-rc.d glassfish.sh defaults

Then start your glassfish server :) if it doesn’t appear to be working, you can tail the logs:

tail -F /var/log/glassfish.log

Done! Easy as pie :)

(thanks to http://bit.ly/qZ4fTj)

Continuous Android Sync (optionally with DropBox)

I like DropBox. I like Android. I just wish there was a way to continously sync my dropbox folders with my phone so they’re always up to date… here’s a way I use to get round this limitation


Start the bodge.

Step 1: DropBox remote.

First, setup DropBox on a computer somewhere running an ssh server, so you can copy files from it over ssh (with rsync), and make sure it’s setup with private key encryption with an agent so you don’t need to enter passphrases/passwords every time

Step 2: RSync from remote to phone

Then, get rsync4android (market link)

That lets you setup rsync jobs from a remote server to your phone (and vice versa… I use it for this, but also for backing up my photos to my PC automatically every night)

Then create an rsync job to download a particular folder from your remote DropBox on your PC into the DropBox folder on your sd card.

Step 3: scheduled automatic rsync jobs

Get tasker (which lets you schedule jobs to happen on a large number of triggers, including time) (market link)

Create a tasker job to fire off your new rsync job to keep your dropbox folder up to date…

Step 4: Enjoy :)

Like I say, it’s a bit around the houses, but seems to do the job :)

Digitoneurolinguistic Hacking

I read the word “Digitoneurolinguistic” in an XKCD comic, about 3 hours after it appeared on Google Reader:

Trochee Fixation

I’ve coined this dNLP because it looks fancy. I might try and drop it into a conversation somehow…

Anyway, it sounded like an intriguing concept; after a quick Google, I found nothing – I’ve done a bit of freelance work for a Hypnotherapy company / practice / clinic (what do you call it?!) in the past, and have read a lot of their site content about neurolinguistic programming (not the digito- kind though) and started wondering what the digital equivalent might be like.

Well, now I’ve finished wondering, and am no better off than I was before. Which means that I’m no better off finding out what what digitoneurolinguistic hacking is either.

HOWEVER!!! On realising there are no Google results for “digitoneurolinguistic”, I decided I’d try and be the first (maybe) – so if you found this page after reading the XKCD comic, drop me a line to say hi :) – Hooray for Google’s indexes!

p.s. for added fun, press (on your keyboard):

↑ ↑ ↓ ↓ ← → ← → B A

(from here) – this may not work yet in my wordpress…

* as a bit of an (quite a lot of an) internet geek, I really, really like that it is now socially acceptable to use a website’s name as a verb :)