Moving Redis options to config values

Signed-off-by: Georg <georg@lysergic.dev>
This commit is contained in:
Georg Pfuetzenreuter 2021-08-26 16:36:34 +02:00
parent a8665607bb
commit 7d9db9dd9d
Signed by: Georg
GPG Key ID: 1DAF57F49F8E8F22
4 changed files with 38 additions and 18 deletions

Binary file not shown.

Binary file not shown.

View File

@ -78,7 +78,7 @@ conf.registerGlobalValue(SnoParser.whois.redis, 'host',
, private=True , private=True
)) ))
conf.registerGlobalValue(SnoParser.whois.redis, 'port', conf.registerGlobalValue(SnoParser.whois.redis, 'port',
registry.String('6379', registry.Integer('6379',
""" """
Redis: Port. Redis: Port.
""" """
@ -98,13 +98,13 @@ conf.registerGlobalValue(SnoParser.whois.redis, 'password',
""" """
)) ))
conf.registerGlobalValue(SnoParser.whois.redis, 'db', conf.registerGlobalValue(SnoParser.whois.redis, 'db',
registry.String('0', registry.Integer('0',
""" """
Redis: Database number. It is recommended to use a dedicated, isolated, Redis instance for SnoParser instead of using an existing one with a different database. Redis: Database number. It is recommended to use a dedicated, isolated, Redis instance for SnoParser instead of using an existing one with a different database.
""" """
)) ))
conf.registerGlobalValue(SnoParser.whois.redis, 'timeout', conf.registerGlobalValue(SnoParser.whois.redis, 'timeout',
registry.String('5', registry.Integer('5',
""" """
Redis: Socket Timeout. The developer does not know what to recommend here, but `5` seems to yield good results. Redis: Socket Timeout. The developer does not know what to recommend here, but `5` seems to yield good results.
""" """

View File

@ -53,15 +53,15 @@ class SnoParser(callbacks.Plugin):
"""Parses the Server Notices from ErgoIRCd""" """Parses the Server Notices from ErgoIRCd"""
threaded = True threaded = True
def redis_connect() -> redis.client.Redis: def redis_connect(self) -> redis.client.Redis:
try: try:
redis_client = redis.Redis( redis_client = redis.Redis(
host = .registryValue('whois.redis.host'), host = self.registryValue('whois.redis.host'),
port = self.registryValue('whois.redis.port'), port = self.registryValue('whois.redis.port'),
password = self.registryValue('whois.redis.password'), password = self.registryValue('whois.redis.password'),
username = self.registryValue('whois.redis.username'), username = self.registryValue('whois.redis.username'),
db = self.registryValue('whois.redis.db'), db = self.registryValue('whois.redis.db'),
socket_timeout = self.registryValue('whois.redis.timeout') socket_timeout = int(self.registryValue('whois.redis.timeout'))
) )
ping = redis_client.ping() ping = redis_client.ping()
if ping is True: if ping is True:
@ -69,9 +69,11 @@ class SnoParser(callbacks.Plugin):
except redis.AuthenticationError: except redis.AuthenticationError:
print("Could not authenticate to Redis backend.") print("Could not authenticate to Redis backend.")
redis_client = redis_connect() def __init__(self, irc):
super().__init__(irc)
self.redis_client = self.redis_connect()
def whois_fresh(sourceip: str) -> dict: def whois_fresh(self, sourceip: str) -> dict:
"""Data from cache.""" """Data from cache."""
asn = 0 asn = 0
subnet = '' subnet = ''
@ -90,22 +92,26 @@ class SnoParser(callbacks.Plugin):
response = whoisout response = whoisout
return response return response
def whois_get_cache(key: str) -> str: def whois_get_cache(self, key: str) -> str:
"""Data from Redis.""" """Data from Redis."""
val = SnoParser.redis_client.get(key) k = self.redis_client.get(key)
# self = SnoParser()
# val = self.redis_client.get(key)
val = k
return val return val
def whois_set_cache(key: str, value: str) -> bool: def whois_set_cache(self, key: str, value: str) -> bool:
"""Data to Redis.""" """Data to Redis."""
state = SnoParser.redis_client.setex(key, timedelta(seconds=3600), value=value,) state = self.redis_client.setex(key, timedelta(seconds=3600), value=value,)
return state return state
def whois_run(sourceip: str) -> dict: def whois_run(self, sourceip: str) -> dict:
"""Whois query router.""" """Whois query router."""
data = SnoParser.whois_get_cache(key=sourceip) data = self.whois_get_cache(key=sourceip)
if data is not None: if data is not None:
data = json.loads(data) data = json.loads(data)
#data["cache"] = True #data["cache"] = True
@ -114,7 +120,7 @@ class SnoParser(callbacks.Plugin):
print(sourceip) print(sourceip)
return data return data
else: else:
data = SnoParser.whois_fresh(sourceip) data = self.whois_fresh(sourceip)
print("DEBUG - ELSE WHOIS_FRESH CALLED") print("DEBUG - ELSE WHOIS_FRESH CALLED")
print(data) print(data)
print(sourceip) print(sourceip)
@ -122,7 +128,7 @@ class SnoParser(callbacks.Plugin):
#data["cache"] = False #data["cache"] = False
print("DEBUG - CACHE: FALSE") print("DEBUG - CACHE: FALSE")
data = json.dumps(data) data = json.dumps(data)
state = SnoParser.whois_set_cache(key=sourceip, value=data) state = self.whois_set_cache(key=sourceip, value=data)
print(data) print(data)
print(sourceip) print(sourceip)
@ -134,6 +140,20 @@ class SnoParser(callbacks.Plugin):
print(data) print(data)
return data return data
def query(self, irc, msg, args, ipaddress):
"""<IP address>
Queries the cache for an address.
"""
data = self.whois_get_cache(key=ipaddress)
ttl = self.redis_client.get(ipaddress)
print(data, ' ', ttl)
#irc.reply(str(data), ' Remaining: ', int(ttl), 's')
irc.reply(data, ttl)
query = wrap(query, ['anything'])
def doNotice(self, irc, msg): def doNotice(self, irc, msg):
(target, text) = msg.args (target, text) = msg.args
@ -155,7 +175,7 @@ class SnoParser(callbacks.Plugin):
asn = 0 asn = 0
subnet = '' subnet = ''
whoisout = SnoParser.whois_run(ip) whoisout = self.whois_run(sourceip=ip)
DictFromSnotice = {'notice': 'connect', 'nickname': nickname, 'username': username, 'host': host, 'ip': ip, 'realname': realname, 'ipCount': ip_seen, 'nickCount': nick_seen} DictFromSnotice = {'notice': 'connect', 'nickname': nickname, 'username': username, 'host': host, 'ip': ip, 'realname': realname, 'ipCount': ip_seen, 'nickCount': nick_seen}
#repl = f"\x02\x1FNOTICE: {DictFromSnotice['notice']} \x0F\x11\x0303==>>\x0F \x02Nick:\x0F {DictFromSnotice['nickname']} \x02Username:\x0F {DictFromSnotice['username']} \x02Hostname:\x0F {DictFromSnotice['host']} \x02IP:\x0F {DictFromSnotice['ip']} \x02Realname:\x0F {DictFromSnotice['realname']} \x02IPcount:\x0F {DictFromSnotice['ipCount']} \x02NickCount:\x0F {DictFromSnotice['nickCount']}" #repl = f"\x02\x1FNOTICE: {DictFromSnotice['notice']} \x0F\x11\x0303==>>\x0F \x02Nick:\x0F {DictFromSnotice['nickname']} \x02Username:\x0F {DictFromSnotice['username']} \x02Hostname:\x0F {DictFromSnotice['host']} \x02IP:\x0F {DictFromSnotice['ip']} \x02Realname:\x0F {DictFromSnotice['realname']} \x02IPcount:\x0F {DictFromSnotice['ipCount']} \x02NickCount:\x0F {DictFromSnotice['nickCount']}"
repl = f"\x02\x1F{DictFromSnotice['notice']} \x0F\x11\x0303==>>\x0F \x02Nick:\x0F {DictFromSnotice['nickname']} \x02Username:\x0F {DictFromSnotice['username']} \x02Hostname:\x0F {DictFromSnotice['host']} \x02IP:\x0F {DictFromSnotice['ip']} {whoisout} \x02Realname:\x0F {DictFromSnotice['realname']} \x02IPcount:\x0F {DictFromSnotice['ipCount']} \x02NickCount:\x0F {DictFromSnotice['nickCount']}" repl = f"\x02\x1F{DictFromSnotice['notice']} \x0F\x11\x0303==>>\x0F \x02Nick:\x0F {DictFromSnotice['nickname']} \x02Username:\x0F {DictFromSnotice['username']} \x02Hostname:\x0F {DictFromSnotice['host']} \x02IP:\x0F {DictFromSnotice['ip']} {whoisout} \x02Realname:\x0F {DictFromSnotice['realname']} \x02IPcount:\x0F {DictFromSnotice['ipCount']} \x02NickCount:\x0F {DictFromSnotice['nickCount']}"