eworm-de.routeros-scripts/fw-addr-lists.rsc

221 lines
8.3 KiB
Text
Raw Normal View History

2023-05-31 10:01:38 +02:00
#!rsc by RouterOS
# RouterOS script: fw-addr-lists
2025-01-02 00:04:06 +01:00
# Copyright (c) 2023-2025 Christian Hesse <mail@eworm.de>
# https://rsc.eworm.de/COPYING.md
2023-05-31 10:01:38 +02:00
#
# requires RouterOS, version=7.16
#
2023-05-31 10:01:38 +02:00
# download, import and update firewall address-lists
# https://rsc.eworm.de/doc/fw-addr-lists.md
2023-05-31 10:01:38 +02:00
:local ExitOK false;
:onerror Err {
:global GlobalConfigReady; :global GlobalFunctionsReady;
:retry { :if ($GlobalConfigReady != true || $GlobalFunctionsReady != true) \
do={ :error ("Global config and/or functions not ready."); }; } delay=500ms max=50;
:local ScriptName [ :jobname ];
2024-03-04 13:48:01 +01:00
:global FwAddrLists;
:global FwAddrListTimeOut;
:global CertificateAvailable;
:global EitherOr;
2024-03-27 23:39:55 +01:00
:global FetchHuge;
:global HumanReadableNum;
2024-03-08 12:45:38 +01:00
:global LogPrint;
2024-03-04 13:48:01 +01:00
:global LogPrintOnce;
:global LogPrintVerbose;
2024-03-04 13:48:01 +01:00
: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;
}
2023-05-31 10:01:38 +02:00
}
}
: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;
}
2024-03-04 13:48:01 +01:00
$WaitFullyConnected;
2023-05-31 10:01:38 +02:00
:if ([ :len [ /log/find where topics=({"script"; "warning"}) \
message=("\$LogPrintOnce: The message is already in log, scripting subsystem may have crashed before!") ] ] > 0) do={
$LogPrintOnce warning $ScriptName ("Scripting subsystem may have crashed, possibly caused by us. Delaying!");
:delay 5m;
}
2024-03-04 13:48:01 +01:00
:local ListComment ("managed by " . $ScriptName);
2023-05-31 10:01:38 +02:00
2024-03-04 13:48:01 +01:00
:foreach FwListName,FwList in=$FwAddrLists do={
:local CntAdd 0;
:local CntRenew 0;
:local CntRemove 0;
:local IPv4Addresses ({});
:local IPv6Addresses ({});
:local Failure false;
2023-05-31 10:01:38 +02:00
2024-03-04 13:48:01 +01:00
:foreach List in=$FwList do={
2024-03-27 23:39:55 +01:00
:local CheckCertificate false;
2024-03-04 13:48:01 +01:00
:local Data false;
:local TimeOut [ $EitherOr [ :totime ($List->"timeout") ] $FwAddrListTimeOut ];
2023-05-31 10:01:38 +02:00
2024-03-04 13:48:01 +01:00
:if ([ :len ($List->"cert") ] > 0) do={
2024-03-27 23:39:55 +01:00
:set CheckCertificate true;
2024-03-04 13:48:01 +01:00
:if ([ $CertificateAvailable ($List->"cert") ] = false) do={
$LogPrint warning $ScriptName ("Downloading required certificate (" . $FwListName . \
" / " . $List->"url" . ") failed, trying anyway.");
2024-03-04 13:48:01 +01:00
}
2023-05-31 10:01:38 +02:00
}
:for I from=1 to=5 do={
2024-03-04 13:48:01 +01:00
:if ($Data = false) do={
:set Data [ :tolf [ $FetchHuge $ScriptName ($List->"url") $CheckCertificate ] ];
2024-03-27 23:39:55 +01:00
:if ($Data = false) do={
:if ($I < 5) do={
$LogPrint debug $ScriptName ("Failed downloading for list '" . $FwListName . \
"', " . $I . ". try from: " . $List->"url");
2024-03-04 13:48:01 +01:00
:delay (($I * $I) . "s");
}
}
}
}
2024-03-04 13:48:01 +01:00
: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");
2024-03-04 13:48:01 +01:00
}
: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 ([ :typeof [ :find $Address "/" ] ] = "nil") do={
:set Address ($Address . "/128");
}
: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={ }
2024-02-23 11:19:56 +01:00
}
2023-05-31 10:01:38 +02:00
}
2024-04-05 23:53:05 +02:00
:foreach Entry in=[ /ip/firewall/address-list/find where \
list=$FwListName comment=$ListComment ] do={
2024-03-04 13:48:01 +01:00
: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 " . $Address . \
" in list '" . $FwListName . "' with " . $TimeOut . ".");
/ip/firewall/address-list/set $Entry timeout=$TimeOut;
:set ($IPv4Addresses->$Branch->$Address);
2024-03-04 13:48:01 +01:00
:set CntRenew ($CntRenew + 1);
} else={
:if ($Failure = false) do={
$LogPrintVerbose debug $ScriptName ("Removing IPv4 address " . $Address . \
" from list '" . $FwListName . ".");
2024-03-04 13:48:01 +01:00
/ip/firewall/address-list/remove $Entry;
:set CntRemove ($CntRemove + 1);
}
2023-05-31 10:01:38 +02:00
}
}
2024-04-05 23:53:05 +02:00
:foreach Entry in=[ /ipv6/firewall/address-list/find where \
list=$FwListName comment=$ListComment ] do={
2024-03-04 13:48:01 +01:00
: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 " . $Address . \
" in list '" . $FwListName . "' with " . $TimeOut . ".");
/ipv6/firewall/address-list/set $Entry timeout=$TimeOut;
:set ($IPv6Addresses->$Branch->$Address);
2024-03-04 13:48:01 +01:00
:set CntRenew ($CntRenew + 1);
} else={
:if ($Failure = false) do={
$LogPrintVerbose debug $ScriptName ("Removing IPv6 address " . $Address . \
" from list '" . $FwListName .".");
2024-03-04 13:48:01 +01:00
/ipv6/firewall/address-list/remove $Entry;
:set CntRemove ($CntRemove + 1);
}
2024-02-23 11:19:56 +01:00
}
}
:foreach BranchName,Branch in=$IPv4Addresses do={
$LogPrintVerbose debug $ScriptName ("Handling branch: " . $BranchName);
:foreach Address,Timeout in=$Branch do={
$LogPrintVerbose debug $ScriptName ("Adding IPv4 address " . $Address . \
" to list '" . $FwListName . "' with " . $Timeout . ".");
:onerror Err {
/ip/firewall/address-list/add list=$FwListName comment=$ListComment \
address=$Address timeout=$Timeout;
:set CntAdd ($CntAdd + 1);
} do={
$LogPrint warning $ScriptName ("Failed to add IPv4 address " . $Address . \
" to list '" . $FwListName . "': " . $Err);
}
2024-03-04 13:48:01 +01:00
}
2023-05-31 10:01:38 +02:00
}
:foreach BranchName,Branch in=$IPv6Addresses do={
$LogPrintVerbose debug $ScriptName ("Handling branch: " . $BranchName);
:foreach Address,Timeout in=$Branch do={
$LogPrintVerbose debug $ScriptName ("Adding IPv6 address " . $Address . \
" to list '" . $FwListName . "' with " . $Timeout . ".");
:onerror Err {
/ipv6/firewall/address-list/add list=$FwListName comment=$ListComment \
address=$Address timeout=$Timeout;
:set CntAdd ($CntAdd + 1);
} do={
$LogPrint warning $ScriptName ("Failed to add IPv6 address " . $Address . \
" to list '" . $FwListName . "': " . $Err);
}
2024-03-04 13:48:01 +01:00
}
2024-02-23 11:19:56 +01:00
}
$LogPrint info $ScriptName ("list: " . $FwListName . \
" (" . [ $HumanReadableNum ($CntAdd + $CntRenew) 1000 ] . ")" . \
" -- added: " . [ $HumanReadableNum $CntAdd 1000 ] . \
" - renewed: " . [ $HumanReadableNum $CntRenew 1000 ] . \
" - removed: " . [ $HumanReadableNum $CntRemove 1000 ]);
2024-03-04 13:48:01 +01:00
}
} do={
:global ExitError; $ExitError $ExitOK [ :jobname ] $Err;
}