|
| 1 | + |
| 2 | +# Synology Dynamic DNS with Cloudflare for both multidomains and subdomains |
| 3 | + |
| 4 | +> Documentation website: https://mrikirill.github.io/SynologyDDNSCloudflareMultidomain/ |
| 5 | +
|
| 6 | +## Table of contents |
| 7 | + |
| 8 | +* [What this script does](#what-this-script-does) |
| 9 | +* [Before you begin](#before-you-begin) |
| 10 | +* [How to install](#how-to-install) |
| 11 | +* [Troubleshooting and known issues](#troubleshooting-and-known-issues) |
| 12 | + + [CloudFlare API free domains limitation](#cloudflare-api-free-domains-limitation) |
| 13 | + + [Connection test failed or error returned](#connection-test-failed-or-error-returned) |
| 14 | + + [Cloudflare no longer listed as a DDNS provider after a DSM update](#cloudflare-no-longer-listed-as-a-ddns-provider-after-dsm-or-srm-updates) |
| 15 | +* [Default Cloudflare ports](#default-cloudflare-ports) |
| 16 | +* [Debug script](#debug) |
| 17 | + |
| 18 | + |
| 19 | +## What this script does |
| 20 | + |
| 21 | +* A PHP script for Synology DSM (and potentially Synology SRM devices) adding support for Cloudflare to Network Centre > Dynamic DNS (DDNS). |
| 22 | +* Supports single domains, multidomains, subdomains and regional domains, or any combination thereof (example: dev.my.domain.com.au, domain.com.uk etc) |
| 23 | +* Easy instalation process |
| 24 | +* Based on CloudFlare API v4 |
| 25 | +* [Supports dual stack IPv4 and IPv6](https://github.com/mrikirill/SynologyDDNSCloudflareMultidomain/pull/13) |
| 26 | + |
| 27 | +## Before you begin |
| 28 | + |
| 29 | +Before starting the installation process, make sure you have (and know) the following information, or have completed these steps: |
| 30 | + |
| 31 | + 1. *Cloudflare credentials:* |
| 32 | + |
| 33 | + a. Know your Cloudflare account username (or [register for an account if you're new to Cloudflare](https://dash.cloudflare.com/sign-up)); and |
| 34 | + |
| 35 | + b. Have your [API key](https://dash.cloudflare.com/profile/api-tokens) - no need to use your Global API key! (More info: [API keys](https://support.cloudflare.com/hc/en-us/articles/200167836-Managing-API-Tokens-and-Keys)). |
| 36 | + |
| 37 | +  |
| 38 | + |
| 39 | + c. Create a API key with following (3) permissions: |
| 40 | + |
| 41 | + **Zone** > **Zone.Settings** > **Read** |
| 42 | + **Zone** > **Zone** > **Read** |
| 43 | + **Zone** > **DNS** > **Edit** |
| 44 | + |
| 45 | + The affected zone ressouces have to be (at least): |
| 46 | + |
| 47 | + **Include** > **All zones from an account** > `<domain>` |
| 48 | + |
| 49 | + 2. *DNS settings:* |
| 50 | + |
| 51 | + Ensure the DNS A record(s) for the domain/zone(s) you wish to update with this script have been created (More information: [Managing DNS records](https://support.cloudflare.com/hc/en-us/articles/360019093151-Managing-DNS-records-in-Cloudflare)). |
| 52 | + |
| 53 | + Your DNS records should appear (or already be setup as follows) in Cloudflare: |
| 54 | + |
| 55 | + (Note: Having Proxied turned on for your A records isn't necessary, but it will prevent those snooping around from easily finding out your current IP address) |
| 56 | + |
| 57 | +  |
| 58 | + |
| 59 | +3. *SSH access to your Synology device:* |
| 60 | + |
| 61 | +If you haven't setup this access, see the following Synology Knowledge Base article: |
| 62 | +[How can I sign in to DSM/SRM with root privilege via SSH?[(https://kb.synology.com/en-id/DSM/tutorial/How_to_login_to_DSM_with_root_permission_via_SSH_Telnet) |
| 63 | + |
| 64 | +4. *SRM users: Knowledge of vi:* |
| 65 | + |
| 66 | +vi is the only text editor available within the [Busybox](https://linux.die.net/man/1/busybox) environment available at the SSH command line on devices running SRM. |
| 67 | + |
| 68 | +For assistance with vi commands, see: |
| 69 | +[Basic vi commands](https://www.cs.colostate.edu/helpdocs/vi.html) |
| 70 | + |
| 71 | + |
| 72 | +## How to install |
| 73 | + |
| 74 | +1. **SSH with root privledges on your supported device:** |
| 75 | + |
| 76 | + a. For DSM Users: |
| 77 | + |
| 78 | + Navigate to __Control Panel > Terminal & SNMP > Enable SSH service__ |
| 79 | + |
| 80 | + b. For SRM users: |
| 81 | + |
| 82 | + Navigate to __Control Panel > Services > System Services > Terminal > Enable SSH service__ |
| 83 | + |
| 84 | +  |
| 85 | + |
| 86 | +2. **Connect via SSH:** Connect to your supported device via SSH and execute command |
| 87 | + |
| 88 | +* For DSM Users |
| 89 | + ``` |
| 90 | + wget https://raw.githubusercontent.com/mrikirill/SynologyDDNSCloudflareMultidomain/master/cloudflare.php -O /usr/syno/bin/ddns/cloudflare.php && sudo chmod 755 /usr/syno/bin/ddns/cloudflare.php |
| 91 | + ``` |
| 92 | + |
| 93 | +* For SRM Users |
| 94 | + Note: Ensure you are connected as root in your SSH session |
| 95 | + ``` |
| 96 | + wget https://raw.githubusercontent.com/mrikirill/SynologyDDNSCloudflareMultidomain/master/cloudflare.php -O /usr/syno/bin/ddns/cloudflare.php && chmod 755 /usr/syno/bin/ddns/cloudflare.php |
| 97 | + ``` |
| 98 | + |
| 99 | + **Note:** For SRM users, you must connect to your device as root. No other username will allow these commands to run. |
| 100 | + |
| 101 | +3. **Update DDNS provider list:** Using a command line editor, insert the text below to your DMS file (Location : __/etc.defaults/ddns_provider.conf__), to add DDNS support via Cloudflare: |
| 102 | + |
| 103 | + ``` |
| 104 | + [Cloudflare] |
| 105 | + modulepath=/usr/syno/bin/ddns/cloudflare.php |
| 106 | + queryurl=https://www.cloudflare.com/ |
| 107 | + ``` |
| 108 | + |
| 109 | + **Note:** For SRM users, break out this [Vim cheat sheet](https://coderwall.com/p/adv71w/basic-vim-commands-for-getting-started), as it's the only text editor available to you. |
| 110 | + |
| 111 | +4. **Update your DDNS settings:** |
| 112 | + |
| 113 | + a. *For DSM Users:* Navigate to __Control Panel > External Access > DDNS__ then add new DDNS |
| 114 | + |
| 115 | + b. *For SRM users:* Navigate to __Network Centre > Internet > QuickConnect & DDNS > DDNS__ and press the Add button: |
| 116 | + |
| 117 | + Add/Update the DDNS settings screen as follows: |
| 118 | + |
| 119 | + * Service provider: Select Cloudflare |
| 120 | + * Hostname: |
| 121 | + For a single domain: __mydomain.com__ |
| 122 | +For multiple domains: __subdomain.mydomain.com---vpn.mydomain.com__ |
| 123 | + (ensure each domain is seperated by three dashes: ---) |
| 124 | + |
| 125 | + __Note: there is 128 symbols limit on Hostname input__ |
| 126 | + * Username: The email address you use for logging in to Cloudflare (optional since the API key is sufficient) |
| 127 | + * Password: Your created Cloudflare API Key |
| 128 | + |
| 129 | +  |
| 130 | + |
| 131 | + Finally, press the test connection button to confirm all information is correctly entered, before pressing Ok to save and confirm your details. |
| 132 | + |
| 133 | +5. Enjoy 🍺 and __don't forget to deactivate SSH (step 1) if you don't need it__. |
| 134 | + |
| 135 | +## Troubleshooting and known issues |
| 136 | + |
| 137 | +### CloudFlare API free domains limitation |
| 138 | + |
| 139 | +CloudFlare API doesn't support domains with a .cf, .ga, .gq, .ml, or .tk TLD (top-level domain) |
| 140 | + |
| 141 | +For more details read here: https://github.com/mrikirill/SynologyDDNSCloudflareMultidomain/issues/28 and https://community.cloudflare.com/t/unable-to-update-ddns-using-api-for-some-tlds/167228/61 |
| 142 | + |
| 143 | +Response example: |
| 144 | + |
| 145 | +``` |
| 146 | +{ |
| 147 | + "result": null, |
| 148 | + "success": false, |
| 149 | + "errors": [ |
| 150 | + { |
| 151 | + "code": 1038, |
| 152 | + "message": "You cannot use this API for domains with a .cf, .ga, .gq, .ml, or .tk TLD (top-level domain). To configure the DNS settings for this domain, use the Cloudflare Dashboard." |
| 153 | + } |
| 154 | + ], |
| 155 | + "messages": [] |
| 156 | +} |
| 157 | +``` |
| 158 | + |
| 159 | +### Connection test failed or error returned |
| 160 | + |
| 161 | +This will manifest as either 1020 error; or the update attempt not showing in your Cloudflare Audit logs. |
| 162 | + |
| 163 | +That generally means you may not have entered something correctly in the DDNS screen for your domain(s). |
| 164 | + |
| 165 | +Revisit [Before you begin](#before-you-begin) to ensure you have all the right information, then go back to Step 4 in [How to install](#how-to-install) to make sure everything is correctly entered. |
| 166 | + |
| 167 | +**Handy hint:** You can also check your Cloudflare Audit logs to see what - if anything - has made it there with your API key (More information: [Understanding Cloudflare Audit Logs](https://support.cloudflare.com/hc/en-us/articles/115002833612-Understanding-Cloudflare-Audit-Logs)). Updates using the API will appear in the Audit logs as a Rec Set action. |
| 168 | + |
| 169 | +### Cloudflare no longer listed as a DDNS provider after DSM or SRM updates |
| 170 | + |
| 171 | +After system updates to either Synology DSM or SRM, you may find that: |
| 172 | +* __/usr/syno/bin/ddns/cloudflare.php__ has been deleted; |
| 173 | +* __/etc.defaults/ddns_provider.conf__ was reset to its default settings (settings for Cloudflare no longer included); and |
| 174 | +* The DDNS settings in your DDNS panel constantly show Cloudflare's status as loading. |
| 175 | + |
| 176 | +If this occurs, simply [repeat the How to install steps](#how-to-install) shown above. |
| 177 | + |
| 178 | +## Default Cloudflare ports |
| 179 | +Source [Identifying network ports compatible with Cloudflare's proxy](https://support.cloudflare.com/hc/en-us/articles/200169156-Identifying-network-ports-compatible-with-Cloudflare-s-proxy) |
| 180 | + |
| 181 | +|HTTP ports supported by Cloudflare | HTTPS ports supported by Cloudflare | |
| 182 | +|--|--| |
| 183 | +| 80 | 443 | |
| 184 | +| 8080 | 2053 | |
| 185 | +| 8880 | 2083 | |
| 186 | +| 2052 | 2087 | |
| 187 | +| 2082 | 2096 | |
| 188 | +| 2086 | 8443 | |
| 189 | +| 2095 | | |
| 190 | + |
| 191 | +## Debug |
| 192 | + |
| 193 | +You can run this script directly to see output logs |
| 194 | + |
| 195 | +* SSH into your Synology system |
| 196 | + |
| 197 | +* Run this command: |
| 198 | + |
| 199 | +``` |
| 200 | +/usr/bin/php -d open_basedir=/usr/syno/bin/ddns -f /usr/syno/bin/ddns/cloudflare.php "" "your-CloudFlare-token" "your---domains---divided---by---dashes" "ip-address" |
| 201 | +``` |
| 202 | + |
| 203 | +* Check output logs |
| 204 | + |
| 205 | +## Credits |
| 206 | + |
| 207 | +<small><i><a href='http://ecotrust-canada.github.io/markdown-toc/'>Table of contents generated with markdown-toc</a></i></small> |
| 208 | +<small><i><a href='https://www.youtube.com/watch?v=Nf7m3h11y-s'>DB Tech - creating API keys and using Cloudflare CNAME for single updates</a></i></small> |
0 commit comments