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