First prettifications of Redis cached whois

Signed-off-by: Georg <georg@lysergic.dev>
This commit is contained in:
Georg Pfuetzenreuter 2021-08-26 13:51:26 +02:00
parent f796a29691
commit 415d5df94c
Signed by: Georg
GPG Key ID: 1DAF57F49F8E8F22
4 changed files with 68 additions and 37 deletions

Binary file not shown.

Binary file not shown.

View File

@ -61,4 +61,53 @@ conf.registerGlobalValue(SnoParser, 'AutoVhost',
conf.registerGlobalValue(SnoParser, 'preventHighlight', conf.registerGlobalValue(SnoParser, 'preventHighlight',
registry.Boolean(True, ("""Toggles in channel highlights with ZWSP"""))) registry.Boolean(True, ("""Toggles in channel highlights with ZWSP""")))
conf.registerGroup(SnoParser, 'whois')
conf.registerGlobalValue(SnoParser.whois, 'debug',
registry.Boolean('false',
"""
SnoParser: True: Very verbose console output. False: Mostly silent operation.
"""
, private=True
))
conf.registerGroup(SnoParser.whois, 'redis')
conf.registerGlobalValue(SnoParser.whois.redis, 'host',
registry.String('127.0.0.1',
"""
Redis: IP address or hostname.
"""
, private=True
))
conf.registerGlobalValue(SnoParser.whois.redis, 'port',
registry.String('6379',
"""
Redis: Port.
"""
, private=True
))
conf.registerGlobalValue(SnoParser.whois.redis, 'username',
registry.String('',
"""
Redis: Username. This is optional and has not been tested. It is recommended to perform initial tests on a local instance with no username and password.
"""
, private=True
))
conf.registerGlobalValue(SnoParser.whois.redis, 'password',
registry.String('',
"""
Redis: Password. This is optional and has not been tested. It is recommended to perform initial tests on a local instance with no username and password.
"""
))
conf.registerGlobalValue(SnoParser.whois.redis, 'db',
registry.String('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.
"""
))
conf.registerGlobalValue(SnoParser.whois.redis, 'timeout',
registry.String('5',
"""
Redis: Socket Timeout. The developer does not know what to recommend here, but `5` seems to yield good results.
"""
))
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: # vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:

View File

@ -56,11 +56,12 @@ class SnoParser(callbacks.Plugin):
def redis_connect() -> redis.client.Redis: def redis_connect() -> redis.client.Redis:
try: try:
redis_client = redis.Redis( redis_client = redis.Redis(
host="localhost", host = .registryValue('whois.redis.host'),
port=6378, port = self.registryValue('whois.redis.port'),
#password="test", password = self.registryValue('whois.redis.password'),
db=0, username = self.registryValue('whois.redis.username'),
socket_timeout=5, db = self.registryValue('whois.redis.db'),
socket_timeout = self.registryValue('whois.redis.timeout')
) )
ping = redis_client.ping() ping = redis_client.ping()
if ping is True: if ping is True:
@ -70,12 +71,12 @@ class SnoParser(callbacks.Plugin):
redis_client = redis_connect() redis_client = redis_connect()
def whois_fresh(coordinates: str) -> dict: def whois_fresh(sourceip: str) -> dict:
"""Data from cache.""" """Data from cache."""
asn = 0 asn = 0
subnet = '' subnet = ''
try: try:
whois = IPWhois(coordinates) whois = IPWhois(sourceip)
whoisres = whois.lookup_rdap(depth=1,retry_count=0) whoisres = whois.lookup_rdap(depth=1,retry_count=0)
results = whoisres results = whoisres
print(results) print(results)
@ -86,10 +87,6 @@ class SnoParser(callbacks.Plugin):
except ipwhois.exceptions.IPDefinedError: except ipwhois.exceptions.IPDefinedError:
whoisout = 'RFC 4291 (Local)' whoisout = 'RFC 4291 (Local)'
# rr = f"{results}"
# response = rr
# return response.json()
# response = results
response = whoisout response = whoisout
return response return response
@ -105,28 +102,30 @@ class SnoParser(callbacks.Plugin):
state = SnoParser.redis_client.setex(key, timedelta(seconds=3600), value=value,) state = SnoParser.redis_client.setex(key, timedelta(seconds=3600), value=value,)
return state return state
def whois_run(coordinates: str) -> dict: def whois_run(sourceip: str) -> dict:
data = SnoParser.whois_get_cache(key=coordinates) """Whois query router."""
data = SnoParser.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
print("DEBUG - CACHE: TRUE") print("DEBUG - CACHE: TRUE")
print(data) print(data)
print(coordinates) print(sourceip)
return data return data
else: else:
data = SnoParser.whois_fresh(coordinates) data = SnoParser.whois_fresh(sourceip)
print("ELSE WHOIS_FRESH CALLED") print("DEBUG - ELSE WHOIS_FRESH CALLED")
print(data) print(data)
print(coordinates) print(sourceip)
if data.startswith("WHOIS"): if data.startswith("WHOIS"):
#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=coordinates, value=data) state = SnoParser.whois_set_cache(key=sourceip, value=data)
print(data) print(data)
print(coordinates) print(sourceip)
if state is True: if state is True:
return json.loads(data) return json.loads(data)
@ -149,31 +148,14 @@ class SnoParser(callbacks.Plugin):
username = couple.group(2) username = couple.group(2)
host = couple.group(3) host = couple.group(3)
#ip = couple.group(4) #ip = couple.group(4)
ip = '::1' ip = '2a03:4000:55:d20::'
realname = couple.group(5) realname = couple.group(5)
ip_seen = 0 ip_seen = 0
nick_seen = 0 nick_seen = 0
asn = 0 asn = 0
subnet = '' subnet = ''
# SnoParser.whois_run(coordinates=ip)
whoisout = SnoParser.whois_run(ip) whoisout = SnoParser.whois_run(ip)
# whoisout = SnoParser.whois_run(whoisout)
# SnoParser.whois_run(ip)
# whoisout = SnoParser.whois_run.coordinates
# try:
# #whois = IPWhois(ip)
# #whoisres = whois.lookup_rdap(depth=1,retry_count=0)
# #results = whoisres
# whoisres = data
# print(results)
# asn = whoisres['asn_registry']
# country = whoisres['asn_country_code']
# description = whoisres['asn_description']
# whoisout = asn + ' ' + country + ' ' + description
# except ipwhois.exceptions.IPDefinedError:
# whoisout = 'RFC 4291 (Local)'
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']}"