Having an home server is great. It can be used as a NAS, as a print server, you can have some virtual machines for whatever purpose you may need and you can host some services there. I’m using it for all of the mentioned reasons, including software development and testing, with a Gitlab and Gitlab-CI services running on premises. I’m also hosting some web applications I developed for my own usage. I needed some of this services to be accessed externally, so I have NAT (Network Address Translation) configured on my Fritz!Box to point some ports to the server.
And it has been great so far! Except, I don’t need it to be running and spending electricity all the time. Additionally, the sound of the fans doesn’t please much the person I’m living with: my girlfriend.
So, how did I make it better?
First of all, I needed a way to turn on my server on demand. For that purpose, many motherboards have this feature called WOL (Wake On Lan). What it does is turning on the device, whenever a magic packet arrives from the network. To send this magic packet to the server, there are some tools, like etherwake, Wakeonlan or powerwake for Linux (there are some tools for Windows as well). Fritz!Box also allows to send this magic packet through the administration page of the router.
The problem is, I need the server to be accessed externally. I didn’t want have another NAT configuration pointing at the server for this. Fritz!Box has a solution for this. There is a configuration which sends a magic packet to the machine, everytime a connection is attempted from the internet to this specific device (through the NAT). This is cool. But…
There is life on the internet
There are crawlers and HTTP scanner applications running on the internet. Since I have NAT configured on HTTP (port 80) and HTTPS (port 443) to the server, everytime a connection is attempted on this ports, my server would turn on. Unfortunately, this didn’t happen only a few times.
So, what could I do? Additionally to the HTTP and HTTPS ports, I also used the SSH (port 21) to access my server, so that I could access my files through an SSH tunnel or turn on a virtual machine when needed. I could disable the NAT for the HTTP and HTTPS when the server is turned off and turn it on again when I needed. But, this is a manual process:
1. Turn on server (could be done remotelly)
2. Open the Fritz!Box administration page
3. Search for the NAT settings
4. Enable the NAT rules
5. Use the server
6. Open the Fritz!Box administration page
7. Search for the NAT settings
8. Disable the NAT rules
9. Turn off server
Furthermore, I can’t access the Fritz!Box from outside the network, unless I would configure the SSH tunnel with port forwarding to the router address.
Damn! I don’t really like it.
Breaking the stuff
I wanted a simple solution for having my server up and running without much work and without exposing it too much. And I’ve got an idea!
What if I could just turn on the server by attempting to connect externally via SSH, and then the server would do the rest with startup and shutdown scripts? What do I need? A way to configure the Fritz!Box from the server with a script.
So, I did some research. I could find an API for the Fritz!Box, but for home automation and call tracking. Couldn’t find anything for remote configuration. Is it a dead-end? Not for me.
Using my experience as a software engineer, I decided to dig into the Fritz!Box administration page and understand what would I need to communicate with it. Then, I decided to grab Node.js and build something with it. The result is the open source project node-fritzbox.
With this command-line tool, it is now possible to communicate to the Fritz!Box with command line or automate it with scripts. So far, I developed the remote administration of the Fritz!Box to:
* List, enable or disable port-forwarding rules
* List active and inactive devices
* Send WOL packet to device
Since this is an open source project, you can develop it further.
The automation with scripts on startup or shutdown is system dependent. For Debian users, here is something that can be used: https://www.debian-administration.org/article/212/An_introduction_to_run-levels
I’m really happy with what I could accomplish. Now I just need to turn on or off the server and it will configure the router itself on startup and shutdown. No stress and no time lost. It just works.
What is your experience? What would you do differently?