kevinds

Broken Address-List Updater

Nov 19th, 2026 (edited)
197
0
176 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.93 KB | None | 0 0
  1. {
  2. /ip firewall address-list
  3. :local update do={
  4. :put "Starting import of address-list: $listname"
  5. :if ($nolog = null) do={:log warning "Starting import of address-list: $listname"}
  6.  
  7. :local maxretry 3
  8. :local retrywaitingtime 120s
  9. :local retryflag true
  10. :for retry from=1 to=$maxretry step=1 do={
  11. :if (retryflag) do={ :set $retryflag false; :set $sounter 0
  12. :if (retry > 1) do={
  13. :put "Source file changed. Retring after a $retrywaitingtime wait..."
  14. :if ($nolog = null) do={:log warning "Source file changed. Retring after a $retrywaitingtime wait..."}
  15. :delay $retrywaitingtime }
  16.  
  17. :local filesize ([/tool fetch url=$url src-address=$IP user=$user password=$password keep-result=no as-value]->"total")
  18. :local start 0
  19. :local maxsize 64000; # reqeusted chunk size
  20. :local end ($maxsize - 1); # because start is zero the maxsize has to be reduced by one
  21. :local partnumber ($filesize / ($maxsize / 1024)); # how many chunk are maxsize
  22. :local remainder ($filesize % ($maxsize / 1024)); # the last partly chunk
  23. :if ($remainder > 0) do={ :set $partnumber ($partnumber + 1) }; # total number of chunks
  24. :if ($heirule != null) do={:put "Using as extra filtering: $heirule"} else={:set $heirule "."}
  25. # remove the current list completely if "erase" is not present (default setting)
  26. :if ($noerase = null) do={
  27. :if ($timeout = null) do={:set $timeout 00:00:00; :do {:foreach i in=[/ip firewall address-list find list=$listname] do={/ip firewall address-list set list=("backup".$listname) $i }} on-error={} } else={
  28. :do {:foreach i in=[/ip firewall address-list find list=$listname dynamic] do={/ip firewall address-list set list=("backup".$listname) $i }} on-error={} };
  29. :put ("Conditional deleting all".$dynamic." entries in address-list: $listname")
  30. :if ($nolog = null) do={:log warning ("Conditional deleting all".$dynamic." entries in address-list: $listname")}
  31. } else={:put "Entries not conditional deleted in address-list: $listname"}; # ENDIF ERASE
  32. :for x from=1 to=$partnumber step=1 do={
  33. # get filesize to be compared to the orignal one and if changed then retry
  34. :local comparesize ([/tool fetch url=$url src-address=$IP user=$user password=$password keep-result=no as-value]->"total")
  35.  
  36. #:set $comparesize 5
  37.  
  38. # fetching the chunks from the webserver when the size of the source file has not changed
  39. # empty array when the source file changed. No processing is done till the next complete retry
  40. :if ($comparesize = $filesize) do={:set $data ([:tool fetch url=$url src-address=$IP user=$user password=$password http-header-field="Range: bytes=$start-$end" output=user as-value]->"data")} else={:set $data [:toarray ""]; :set $retryflag true}
  41. #:if ($ownposix = null) do={
  42. # determining the used delimiter in the list if not provided in the config
  43. # this only run once and so the impact on the import time is low
  44. :local ipv4Posix "^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}"
  45. :local ipv4rangePosix "^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}/[0-9]{1,2}"
  46. :local domainPosix "^.+\\.[a-z.]{2,7}"
  47. :local sdata $data;
  48. :while ([:len $sdata]!=0 && $delimiter = null) do={ # The check on length of $sdata is for if no delimiter is found.
  49. :local sline [:pick $sdata 0 [:find $sdata "\n"]]; :local slen [:len $sline];
  50. # set posix depending of type of data used in the list
  51. :if ($sline ~ $ipv4Posix) do={:set $posix $ipv4Posix; :set $iden "List identified as a IPv4 list"}
  52. :if ($sline ~ $ipv4rangePosix) do={:set $posix $ipv4rangePosix; :set $iden "List identified as a IPv4 with ranges list"}
  53. :if ($sline ~ $domainPosix) do={:set $posix $domainPosix; :set $iden "List identified as a domain list"}
  54. :if ($sline ~ $posix) do={:put $iden}
  55. :if ($sline ~ $posix) do={ # only explore the line if there is match at the start of the line.
  56. :do {:if ([:pick $sline 0 ($slen-$send)] ~ ($posix."\$")|| $send > $slen) do={:set $delimiter [:pick $sline ($slen-$send) ($slen-($send-1))]; :set $result true} else={:set $send ($send+1);} } while (!$result);
  57. }; #IF posix
  58. :set $sdata [:pick $sdata ([:find $sdata "\n"]+1) [:len $sdata]];
  59. :if ($delimiter != null) do={:local sdata [:toarray ""]}; #Clear array sdata and it is not needed anymore and triggering so the While to end
  60. }; #WHILE END $sdata
  61. :local sdata [:toarray ""]
  62. #} else={:put "User defind Posix: $ownposix"; :set $posix $ownposix } ; # ENDIF ownposix = null
  63. :if ($posix = null && $delimiter != null) do={:set $posix "."; :put "Using config-line defined delimiter: \"$delimiter\""}; # delimter provided by config line
  64. :if (!retryflag) do={:put "Reading Part: $x $start - $end"}
  65. :if ($timeout = null) do={:local timeout 00:00:00}; # if no timeout is defined make it a static entry.
  66. # Only remove the first line only if you are not at the start of list
  67.  
  68. :if ($start > 0) do={:set $data [:pick $data ([:find $data "\n"]+1) [:len $data]]}
  69. :while ([:len $data]!=0) do={
  70. :local line [:pick $data 0 [:find $data "\n"]]; # create only once and checked twice as local variable
  71. :if ( $line ~ $posix && $line~heirule) do={
  72. :do {add list=$listname address=[:pick $data 0 [:find $data $delimiter]] comment=$comment timeout=$timeout; :set $counter ($counter + 1)} on-error={}; # on error avoids any panics
  73. }; # if IP address && extra filter if present
  74. :set $data [:pick $data ([:find $data "\n"]+1) [:len $data]]; # removes the just added IP from the data array
  75. # Cut of the end of the chunks by removing the last lines...very dirty but it works
  76. :if ([:len $data] < 256) do={:set $data [:toarray ""]}
  77. }; # while
  78.  
  79. :set $start (($start-512) + $maxsize); # shifts the subquential start back by 512
  80. :set $end (($end-512) + $maxsize); # shift the subquential ends back by 512 to keep the
  81. }; # if retryflag
  82. }; #do for x
  83.  
  84. }; # for retry
  85. :if ($counter < 1) do={:set $resultline "Import was NOT successfull! Check if the list $listname is still being maintained."} else={:set $resultline "Completed reading $counter items into address-list $listname." }
  86. :put $resultline
  87. :if ($nolog = null) do={:log warning $resultline }
  88. :if ($counter > 0) do={:do {/ip firewall address-list remove [find where list=("backup".$listname)]} on-error={} } else={
  89. :do {:foreach i in=[/ip firewall address-list find list=("backup".$listname)] do={/ip firewall address-list set list=$listname $i }} on-error={}
  90. :put "Restoring backup list: $listname"
  91. :if ($nolog = null) do={:log warning "Restoring backup list: $listname"}
  92. }; # if counter restore on failure and remove on success
  93. }; # do
  94. :global currentIP
  95. $update url=https://binarydefense.com/banlist.txt IP=$currentIP listname=ATIF user="anonymous" password="anonymous" timeout=7d noerase=1 delimiter=("\n")
  96. $update url="https://lists.blocklist.de/lists/all.txt" IP=$currentIP listname=blocklist-de user="anonymous" password="anonymous" timeout=7d noerase=1
  97. }
  98.  
  99. # $update configuration
  100. # url= https://name.of.the.list
  101. # listname= name of address-list
  102. # IP=$currentIP Leave in place. Script didn't work when src-address=$currentIP was used above
  103. # user= http basic authentication username (use "anonymous" if not needed)
  104. # password= http basic authentication password (use "anonymous" if not needed)
  105.  
  106. # Optinal settings
  107. # timeout= the time the entry should be active. If omitted then static entries are created.
  108. # comment= puts this comment on every line in the choosen address-list (default: no comment)
  109. # heirule= this will select on a word on each line if to import or not (default: no heirule)
  110. # noerase= any value, then the current list is not erased (default: erase)
  111. # nolog= any value, then don't write to the log (default: writing to log)
  112. # delimiter= which characters separate addresses if not automatically detected.
  113.  
Advertisement
Add Comment
Please, Sign In to add comment