| 1 | # -*- coding: utf-8 -*-
|
|---|
| 2 | #
|
|---|
| 3 | # Copyright (C) 2006 Edgewall Software
|
|---|
| 4 | # All rights reserved.
|
|---|
| 5 | #
|
|---|
| 6 | # This software is licensed as described in the file COPYING, which
|
|---|
| 7 | # you should have received as part of this distribution. The terms
|
|---|
| 8 | # are also available at http://trac.edgewall.com/license.html.
|
|---|
| 9 | #
|
|---|
| 10 | # This software consists of voluntary contributions made by many
|
|---|
| 11 | # individuals. For the exact contribution history, see the revision
|
|---|
| 12 | # history and logs, available at http://projects.edgewall.com/trac/.
|
|---|
| 13 |
|
|---|
| 14 | from datetime import datetime, timedelta
|
|---|
| 15 |
|
|---|
| 16 | from trac.config import IntOption
|
|---|
| 17 | from trac.core import Component, implements
|
|---|
| 18 |
|
|---|
| 19 | from tracspamfilter.api import IFilterStrategy, N_
|
|---|
| 20 | from tracspamfilter.model import LogEntry
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 | class IPThrottleFilterStrategy(Component):
|
|---|
| 24 | """Spam filter strategy that throttles multiple subsequent submissions from
|
|---|
| 25 | the same IP address.
|
|---|
| 26 | """
|
|---|
| 27 | implements(IFilterStrategy)
|
|---|
| 28 |
|
|---|
| 29 | karma_points = IntOption('spam-filter', 'ip_throttle_karma', '3',
|
|---|
| 30 | """By how many points exceeding the configured maximum number of
|
|---|
| 31 | posts per hour impacts the overall score.""",
|
|---|
| 32 | doc_domain='tracspamfilter')
|
|---|
| 33 |
|
|---|
| 34 | max_posts = IntOption('spam-filter', 'max_posts_by_ip', '10',
|
|---|
| 35 | """The maximum allowed number of submissions per hour from a single
|
|---|
| 36 | IP address. If this limit is exceeded, subsequent submissions get
|
|---|
| 37 | negative karma.""", doc_domain='tracspamfilter')
|
|---|
| 38 |
|
|---|
| 39 | # IFilterStrategy implementation
|
|---|
| 40 |
|
|---|
| 41 | def is_external(self):
|
|---|
| 42 | return False
|
|---|
| 43 |
|
|---|
| 44 | def test(self, req, author, content, ip):
|
|---|
| 45 | threshold = datetime.now() - timedelta(hours=1)
|
|---|
| 46 | num_posts = 0
|
|---|
| 47 |
|
|---|
| 48 | for entry in LogEntry.select(self.env, ipnr=ip):
|
|---|
| 49 | if datetime.fromtimestamp(entry.time) < threshold:
|
|---|
| 50 | break
|
|---|
| 51 | num_posts += 1
|
|---|
| 52 |
|
|---|
| 53 | if num_posts > self.max_posts:
|
|---|
| 54 | return -abs(self.karma_points) * num_posts / self.max_posts, \
|
|---|
| 55 | N_("Maximum number of posts per hour for this IP exceeded")
|
|---|
| 56 |
|
|---|
| 57 | def train(self, req, author, content, ip, spam=True):
|
|---|
| 58 | return 0
|
|---|