First working Redis cache

Signed-off-by: Georg <georg@lysergic.dev>
This commit is contained in:
Georg Pfuetzenreuter 2021-08-26 12:50:25 +02:00
parent c4941a26c1
commit f796a29691
Signed by: Georg
GPG Key ID: 1DAF57F49F8E8F22
2 changed files with 32 additions and 11 deletions

Binary file not shown.

View File

@ -45,6 +45,7 @@ import time
import sqlite3 import sqlite3
import redis import redis
import json import json
from datetime import timedelta
from ipwhois import IPWhois from ipwhois import IPWhois
import ipwhois import ipwhois
@ -81,13 +82,16 @@ class SnoParser(callbacks.Plugin):
asn = whoisres['asn_registry'] asn = whoisres['asn_registry']
country = whoisres['asn_country_code'] country = whoisres['asn_country_code']
description = whoisres['asn_description'] description = whoisres['asn_description']
whoisout = asn + ' ' + country + ' ' + description whoisout = 'WHOIS ' + asn + ' ' + country + ' ' + description
except ipwhois.exceptions.IPDefinedError: except ipwhois.exceptions.IPDefinedError:
whoisout = 'RFC 4291 (Local)' whoisout = 'RFC 4291 (Local)'
rr = f"{results}" # rr = f"{results}"
response = SnoParser.redis_client.get(rr) # response = rr
return response.json() # return response.json()
# response = results
response = whoisout
return response
def whois_get_cache(key: str) -> str: def whois_get_cache(key: str) -> str:
"""Data from Redis.""" """Data from Redis."""
@ -98,24 +102,37 @@ class SnoParser(callbacks.Plugin):
def whois_set_cache(key: str, value: str) -> bool: def whois_set_cache(key: str, value: str) -> bool:
"""Data to Redis.""" """Data to Redis."""
state = 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(coordinates: str) -> dict:
data = SnoParser.whois_get_cache(key=coordinates) data = SnoParser.whois_get_cache(key=coordinates)
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(data)
print(coordinates)
return data return data
else: else:
data = SnoParser.whois_fresh(coordinates) data = SnoParser.whois_fresh(coordinates)
if data.get("code") == "Ok": print("ELSE WHOIS_FRESH CALLED")
data["cache"] = False print(data)
print(coordinates)
if data.startswith("WHOIS"):
#data["cache"] = False
print("DEBUG - CACHE: FALSE")
data = json.dumps(data) data = json.dumps(data)
state = self.whois_set_cache(key=coordinates, value=data) state = SnoParser.whois_set_cache(key=coordinates, value=data)
print(data)
print(coordinates)
if state is True: if state is True:
return json.loads(data) return json.loads(data)
else:
print("Data does not start with correct string")
print(data)
return data return data
@ -132,14 +149,18 @@ 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 = '81.217.9.47' ip = '::1'
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) # SnoParser.whois_run(coordinates=ip)
whoisout = SnoParser.whois_run(ip)
# whoisout = SnoParser.whois_run(whoisout)
# SnoParser.whois_run(ip)
# whoisout = SnoParser.whois_run.coordinates
# try: # try:
# #whois = IPWhois(ip) # #whois = IPWhois(ip)