From f796a296911f71edc80e9e24e33413ce91af9003 Mon Sep 17 00:00:00 2001 From: Georg Date: Thu, 26 Aug 2021 12:50:25 +0200 Subject: [PATCH] First working Redis cache Signed-off-by: Georg --- __pycache__/plugin.cpython-38.pyc | Bin 7286 -> 7481 bytes plugin.py | 43 ++++++++++++++++++++++-------- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/__pycache__/plugin.cpython-38.pyc b/__pycache__/plugin.cpython-38.pyc index 70c3898b1d88d2f89e8863963c2f7192cf360052..790a6db71b736566d75de6f2a4931fe43b5da64e 100644 GIT binary patch delta 3138 zcmZ`*-BVlF5x?i^>gq}e&?l0RWG)5_GA4}^Y-}34H6J!KSlE0xHYTwZof{Aci5y*O zM>(n{Dn~Z1LnZEQ7=tCbheeiUqGkM5s`p{`-x_cxce(A1YL{w6)Ghj-|L2UrJEdypn49H6b3io!hXemA5lr{FgXYazZ$?ZgN5NY13!I z;6F<+O5=kO4fu}3xAY+hN-Bh^6}5az{8AhBQG=6+LfsLC>-ZGUv`D;66#S!}ig8CN zsJ9hvRNUN+1VpdXdbO;-F*Q3qZ~mK@W#8P@adY^Ui)YP&nPN7RPo;Dgf;hF7e6hH` zwRw-x%qBC%m(Bq#&`s;s-&sIBfgIN1Y@ ztx@$6A&RYW(s(NPpik~}ipWIE!HLPS#cSq}Ieuk)YI4+Em|L7IMln6zk>%tFyNOLm6Y z=7XHQYG#X!Sy@!ETwx{Uu6N=R?5b&NrDCDx%oj7X#7uN{5!D{KyY%1$Y_B=#*2ZRu zjllwRs|VLL3bEJ~aowF9&V!}^TiwTUF2t4+F| zqjU=&H_3i2De6^V`!yY|bV`n$$8to9bz)fPC_M+nkka{x{V;m(z zFG7u~`nH4poQ_uFl0f{01P(f#IHuD{`fXXQ1Q1Ko*N$}hQj^$aK=np{2hew8N)^uP zLFus5SB~#A9h1jWV_rJmL=%-0k~SRKLkF}=QpCEMbdq;*lV06%9IDkdh%^O}stTP3 z_&pg^f38HYH$uHdXPW4GrCSE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SbN diff --git a/plugin.py b/plugin.py index 7cab169..bfa31a9 100644 --- a/plugin.py +++ b/plugin.py @@ -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)