Doh server : automatic fallback to standard dns servers if fail (and switchback)

Hi,

This is my script for monitor doh server and if not working, fallback to standards dns servers.
Can be scheduled each 2min ; when doh server will be working again, switchback again.

:global dohServer "https://dns.nextdns.io/__id__"
:global testDomain "free.fr"
:global currentDoh ""
:global dohAvailable true
:global dnsServers "\
            1.1.1.1,\
            8.8.8.8,\
            185.222.222.222,\
            9.9.9.9,\
            1.0.0.1,\
            8.8.4.4,\
            45.11.45.11,\
            149.112.112.112,\
            2606:4700:4700::1111,\
            2001:4860:4860::8888,\
            2a09::,\
            2620:fe::fe,\
            2606:4700:4700::1001,\
            2001:4860:4860::8844,\
            2a11::,2620:fe::9"


# func to test doh server
:local testDohServerResolution do={
    :local result [/tool fetch url="$dohServer/dns-query?name=$testDomain&type=A" mode=https check-certificate=no duration=3s output=user as-value]
    :if ($result->"status" = "finished" && $result->"downloaded" > 0) do={
        :return true
    } else={
        :return false
    }
}


# get current doh server
:set currentDoh [/ip dns get use-doh-server]


# if doh server is used, test if is still working, if not, test it work now
:if ( $dohServer = $currentDoh ) do={
    :log info "doh server configured: testing if working correctly..."
    :set dohAvailable [$testDohServerResolution dohServer=$dohServer testDomain=$testDomain]

    :if (!$dohAvailable) do={
        :log error "doh server not working, switching to standard dns servers"
        /ip dns set use-doh-server=""
        /ip dns set servers="$dnsServers"
    } else={
        :log info "doh server is working, nothing to do"
    }
} else={
    :log info "doh server not configured: testing if it work now..."
    :set dohAvailable [$testDohServerResolution dohServer=$dohServer testDomain=$testDomain]

    :if ($dohAvailable) do={
        :log warning "doh server is working, switching to doh server"
        /ip dns set servers=""
        /ip dns set use-doh-server="$dohServer"
    } else={
        :log error "doh server still not working, nothing to do"
    }
}

As suggestion, you might want to add some check using :resolve just to make sure the local resolver via UDP is also working. While you check the DoH server resolves via fetch, a final sanity check that everything works via /ip/dns might round out your DNS check routine.