Skip to content

Instantly share code, notes, and snippets.

@Pliner
Last active January 8, 2024 14:51
  • Star 29 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Pliner/eb368a0836a11a49ae99bc29c195ad0d to your computer and use it in GitHub Desktop.
Mikrotik antifilter.download update script
:do {
:do {
/file remove "/ipsum.rsc";
/file remove "/subnet.rsc";
} on-error={}
:put "Downloading ipsum.rsc...";
:do {
/tool fetch url="https://antifilter.download/list/ipsum.rsc" dst-path="/ipsum.rsc"
} on-error={
:put "Error. Download failed";
}
:put "Downloading subnet.rsc...";
:do {
/tool fetch url="https://antifilter.download/list/subnet.rsc" dst-path="/subnet.rsc"
} on-error={
:put "Error. Download failed";
}
/ip firewall address-list remove [/ip firewall address-list find list=rkn]
:put "Importing ipsum.rsc...";
:do {
/import "/ipsum.rsc";
} on-error={
:put "import failed. unknown error.";
}
:put "Importing subnet.rsc...";
:do {
/import "/subnet.rsc";
} on-error={
:put "import failed. unknown error.";
}
:put "Update Complete.";
}
@ailinykh
Copy link

ailinykh commented Oct 2, 2021

:do {
    :do {
        /file remove "/allyouneed.rsc";
    } on-error={}

    :put "Downloading allyouneed.rsc...";
    :do {
	/tool fetch url="https://antifilter.download/list/allyouneed.rsc" dst-path="/allyouneed.rsc"
    } on-error={
        :put "Error. Download failed";
    }
	
    /ip firewall address-list remove [/ip firewall address-list find list=rkn]	
	
    :put "Importing allyouneed.rsc...";
    :do {
        /import "/allyouneed.rsc";
    } on-error={
        :put "import failed. unknown error.";
    }

    :put "Update Complete.";
}

@KPEBETKA
Copy link

Более безопасный вариант

:do {

    :local retryflag true;
    :local maxretry 3;
    :local delay 120s;
    :local url "https://antifilter.download/list/allyouneed.lst";
    :local listname "list-antifilter";

    :for retry from=1 to=$maxretry step=1 do={

        :if (retryflag) do={

            :set $retryflag false;
            :set $counter 0;

            :if (retry > 1) do={
                :delay $delay;
            };

            :do {
                /ip firewall address-list remove [find where list=($listname."-updated")];
            } on-error={};

            :do {
                /ip firewall address-list add list=($listname."-updated") address=antifilter.download comment="antifilter.download";
            } on-error={};

            :local filesize ([/tool fetch url=$url keep-result=no as-value]->"total");
            :local chunksize 64000;
            :local start 0;
            :local end ($chunksize - 1);
            :local chunks ($filesize / ($chunksize / 1024));
            :local lastchunk ($filesize % ($chunksize / 1024));

            :if ($lastchunk > 0) do={
                :set $chunks ($chunks + 1);
            };

            :for chunk from=1 to=$chunks step=1 do={

                :local comparesize ([/tool fetch url=$url keep-result=no as-value]->"total");

                :if ($comparesize = $filesize) do={
                    :set $data ([:tool fetch url=$url http-header-field="Range: bytes=$start-$end" output=user as-value]->"data");
                } else={
                    :set $data [:toarray ""];
                    :set $retryflag true;
                };

                :local regexp "^((25[0-5]|(2[0-4]|[01]?[0-9]?)[0-9])\\.){3}(25[0-5]|(2[0-4]|[01]?[0-9]?)[0-9])(\\/(3[0-2]|[0-2]?[0-9])){0,1}\$";

                :if ($start > 0) do={
                    :set $data [:pick $data ([:find $data "\n"]+1) [:len $data]];
                };
                
                :while ([:len $data]!=0) do={
                    :local line [:pick $data 0 [:find $data "\n"]];

                    :if ( $line ~ $regexp ) do={    
                        :do {
                            /ip firewall address-list add list=($listname."-updated") address=$line;
                            :set $counter ($counter + 1);
                        } on-error={};        
                    };

                    :set $data [:pick $data ([:find $data "\n"]+1) [:len $data]];

                    :if ([:len $data] < 256) do={
                        :set $data [:toarray ""];
                    };
                };

                :set $start (($start-512) + $chunksize); 
                :set $end (($end-512) + $chunksize); 
            
            };
        
        };

    };

    :if ($counter > 0) do={
        :do {
            /ip firewall address-list remove [find where list=$listname];
        } on-error={};

        :do {
            :foreach address in=[/ip firewall address-list find list=($listname."-updated")] do={
                :do {
                    /ip firewall address-list set list=$listname $address;
                } on-error={};
            };
        } on-error={};
    };

} on-error={};

Идея позаимствована тут

@mmaxs
Copy link

mmaxs commented Nov 13, 2021

@KPEBETKA, не плохо.

@To-Azamat
Copy link

:foreach address in=[/ip firewall address-list find list=($listname."-updated")] do={

Don't you miss a WHERE argument here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment