vectops / scripts

Suite of scripts to make our life easier.
0 stars 1 forks source link

A couple of small suggestions #2

Open aka13-404 opened 3 months ago

aka13-404 commented 3 months ago

Hey guys,

Found your repo and scripts by accident on google, specifically https://vectops.com/post/2023/configure-mikrotik-with-cloudflare-ddns/ Good writeup, but I have 2 cents on how to make stuff a bit nicer.

Feel free to ignore/close if you don't consider it valuable, feel free to expand/add alternative from my code attached to the issue. I felt that a formal pull request would be too formal.

  1. For quite a while, you don't have to schedule scripts, if you are getting the IP from the provider with the built in dhcp-client. So unless you are doing PPPoE, you can simply doubleclick corresponding dhcp-client and use the script there. It will execute when the lease changes.

  2. Going with the general API key is not a smart move. For a while, you can generate api tokens with limited scope, which also removes the need of the email header. the scope zone.dns for your desired zone is enough

  3. I dunno why you use PUT and the whole payload. a PATCH and the IP only will be sufficient.

Here is the updated version:

#zone id (domain) for which the records will be updated
:local cfZoneId ""
#api token with zone.dns permissions for the cfzoneid
:local cfApiToken ""
#record id which to update
#could be made into an array and iterator, but for me with one * and one @entry its not worth it
:local cfRecordId1 ""
:local cfRecordId2 ""

:local cfUrl "https://api.cloudflare.com/client/v4/zones/$cfZoneId/dns_records/"
:local cfAuthHeader "Authorization: Bearer $cfApiToken,Content-Type:application/json"

:if ($bound=1) do={

    :log info "IP lease received, updating DDNS"
    :local cfBody "{\"content\": \"$"lease-address"\"}"
    #This has cost me about 2 hours of my life. For whatever reason, if fetch executes immediately after receiving the lease-address, it will fail with "Download from api.cloudflare.com FAILED: Address not available"
    :delay 1

    /tool fetch url="$cfUrl$cfRecordId1" http-header-field=$cfAuthHeader http-data=$cfBody mode=https keep-result=no http-method=patch
    /tool fetch url="$cfUrl$cfRecordId2" http-header-field=$cfAuthHeader http-data=$cfBody mode=https keep-result=no http-method=patch

} else={
    :log info "No IP lease, nothing to be done with DDNS"
}
:log info "DHCP Client script ran successfully"
soyaletz commented 2 months ago

Hey,

I have 2 sites, 1 with dhcp and 1 with pppoe server. On the dhcp site I have something similar, when the lease is done it sends the change through the API. The same thing happens on the pppoe site, newly configured in /ppp/profile in the on-up section.

The original script is made about 1-2 years ago, when my knowledge about mikrotik and cloudflare was very simple, today I have some changes but I haven't written about it.

Thanks for writing to us. If you want to contribute, please create a new file with your changes and make a pull request with your new changes and we will make a blog post with this update.

Regards,