Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Created by Dmitriy Mozgovoy on 24.05.2017.
- :local maxEntriesCount 1024;
- :local listName "UA-BLACKLIST"
- :local tempFile "ips.txt"
- :local apiPath ""; #https://uablacklist.net/ips.txt
- :if ( $apiPath!="") do={
- /log info "fetching UA blacklist registry ($apiPath)...";
- /tool fetch url=$apiPath dst-path=$tempFile
- }
- :local counter 0;
- :local ipList [:toarray ""];
- :local added [:toarray ""];
- :local removed [:toarray ""];
- :local lineEnd 0;
- :local line "";
- :local lastEnd 0;
- :if ( [/file get [/file find name=$tempFile] size] > 0 ) do={
- :put "Analyzing...";
- /log info "Analyzing data in [$tempFile]..."
- :local rule [/ip firewall filter find dst-address-list=$listName]
- :if ([:len $rule]=0) do={
- /log info "firewall rule not found. Adding...";
- /ip firewall filter add chain=forward dst-address-list="$listName" comment="rule for $listName" action=reject reject-with=icmp-host-unreachable place-before=0;
- }
- :local content [/file get [/file find name=$tempFile] contents] ;
- :local contentLen [ :len $content ] ;
- :do {
- :set $counter (counter+1);
- :set lineEnd [:find $content "\n" $lastEnd ] ;
- :if ($lineEnd < $lastEnd) do={
- :set $lineEnd $contentLen;
- }
- :set line [:pick $content $lastEnd $lineEnd] ;
- :set lastEnd ( $lineEnd + 1 ) ;
- :if ( [:len $line] > 0 ) do={
- :set $ipList ($ipList, $line);
- :if ( [:len [/ip firewall address-list find address=$line list=$listName]] =0 ) do={
- /ip firewall address-list add list="$listName" address="$line";
- :set $added ($added, $line);
- /log warning "New address ($line) added to $listName";
- } else={
- /log warning "Rule for address ($line) already exists in $listName";
- }
- }
- } while ($lineEnd < $contentLen && $counter < $maxEntriesCount )
- }
- /ip firewall address-list
- :foreach x in=[find list=$listName] do={
- :local addr [get $x address];
- :local found false;
- :foreach ip in=$ipList do={
- :if ( $ip=addr ) do={
- :set found true;
- }
- }
- :if ( !found ) do={
- :set $removed ($removed, $addr);
- /ip firewall address-list remove $x;
- /log warning "Remove IP ($addr) from $listName";
- }
- }
- :local newCount [:len $added];
- :local removedCount [:len $removed];
- :local body "";
- :if ( $newCount>0 || $removedCount>0 ) do={
- :local index 0;
- :if ( $newCount > 0 ) do={
- :set $body ("Added new address to $listName (count: " . $newCount . ")\n [");
- :foreach ip in=$added do={
- :set $index ( $index+1 );
- :set $body ($body . " $ip ");
- :if ( $index < $newCount ) do={
- :set $body ($body. ",");
- }
- }
- :set $body ($body . "]");
- }
- :set $index 0;
- :if ( $removedCount > 0 ) do={
- :set $body ($body . "\nRemoved address from $listName (count: " . $removedCount . ")\n [");
- :foreach ip in=$removed do={
- :set $index ( $index+1 );
- :set $body ($body . " $ip ");
- :if ( $index < $removedCount ) do={
- :set $body ($body. ",");
- }
- }
- :set $body ($body . "]");
- }
- /log warning ("$listName has been updated. $body");
- :if ($destMail!="") do {
- /tool e-mail send to=$destMail subject=("[" .[/system identity get name] . "] The $listName has been synchronized with registry in $tempFile ($apiPath). Found some changes") body=$body;
- /log info "Email has been sent to $destMail";
- }
- :beep frequency=600 length=200ms;
- } else={
- /log info ("$listName has been synchronized. No changes were found");
- }
- :put "Done.";
Advertisement