iTunes Home Sharing over a WAN Connection

This is a little rough, but I wanted to get it posted before I’d forgotten how I got it to work.

There’s a great tool available, called ShareTool, which makes Bonjour-based services on your home network available to you wherever you go.  I use it all the time for iTunes streaming, SSH, and file sharing, especially when I’m on networks that aren’t entirely friendly to Back To My Mac.

In case you’re interested, here’s a link to the tool (it is now freeware): http://www.bainsware.com/

The nice thing about ShareTool, is that it makes all of these services available only to your machine, so you don’t have to worry about inadvertently sharing these services to everybody on your LAN.  This is nice in the general case, but, when I travel, I carry an Apple TV, which I would like to somehow link back to my home iTunes collection, so I can stream movies.  Unfortunately, ShareTool doesn’t fit the bill for this.

So, I set to trying to figure out how ShareTool does its magic.  Everything seemed to work, except iTunes Home Sharing.  I won’t get into the details of service discovery and ssh port forwarding here, but if you Google around, there are plenty of places where you can find it.

Anyway, here’s how you get iTunes Home Sharing to forward across your WAN connection.

First, establish an ssh connection back to your iTunes server (or some server on the same subnet).  We’ll worry about port forwarding later.  Type the following command:

dns-sd -Z _home-sharing local

This will give you a bunch of output, for each home-shared library on your LAN.  Find the set of three records associated with the library you wish to share.

The first thing we need to establish, is the host and port number on which home sharing is listening.  This is typically 3689, but can also be some other port, if that port wasn’t available when you started iTunes.  Look at the SRV record.  For example:

Home\032Library._home-sharing._tcp              SRV     0 0 3689 portnoy.local.

In this case, home sharing is listening on portnoy.local, port 3689.

Open up another window, and forward a port on your local machine to port 3689 on the remote machine.

ssh -Cgv -L9999:portnoy.local:3689

Now you’ve got the port forwarded.  The next thing to do, is advertise the service on your local network.

Go back to the window that has the output from the dns-sd -Z command.  Find the hG field in the TXT record.  It should look something like this:

hG=00000000-1234-5678-9abc-0123456789ab

Now, on your local machine, construct a command like this:

dns-sd -R "Remote Library" _home-sharing._tcp.,_<1> local 9999 <2>

Where <1> is the value from the hG field (note the leading comma and underscore), and <2> is everything in the TXT record from the dns-sd -Z command.  The magic is in the ,_<1> section, which advertises a (required) subtype for the home share.  That, incidentally, is what took me hours to figure out.  Your final command should look something like this:

dns-sd -R "Remote Home Library" _home-sharing._tcp.,_00000000-1234-5678-9abc-0123456789ab local 9999 "txtvers=1" "dmv=131081" "hQ=3163" "MID=0x89A3716DA6EBA0AE" "Version=196619" "iTSh Version=196618" "PrVs=65538" "Machine Name=Home Library" "OSsi=0x1F6" "hG=00000000-1234-5678-9abc-0123456789ab" "Database ID=CAB178D4987ABCDE" "Machine ID=A59AAB979EB4" "hC=12345678-1234-5678-9abc-bf8319cb8743"

If everything went well, you should see activity in your SSH window, and your home library should be available to all of the devices on your LAN!