Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use api token instead of key. #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# mikrotik-cloudflare-ddns-scripts

This simple scripts are designed to implement `DDNS` feature using the service [Cloudflare](https://www.cloudflare.com/).
This simple scripts are designed to implement DDNS feature using the [Cloudflare API](https://developers.cloudflare.com/api).

### Requirements and dependences

Expand All @@ -12,12 +12,33 @@ Depends on [Mikrotik JSON Parser](https://github.com/Winand/mikrotik-json-parser

Each script (IPv4 and IPv6) has a configuration area. Just insert your values.

First of all you need your `Cloudflare API` key. Just go to the `Cloudflare` [site](https://www.cloudflare.com/) `My Profile -> API Keys section -> Global API Key -> View`. Follow the instructions. Now you have your `API` key. Keep it safe.

The service does not allow easy retrieval of required Zone and DNS record identifiers. This is only possible through a REST API methods [List Zones](https://developers.cloudflare.com/api/operations/zones-get) and [List DNS Records](https://developers.cloudflare.com/api/operations/dns-records-for-a-zone-list-dns-records). Using any REST client (I use [Advanced REST client](https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo) for `Chrome Browser`), sending a request, you will receive the `JSON` answer with necessary Zone and DNS Record IDs.

Insert all variables in scripts and install in your `Mikrotik` device.

First of all you need your Cloudflare API Token. Create the [Cloudflare API Token](https://dash.cloudflare.com/profile/api-tokens), with DNS edit permission for your zone. Then grab your token, and follow the instructions. Keep the API Token safe.

You can go to your site's [cloudflare dashboard](https://dash.cloudflare.com) to retrieve zone id, on the URL it is `https://dash.cloudflare.com/$zone_id/$domain_name`, but DNS Record ID is only possible through REST API.

REST API methods [List Zones](https://developers.cloudflare.com/api/operations/zones-get) and [List DNS Records](https://developers.cloudflare.com/api/operations/dns-records-for-a-zone-list-dns-records). Using any REST client (e.g. [HTTPie](https://httpie.io/app), or [curl](https://curl.se)), sending a request, you will receive the JSON answer with necessary Zone and DNS Record IDs.

Insert all variables in scripts and install in your RouterOS device.

#### Get zone id with cURL
Replace `$api_token` with your [api token](https://dash.cloudflare.com/profile/api-tokens).
```shell
curl --request GET \
--url https://api.cloudflare.com/client/v4/zones \
--header "Authorization: Bearer $api_token" \
--header "Content-Type: application/json"
```

#### Get record id with cURL
Get dns record id. Replace `$api_token` with your [api token](https://dash.cloudflare.com/profile/api-tokens). And `$zone_id` with zone id from [above](#Get-zone-id-with-cURL).
```shell
curl --request GET \
--url https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records \
--header "Authorization: Bearer $api_token" \
--header "Content-Type: application/json"
```
> [!TIP]
> You can use jq to prettify json output.
### Running

You may add this script to system scheduler as periodically task.
Expand Down
9 changes: 3 additions & 6 deletions cloudflare4ddns
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@
:local wanif "wan1"

# Cloudflare section
:local email "e-mail"
:local key "token"
:local apiToken "token"
:local zoneId "zoneId"
:local hostId "hostId"

# Domain hostname
:local hostName "host.yourdomain.com"

# ** END OF CONFIGURE SECTION **
Expand All @@ -32,8 +29,8 @@
:log info "[Cloudflare DDNS] WAN IPv4 address for interface $wanif has been changed to $ip4new."

:local url "https://api.cloudflare.com/client/v4/zones/$zoneId/dns_records/$hostId"
:local header "X-Auth-Email: $email, X-Auth-Key: $key, content-type: application/json"
:local data "{\"type\":\"A\",\"name\":\"$hostName\",\"content\":\"$ip4new\",\"ttl\":120}"
:local header "Authorization: Bearer $apiToken, Content-Type: application/json"
:local data "{\"type\":\"A\",\"name\":\"$hostName\",\"content\":\"$ip4new\"}"

# :log info "[Cloudflare DDNS] URL: $url"
# :log info "[Cloudflare DDNS] HEADER: $header"
Expand Down
9 changes: 3 additions & 6 deletions cloudflare6ddns
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@
:local wanif "lan1"

# Cloudflare section
:local email "e-mail"
:local key "token"
:local apiToken "token"
:local zoneId "zoneId"
:local hostId "hostId"

# Domain hostname
:local hostName "host.yourdomain.com"

# ** END OF CONFIGURE SECTION **
Expand All @@ -32,8 +29,8 @@
:log info "[Cloudflare DDNS] IPv6 address for interface $wanif has been changed to $ip6new."

:local url "https://api.cloudflare.com/client/v4/zones/$zoneId/dns_records/$hostId"
:local header "X-Auth-Email: $email, X-Auth-Key: $key, content-type: application/json"
:local data "{\"type\":\"AAAA\",\"name\":\"$hostName\",\"content\":\"$ip6new\",\"ttl\":120}"
:local header "Authorization: Bearer $apiToken, Content-Type: application/json"
:local data "{\"type\":\"AAAA\",\"name\":\"$hostName\",\"content\":\"$ip6new\"}"

# :log info "[Cloudflare DDNS] URL: $url"
# :log info "[Cloudflare DDNS] HEADER: $header"
Expand Down