2016-03-26 00:39:20 +01:00
|
|
|
# servprotect.py: Protects against KILL and nick collision floods
|
|
|
|
from expiringdict import ExpiringDict
|
|
|
|
|
2017-02-22 02:02:26 +01:00
|
|
|
from pylinkirc import utils, conf
|
2016-06-21 03:18:54 +02:00
|
|
|
from pylinkirc.log import log
|
2016-03-26 00:39:20 +01:00
|
|
|
|
2017-02-22 02:02:26 +01:00
|
|
|
# check for definitions
|
|
|
|
servprotect_conf = conf.conf.get('servprotect', {})
|
2017-08-03 19:10:28 +02:00
|
|
|
length = servprotect_conf.get('length', 10)
|
2017-02-22 02:02:26 +01:00
|
|
|
age = servprotect_conf.get('age', 10)
|
|
|
|
|
|
|
|
savecache = ExpiringDict(max_len=length, max_age_seconds=age)
|
|
|
|
killcache = ExpiringDict(max_len=length, max_age_seconds=age)
|
2016-03-26 00:39:20 +01:00
|
|
|
|
|
|
|
def handle_kill(irc, numeric, command, args):
|
|
|
|
"""
|
|
|
|
Tracks kills against PyLink clients. If too many are received,
|
|
|
|
automatically disconnects from the network.
|
|
|
|
"""
|
|
|
|
|
2017-06-30 08:01:39 +02:00
|
|
|
if (args['userdata'] and irc.is_internal_server(args['userdata'].server)) or irc.is_internal_client(args['target']):
|
2017-05-21 00:02:04 +02:00
|
|
|
if killcache.setdefault(irc.name, 1) >= length:
|
|
|
|
log.error('(%s) servprotect: Too many kills received, aborting!', irc.name)
|
|
|
|
irc.disconnect()
|
|
|
|
|
|
|
|
log.debug('(%s) servprotect: Incrementing killcache by 1', irc.name)
|
|
|
|
killcache[irc.name] += 1
|
2016-03-26 00:39:20 +01:00
|
|
|
|
|
|
|
utils.add_hook(handle_kill, 'KILL')
|
|
|
|
|
|
|
|
def handle_save(irc, numeric, command, args):
|
|
|
|
"""
|
|
|
|
Tracks SAVEs (nick collision) against PyLink clients. If too many are received,
|
|
|
|
automatically disconnects from the network.
|
|
|
|
"""
|
2017-06-30 08:01:39 +02:00
|
|
|
if irc.is_internal_client(args['target']):
|
2017-05-21 00:02:04 +02:00
|
|
|
if savecache.setdefault(irc.name, 0) >= length:
|
|
|
|
log.error('(%s) servprotect: Too many nick collisions, aborting!', irc.name)
|
|
|
|
irc.disconnect()
|
2016-03-26 00:39:20 +01:00
|
|
|
|
2017-05-21 00:02:04 +02:00
|
|
|
log.debug('(%s) servprotect: Incrementing savecache by 1', irc.name)
|
|
|
|
savecache[irc.name] += 1
|
2016-03-26 00:39:20 +01:00
|
|
|
|
|
|
|
utils.add_hook(handle_save, 'SAVE')
|