From 7d9db9dd9d75b5a3aaff4617b95563125d9c1bdf Mon Sep 17 00:00:00 2001 From: Georg Date: Thu, 26 Aug 2021 16:36:34 +0200 Subject: [PATCH] Moving Redis options to config values Signed-off-by: Georg --- __pycache__/config.cpython-38.pyc | Bin 2224 -> 2233 bytes __pycache__/plugin.cpython-38.pyc | Bin 7679 -> 8221 bytes config.py | 6 ++-- plugin.py | 50 +++++++++++++++++++++--------- 4 files changed, 38 insertions(+), 18 deletions(-) diff --git a/__pycache__/config.cpython-38.pyc b/__pycache__/config.cpython-38.pyc index bf4d66cdf6aa34fd0f929b62c657d5aca3c5f01d..ee342f3ce1d412b456e0e698371a56d199263644 100644 GIT binary patch delta 81 zcmdlWxKoffl$V!_0SJ=X)Ds_X k6xCFw1^k<*Fmkgo3QS(WUe0-o-7~KwH9fUxvN*>~01`SC00000 delta 71 zcmdlfxIvINl$V!_0SLBKt0z9#$ScOk$TeAoF;to>ML9)fA=5%;Mut?z1)QlIS-e?% aDXOVV3%E81J9w|~HDN&RZ%AzXCHXU1y)7Xh+HFj(#{)j(h%a#@8gFbUl5+&-W z?j2=IU4jN|#Bf@pD)y;~RETKO#(fJ46h->hKC}h$7aUp?=t}_u4N#yjZGpBkOG=hy z_XzI(c6N5=x3jZ*d;dMUe!TTsEEZAV8ToXNJ^Jbwt!pIlh+KT6W~Y2=^dW6+La+i9h( zYZ`5%agelajY!ZWwBxj$cHC2@5>C=d(9{gMt4<|oCryK_op#Y~s5@v6eF5s!eMMKM zjLvZ-ml3~HM`{FFH6IOXoX|=5dmM_DNne>GT-i`5sW)o1S!>N&Z`MYg_?px->*Ow> zp@(LbaI&G-m8!~BYRy}0f|KjYxN==_cj*dPHs&J{wnP;C`KI_#dpr;d4Cc_H7Z_J$ z6=Tj!(ev5zgk7pQfmU>V@gsd;w-)H0Q<@H=M3e4924W|_zYSva-sfJ-$GO#S#oQ?~_RvPO=fd|PNC{U0vWG8)R3{bZ0#AtOvq^sl7{E_&TWSC+s>7t4kVp# z`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onRxPr)7W5 zUr*s_ka+q%r(E)kVmU`0wi9(upf~^-WUlk5;Ldplt=wO5*m;mcmIvi33T4AEP1}>~ zua^X?lnOBG?1ctufmkG~ov~XoH2Aetbqb%> zyve>5#n9E7>szyU0K{P$;683~KM(L09^@g~vSL*uG{_?~)TMl(&{jZg5`_UpB#Hux zNfZavE>Q;$gLVSOopRg-s9T~PfRcz-qtz(<&HOw?xXPpJ8c}EuvRWqW545+5QewLG zU@Z;w65NE{^#SUneLT+Fc?VDMPP%i&1d{z=xK45a(4cg-3($~6y8&e++5_kIN45SlfvO9 zg}`OpUM zw6=Ss$7Iqq_6vGkoCbkfL@n- zaf1(liD}*s?MFNSrBVs5$(X?lP6D| zYD=r5h5Y>Swltf-sZd(I=Gd&1yKkcmSEB4v*>N{y9Vqjq)yre3F$p8Kuc0GoRc|#cp0BW+ zTPT-Mh6m*$5T3qzdGtbh>dAtncn78a%`mcJxP5#F#>vpQw8T(}Wl^&`E&d+w5`SvH z65iCQPm5&7XPuiuq~Mv2d(s{8vSZ4$(afw!B(4MfQX)m>#7`1Eq#%Bq7&hg~S@EyL zI!rEiCdj0C-udewiaosyaj^w)v+G#MBeydxo}OcO9k8|5wb#U}Cc7g3)|CpUP+Kdo zyrbcWcK{lBj^@3&N_Q1H6!WzWf|k)%F`0TZBnX zXuSut(M((%>3z4BL`!mWdk_s^(+`cO0XI*lg}f_|cy=0v`X}XbQ6BB=3TiH6gOsJ^ zEwRY7;U!Yu9o_-p;0D5Z4kvmvY^vc{*o>NC(kEI{!^9JpQb))m@o6ggo@8_plsyZs p4YpgjZ_6tidmGKkd#^cGtlTY>q;1bQSIjxiYre%lQYXmG{{!_MHH82G diff --git a/config.py b/config.py index 3957a57..cb1084a 100644 --- a/config.py +++ b/config.py @@ -78,7 +78,7 @@ conf.registerGlobalValue(SnoParser.whois.redis, 'host', , private=True )) conf.registerGlobalValue(SnoParser.whois.redis, 'port', - registry.String('6379', + registry.Integer('6379', """ Redis: Port. """ @@ -98,13 +98,13 @@ conf.registerGlobalValue(SnoParser.whois.redis, 'password', """ )) 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. """ )) 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. """ diff --git a/plugin.py b/plugin.py index 997b2bd..39e2457 100644 --- a/plugin.py +++ b/plugin.py @@ -53,25 +53,27 @@ class SnoParser(callbacks.Plugin): """Parses the Server Notices from ErgoIRCd""" threaded = True - def redis_connect() -> redis.client.Redis: + def redis_connect(self) -> redis.client.Redis: try: redis_client = redis.Redis( - host = .registryValue('whois.redis.host'), + host = self.registryValue('whois.redis.host'), port = self.registryValue('whois.redis.port'), password = self.registryValue('whois.redis.password'), username = self.registryValue('whois.redis.username'), 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() if ping is True: return redis_client except redis.AuthenticationError: print("Could not authenticate to Redis backend.") - - redis_client = redis_connect() - def whois_fresh(sourceip: str) -> dict: + def __init__(self, irc): + super().__init__(irc) + self.redis_client = self.redis_connect() + + def whois_fresh(self, sourceip: str) -> dict: """Data from cache.""" asn = 0 subnet = '' @@ -90,22 +92,26 @@ class SnoParser(callbacks.Plugin): response = whoisout return response - def whois_get_cache(key: str) -> str: + def whois_get_cache(self, key: str) -> str: """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 - def whois_set_cache(key: str, value: str) -> bool: + def whois_set_cache(self, key: str, value: str) -> bool: """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 - def whois_run(sourceip: str) -> dict: + def whois_run(self, sourceip: str) -> dict: """Whois query router.""" - data = SnoParser.whois_get_cache(key=sourceip) + data = self.whois_get_cache(key=sourceip) if data is not None: data = json.loads(data) #data["cache"] = True @@ -114,7 +120,7 @@ class SnoParser(callbacks.Plugin): print(sourceip) return data else: - data = SnoParser.whois_fresh(sourceip) + data = self.whois_fresh(sourceip) print("DEBUG - ELSE WHOIS_FRESH CALLED") print(data) print(sourceip) @@ -122,7 +128,7 @@ class SnoParser(callbacks.Plugin): #data["cache"] = False print("DEBUG - CACHE: FALSE") 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(sourceip) @@ -134,6 +140,20 @@ class SnoParser(callbacks.Plugin): print(data) return data + def query(self, irc, msg, args, ipaddress): + """ + 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): (target, text) = msg.args @@ -155,7 +175,7 @@ class SnoParser(callbacks.Plugin): asn = 0 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} #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']}"