Что нового

[RegExp] конвертор списков фильтров

damien2008

Осваивающий
Сообщения
178
Репутация
34
Версия AutoIT: 3+
Описание: безопасность ПК (Windows, Linux, etc.)
Примечания:
+ автоматизация для прокси;
+ bFilter бесплатен;
+ списки Ad Block Plus обновляются чаще;
+ можно конвертировать собранные из Firefox'а.
+ и т.д.

Добрый день.
Есть такая задача.

Для повышения безопасности при работе в сети ОС Linux || Windows $v || etc. и блокирования нежелательного трафика используется Bfilter.

В качестве списков блокирования используются листы Adblock Plus

Есть скрипт на Perl'e который конвертирует эти списки для Bfilter'а - adblock2bfilter.pl (прилагается под катом).
Помогите перевести скрипт на Autoit; простенький drag&drop с сохранением.

Если кому идея интересна, прошу помочь.

adblock2bfilter.pl
#!/usr/bin/perl

use strict; # to catch stupid errors

#
# BFilter directives for blocking and exempting Adblock Plus URL patterns
# (Strong and weak versions required, see comments below)
#
my $BLOCK_PREFIX_STRONG = "++++++";
my $BLOCK_PREFIX_WEAK = "+++";
my $EXEMPT_PREFIX_STRONG = "ALLOW";
my $EXEMPT_PREFIX_WEAK = "---";

#
# The main loop
#
while (defined(my $thisLine = <STDIN>))
{
print processLine(trim($thisLine));
}
exit;


####################################
# Subroutines
####################################

#
# Process a single line from STDIN
#
sub processLine()
{
# The incoming line has been stripped of leading and trailing
# white space and the terminating newline.
my ($line) = @_;

# Copy blank lines
if ($line =~ m/^$/)
{
return "\n";
}

# Handle comment and Adblock Plus header lines
if ($line =~ m/^[\[!]/)
{
return "# $line\n";;
}

# Handle unimplemented lines
# (These are Adblock Plus element-hiding patterns)
if ($line =~ m/#/)
{
return "# <Element> $line\n";
}

# See if this is an Exception pattern
# If so, strip "@@" from the front of the line.
my $isException = ($line =~ m/^@@/);
if ($isException)
{
$line =~ s/^@@//;
}

# Is the pattern already in regex form?
my $isRegEx = ($line =~ m/^\/.*\/$/);

# Used to construct the output line.
my $prefix;
my $pattern;

# We process regex patterns differently from others.
if ($isRegEx)
{
# Use the existing regex with an appropriate BFilter prefix.
$prefix = $isException ? $EXEMPT_PREFIX_STRONG : $BLOCK_PREFIX_STRONG;
$pattern = $line;
}
else
{
# If the pattern includes "filter options", which BFilter cannot support,
# strip them from the line and use a weak BFilter prefix.
my $isWeak = ($line =~ m/\$/);
if ($isWeak)
{
$line =~ s/\$.*//;
}

# Check to see whether the pattern is anchored at one or both ends
# if so, strip the anchor characters from the line.
my $anchoredStartDomain = ($line =~ m/^\|\|/);
my $anchoredStart = ($line =~ m/^\|/);
my $anchoredEnd = ($line =~ m/\|$/);
if ($anchoredStart || $anchoredEnd)
{
$line =~ s/\|//g;
}

# Remove redundant strings of wildcards
$line =~ s/\*+/\*/g;

# If the pattern contains a literal '?' or '^', we need to convert it
# to a regex because '?' is used as a wild card in BFilter and '^' needs to
# be processed as a set of alternative characters.
# Also, we will force domain-anchored patterns to be a RegEx
my $convertToRegEx = $anchoredStartDomain || ($line =~ m/\?|\^/);

# Catch unexpected '|' and treat the line as a comment.
my $invalidLine = ($line =~ m/\|/);

if ($convertToRegEx)
{
# We replace ? with \? in the output regex.
# We also need to escape certain other characters that
# have special meaning in a regex: . + \
$line =~ s/([\?\/\.\+])/\\\1/g;

# We replace ^ with a set of break characters
$line =~ s/\^/(?![A-Za-z0-9\\-.%])/g;

# Are there wildcards at the unanchored beginning and/or end
# of a pattern? If so, remove them now.
if (!$anchoredStart)
{
$line =~ s/^\*+//;
}
if (!$anchoredEnd)
{
$line =~ s/\*+$//;
}

# Translate * to .* in the regex.
$line =~ s/[\*]/\.\*/g;

# Build the regex version of the pattern.
$pattern = "/";
if ($anchoredStartDomain)
{
$pattern = $pattern . 'http(s)?://';
}
elsif (!$anchoredStart)
{
$pattern = $pattern . '.*';
}

$pattern = $pattern . $line;

if (!$anchoredEnd)
{
$pattern = $pattern . '.*';
}

$pattern = $pattern . "/";
}
else
{
# regex not required. Construct an output pattern with
# appropriate anchoring.
$pattern = "";

if (!$anchoredStart)
{
# Add a wildcard if there is not already one at the start.
if (!($line =~ m/^\*/))
{
$pattern = $pattern . "*";
}
}
$pattern = $pattern . $line;
if (!$anchoredEnd)
{
# Add a wildcard if there is not already one at the end.
if (!($line =~ m/\*$/))
{
$pattern = $pattern . "*";
}
}
}

# Use the approprite prefix - weak or strong.
if ($isException)
{
$prefix = $isWeak ? $EXEMPT_PREFIX_WEAK : $EXEMPT_PREFIX_STRONG;
}
else
{
$prefix = $isWeak ? $BLOCK_PREFIX_WEAK : $BLOCK_PREFIX_STRONG;
}

# Flag invalid lines
if ($invalidLine)
{
$prefix = "# <Invalid> " . $prefix;
}
}

return "$prefix $pattern\n";
}

#
# Remove the newine from the string then
# strip leading and trailing white space.
#
sub trim()
{
my ($string) = @_;

chomp $string;
$string =~ s/^\s+//;
$string =~ s/\s+$//;

return $string;
}

образцы

пример исходного фильтра:
.info/baner/
.ngs.ru/*.swf?pid
.ru/125x125.
.ru/adv/
||77.120.122.117^*/banners/
||77.91.228.66^$third-party
||78.140.145.178^$third-party
||80.93.49.192^$third-party
||80.93.56.187^$third-party
@@||liveinternet.ru/ad/banner$script
@@||novoteka.ru/show.cgi?adp$script,domain=novoteka.ru
###b_adv
###banner-zone-righ
###banner-zone-top
###ggogle_AD
yandex.ru###banner
yandex.ru##.b-banner
yandex.ru##.somebanner

пример конечного фильтра:
# Hover ads (DHTML popups) to block

FORBID /http(s)?://(www\.)?bin-layer.de/popup.*\.js/
FORBID /http(s)?://(www\.)?code-server\.biz/code/.*/
FORBID /http(s)?://(www\.)?effili.com/api/.*/
FORBID /http(s)?://(www\.)?view4cash\..*/.*/
FORBID /http(s)?://([^/]+\.)?(layer|sponsor)(-)?ad(s)?\..*/.*/
FORBID /http(s)?://([^/]+\.)?euros4click\..*/.*/
FORBID /http(s)?://engine\.adnet\.ru/.*/
FORBID */layer/layer.php*

# Tooltip ads to block

JS /http(s)?://kona\.kontera\.com/javascript/.*/
JS /http(s)?://autocontext\.begun\.ru/.*\.js/
JS /http(s)?://cls.assoc-amazon.com/.*/.*/
JS /http(s)?://ypn-js.overture.com/.*/.*/
JS *pagead*.googlesyndication.*/pagead/*
FORBID /http(s)?://[^/]+\.intellitxt\.com/intellitxt/.*/
FORBID /http(s)?://itxt\.vibrantmedia\.com/.*/
FORBID /http(s)?://ctxt.tribalfusion.com/.*/.*/

# Specific hostnames to block

AD *atwola.com/content/*
AD *google-analytics.*
AD *googleadservices.*
AD *rcm.amazon.com/*/*
AD *sourceforge.net/images/marketplace/*
AD /http(s)?://([^/]*\.)?ad(bot|brite|bureau|butler|center|central|centriconline|click(s)?|client|clix(2)?|code(s)?|complete|cont(roller|ent)|count(er)?|cycle|farm[0-9]?|img|image|juggler|legend|popup(s)?|s|serv(er(plus)?|ing)?)?\..*/
AD /http(s)?://([^/]*\.)?advert(ist|agent|ising|isement(s)?|power|serve|s)\..*/
AD /http(s)?://([^/]*\.)?banner(-)?(4all|box(es)?|community|connect|host|image|landia|exchange|master|mall|markt|power|server|swap|4cash|ad(s|bank)?|wizard|(-)?link|s)\..*/
AD /http(s)?://([^/]*\.)?engine\.ad(land|net|rorer)\.ru.*/
AD /http(s)?://([^/]*\.)?casal(e)?media\..*/
AD /http(s)?://([^/]*\.)?cash(4banner|4popup|4views|count|create|engines|fiesta|partner)\..*/
AD /http(s)?://([^/]*\.)?(c|k)licksor\..*/
AD /http(s)?://([^/]*\.)?double(-)?click\..*/.*/
AD /http(s)?://([^/]*\.)?etracker\.de/.*/
AD /http(s)?://([^/]*\.)?falkag\.(de|net)/.*/
AD /http(s)?://([^/]*\.)?hitbox(enterprise|wireless)\..*/.*/
AD /http(s)?://([^/]*\.)?ivwbox\.de/.*/
AD /http(s)?://([^/]*\.)?img.blogads.com/.*/.*/
AD /http(s)?://([^/]*\.)?link(\.link|4ads|buddies|exchange|positions|price|rain|refferal|shighway|swaper|synergy)\..*/
AD /http(s)?://([^/]*\.)?nedstat(basic)?\..*/
AD /http(s)?://([^/]*\.)?pages\.etology\.com.*/
AD /http(s)?://([^/]*\.)?spymagic\.com.*/
AD /http(s)?://([^/]*\.)?tradedoubler\.com.*/
AD /http(s)?://([^/]*\.)?uimserv\.net/.*/
AD /http(s)?://([^/]*\.)?yield(x|manager)\..*/

# Javascript to block

JS */oasconfig/*
JS */ar.atwola.com/file/*.js
JS /http(s)?://imagesrv.adition.com/js/.*\.js/
JS /http(s)?://js\.redtram\.com/.*/
JS /http(s)?://media\.funpic\.de/.*/
JS /http(s)?://www\.fortunecity\.com/js/.*/

# Common ad paths to block

AD /.*/(_)?banner(s)?/.*/
AD /.*/ad(image|popup|vertis(ing|ement)|s)?\..*/
AD /.*/(page)?ad(s|frame|v)?([1-9])?/.*/
AD /.*/reklam(e|a)/.*/
AD /.*/sponsor(s|en|ing|ad(s)?)?/.*/
AD /.*/werbung(en)?/.*/
AD /.*/(view)?ad(vertisement|sense)?(s)?/.*/

# Some hints

++++++ /http(s)?://ads[\d]*\..*/
+++++ /.*/(ad[sv]?|advert(isements?)?|banners?)[^a-z].*/
++++ /http(s)?://clk\..*/
++++ *banner*
+++ /.*(c|k)lick(s)?.*/
+++ /.*count(er|omat).*/
-6 /http://.*\.images.search.yahoo.com/search/images/.*/
-5 http://*.wikipedia.org/wiki/*
 

madmasles

Модератор
Глобальный модератор
Сообщения
7,790
Репутация
2,322
damien2008
Может лучше привести пример исходного файла-фильтра и конвертированного?

PS
Образцы фильтров можно выдрать еще из Ad Muncher`a.
 

Suppir

Продвинутый
Сообщения
967
Репутация
62
damien2008
приятель, здесь работы на полдня минимум ;)
 
Автор
damien2008

damien2008

Осваивающий
Сообщения
178
Репутация
34
помогите пожалуйста) если можете.
может и я вам чем-нибудь помогу
 

`p r o x y

«Улыбайтесь, господа!»
Команда форума
Глобальный модератор
Сообщения
596
Репутация
157
Что-то не пойму закономерность.
В примере исходного фильтра есть строчка:
А где yandex в примере конечного фильтра?

А в целом, задача для стандартных функций AutoIt. Поэтому перевести скрипт с Perl будет легко. Другое дело не все знают Perl. Например, для меня загадка, как интерпретировать эту строчку:
Код:
my ($line) = @_;

+ Это раздел для поиска решений по коду. А темы аля "сделайте все за меня" в разделе Стол заказов.
 
Автор
damien2008

damien2008

Осваивающий
Сообщения
178
Репутация
34
я думаю многим пригодится эта тема по ряду причин.

- bFilter бесплатен;
- списки Ad Block Plus обновляются чаще;
+ можно конвертировать собранные из Firefox'а.
можно вообще выделить в отдельный раздел.

+ и т.д.

PS исправил.
 

kaster

Мой Аватар, он лучший самый
Команда форума
Глобальный модератор
Сообщения
4,020
Репутация
626
damien2008
никто не спорит. просто есть несколько негласных правил, по которым темы классифицируются как относящиеся к тому или иному разделу форума.
если у тебя есть заготовки, и ты хочешь чтобы тебе помогли или направили, то это раздел для новичков
если у тебя нет ничего (именно на AutoIt) и ты просишь чтобы тебе это сделали, то это раздел заказов. все просто
от этого тема не потеряет своей полезности.
PS: оформи шапку
 
Верх