A Super Simple Reverse-Proxy Script Using Cloudflare API
For nerd reasons, I've been researching strategies for setting up a reverse-proxy. A reverse-proxy, with a few other steps, allows people to reach a computer behind a router just by visiting a URL. So, if you wanted to host a public website from a computer at your house, you'd likely need a reverse-proxy.
Reverse-proxy software grabs the “public” IP address of your computer, then uses a special service (called a “Dynamic DNS service”) to connect the domain name (URL) for your site to your IP address. That way, when someone visits “awesomewebsite.com”, they'll get sent to your computer hosting the site*. Many of the directions I've found recommended a program called ddclient, which does this automatically for a variety of services. Unfortunately, as of July 4th, 2023, this project is no longer maintained.
However, one of the references I found also noted that Cloudflare, whose systems protect websites from a range of different threats, can basically act as a dynamic DNS service through their API. I was intrigued, so I put together a super simple reverse-proxy script using the Cloudflare API – and it actually worked!
The full script and instructions can be found here. This post provides a quick walkthrough of how it works.
There's three key steps.
The first step is “getting your IP address”. This is made easy due to Ipify, which has a simple (and free!) API for returning your IP address. A quick call to https://api.ipify.org returns a response including your IP.
The second step is getting the record information for your domain from your Cloudflare account. (This assumes that you already set up an account with Cloudflare – they have a free tier! – and have added the domain name for your site to your Cloudflare account.) In order to update the DNS records for your site (which connect your domain name and IP), you need to get the object ID's associated with those records. One call to Cloudflare's DNS API using the zone ID for your domain name returns all the records associated with that domain. A quick parsing of the response lets you find the id's of the records that could be updated, and verify if an update is actually needed.
The third step (if necessary) is updating the record information for your domain with Cloudflare. Using the id's from the previous step, you call each record that redirects that IP address type (usually two “A”-type records – one for the vanilla domain, and one starting with “www”), and update the IP address.
And that's it!
Deploying this system is relatively straightforward. You need to copy the repository to your computer, rename “config.example” to “config.yaml”, fill in the details for your specific site and Cloudflare account in “config.yaml”, then run the script periodically (like with a cron job in Linux or a scheduled task in Windows). Overall, though, the amount of set-up work is very similar to installing someone else's program – except, now, you have full control over a simple-yet-powerful system that does the same thing and is fully transparent. Isn't that great?
*I'm purposefully glossing over some of the gritty details here, but the description above covers the essentials.
Written by Dulany Weaver. Copyright 2022-2024. All rights reserved.