From 415d5df94ce6ed024302852dd3fca6bc07dcaf99 Mon Sep 17 00:00:00 2001 From: Georg Date: Thu, 26 Aug 2021 13:51:26 +0200 Subject: [PATCH] First prettifications of Redis cached whois Signed-off-by: Georg --- __pycache__/config.cpython-38.pyc | Bin 1008 -> 2224 bytes __pycache__/plugin.cpython-38.pyc | Bin 7481 -> 7679 bytes config.py | 49 ++++++++++++++++++++++++++ plugin.py | 56 ++++++++++-------------------- 4 files changed, 68 insertions(+), 37 deletions(-) diff --git a/__pycache__/config.cpython-38.pyc b/__pycache__/config.cpython-38.pyc index 2c3345fdb3e8c5b74bd76245e270729ba83d62b7..bf4d66cdf6aa34fd0f929b62c657d5aca3c5f01d 100644 GIT binary patch literal 2224 zcmd5+OLH4V5Z+zw>SfDu9+(gy86YH45hMpH1Y9l`5DLhFlnXmC=t4%C_U<~nGh$}e zjs!lzmH%LKgEPNoPE>K?%#EVT^vp_%Tm;VS)^yEuf79Ljb#FgtwVED2zkG9@{(9E) zezS-9r-6s}aVhP49`l%=dAZ;7^Pm^_-uNPFf~=BsyUbN%!3%+Mx7P9Dj zuNQsk@!$)ORap&BRq&5$ta9x4>bPq_oz)>YYO)4v9tX!2^iygVoOY45PHC4=yTHz{ z<(YO7J)AwpY|49kEp`rqy#;ojT|nG(KU5|@5r?*wdF0*_dmgb(c9Fes3X56owa|LW zSzlx?J=6NKvtDLbo@pg6|I6%^S*BC3uR6da;j6oX_k)N#x%CL*rZZ{9AM>UkKNmK7ncfAeCuHh z|Nc)XjQ>PdQRT#+tKCH8(2YUJxSORp7c!;Yp^K=xGw)rSRT_7OBU4|`Xx?XZ;~F~o z1D7Xl`<#j&p~8lB@o(UU`Pf0&Q-|17he2LB#BMsoZqm_Fl~q`1>&0x2(S2zXZ-=G| zPlhnircU`tCn=9jLt(1rHW+bfgKW2J=Bz4)qrT9cuM1F?9wishBp0n{S+QhAfWj=5 zyn-w_ZAa*y?NdGg_RLE&qGa7(Z_n``7V zOf?%3m1dX_5kru!Ydg*(c4BQP(|xMJL=qSV4&C=RZoPGF3^xFbs!|0<~u!~&USN(`hR5`u)gtE$3M>#`w`W&Pq9IGk@rFV3v0*$ z5`+|{18fF%6NwdBxpSp|${LdUv=f~gC?jGQX~Y4691?hfW7F;hpOB26)7@Q=wguTF zp9#S=o#uc&Yy8{e(R|)pV(=I=*)GktEdcY|hs^dcVFLdp+YpbrI3NcJ)wcGtRv-xm zyGis>DGFg5;H zZqUeOJh>y~l$V!_0SJP8R1;@#GcY^`agYHAkmCTv#nTuj>P%rv;S6Tb6x_Uo@gO5( k)Z{c~8z8xu*#k(vVYX({<{9 diff --git a/__pycache__/plugin.cpython-38.pyc b/__pycache__/plugin.cpython-38.pyc index 790a6db71b736566d75de6f2a4931fe43b5da64e..8a822d77e271d02b6cbb951c96555aedee90a480 100644 GIT binary patch delta 1768 zcmZ8hPfQ$D7@s%)c6Q5d7Z!FG*uv13(rpVxkQlebB7d~B77HvCQz;qt4ean|X8UF~ zlL6Scr<$I!P6ujO>Deqycj*`_YI3sce3C2-tYVWzVE%S zbN^h5%|xRS0e-P}x{T(5$1zFT4#Rc?{_b5MmAogc5Kr8ZDA^^R3|y>nsmA3RS880{ zB{cN0x+PSF9c5S8B2_}QRgFzmg`2`O{OvYb0I%w5gfSxE=Y?qaJ|V(ef-P>(2^7?_ zk%>xtqv%*}4>KvyqT_ncBPEA}5MtU&dIzjQ)M?z^AVr_~D;QLs`vNg1mfX3e#gzI4nQP%w@~<*ygOURyKXvHP@E- zFH%3zc}6~y&VY_UL?RRI@cT>E_^f<7thr8^6-=wN_r5$t$VvX4a-MYZKa>)=%Bgya zi2MW9iJrnZ(z5AgMK;Lg&{;CiyF=&5AfF4_okJka%0V(L&{SO0Gw-ql;u0GK#)kN> zpkLrgEq_d04YM3}w@UcuTHDoWXv~gM6?VxMG@HuU6i=>_8W)gP$&N~)+3YLOPy>m$ z6RN^^fQ_)CRY8M>_)l7D8QcENzWZG=E!c6e2pWChvkE8`vD=lzO>DM)r);uKok1r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=vq8@l!@Ox^vho-`yR;rucnh2TYVEcRycDuiB3#_V(`{52Ore9$e ze4d`b+y8UAzf-85U0W7Pm6wH5_bQm{lXNyk=Mo%5Q2E1x&%;$)^H;XL_P_nmPn`sD~ql<(6E(TDI@Q z*`8rVi6r)xdM4SRhmvm?Z{d*Fgl;w(wquDH#;bZh-?(npnl^mb+cUXIvECyfZ4g>0 zCa(zXU8)LiCpLgM%{CAs4F2hTTmX9i1R<~T&u|dNou=KiU&l(&7m+_Clg9tIz&-fw zM4z^fwiL*Hjp^W^vZQ~8;P7LD>tg>@%YM*l{ICn(aot+bVSI*^4+tn7z7G3+qp=QM vj;G^#ryf6PY{OrDMRp5DvXiU{x3i~G(v5}JGHwK?(S6KZ&Z?|vy diff --git a/config.py b/config.py index a3a96e9..4c4f08d 100644 --- a/config.py +++ b/config.py @@ -61,4 +61,53 @@ conf.registerGlobalValue(SnoParser, 'AutoVhost', conf.registerGlobalValue(SnoParser, 'preventHighlight', 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: diff --git a/plugin.py b/plugin.py index bfa31a9..6b29756 100644 --- a/plugin.py +++ b/plugin.py @@ -56,11 +56,12 @@ class SnoParser(callbacks.Plugin): def redis_connect() -> redis.client.Redis: try: redis_client = redis.Redis( - host="localhost", - port=6378, - #password="test", - db=0, - socket_timeout=5, + host = .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') ) ping = redis_client.ping() if ping is True: @@ -70,12 +71,12 @@ class SnoParser(callbacks.Plugin): redis_client = redis_connect() - def whois_fresh(coordinates: str) -> dict: + def whois_fresh(sourceip: str) -> dict: """Data from cache.""" asn = 0 subnet = '' try: - whois = IPWhois(coordinates) + whois = IPWhois(sourceip) whoisres = whois.lookup_rdap(depth=1,retry_count=0) results = whoisres print(results) @@ -86,10 +87,6 @@ class SnoParser(callbacks.Plugin): except ipwhois.exceptions.IPDefinedError: whoisout = 'RFC 4291 (Local)' -# rr = f"{results}" -# response = rr -# return response.json() -# response = results response = whoisout return response @@ -105,28 +102,30 @@ class SnoParser(callbacks.Plugin): 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) + def whois_run(sourceip: str) -> dict: + """Whois query router.""" + + data = SnoParser.whois_get_cache(key=sourceip) if data is not None: data = json.loads(data) #data["cache"] = True print("DEBUG - CACHE: TRUE") print(data) - print(coordinates) + print(sourceip) return data else: - data = SnoParser.whois_fresh(coordinates) - print("ELSE WHOIS_FRESH CALLED") + data = SnoParser.whois_fresh(sourceip) + print("DEBUG - ELSE WHOIS_FRESH CALLED") print(data) - print(coordinates) + print(sourceip) if data.startswith("WHOIS"): #data["cache"] = False print("DEBUG - CACHE: FALSE") 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(coordinates) + print(sourceip) if state is True: return json.loads(data) @@ -149,31 +148,14 @@ class SnoParser(callbacks.Plugin): username = couple.group(2) host = couple.group(3) #ip = couple.group(4) - ip = '::1' + ip = '2a03:4000:55:d20::' realname = couple.group(5) ip_seen = 0 nick_seen = 0 asn = 0 subnet = '' -# 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) -# #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} #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']}"