-
Notifications
You must be signed in to change notification settings - Fork 320
/
Copy pathfw-addr-lists.rsc
214 lines (195 loc) · 7.78 KB
/
fw-addr-lists.rsc
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!rsc by RouterOS
# RouterOS script: fw-addr-lists
# Copyright (c) 2023-2025 Christian Hesse <mail@eworm.de>
# https://rsc.eworm.de/COPYING.md
#
# requires RouterOS, version=7.16
#
# download, import and update firewall address-lists
# https://rsc.eworm.de/doc/fw-addr-lists.md
:global GlobalFunctionsReady;
:while ($GlobalFunctionsReady != true) do={ :delay 500ms; }
:local ExitOK false;
:do {
:local ScriptName [ :jobname ];
:global FwAddrLists;
:global FwAddrListTimeOut;
:global CertificateAvailable;
:global EitherOr;
:global FetchHuge;
:global HumanReadableNum;
:global LogPrint;
:global LogPrintOnce;
:global LogPrintVerbose;
:global ScriptLock;
:global WaitFullyConnected;
:local FindDelim do={
:local ValidChars "0123456789.:/ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-";
:for I from=0 to=[ :len $1 ] do={
:if ([ :typeof [ :find $ValidChars [ :pick ($1 . " ") $I ] ] ] != "num") do={
:return $I;
}
}
}
:local GetBranch do={
:global EitherOr;
:return [ :pick [ :convert transform=md5 to=hex [ :pick $1 0 [ $EitherOr [ :find $1 "/" ] [ :len $1 ] ] ] ] 0 2 ];
}
:if ([ $ScriptLock $ScriptName ] = false) do={
:set ExitOK true;
:error false;
}
$WaitFullyConnected;
:local ListComment ("managed by " . $ScriptName);
:foreach FwListName,FwList in=$FwAddrLists do={
:local CntAdd 0;
:local CntRenew 0;
:local CntRemove 0;
:local IPv4Addresses ({});
:local IPv6Addresses ({});
:local Failure false;
:foreach List in=$FwList do={
:local CheckCertificate false;
:local Data false;
:local TimeOut [ $EitherOr [ :totime ($List->"timeout") ] $FwAddrListTimeOut ];
:if ([ :len ($List->"cert") ] > 0) do={
:set CheckCertificate true;
:if ([ $CertificateAvailable ($List->"cert") ] = false) do={
$LogPrint warning $ScriptName ("Downloading required certificate (" . $FwListName . \
" / " . $List->"url" . ") failed, trying anyway.");
}
}
:for I from=1 to=5 do={
:if ($Data = false) do={
:set Data [ :tolf [ $FetchHuge $ScriptName ($List->"url") $CheckCertificate ] ];
:if ($Data = false) do={
:if ($I < 5) do={
$LogPrint debug $ScriptName ("Failed downloading for list '" . $FwListName . \
"', " . $I . ". try from: " . $List->"url");
:delay (($I * $I) . "s");
}
}
}
}
:if ($Data = false) do={
:set Data "";
:set Failure true;
$LogPrint warning $ScriptName ("Failed downloading for list '" . $FwListName . \
"' from: " . $List->"url");
} else={
$LogPrint debug $ScriptName ("Downloaded " . [ $HumanReadableNum [ :len $Data ] 1024 ] . \
"B for list '" . $FwListName . "' from: " . $List->"url");
}
:foreach Line in=[ :deserialize $Data delimiter="\n" from=dsv options=dsv.plain ] do={
:set Line ($Line->0);
:local Address;
:if ([ :pick $Line 0 1 ] = "{") do={
:do {
:set Address [ :tostr ([ :deserialize from=json $Line ]->"cidr") ];
} on-error={ }
} else={
:set Address ([ :pick $Line 0 [ $FindDelim $Line ] ] . ($List->"cidr"));
}
:do {
:local Branch [ $GetBranch $Address ];
:if ($Address ~ "^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}(/[0-9]{1,2})?\$") do={
:if ($Address ~ "/32\$") do={
:set Address [ :pick $Address 0 ([ :len $Address ] - 3) ];
}
:set ($IPv4Addresses->$Branch->$Address) $TimeOut;
:error true;
}
:if ($Address ~ "^[0-9a-zA-Z]*:[0-9a-zA-Z:\\.]+(/[0-9]{1,3})?\$") do={
:if ($Address ~ "/128\$") do={
:set Address [ :pick $Address 0 ([ :len $Address ] - 4) ];
}
:set ($IPv6Addresses->$Branch->$Address) $TimeOut;
:error true;
}
:if ($Address ~ "^[\\.a-zA-Z0-9-]+\\.[a-zA-Z]{2,}\$") do={
:set ($IPv4Addresses->$Branch->$Address) $TimeOut;
:set ($IPv6Addresses->$Branch->$Address) $TimeOut;
:error true;
}
} on-error={ }
}
}
:foreach Entry in=[ /ip/firewall/address-list/find where \
list=$FwListName comment=$ListComment ] do={
:local Address [ /ip/firewall/address-list/get $Entry address ];
:local Branch [ $GetBranch $Address ];
:local TimeOut ($IPv4Addresses->$Branch->$Address);
:if ([ :typeof $TimeOut ] = "time") do={
$LogPrintVerbose debug $ScriptName ("Renewing IPv4 address in list '" . $FwListName . \
"' with " . $TimeOut . ": " . $Address);
/ip/firewall/address-list/set $Entry timeout=$TimeOut;
:set ($IPv4Addresses->$Branch->$Address);
:set CntRenew ($CntRenew + 1);
} else={
:if ($Failure = false) do={
$LogPrintVerbose debug $ScriptName ("Removing IPv4 address from list '" . $FwListName . \
"': " . $Address);
/ip/firewall/address-list/remove $Entry;
:set CntRemove ($CntRemove + 1);
}
}
}
:foreach Entry in=[ /ipv6/firewall/address-list/find where \
list=$FwListName comment=$ListComment ] do={
:local Address [ /ipv6/firewall/address-list/get $Entry address ];
:local Branch [ $GetBranch $Address ];
:local TimeOut ($IPv6Addresses->$Branch->$Address);
:if ([ :typeof $TimeOut ] = "time") do={
$LogPrintVerbose debug $ScriptName ("Renewing IPv6 address in list '" . $FwListName . \
"' with " . $TimeOut . ": " . $Address);
/ipv6/firewall/address-list/set $Entry timeout=$TimeOut;
:set ($IPv6Addresses->$Branch->$Address);
:set CntRenew ($CntRenew + 1);
} else={
:if ($Failure = false) do={
$LogPrintVerbose debug $ScriptName ("Removing IPv6 address from list '" . $FwListName . \
"': " . $Address);
/ipv6/firewall/address-list/remove $Entry;
:set CntRemove ($CntRemove + 1);
}
}
}
:foreach BranchName,Branch in=$IPv4Addresses do={
$LogPrintVerbose debug $ScriptName ("Handling branch: " . $BranchName);
:foreach Address,Timeout in=$Branch do={
$LogPrintVerbose debug $ScriptName ("Adding IPv4 address to list '" . $FwListName . \
"' with " . $Timeout . ": " . $Address);
:do {
/ip/firewall/address-list/add list=$FwListName comment=$ListComment \
address=$Address timeout=$Timeout;
:set CntAdd ($CntAdd + 1);
} on-error={
$LogPrint warning $ScriptName ("Failed to add IPv4 address to list '" . $FwListName . \
"': " . $Address);
}
}
}
:foreach BranchName,Branch in=$IPv6Addresses do={
$LogPrintVerbose debug $ScriptName ("Handling branch: " . $BranchName);
:foreach Address,Timeout in=$Branch do={
$LogPrintVerbose debug $ScriptName ("Adding IPv6 address to list '" . $FwListName . \
"' with " . $Timeout . ": " . $Address);
:do {
/ipv6/firewall/address-list/add list=$FwListName comment=$ListComment \
address=$Address timeout=$Timeout;
:set CntAdd ($CntAdd + 1);
} on-error={
$LogPrint warning $ScriptName ("Failed to add IPv6 address to list '" . $FwListName . \
"': " . $Address);
}
}
}
$LogPrint info $ScriptName ("list: " . $FwListName . \
" (" . [ $HumanReadableNum ($CntAdd + $CntRenew) 1000 ] . ")" . \
" -- added: " . [ $HumanReadableNum $CntAdd 1000 ] . \
" - renewed: " . [ $HumanReadableNum $CntRenew 1000 ] . \
" - removed: " . [ $HumanReadableNum $CntRemove 1000 ]);
}
} on-error={
:global ExitError; $ExitError $ExitOK [ :jobname ];
}