Access your Synology NAS with a custom domain on Bunny.net (DDNS)
Tags: synology, nas, javascript, tutorial
Published: 2024-05-04 13:33:26

This article is born because Bunny.net does not support Dynamic DNS (for the moment?), I contacted the support but I received a negative result:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8f3zu8rnx27my62120lz.png)

I wanted to migrate all my domain from OVH/Cloudflare to Bunny, because their DNS performances are on the top chart (they are the best?), it's free, the UI/UX of the admin panel is clean, and MOST OF ALL: <u>They got rabbits EVERYWHERE</u>.

![](https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExejY2eHBrNnc2dml4ZG04N3BsbGk2MTI0Y2tqc3ljZDUwc3h0NjB0diZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/cno2xWuCqkG6A/giphy.gif)

However there is a big problem: to access my Synology NAS through a custom domain name, DDNS is required because:
* My internet provider **changes IP everytime the Internet Box restarts**, so I have to notify my DNS provider of the new IP! 
* My internet provider doesn't give fixed IPV4 for free (to be honest I won't pay).

After experimenting, I created a Node script executed regularly on my NAS that:
* Save the current IP of the NAS in a cache.
* 5 Minute later, fetch the current IP and compared it with the cached IP: if the IP is different, I call the Bunny DNS API to update the DNS records value with the new IP! Finally save the new IP on the cache.
* Repeat this process 5 Minute later: compare the current IP and cache IP again, etc...

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bt9114jkik0it5b4gh0c.png)

## Bunny Setup

First, make sure you have loaded your domain on your Bunny admin interface, if not, here is the setup link:
https://dash.bunny.net/dns/zones/add

Create a `A` Record and set the public IP of your Synology NAS. If the IP is not correct, it will be corrected with the Node Script on the next section:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ilamulbtfu1o588s2hn3.png)

In my case, I have a subdomain `photos.domain.com` pointing to my Synology NAS. The `Value` column shows the IP you entered (in my case I prefer to keep it private :D).

Finally on your Bunny account, get you API key, it will be use to update the IP of your `A` Record:
https://dash.bunny.net/account/settings

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p1dfqyecq408f4xewbh9.png)

## Synology Setup

Synology NAS has Node already installed, so I decided to create a Node Script instead of a Bash script, copy the following file to the directory of your choice. Personnally, I copied to `/volume1/home/admin/ddns-bunny.js`:

https://github.com/steevepay/ddns-bunny-synology/blob/v1.0.0/ddns-bunny.js

Let's create a new Task executing the Script regularly, like every minutes.
1. Open the `Control Panel` application > Click on `Task Scheduler`
2. On the bar, click on `Create` > `Schedule Task` > `User Defined Script` ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ahztr4xjk0zm309addg.png)
3. Name the new Task like `DDNS Bunny` and select a User, like the admin. ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fgpzdda2879dhymtaujf.png)
4. Now select the second tab **Schedule**: Ensure the `Run on the following day` is selected, and set at `Daily`. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zxgoe643phy4cwa798ya.png)
5. On the second tab **Schedule**: Set the start time at midnight, and check `Continue running the same day`. Set the Repeat into to `1 minutes` and `Last Run Time` to `23h55`. ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iw8e3i6vqees9p16xzfx.png)
6. Now select the third tab **Task Settings**, and add to the `"User Defined Script"`the following command. **BUNNY_ACCESS_KEY="" NAS_URL="photos.domain.com" node /volume1/homes/admin/ddns-bunny.js**. The command must written in one single line, otherwise the API KEY and the URL won't be retrieved by the script. Write your Bunny API key under the first double quotes for the environment variable `BUNNY_ACCESS_KEY`. Then write the domain of your NAS between the second double quotes for the env `NAS_URL`, without **https://**. Finally the command execute the Script with Node. ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4xkpl51jj51uoc43ofdw.png)
7. To verify if the script is working, enable the option "Send Run details by emails" and provide your email. ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/64hovi9esab7aifscewb.png)
8. Save the new task, then wait one minute, to verify if everything went fine:
   * **Check the email you received:** it should print `OK {YOUR PUBLIC IP}`. If something went wrong, it should print an error message detailing the issue.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gjxs31n9mngo6u85vgy8.png)
   * **A configuration file has been created:** Open the directory `/volume1/homes/admin/`, and you see a JSON file named **DDNS_BUNNY_CONFIG.json**. It is used to save: your current public IP, and metadata to requests the Bunny API.
   * **Visit your custom domain:** Your Synology panel should load
   * **Verify the IP:** open your Bunny DNS panel, you should see the public IP of your NAS
   * **If you didn't received email:** You probably didn't configure right your NAS to send emails.

## Known limitations

* The script can update only **one domain** at the moment, I'm working on supporting multiple domains.
* The script lacks of Unit Tests

## Conclusion

Now Bunny.net supports DDNS by simply requesting their DNS API, and I can migrate all my domains to Bunny, youhouuu 🐰🎉