From: zapata Date: Wed, 15 Aug 2007 22:25:17 +0000 (+0000) Subject: user agent filter now works with substrings X-Git-Url: http://erislabs.net/gitweb/?p=mir.git;a=commitdiff_plain;h=4a89797fdd52882925b072123719c21fc5b370f3 user agent filter now works with substrings --- diff --git a/source/mircoders/abuse/FilterEngine.java b/source/mircoders/abuse/FilterEngine.java index 73da8513..f21885c7 100755 --- a/source/mircoders/abuse/FilterEngine.java +++ b/source/mircoders/abuse/FilterEngine.java @@ -154,7 +154,8 @@ public class FilterEngine { Filter filter = new Filter(entity); introduceFilter(filter); } - catch (AbuseExc e) { + catch (Throwable e) { + logger.debug("Misbehaving filer: " + entity.toString() + ": " + e); } } } @@ -264,7 +265,7 @@ public class FilterEngine { int lastPriority = Integer.parseInt(lastPriorityString); priority = Integer.toString(lastPriority + 1); } - catch (Exception e) { + catch (Throwable e) { } } diff --git a/source/mircoders/abuse/RegularExpressionFilterType.java b/source/mircoders/abuse/RegularExpressionFilterType.java index 8eceafa4..c62abbef 100755 --- a/source/mircoders/abuse/RegularExpressionFilterType.java +++ b/source/mircoders/abuse/RegularExpressionFilterType.java @@ -28,7 +28,6 @@ package mircoders.abuse; -import gnu.regexp.RE; import mir.entity.Entity; import mir.session.Request; @@ -36,6 +35,8 @@ import java.util.Arrays; import java.util.Iterator; import java.util.List; +import org.apache.oro.text.regex.*; + /** * A description of a regular expression filter. @@ -75,69 +76,66 @@ import java.util.List; public FilterInstance constructFilterInstance(final String anExpression) throws AbuseExc { try { - new RE(anExpression); - - return new FilterInstance() { + int flags = 0; - public boolean test(Entity anEntity, Request aRequest) { + if (!caseSensitive) { + flags |= Perl5Compiler.CASE_INSENSITIVE_MASK; + } - try { - Iterator j; - int flags = 0; + final Pattern pattern = (new Perl5Compiler()).compile(anExpression, flags); - if (!caseSensitive) { - flags |= RE.REG_ICASE; - } + return new FilterInstance() { - RE regularExpression = new RE(anExpression, flags); + public boolean test(Entity anEntity, Request aRequest) { + PatternMatcher matcher = new Perl5Matcher(); - switch (fieldKind) { - case REQUEST_HEADERS: - if (selectedFields != null) { - j = selectedFields.iterator(); + Iterator j; - while (j.hasNext()) { - String fieldName = (String) j.next(); - String field = aRequest.getHeader(fieldName); - - if (exactMatch) { - if (field != null && regularExpression.isMatch(field)) { - return true; - } - } - else { - if (field != null && regularExpression.getMatch(field) != null) { - return true; - } - } - } - } - break; - case ENTITY_FIELDS: - if (selectedFields != null) { - j = selectedFields.iterator(); - } - else { - j = anEntity.getFieldNames().iterator(); - } + switch (fieldKind) { + case REQUEST_HEADERS: + if (selectedFields != null) { + j = selectedFields.iterator(); while (j.hasNext()) { - String field = anEntity.getFieldValue( (String) j.next()); + String fieldName = (String) j.next(); + String field = aRequest.getHeader(fieldName); if (exactMatch) { - if (field != null && regularExpression.isMatch(field)) { + if (field != null && matcher.matches(field, pattern)) { return true; } } else { - if (field != null && regularExpression.getMatch(field) != null) { + if (field != null && matcher.contains(field, pattern)) { return true; } } } - } - } - catch (Throwable t) { + } + break; + + case ENTITY_FIELDS: + if (selectedFields != null) { + j = selectedFields.iterator(); + } + else { + j = anEntity.getFieldNames().iterator(); + } + + while (j.hasNext()) { + String field = anEntity.getFieldValue( (String) j.next()); + + if (exactMatch) { + if (field != null && matcher.matches(field, pattern)) { + return true; + } + } + else { + if (field != null && matcher.contains(field, pattern)) { + return true; + } + } + } } return false; } @@ -147,7 +145,7 @@ import java.util.List; } }; } - catch (Throwable t) { + catch (MalformedPatternException t) { throw new AbuseExc("Invalid expression: " + t.getMessage()); } } diff --git a/source/mircoders/abuse/SubStringFilterType.java b/source/mircoders/abuse/SubStringFilterType.java new file mode 100644 index 00000000..709d5ef0 --- /dev/null +++ b/source/mircoders/abuse/SubStringFilterType.java @@ -0,0 +1,114 @@ +package mircoders.abuse; + +import mir.entity.Entity; +import mir.session.Request; + +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; + +/** + * Instances of this filter type will match if the filter expression is a substringo + * of the value(s) tested agains + */ + public class SubStringFilterType extends AbstractFilterType { + private boolean exactMatch; + private boolean caseSensitive; + private int fieldKind; + private List selectedFields; + + public static final int ENTITY_FIELDS = 0; + public static final int REQUEST_HEADERS = 2; + + public SubStringFilterType(String aName) { + this(aName, false, false, null); + } + + public SubStringFilterType(String aName, boolean aCaseSensitive, boolean anExactMatch, String[] aSelectedFields) { + this (aName, aCaseSensitive, anExactMatch, ENTITY_FIELDS, aSelectedFields); + } + + public SubStringFilterType(String aName, boolean aCaseSensitive, boolean anExactMatch, int aFieldKind, String[] aSelectedFields) { + super(aName); + + fieldKind = aFieldKind; + + caseSensitive = aCaseSensitive; + exactMatch = anExactMatch; + if (aSelectedFields==null) { + selectedFields = null; + } + else { + selectedFields = Arrays.asList(aSelectedFields); + } + } + + + public FilterInstance constructFilterInstance(final String anExpression) throws AbuseExc { + return new FilterInstance() { + public boolean test(Entity anEntity, Request aRequest) { + Iterator j; + String expression = anExpression; + if (!caseSensitive) { + expression = anExpression.toUpperCase(); + } + + switch (fieldKind) { + case REQUEST_HEADERS: + if (selectedFields != null) { + j = selectedFields.iterator(); + + while (j.hasNext()) { + String fieldName = (String) j.next(); + String field = aRequest.getHeader(fieldName); + if (test(field, expression)) { + return true; + } + } + } + break; + + case ENTITY_FIELDS: + if (selectedFields != null) { + j = selectedFields.iterator(); + } + else { + j = anEntity.getFieldNames().iterator(); + } + + while (j.hasNext()) { + String field = anEntity.getFieldValue( (String) j.next()); + + if (test(field, expression)) { + return true; + } + } + } + return false; + } + + private boolean test(String aValue, String anExpression) { + if (!caseSensitive) { + aValue=aValue.toUpperCase(); + } + + if (exactMatch) { + if (aValue!= null && aValue.equals(anExpression)) { + return true; + } + } + else { + if (aValue!= null && aValue.indexOf(anExpression)>=0) { + return true; + } + } + + return false; + } + + public String status() { + return null; + } + }; + } +} diff --git a/source/mircoders/abuse/URLBlacklistFilterType.java b/source/mircoders/abuse/URLBlacklistFilterType.java index 1bb8b034..74c11990 100644 --- a/source/mircoders/abuse/URLBlacklistFilterType.java +++ b/source/mircoders/abuse/URLBlacklistFilterType.java @@ -75,16 +75,6 @@ public class URLBlacklistFilterType extends AbstractFilterType { }; } - public static void main(String[] someArguments) { - HashSet set = new HashSet(); - harvestURLDomains("[URL]https://www.nel9la41.org/informatica[/URL] [URL]http://www.ceud.org/liberi[/URL] [URL]http://www.e6tate.org/danni[/URL] [URL]http://www.e6tate.org/shakira[/URL] [URL]http://www.ceud.org/musica[/URL] [URL]http://www.nel9la41.org/troie[/URL] [URL]http://www.nel9la41.org/corriere-della-sera[/URL] [URL]http://www.e6tate.org/serie-a[/URL] [URL]http://www.nel9la41.org/calciomercato[/URL] [URL]http://www.e6tate.org/vita[/URL] [URL]http://www.e6tate.org/pene[/URL] [URL]http://www.e6tate.org/barzellette[/URL] [URL]http://www.ceud.org/pagine-bianche[/URL] [URL]http://www.e6tate.org/playboy[/URL] [URL]http://www.nel9la41.org/totti[/URL] [URL]http://www.e6tate.org/trenitalia[/URL] [URL]http://www.ceud.org/bambini[/URL] [URL]http://www.ceud.org/pornografia[/URL] [URL]http://www.e6tate.org/corriere-della-sera[/URL] [URL]http://www.nel9la41.org/musica[/URL] [URL]http://www.ceud.org/serie-a[/URL] [URL]http://www.nel9la41.org/barzellette[/URL] [URL]http://www.ceud.org/turismo[/URL] [URL]http://www.ceud.org/del-piero[/URL] [URL]http://www.nel9la41.org/calcio[/URL] [URL]http://www.nel9la41.org/google[/URL] [URL]http://www.e6tate.org/sesso[/URL] [URL]http://www.nel9la41.org/jesse-mccartney[/URL]", set); - Iterator i = set.iterator(); - while (i.hasNext()) { - System.out.println(i.next()); - } - } - - private static void harvestURLDomains(String someText, Set someResult) { Perl5Matcher matcher = new Perl5Matcher(); @@ -135,7 +125,6 @@ public class URLBlacklistFilterType extends AbstractFilterType { } } - private Set whiteList; private static Pattern URL_EXPRESSION;