aboutsummaryrefslogtreecommitdiffstats
path: root/netwatch-dns.rsc
blob: a7c75e89699e3e1b1cea81e6501309556f9363e9 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!rsc by RouterOS
# RouterOS script: netwatch-dns
# Copyright (c) 2022-2024 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 IsDNSResolving;
:global LogPrintExit2;
:global ParseKeyValueStore;
:global ScriptLock;

$ScriptLock $0;

:if ([ /system/resource/get uptime ] < 5m30s) 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~"\\bdns\\b" status="up" ] do={
  :local HostVal [ /tool/netwatch/get $Host ];
  :local HostInfo [ $ParseKeyValueStore ($HostVal->"comment") ];

  :if ($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 DohCertVerify [ /ip/dns/get verify-doh-cert ];
:local DohCurrent [ /ip/dns/get use-doh-server ];
:local DohServers ({});

:foreach Host in=[ /tool/netwatch/find where comment~"\\bdoh\\b" status="up" ] do={
  :local HostVal [ /tool/netwatch/get $Host ];
  :local HostInfo [ $ParseKeyValueStore ($HostVal->"comment") ];
  :local HostName [ /ip/dns/static/find where name address=($HostVal->"host") \
      (!type or type="A" or type="AAAA") !disabled !dynamic ];
  :if ([ :len $HostName ] > 0) do={
    :set HostName [ /ip/dns/static/get ($HostName->0) name ];
  }

  :if ($HostInfo->"doh" = true && $HostInfo->"disabled" != true) do={
    :if ([ :len ($HostInfo->"doh-url") ] = 0) do={
      :set ($HostInfo->"doh-url") ("https://" . [ $EitherOr $HostName ($HostVal->"host") ] . "/dns-query");
    }

    :if ($DohCurrent = $HostInfo->"doh-url") do={
      $LogPrintExit2 debug $0 ("Current DoH server is still up.") true;
    }

    :set ($DohServers->[ :len $DohServers ]) $HostInfo;
  }
}

:if ([ :len $DohCurrent ] > 0 && [ :len $DohServers ] = 0) do={
  $LogPrintExit2 info $0 ("DoH server (" . $DohCurrent . ") is down, disabling.") false;
  /ip/dns/set use-doh-server="";
  /ip/dns/cache/flush;
}

:foreach DohServer in=$DohServers do={
  $LogPrintExit2 info $0 ("Updating DoH server: " . ($DohServer->"doh-url")) false;
  :if ([ :len ($DohServer->"doh-cert") ] > 0) do={
    :set DohCertVerify true;
    /ip/dns/set use-doh-server="";
    :if ([ $CertificateAvailable ($DohServer->"doh-cert") ] = false) do={
      $LogPrintExit2 warning $0 ("Downloading certificate failed, trying without.") false;
    }
  }
  /ip/dns/set use-doh-server=($DohServer->"doh-url") verify-doh-cert=$DohCertVerify;
  /ip/dns/cache/flush;
  :if ([ $IsDNSResolving ] = true) do={
    $LogPrintExit2 debug $0 ("DoH server is functional.") true;
  } else={
    /ip/dns/set use-doh-server="";
    $LogPrintExit2 warning $0 ("DoH server not functional, trying next.") false;
  }
}