2023-05-31 08:01:38 +00:00
|
|
|
#!rsc by RouterOS
|
|
|
|
# RouterOS script: fw-addr-lists
|
|
|
|
# Copyright (c) 2023 Christian Hesse <mail@eworm.de>
|
|
|
|
# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md
|
|
|
|
#
|
|
|
|
# download, import and update firewall address-lists
|
|
|
|
# https://git.eworm.de/cgit/routeros-scripts/about/doc/fw-addr-lists.md
|
|
|
|
|
|
|
|
:local 0 "fw-addr-lists";
|
|
|
|
:global GlobalFunctionsReady;
|
|
|
|
:while ($GlobalFunctionsReady != true) do={ :delay 500ms; }
|
|
|
|
|
|
|
|
:global FwAddrLists;
|
|
|
|
:global FwAddrListTimeOut;
|
|
|
|
|
|
|
|
:global CertificateAvailable;
|
2023-11-30 12:18:15 +00:00
|
|
|
:global EitherOr;
|
2023-05-31 08:01:38 +00:00
|
|
|
:global LogPrintExit2;
|
2023-12-13 14:57:28 +00:00
|
|
|
:global LogPrintOnce;
|
2023-05-31 08:01:38 +00:00
|
|
|
:global ScriptLock;
|
|
|
|
:global WaitFullyConnected;
|
|
|
|
|
|
|
|
:local FindDelim do={
|
2023-06-12 07:14:36 +00:00
|
|
|
:local ValidChars "0123456789./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-";
|
2023-05-31 08:01:38 +00:00
|
|
|
:for I from=0 to=[ :len $1 ] do={
|
|
|
|
:if ([ :typeof [ :find $ValidChars [ :pick ($1 . " ") $I ] ] ] != "num") do={
|
|
|
|
:return $I;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$ScriptLock $0;
|
|
|
|
$WaitFullyConnected;
|
|
|
|
|
|
|
|
:local ListComment ("managed by " . $0);
|
|
|
|
|
|
|
|
:foreach FwListName,FwList in=$FwAddrLists do={
|
|
|
|
:local Addresses ({});
|
|
|
|
:local CntAdd 0;
|
|
|
|
:local CntRenew 0;
|
|
|
|
:local CntRemove 0;
|
|
|
|
:local Failure false;
|
|
|
|
|
|
|
|
:foreach List in=$FwList do={
|
|
|
|
:local CheckCertificate "no";
|
2023-06-23 07:35:00 +00:00
|
|
|
:local Data false;
|
2023-11-30 12:18:15 +00:00
|
|
|
:local TimeOut [ $EitherOr [ :totime ($List->"timeout") ] $FwAddrListTimeOut ];
|
2023-05-31 08:01:38 +00:00
|
|
|
|
|
|
|
:if ([ :len ($List->"cert") ] > 0) do={
|
|
|
|
:set CheckCertificate "yes-without-crl";
|
|
|
|
:if ([ $CertificateAvailable ($List->"cert") ] = false) do={
|
|
|
|
$LogPrintExit2 warning $0 ("Downloading required certificate failed, trying anyway.") false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 19:15:23 +00:00
|
|
|
:for I from=1 to=4 do={
|
2023-06-23 07:35:00 +00:00
|
|
|
:if ($Data = false) do={
|
|
|
|
:do {
|
2023-10-19 19:15:23 +00:00
|
|
|
:set Data ([ /tool/fetch check-certificate=$CheckCertificate output=user \
|
|
|
|
($List->"url") as-value ]->"data");
|
2023-06-23 07:35:00 +00:00
|
|
|
} on-error={
|
2023-10-19 19:15:23 +00:00
|
|
|
:if ($I < 4) do={
|
|
|
|
$LogPrintExit2 debug $0 ("Failed downloading, " . $I . ". try: " . $List->"url") false;
|
|
|
|
:delay (($I * $I) . "s");
|
|
|
|
}
|
2023-06-23 07:35:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
:if ($Data = false) do={
|
|
|
|
:set Data "";
|
2023-05-31 08:01:38 +00:00
|
|
|
:set Failure true;
|
|
|
|
$LogPrintExit2 warning $0 ("Failed downloading list from: " . $List->"url") false;
|
|
|
|
}
|
|
|
|
|
2023-12-13 14:57:28 +00:00
|
|
|
:if ([ :len $Data ] > 63000) do={
|
|
|
|
$LogPrintOnce warning $0 ("The list is huge and may be truncated: " . $List->"url");
|
|
|
|
}
|
|
|
|
|
2023-05-31 08:01:38 +00:00
|
|
|
:while ([ :len $Data ] != 0) do={
|
|
|
|
:local Line [ :pick $Data 0 [ :find $Data "\n" ] ];
|
|
|
|
:local Address ([ :pick $Line 0 [ $FindDelim $Line ] ] . ($List->"cidr"));
|
2023-06-12 07:14:36 +00:00
|
|
|
:if ($Address ~ "^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}(/[0-9]{1,2})?\$" || \
|
|
|
|
$Address ~ "^[\\.a-zA-Z0-9-]+\\.[a-zA-Z]{2,}\$") do={
|
2023-11-30 12:18:15 +00:00
|
|
|
:set ($Addresses->$Address) $TimeOut;
|
2023-05-31 08:01:38 +00:00
|
|
|
}
|
|
|
|
:set Data [ :pick $Data ([ :len $Line ] + 1) [ :len $Data ] ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
:foreach Entry in=[ /ip/firewall/address-list/find where list=$FwListName comment=$ListComment ] do={
|
|
|
|
:local Address [ /ip/firewall/address-list/get $Entry address ];
|
2023-11-30 12:18:15 +00:00
|
|
|
:if ([ :typeof ($Addresses->$Address) ] = "time") do={
|
|
|
|
$LogPrintExit2 debug $0 ("Renewing for " . ($Addresses->$Address) . ": " . $Address) false;
|
|
|
|
/ip/firewall/address-list/set $Entry timeout=($Addresses->$Address);
|
2023-05-31 08:01:38 +00:00
|
|
|
:set ($Addresses->$Address);
|
|
|
|
:set CntRenew ($CntRenew + 1);
|
|
|
|
} else={
|
|
|
|
:if ($Failure = false) do={
|
|
|
|
$LogPrintExit2 debug $0 ("Removing: " . $Address) false;
|
|
|
|
/ip/firewall/address-list/remove $Entry;
|
|
|
|
:set CntRemove ($CntRemove + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
:foreach Address,Ignore in=$Addresses do={
|
2023-11-30 12:18:15 +00:00
|
|
|
$LogPrintExit2 debug $0 ("Adding for " . ($Addresses->$Address) . ": " . $Address) false;
|
2023-05-31 08:01:38 +00:00
|
|
|
:do {
|
2023-11-30 12:18:15 +00:00
|
|
|
/ip/firewall/address-list/add list=$FwListName comment=$ListComment address=$Address timeout=($Addresses->$Address);
|
2023-05-31 08:01:38 +00:00
|
|
|
:set ($Addresses->$Address);
|
|
|
|
:set CntAdd ($CntAdd + 1);
|
|
|
|
} on-error={
|
|
|
|
$LogPrintExit2 warning $0 ("Failed to add address " . $Address . " to list '" . $FwListName . "'.") false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-06 09:35:02 +00:00
|
|
|
$LogPrintExit2 info $0 ("list: " . $FwListName . " -- added: " . $CntAdd . " - renewed: " . $CntRenew . " - removed: " . $CntRemove) false;
|
2023-05-31 08:01:38 +00:00
|
|
|
}
|