Improve #21

Open
Georg wants to merge 5 commits from improve into master
2 changed files with 62 additions and 106 deletions

View File

@ -104,20 +104,10 @@ conf.registerGlobalValue(
# REDIS related settings below: # REDIS related settings below:
### ###
conf.registerGroup(SnoParser, "redis") conf.registerGroup(SnoParser, "redis")
conf.registerGroup(SnoParser.redis, "db")
conf.registerGlobalValue( conf.registerGlobalValue(
SnoParser.redis, SnoParser.redis.db,
"db1", "ips",
registry.Integer(
1,
"""
Redis: Database number for counting of NICKNAMES.
""",
private=True,
),
)
conf.registerGlobalValue(
SnoParser.redis,
"db2",
registry.Integer( registry.Integer(
2, 2,
""" """
@ -126,11 +116,33 @@ conf.registerGlobalValue(
private=True, private=True,
), ),
) )
conf.registerGlobalValue(
SnoParser.redis.db,
"nicks",
registry.Integer(
1,
"""
Redis: Database number for counting of NICKNAMES.
""",
private=True,
),
)
conf.registerGlobalValue(
SnoParser.redis.db,
"whois",
registry.Integer(
0,
"""
Redis: Database number for WHOIS query caching.
""",
),
)
conf.registerGlobalValue( conf.registerGlobalValue(
SnoParser.redis, SnoParser.redis,
"host", "host",
registry.String( registry.String(
"127.0.0.1", "localhost",
""" """
Redis: IP address or hostname. Redis: IP address or hostname.
""", """,
@ -218,16 +230,5 @@ conf.registerGlobalValue(
private=True, private=True,
), ),
) )
conf.registerGroup(SnoParser.whois, "redis")
conf.registerGlobalValue(
SnoParser.whois.redis,
"db",
registry.Integer(
0,
"""
Redis: Database number for WHOIS query caching.
""",
),
)
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: # vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:

117
plugin.py
View File

@ -29,14 +29,8 @@
### ###
from supybot import ( from supybot import (
utils,
plugins,
ircutils, ircutils,
callbacks, callbacks,
ircdb,
conf,
log,
world,
ircmsgs, ircmsgs,
) )
from supybot.commands import * from supybot.commands import *
@ -51,15 +45,11 @@ except ImportError:
_ = lambda x: x _ = lambda x: x
import re import re
import os
import sys
import time
import sqlite3
import redis import redis
import json import json
from datetime import timedelta from datetime import timedelta
from ipwhois import IPWhois from ipwhois import IPWhois
import ipwhois from ipwhois.exceptions import IPDefinedError
class SnoParser(callbacks.Plugin): class SnoParser(callbacks.Plugin):
@ -67,62 +57,38 @@ class SnoParser(callbacks.Plugin):
threaded = True threaded = True
def redis_connect_whois(self) -> redis.client.Redis: def _redis_connect(self, db) -> redis.client.Redis:
try: try:
redis_client_whois = redis.Redis( redis_client = redis.Redis(
host=self.registryValue("redis.host"), host=self.registryValue("redis.host"),
port=self.registryValue("redis.port"), port=self.registryValue("redis.port"),
password=self.registryValue("redis.password"), password=self.registryValue("redis.password"),
username=self.registryValue("redis.username"), username=self.registryValue("redis.username"),
db=self.registryValue("whois.redis.db"), db=self.registryValue(f"redis.db.{db}"),
socket_timeout=int(self.registryValue("redis.timeout")), socket_timeout=int(self.registryValue("redis.timeout")),
) )
ping = redis_client_whois.ping() ping = redis_client.ping()
if ping is True: if ping is True:
return redis_client_whois return redis_client
except redis.AuthenticationError:
print("Could not authenticate to Redis backend.")
def redis_connect_nicks(self) -> redis.client.Redis:
try:
redis_client_nicks = redis.Redis(
host=self.registryValue("redis.host"),
port=self.registryValue("redis.port"),
password=self.registryValue("redis.password"),
username=self.registryValue("redis.username"),
db=self.registryValue("redis.db1"),
socket_timeout=int(self.registryValue("redis.timeout")),
)
ping = redis_client_nicks.ping()
if ping is True:
return redis_client_nicks
except redis.AuthenticationError:
print("Could not authenticate to Redis backend.")
def redis_connect_ips(self) -> redis.client.Redis:
try:
redis_client_ips = redis.Redis(
host=self.registryValue("redis.host"),
port=self.registryValue("redis.port"),
password=self.registryValue("redis.password"),
username=self.registryValue("redis.username"),
db=self.registryValue("redis.db2"),
socket_timeout=int(self.registryValue("redis.timeout")),
)
ping = redis_client_ips.ping()
if ping is True:
return redis_client_ips
except redis.AuthenticationError: except redis.AuthenticationError:
print("Could not authenticate to Redis backend.") print("Could not authenticate to Redis backend.")
def __init__(self, irc): def __init__(self, irc):
super().__init__(irc) super().__init__(irc)
self.redis_client_whois = self.redis_connect_whois() self.redis_clients = {
self.redis_client_nicks = self.redis_connect_nicks() 'ips': self._redis_connect("ips"),
self.redis_client_ips = self.redis_connect_ips() 'nicks': self._redis_connect("nicks"),
'whois': self._redis_connect("whois"),
}
def _get_from_cache(self, db, key):
""" Get value from Redis cache """
return self.redis_clients[db].get(key)
def whois_fresh(self, sourceip: str) -> dict: def whois_fresh(self, sourceip: str) -> dict:
"""Data from WHOIS backend (IANA or respective RIR).""" """Data from WHOIS backend (IANA or respective RIR)."""
asn = 0 asn = 0
subnet = "" subnet = ""
try: try:
@ -135,37 +101,25 @@ class SnoParser(callbacks.Plugin):
country = whoisres["asn_country_code"] country = whoisres["asn_country_code"]
description = whoisres["asn_description"] description = whoisres["asn_description"]
whoisout = asn + " " + country + " " + description whoisout = asn + " " + country + " " + description
except ipwhois.exceptions.IPDefinedError: except IPDefinedError:
whoisout = "RFC 4291 (Local)" whoisout = "RFC 4291 (Local)"
response = whoisout response = whoisout
return response return response
def whois_get_cache(self, key: str) -> str:
"""Data from Redis."""
k = self.redis_client_whois.get(key)
# self = SnoParser()
# val = self.redis_client_whois.get(key)
val = k
return val
def whois_set_cache(self, key: str, value: str) -> bool: def whois_set_cache(self, key: str, value: str) -> bool:
"""Data to Redis.""" """Data to Redis."""
duration = int(self.registryValue("whois.ttl")) return self.redis_clients['whois'].setex(
state = self.redis_client_whois.setex(
key, key,
timedelta(seconds=duration), timedelta(seconds=int(self.registryValue("whois.ttl"))),
value=value, value=value,
) )
return state
def whois_run(self, sourceip: str) -> dict: def whois_run(self, sourceip: str) -> dict:
"""Whois query router.""" """Whois query router."""
data = self.whois_get_cache(key=sourceip) data = self._get_from_cache("whois", sourceip)
if data is not None: if data is not None:
data = json.loads(data) data = json.loads(data)
if self.registryValue("whois.debug"): if self.registryValue("whois.debug"):
@ -201,14 +155,14 @@ class SnoParser(callbacks.Plugin):
def nick_run(self, nickname: str) -> dict: def nick_run(self, nickname: str) -> dict:
"""Tracks nicknames""" """Tracks nicknames"""
data = self.redis_client_nicks.get(nickname) data = self._get_from_cache("nicks", nickname)
if data is not None: if data is not None:
if self.registryValue("debug"): if self.registryValue("debug"):
print("SNOPARSER DEBUG - NICK_RUN, SEEN: TRUE") print("SNOPARSER DEBUG - NICK_RUN, SEEN: TRUE")
print(nickname) print(nickname)
print(data) print(data)
self.redis_client_nicks.incrby(nickname, amount=1) self.redis_clients['nicks'].incrby(nickname, amount=1)
if data: if data:
decoded = data.decode("utf-8") decoded = data.decode("utf-8")
return decoded return decoded
@ -219,7 +173,7 @@ class SnoParser(callbacks.Plugin):
print("SNOPARSER DEBUG _ NICK_RUN, SEEN: FALSE") print("SNOPARSER DEBUG _ NICK_RUN, SEEN: FALSE")
print(nickname) print(nickname)
print(data) print(data)
self.redis_client_nicks.set(nickname, value="1") self.redis_clients['nicks'].set(nickname, value="1")
if data: if data:
decoded = data.decode("utf-8") decoded = data.decode("utf-8")
return decoded return decoded
@ -229,14 +183,13 @@ class SnoParser(callbacks.Plugin):
def ip_run(self, ipaddress: str) -> dict: def ip_run(self, ipaddress: str) -> dict:
"""Tracks IP addresses""" """Tracks IP addresses"""
data = self.redis_client_ips.get(ipaddress) data = self._get_from_cache("ips", ipaddress)
if data is not None: if data is not None:
if self.registryValue("debug"): if self.registryValue("debug"):
print("SNOPARSER DEBUG - IP_RUN, SEEN: TRUE") print("SNOPARSER DEBUG - IP_RUN, SEEN: TRUE")
print(ipaddress) print(ipaddress)
print(data) print(data)
self.redis_client_ips.incrby(ipaddress, amount=1) self.redis_clients['ips'].incrby(ipaddress, amount=1)
if data: if data:
decoded = data.decode("utf-8") decoded = data.decode("utf-8")
return decoded return decoded
@ -247,7 +200,7 @@ class SnoParser(callbacks.Plugin):
print("SNOPARSER DEBUG _ IP_RUN, SEEN: FALSE") print("SNOPARSER DEBUG _ IP_RUN, SEEN: FALSE")
print(ipaddress) print(ipaddress)
print(data) print(data)
self.redis_client_ips.set(ipaddress, value="1") self.redis_clients['ips'].set(ipaddress, value="1")
if data: if data:
decoded = data.decode("utf-8") decoded = data.decode("utf-8")
return decoded return decoded
@ -259,13 +212,15 @@ class SnoParser(callbacks.Plugin):
Queries the cache for an address. Queries the cache for an address.
""" """
data = self.whois_get_cache(key=ipaddress) data = self.redis_clients['whois'].get(ipaddress)
decoded_data = data.decode("utf-8") if data is not None:
ttl = self.redis_client_whois.ttl(ipaddress) data = data.decode("utf-8")
count = self.redis_client_ips.get(ipaddress) ttl = self.redis_clients['whois'].ttl(ipaddress)
decoded_count = count.decode("utf-8") count = self.redis_clients['ips'].get(ipaddress)
if count is not None:
count = count.decode("utf-8")
print("SnoParser manual query: ", data, " ", ttl) print("SnoParser manual query: ", data, " ", ttl)
irc.reply(f"{decoded_data} - Count: {decoded_count} - Remaining: {ttl}s") irc.reply(f"{data} - Count: {count} - Remaining: {ttl}s")
ipquery = wrap(ipquery, ["ip"]) ipquery = wrap(ipquery, ["ip"])
@ -433,7 +388,7 @@ class SnoParser(callbacks.Plugin):
self._sendSnotice(irc, msg, repl) self._sendSnotice(irc, msg, repl)
if "OPER" in text and "Client opered up" in text: if "OPER" in text and "Client opered up" in text:
operregex = "^-OPER- Client opered up \[(.*)\, \ (.*)\]$" operregex = "^-OPER- Client opered up \[(.*), (.*)\]$"
couple = re.match(operregex, text) couple = re.match(operregex, text)
hostmask = couple.group(1) hostmask = couple.group(1)
oper = couple.group(2) oper = couple.group(2)