mirror of
https://github.com/nymurbd/MikroTik-scripts.git
synced 2025-07-13 03:24:30 +02:00
netwatch-dns: move code into function
This commit is contained in:
parent
400516295e
commit
458fd1fdcd
1 changed files with 98 additions and 93 deletions
|
@ -8,28 +8,30 @@
|
||||||
# monitor and manage dns/doh with netwatch
|
# monitor and manage dns/doh with netwatch
|
||||||
# https://git.eworm.de/cgit/routeros-scripts/about/doc/netwatch-dns.md
|
# https://git.eworm.de/cgit/routeros-scripts/about/doc/netwatch-dns.md
|
||||||
|
|
||||||
:local 0 [ :jobname ];
|
|
||||||
:global GlobalFunctionsReady;
|
:global GlobalFunctionsReady;
|
||||||
:while ($GlobalFunctionsReady != true) do={ :delay 500ms; }
|
:while ($GlobalFunctionsReady != true) do={ :delay 500ms; }
|
||||||
|
|
||||||
:global CertificateAvailable;
|
:local Main do={
|
||||||
:global EitherOr;
|
:local ScriptName [ :tostr $1 ];
|
||||||
:global LogPrintExit2;
|
|
||||||
:global ParseKeyValueStore;
|
|
||||||
:global ScriptLock;
|
|
||||||
|
|
||||||
$ScriptLock $0;
|
:global CertificateAvailable;
|
||||||
|
:global EitherOr;
|
||||||
|
:global LogPrintExit2;
|
||||||
|
:global ParseKeyValueStore;
|
||||||
|
:global ScriptLock;
|
||||||
|
|
||||||
:local SettleTime (5m30s - [ /system/resource/get uptime ]);
|
$ScriptLock $ScriptName;
|
||||||
:if ($SettleTime > 0s) do={
|
|
||||||
$LogPrintExit2 info $0 ("System just booted, giving netwatch " . $SettleTime . " to settle.") true;
|
|
||||||
}
|
|
||||||
|
|
||||||
:local DnsServers ({});
|
:local SettleTime (5m30s - [ /system/resource/get uptime ]);
|
||||||
:local DnsFallback ({});
|
:if ($SettleTime > 0s) do={
|
||||||
:local DnsCurrent [ /ip/dns/get servers ];
|
$LogPrintExit2 info $ScriptName ("System just booted, giving netwatch " . $SettleTime . " to settle.") true;
|
||||||
|
}
|
||||||
|
|
||||||
:foreach Host in=[ /tool/netwatch/find where comment~"\\bdns\\b" status="up" ] do={
|
:local DnsServers ({});
|
||||||
|
:local DnsFallback ({});
|
||||||
|
:local DnsCurrent [ /ip/dns/get servers ];
|
||||||
|
|
||||||
|
:foreach Host in=[ /tool/netwatch/find where comment~"\\bdns\\b" status="up" ] do={
|
||||||
:local HostVal [ /tool/netwatch/get $Host ];
|
:local HostVal [ /tool/netwatch/get $Host ];
|
||||||
:local HostInfo [ $ParseKeyValueStore ($HostVal->"comment") ];
|
:local HostInfo [ $ParseKeyValueStore ($HostVal->"comment") ];
|
||||||
|
|
||||||
|
@ -41,29 +43,29 @@ $ScriptLock $0;
|
||||||
:set DnsFallback ($DnsFallback, $HostVal->"host");
|
:set DnsFallback ($DnsFallback, $HostVal->"host");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
:if ([ :len $DnsServers ] > 0) do={
|
:if ([ :len $DnsServers ] > 0) do={
|
||||||
:if ($DnsServers != $DnsCurrent) do={
|
:if ($DnsServers != $DnsCurrent) do={
|
||||||
$LogPrintExit2 info $0 ("Updating DNS servers: " . [ :tostr $DnsServers ]) false;
|
$LogPrintExit2 info $ScriptName ("Updating DNS servers: " . [ :tostr $DnsServers ]) false;
|
||||||
/ip/dns/set servers=$DnsServers;
|
/ip/dns/set servers=$DnsServers;
|
||||||
/ip/dns/cache/flush;
|
/ip/dns/cache/flush;
|
||||||
}
|
}
|
||||||
} else={
|
} else={
|
||||||
:if ([ :len $DnsFallback ] > 0) do={
|
:if ([ :len $DnsFallback ] > 0) do={
|
||||||
:if ($DnsFallback != $DnsCurrent) do={
|
:if ($DnsFallback != $DnsCurrent) do={
|
||||||
$LogPrintExit2 info $0 ("Updating DNS servers to fallback: " . \
|
$LogPrintExit2 info $ScriptName ("Updating DNS servers to fallback: " . \
|
||||||
[ :tostr $DnsFallback ]) false;
|
[ :tostr $DnsFallback ]) false;
|
||||||
/ip/dns/set servers=$DnsFallback;
|
/ip/dns/set servers=$DnsFallback;
|
||||||
/ip/dns/cache/flush;
|
/ip/dns/cache/flush;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
:local DohCurrent [ /ip/dns/get use-doh-server ];
|
:local DohCurrent [ /ip/dns/get use-doh-server ];
|
||||||
:local DohServers ({});
|
:local DohServers ({});
|
||||||
|
|
||||||
:foreach Host in=[ /tool/netwatch/find where comment~"\\bdoh\\b" status="up" ] do={
|
:foreach Host in=[ /tool/netwatch/find where comment~"\\bdoh\\b" status="up" ] do={
|
||||||
:local HostVal [ /tool/netwatch/get $Host ];
|
:local HostVal [ /tool/netwatch/get $Host ];
|
||||||
:local HostInfo [ $ParseKeyValueStore ($HostVal->"comment") ];
|
:local HostInfo [ $ParseKeyValueStore ($HostVal->"comment") ];
|
||||||
:local HostName [ /ip/dns/static/find where name address=($HostVal->"host") \
|
:local HostName [ /ip/dns/static/find where name address=($HostVal->"host") \
|
||||||
|
@ -78,23 +80,23 @@ $ScriptLock $0;
|
||||||
}
|
}
|
||||||
|
|
||||||
:if ($DohCurrent = $HostInfo->"doh-url") do={
|
:if ($DohCurrent = $HostInfo->"doh-url") do={
|
||||||
$LogPrintExit2 debug $0 ("Current DoH server is still up: " . $DohCurrent) true;
|
$LogPrintExit2 debug $ScriptName ("Current DoH server is still up: " . $DohCurrent) true;
|
||||||
}
|
}
|
||||||
|
|
||||||
:set ($DohServers->[ :len $DohServers ]) $HostInfo;
|
:set ($DohServers->[ :len $DohServers ]) $HostInfo;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
:if ([ :len $DohCurrent ] > 0) do={
|
:if ([ :len $DohCurrent ] > 0) do={
|
||||||
$LogPrintExit2 info $0 ("Current DoH server is down, disabling: " . $DohCurrent) false;
|
$LogPrintExit2 info $ScriptName ("Current DoH server is down, disabling: " . $DohCurrent) false;
|
||||||
/ip/dns/set use-doh-server="";
|
/ip/dns/set use-doh-server="";
|
||||||
/ip/dns/cache/flush;
|
/ip/dns/cache/flush;
|
||||||
}
|
}
|
||||||
|
|
||||||
:foreach DohServer in=$DohServers do={
|
:foreach DohServer in=$DohServers do={
|
||||||
:if ([ :len ($DohServer->"doh-cert") ] > 0) do={
|
:if ([ :len ($DohServer->"doh-cert") ] > 0) do={
|
||||||
:if ([ $CertificateAvailable ($DohServer->"doh-cert") ] = false) do={
|
:if ([ $CertificateAvailable ($DohServer->"doh-cert") ] = false) do={
|
||||||
$LogPrintExit2 warning $0 ("Downloading certificate failed, trying without.") false;
|
$LogPrintExit2 warning $ScriptName ("Downloading certificate failed, trying without.") false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,7 +108,7 @@ $ScriptLock $0;
|
||||||
"\01\00" . "\00\01" . "\00\00" . "\00\00" . "\00\00" . "\09doh-check\05eworm\02de\00" . \
|
"\01\00" . "\00\01" . "\00\00" . "\00\00" . "\00\00" . "\09doh-check\05eworm\02de\00" . \
|
||||||
"\00\10" . "\00\01") ]) as-value ]->"data");
|
"\00\10" . "\00\01") ]) as-value ]->"data");
|
||||||
} on-error={
|
} on-error={
|
||||||
$LogPrintExit2 warning $0 ("Request to DoH server failed (network or certificate issue): " . \
|
$LogPrintExit2 warning $ScriptName ("Request to DoH server failed (network or certificate issue): " . \
|
||||||
($DohServer->"doh-url")) false;
|
($DohServer->"doh-url")) false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -114,10 +116,13 @@ $ScriptLock $0;
|
||||||
:if ([ :typeof [ :find $Data "doh-check-OK" ] ] = "num") do={
|
:if ([ :typeof [ :find $Data "doh-check-OK" ] ] = "num") do={
|
||||||
/ip/dns/set use-doh-server=($DohServer->"doh-url") verify-doh-cert=yes;
|
/ip/dns/set use-doh-server=($DohServer->"doh-url") verify-doh-cert=yes;
|
||||||
/ip/dns/cache/flush;
|
/ip/dns/cache/flush;
|
||||||
$LogPrintExit2 info $0 ("Setting DoH server: " . ($DohServer->"doh-url")) true;
|
$LogPrintExit2 info $ScriptName ("Setting DoH server: " . ($DohServer->"doh-url")) true;
|
||||||
} else={
|
} else={
|
||||||
$LogPrintExit2 warning $0 ("Received unexpected response from DoH server: " . \
|
$LogPrintExit2 warning $ScriptName ("Received unexpected response from DoH server: " . \
|
||||||
($DohServer->"doh-url")) false;
|
($DohServer->"doh-url")) false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$Main [ :jobname ];
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue