diff --git a/config.py b/config.py index 14654c5..f58e176 100644 --- a/config.py +++ b/config.py @@ -104,20 +104,10 @@ conf.registerGlobalValue( # REDIS related settings below: ### conf.registerGroup(SnoParser, "redis") +conf.registerGroup(SnoParser.redis, "db") conf.registerGlobalValue( - SnoParser.redis, - "db1", - registry.Integer( - 1, - """ - Redis: Database number for counting of NICKNAMES. - """, - private=True, - ), -) -conf.registerGlobalValue( - SnoParser.redis, - "db2", + SnoParser.redis.db, + "ips", registry.Integer( 2, """ @@ -126,11 +116,33 @@ conf.registerGlobalValue( 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( SnoParser.redis, "host", registry.String( - "127.0.0.1", + "localhost", """ Redis: IP address or hostname. """, @@ -218,16 +230,5 @@ conf.registerGlobalValue( 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: diff --git a/plugin.py b/plugin.py index b0d13bd..de93e94 100644 --- a/plugin.py +++ b/plugin.py @@ -29,14 +29,8 @@ ### from supybot import ( - utils, - plugins, ircutils, callbacks, - ircdb, - conf, - log, - world, ircmsgs, ) from supybot.commands import * @@ -51,15 +45,11 @@ except ImportError: _ = lambda x: x import re -import os -import sys -import time -import sqlite3 import redis import json from datetime import timedelta from ipwhois import IPWhois -import ipwhois +from ipwhois.exceptions import IPDefinedError class SnoParser(callbacks.Plugin): @@ -67,62 +57,38 @@ class SnoParser(callbacks.Plugin): threaded = True - def redis_connect_whois(self) -> redis.client.Redis: + def _redis_connect(self, db) -> redis.client.Redis: try: - redis_client_whois = redis.Redis( + redis_client = 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("whois.redis.db"), + db=self.registryValue(f"redis.db.{db}"), socket_timeout=int(self.registryValue("redis.timeout")), ) - ping = redis_client_whois.ping() + ping = redis_client.ping() if ping is True: - return redis_client_whois - 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 + return redis_client except redis.AuthenticationError: print("Could not authenticate to Redis backend.") def __init__(self, irc): super().__init__(irc) - self.redis_client_whois = self.redis_connect_whois() - self.redis_client_nicks = self.redis_connect_nicks() - self.redis_client_ips = self.redis_connect_ips() + self.redis_clients = { + '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: """Data from WHOIS backend (IANA or respective RIR).""" + asn = 0 subnet = "" try: @@ -135,37 +101,25 @@ class SnoParser(callbacks.Plugin): country = whoisres["asn_country_code"] description = whoisres["asn_description"] whoisout = asn + " " + country + " " + description - except ipwhois.exceptions.IPDefinedError: + except IPDefinedError: whoisout = "RFC 4291 (Local)" response = whoisout 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: """Data to Redis.""" - duration = int(self.registryValue("whois.ttl")) - state = self.redis_client_whois.setex( + return self.redis_clients['whois'].setex( key, - timedelta(seconds=duration), + timedelta(seconds=int(self.registryValue("whois.ttl"))), value=value, ) - return state def whois_run(self, sourceip: str) -> dict: """Whois query router.""" - data = self.whois_get_cache(key=sourceip) + data = self._get_from_cache("whois", sourceip) if data is not None: data = json.loads(data) if self.registryValue("whois.debug"): @@ -201,14 +155,14 @@ class SnoParser(callbacks.Plugin): def nick_run(self, nickname: str) -> dict: """Tracks nicknames""" - data = self.redis_client_nicks.get(nickname) + data = self._get_from_cache("nicks", nickname) if data is not None: if self.registryValue("debug"): print("SNOPARSER DEBUG - NICK_RUN, SEEN: TRUE") print(nickname) print(data) - self.redis_client_nicks.incrby(nickname, amount=1) + self.redis_clients['nicks'].incrby(nickname, amount=1) if data: decoded = data.decode("utf-8") return decoded @@ -219,7 +173,7 @@ class SnoParser(callbacks.Plugin): print("SNOPARSER DEBUG _ NICK_RUN, SEEN: FALSE") print(nickname) print(data) - self.redis_client_nicks.set(nickname, value="1") + self.redis_clients['nicks'].set(nickname, value="1") if data: decoded = data.decode("utf-8") return decoded @@ -229,14 +183,13 @@ class SnoParser(callbacks.Plugin): def ip_run(self, ipaddress: str) -> dict: """Tracks IP addresses""" - data = self.redis_client_ips.get(ipaddress) - + data = self._get_from_cache("ips", ipaddress) if data is not None: if self.registryValue("debug"): print("SNOPARSER DEBUG - IP_RUN, SEEN: TRUE") print(ipaddress) print(data) - self.redis_client_ips.incrby(ipaddress, amount=1) + self.redis_clients['ips'].incrby(ipaddress, amount=1) if data: decoded = data.decode("utf-8") return decoded @@ -247,7 +200,7 @@ class SnoParser(callbacks.Plugin): print("SNOPARSER DEBUG _ IP_RUN, SEEN: FALSE") print(ipaddress) print(data) - self.redis_client_ips.set(ipaddress, value="1") + self.redis_clients['ips'].set(ipaddress, value="1") if data: decoded = data.decode("utf-8") return decoded @@ -259,13 +212,15 @@ class SnoParser(callbacks.Plugin): Queries the cache for an address. """ - data = self.whois_get_cache(key=ipaddress) - decoded_data = data.decode("utf-8") - ttl = self.redis_client_whois.ttl(ipaddress) - count = self.redis_client_ips.get(ipaddress) - decoded_count = count.decode("utf-8") + data = self.redis_clients['whois'].get(ipaddress) + if data is not None: + data = data.decode("utf-8") + ttl = self.redis_clients['whois'].ttl(ipaddress) + count = self.redis_clients['ips'].get(ipaddress) + if count is not None: + count = count.decode("utf-8") 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"]) @@ -433,7 +388,7 @@ class SnoParser(callbacks.Plugin): self._sendSnotice(irc, msg, repl) 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) hostmask = couple.group(1) oper = couple.group(2)