aboutsummaryrefslogtreecommitdiffstats
path: root/netwatch-dns.rsc
diff options
context:
space:
mode:
authorGravatar Christian Hesse <mail@eworm.de>2023-03-07 10:57:42 +0100
committerGravatar Christian Hesse <mail@eworm.de>2023-03-07 22:26:01 +0100
commita832fd04ef085ef7e85000843a8e8fa59ce36dfb (patch)
treec977ca4b5167d8aa07dbe6bfe15fca8f467db0d9 /netwatch-dns.rsc
parent1e6e0646e256c4acb9f8470853d0699286af010e (diff)
rename scripts and add file extension ".rsc"change-95
No functional change for the user... The migration is done automatically.
Diffstat (limited to 'netwatch-dns.rsc')
-rw-r--r--netwatch-dns.rsc94
1 files changed, 94 insertions, 0 deletions
diff --git a/netwatch-dns.rsc b/netwatch-dns.rsc
new file mode 100644
index 0000000..4eb3285
--- /dev/null
+++ b/netwatch-dns.rsc
@@ -0,0 +1,94 @@
+#!rsc by RouterOS
+# RouterOS script: netwatch-dns
+# Copyright (c) 2022-2023 Christian Hesse <mail@eworm.de>
+# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md
+#
+# monitor and manage dns/doh with netwatch
+# https://git.eworm.de/cgit/routeros-scripts/about/doc/netwatch-dns.md
+
+:local 0 "netwatch-dns";
+:global GlobalFunctionsReady;
+:while ($GlobalFunctionsReady != true) do={ :delay 500ms; }
+
+:global CertificateAvailable;
+:global EitherOr;
+:global LogPrintExit2;
+:global ParseKeyValueStore;
+:global ScriptLock;
+
+$ScriptLock $0;
+
+:if ([ /system/resource/get uptime ] < 5m) do={
+ $LogPrintExit2 info $0 ("System just booted, giving netwatch some time to settle.") true;
+}
+
+:local DnsServers ({});
+:local DnsFallback ({});
+:local DnsCurrent [ /ip/dns/get servers ];
+
+:foreach Host in=[ /tool/netwatch/find where comment~"dns" !disabled ] do={
+ :local HostVal [ /tool/netwatch/get $Host ];
+ :local HostInfo [ $ParseKeyValueStore ($HostVal->"comment") ];
+
+ :if ($HostVal->"status" = "up" && $HostInfo->"disabled" != true) do={
+ :if ($HostInfo->"dns" = true) do={
+ :set DnsServers ($DnsServers, $HostVal->"host");
+ }
+ :if ($HostInfo->"dns-fallback" = true) do={
+ :set DnsFallback ($DnsFallback, $HostVal->"host");
+ }
+ }
+}
+
+:if ([ :len $DnsServers ] > 0) do={
+ :if ($DnsServers != $DnsCurrent) do={
+ $LogPrintExit2 info $0 ("Updating DNS servers: " . [ :tostr $DnsServers ]) false;
+ /ip/dns/set servers=$DnsServers;
+ /ip/dns/cache/flush;
+ }
+} else={
+ :if ([ :len $DnsFallback ] > 0) do={
+ :if ($DnsFallback != $DnsCurrent) do={
+ $LogPrintExit2 info $0 ("Updating DNS servers to fallback: " . \
+ [ :tostr $DnsFallback ]) false;
+ /ip/dns/set servers=$DnsFallback;
+ /ip/dns/cache/flush;
+ }
+ }
+}
+
+:local DohServer "";
+:local DohCurrent [ /ip/dns/get use-doh-server ];
+:local DohCert "";
+
+:foreach Host in=[ /tool/netwatch/find where comment~"doh" !disabled ] do={
+ :local HostVal [ /tool/netwatch/get $Host ];
+ :local HostInfo [ $ParseKeyValueStore ($HostVal->"comment") ];
+
+ :if ($HostVal->"status" = "up" && $HostInfo->"doh" = true && \
+ $HostInfo->"disabled" != true && $DohServer = "") do={
+ :set DohServer [ $EitherOr ($HostInfo->"doh-url") \
+ ("https://" . $HostVal->"host" . "/dns-query") ];
+ :set DohCert ($HostInfo->"doh-cert");
+ }
+}
+
+:if ($DohServer != "") do={
+ :if ($DohServer != $DohCurrent) do={
+ $LogPrintExit2 info $0 ("Updating DoH server: " . $DohServer) false;
+ :if ([ :len $DohCert ] > 0) do={
+ /ip/dns/set use-doh-server="";
+ :if ([ $CertificateAvailable $DohCert ] = false) do={
+ $LogPrintExit2 warning $0 ("Downloading certificate failed, trying without.") false;
+ }
+ }
+ /ip/dns/set use-doh-server=$DohServer;
+ /ip/dns/cache/flush;
+ }
+} else={
+ :if ($DohCurrent != "") do={
+ $LogPrintExit2 info $0 ("DoH server (" . $DohCurrent . ") is down, disabling.") false;
+ /ip/dns/set use-doh-server="";
+ /ip/dns/cache/flush;
+ }
+}