Banning Feed Reader Background app, as it's causing a ton of bogus requests.

This commit is contained in:
Samuel Clay 2013-06-25 21:48:22 -07:00
parent 34dec1f322
commit 3f786a65c6
4 changed files with 26 additions and 3 deletions

View file

@ -3,10 +3,11 @@ import re
import random import random
import time import time
from utils import log as logging from utils import log as logging
from django.http import HttpResponse
from django.conf import settings from django.conf import settings
from django.db import connection from django.db import connection
from django.template import Template, Context from django.template import Template, Context
from utils import json_functions as json
class LastSeenMiddleware(object): class LastSeenMiddleware(object):
def process_response(self, request, response): def process_response(self, request, response):
@ -175,3 +176,21 @@ class ServerHostnameMiddleware:
class TimingMiddleware: class TimingMiddleware:
def process_request(self, request): def process_request(self, request):
setattr(request, 'start_time', time.time()) setattr(request, 'start_time', time.time())
BANNED_USER_AGENTS = (
'feed reader-background',
'feed reader-windows',
)
class UserAgentBanMiddleware:
def process_request(self, request):
user_agent = request.environ.get('HTTP_USER_AGENT', 'missing').lower()
if any(ua in user_agent for ua in BANNED_USER_AGENTS):
data = {
'error': 'User agent banned: %s' % user_agent,
'code': -1
}
logging.user(request, "~FB~SN~BBBanned UA: ~SB%s" % (user_agent))
return HttpResponse(json.encode(data), status=403, mimetype='text/json')

View file

@ -261,7 +261,10 @@ class Profile(models.Model):
transaction = PayPalIPN.objects.filter(custom=self.user.username, transaction = PayPalIPN.objects.filter(custom=self.user.username,
txn_type='subscr_payment')[0] txn_type='subscr_payment')[0]
refund = paypal.refund_transaction(transaction.txn_id) refund = paypal.refund_transaction(transaction.txn_id)
try:
refunded = int(float(refund['raw']['TOTALREFUNDEDAMOUNT'][0])) refunded = int(float(refund['raw']['TOTALREFUNDEDAMOUNT'][0]))
except KeyError:
refunded = int(transaction.amount)
logging.user(self.user, "~FRRefunding paypal payment: $%s" % refunded) logging.user(self.user, "~FRRefunding paypal payment: $%s" % refunded)
self.cancel_premium() self.cancel_premium()

View file

@ -103,6 +103,7 @@ MIDDLEWARE_CLASSES = (
'apps.profile.middleware.TimingMiddleware', 'apps.profile.middleware.TimingMiddleware',
'apps.profile.middleware.LastSeenMiddleware', 'apps.profile.middleware.LastSeenMiddleware',
'apps.profile.middleware.SQLLogToConsoleMiddleware', 'apps.profile.middleware.SQLLogToConsoleMiddleware',
'apps.profile.middleware.UserAgentBanMiddleware',
'subdomains.middleware.SubdomainMiddleware', 'subdomains.middleware.SubdomainMiddleware',
'apps.profile.middleware.SimpsonsMiddleware', 'apps.profile.middleware.SimpsonsMiddleware',
'apps.profile.middleware.ServerHostnameMiddleware', 'apps.profile.middleware.ServerHostnameMiddleware',

View file

@ -4,6 +4,7 @@ from datetime import datetime, timedelta
import functools import functools
import hashlib import hashlib
class ratelimit(object): class ratelimit(object):
"Instances of this class can be used as decorators" "Instances of this class can be used as decorators"
# This class is designed to be sub-classed # This class is designed to be sub-classed
@ -84,7 +85,6 @@ class ratelimit(object):
return key return key
def disallowed(self, request): def disallowed(self, request):
"Over-ride this method if you want to log incidents"
return HttpResponse('Rate limit exceeded', status=429) return HttpResponse('Rate limit exceeded', status=429)
def expire_after(self): def expire_after(self):