def POST(self): '''提交一个session_id把这个session_id添加到黑名单中''' x = web.input() session_id = x['session_id'] log = get_log(conf.blacklist_session_log) log.info(session_id)
from webchat.db.models import load_orm from webchat.db.models import Notice as Notice_ from webchat.db.models import Channel as Channel_ from webchat.db.models_mem import User_channel_map as Ucm_ from webchat.common.log import get_log from webchat.common.blacklist import in_blacklist, blacklist_create from webchat.common.freq import freq_filter from webchat import index_map reload(sys) sys.setdefaultencoding('utf-8') LOG = get_log('webchat.main') urls = ( '', 'Home', '/', 'Home', '/index.html', 'Index', '/nickname', 'Nickname', '/nickname_check', 'Nickname_check', '/simple_chat', 'Simple_chat', '/message', 'Message', '/notice/(.*)', 'Notice', '/notice_reply_grid', 'Notice_reply_grid', '/notice_grid', 'Notice_grid', '/channel/(.*)', 'Channel', '/ucm/(.+)', 'Ucm', '/blacklist', 'Blacklist',
#!/usr/bin/python #-*- coding: utf-8 -*- import time from webchat import conf from webchat.common.log import get_log from webchat.common.blacklist import blacklist_create LOG = get_log('webchat.common.freq') freq = {'ip':{'count':0, 'last_up_time':0},} def freq_filter(client, count_max=10, time_max=10): ''' 到频率极限返回True, 否则返回False''' client_freq = freq.get(client) time_now = time.time() if client_freq is None: freq.update({client:{'count':1, 'last_up_time':time_now}}) return '' client_freq['count'] += 1 count = client_freq.get('count') if count > count_max: last_up_time = client_freq.get('last_up_time') if time_now - last_up_time <= time_max: blacklist_create(client, timeout=60*30) LOG.info('client: %s insert to blacklist, \ is too freq, %s, %s')%(client, count_max, time_max) return True else: client_freq['last_up_time'] = time_now client_freq['count'] = 1
#!/usr/bin/python #-*- coding: utf-8 -*- import time from webchat import conf from webchat.common.log import get_log #TODO #要考虑到blacklist的特殊性,暂时设置大小为1000M,防止会滚 blacklist_log = get_log('web_blacklist', log_file=conf.blacklist_ip_log, max_mb=1000) #blacklist = {'client':'expire_time'} blacklist = {} def blacklist_load(): time_now = time.time() with open(conf.blacklist_ip_log, 'r') as f: for line in f.readlines(): client_expire_time = line.split('-')[-1].strip() client = client_expire_time.split(':')[0] expire_time = client_expire_time.split(':')[1] if expire_time < time_now: continue blacklist.update({client:expire_time}) def blacklist_create(client, timeout=60**60*1): time_now = time.time() expire_time = time_now + timeout blacklist.update({client:exipre_time})
# Hook to ensure connection resets don't blow up our servers. # Remove with next release of Eventlet that has it in the set already. from errno import ECONNRESET wsgi.ACCEPT_ERRNO.add(ECONNRESET) from eventlet.green import socket, ssl from webob import Request, Response from webob.exc import HTTPAccepted, HTTPCreated from webchat.common.message_hander import get_hander from webchat.common.log import get_log LOG = get_log("webchat.common.wsgi_server") running = True channels = set() chunk_size = 65535 def get_socket(default_port=8080): """Bind socket to bind ip:port in conf :param conf: Configuration dict to read settings from :param default_port: port to use if not specified in conf :returns : a socket object as returned from socket.listen or ssl.wrap_socket if conf specifies cert_file """ bind_addr = ("0.0.0.0", default_port)