From c4941a26c1f1d127a7eaa315705aedc439edb15d Mon Sep 17 00:00:00 2001 From: Georg Date: Thu, 26 Aug 2021 11:41:41 +0200 Subject: Init Signed-off-by: Georg --- __pycache__/__init__.cpython-38.pyc | Bin 0 -> 637 bytes __pycache__/config.cpython-38.pyc | Bin 0 -> 1008 bytes __pycache__/plugin.cpython-38.pyc | Bin 0 -> 7286 bytes plugin.py | 98 ++++++++++++++++++++++++++++++++++-- 4 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 __pycache__/__init__.cpython-38.pyc create mode 100644 __pycache__/config.cpython-38.pyc create mode 100644 __pycache__/plugin.cpython-38.pyc diff --git a/__pycache__/__init__.cpython-38.pyc b/__pycache__/__init__.cpython-38.pyc new file mode 100644 index 0000000..de1a3d3 Binary files /dev/null and b/__pycache__/__init__.cpython-38.pyc differ diff --git a/__pycache__/config.cpython-38.pyc b/__pycache__/config.cpython-38.pyc new file mode 100644 index 0000000..2c3345f Binary files /dev/null and b/__pycache__/config.cpython-38.pyc differ diff --git a/__pycache__/plugin.cpython-38.pyc b/__pycache__/plugin.cpython-38.pyc new file mode 100644 index 0000000..70c3898 Binary files /dev/null and b/__pycache__/plugin.cpython-38.pyc differ diff --git a/plugin.py b/plugin.py index 8c3717e..7cab169 100644 --- a/plugin.py +++ b/plugin.py @@ -43,17 +43,87 @@ import os import sys import time import sqlite3 +import redis +import json +from ipwhois import IPWhois +import ipwhois class SnoParser(callbacks.Plugin): """Parses the Server Notices from ErgoIRCd""" threaded = True + def redis_connect() -> redis.client.Redis: + try: + redis_client = redis.Redis( + host="localhost", + port=6378, + #password="test", + db=0, + socket_timeout=5, + ) + 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(coordinates: str) -> dict: + """Data from cache.""" + asn = 0 + subnet = '' + try: + whois = IPWhois(coordinates) + whoisres = whois.lookup_rdap(depth=1,retry_count=0) + results = whoisres + 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)' + + rr = f"{results}" + response = SnoParser.redis_client.get(rr) + return response.json() + + def whois_get_cache(key: str) -> str: + """Data from Redis.""" + + val = SnoParser.redis_client.get(key) + return val + + def whois_set_cache(key: str, value: str) -> bool: + """Data to Redis.""" + + state = 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 + return data + else: + data = SnoParser.whois_fresh(coordinates) + if data.get("code") == "Ok": + data["cache"] = False + data = json.dumps(data) + state = self.whois_set_cache(key=coordinates, value=data) + + if state is True: + return json.loads(data) + return data + + def doNotice(self, irc, msg): (target, text) = msg.args + if target == irc.nick: - # server notices CONNECT, KILL, XLINE, NICK, ACCOUNT - text = ircutils.stripFormatting(text) if 'CONNECT' in text: connregex = "^-CONNECT- Client connected \[(.+)\] \[u\:~(.+)\] \[h\:(.+)\] \[ip\:(.+)\] \[r\:(.+)\]$" @@ -61,12 +131,32 @@ class SnoParser(callbacks.Plugin): nickname = couple.group(1) username = couple.group(2) host = couple.group(3) - ip = couple.group(4) + #ip = couple.group(4) + ip = '81.217.9.47' realname = couple.group(5) ip_seen = 0 nick_seen = 0 + asn = 0 + subnet = '' + + SnoParser.whois_run(coordinates=ip) + +# 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\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']}" + self._sendSnotice(irc, msg, repl) if 'XLINE' in text and 'temporary' in text: xlineregex = "^-XLINE- (.+) \[(.+)\] added temporary \((.+)\) (K-Line|D-Line) for (.+)$" -- cgit v1.2.3 From f796a296911f71edc80e9e24e33413ce91af9003 Mon Sep 17 00:00:00 2001 From: Georg Date: Thu, 26 Aug 2021 12:50:25 +0200 Subject: 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 70c3898..790a6db 100644 Binary files a/__pycache__/plugin.cpython-38.pyc and b/__pycache__/plugin.cpython-38.pyc differ 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) -- cgit v1.2.3 From 415d5df94ce6ed024302852dd3fca6bc07dcaf99 Mon Sep 17 00:00:00 2001 From: Georg Date: Thu, 26 Aug 2021 13:51:26 +0200 Subject: 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 2c3345f..bf4d66c 100644 Binary files a/__pycache__/config.cpython-38.pyc and b/__pycache__/config.cpython-38.pyc differ diff --git a/__pycache__/plugin.cpython-38.pyc b/__pycache__/plugin.cpython-38.pyc index 790a6db..8a822d7 100644 Binary files a/__pycache__/plugin.cpython-38.pyc and b/__pycache__/plugin.cpython-38.pyc differ 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']}" -- cgit v1.2.3 From a8665607bbef6f96955ef53e1c14a4aa85a1f8d8 Mon Sep 17 00:00:00 2001 From: Georg Date: Thu, 26 Aug 2021 13:52:34 +0200 Subject: Adding name to license blocks Signed-off-by: Georg --- config.py | 2 +- plugin.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/config.py b/config.py index 4c4f08d..3957a57 100644 --- a/config.py +++ b/config.py @@ -1,5 +1,5 @@ ### -# Copyright (c) 2021, mogad0n +# Copyright (c) 2021, mogad0n and Georg Pfuetzenreuter # All rights reserved. # # Redistribution and use in source and binary forms, with or without diff --git a/plugin.py b/plugin.py index 6b29756..997b2bd 100644 --- a/plugin.py +++ b/plugin.py @@ -1,5 +1,5 @@ ### -# Copyright (c) 2021, mogad0n +# Copyright (c) 2021, mogad0n and Georg Pfuetzenreuter # All rights reserved. # # Redistribution and use in source and binary forms, with or without -- cgit v1.2.3 From 7d9db9dd9d75b5a3aaff4617b95563125d9c1bdf Mon Sep 17 00:00:00 2001 From: Georg Date: Thu, 26 Aug 2021 16:36:34 +0200 Subject: 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 bf4d66c..ee342f3 100644 Binary files a/__pycache__/config.cpython-38.pyc and b/__pycache__/config.cpython-38.pyc differ diff --git a/__pycache__/plugin.cpython-38.pyc b/__pycache__/plugin.cpython-38.pyc index 8a822d7..37a61da 100644 Binary files a/__pycache__/plugin.cpython-38.pyc and b/__pycache__/plugin.cpython-38.pyc differ 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']}" -- cgit v1.2.3 From a458460cec77aceefdcd9b966e5c1813ea16c883 Mon Sep 17 00:00:00 2001 From: Georg Date: Thu, 26 Aug 2021 16:58:04 +0200 Subject: Adding option for sample IP Signed-off-by: Georg --- __pycache__/config.cpython-38.pyc | Bin 2233 -> 2453 bytes __pycache__/plugin.cpython-38.pyc | Bin 8221 -> 8248 bytes config.py | 10 ++++++++++ plugin.py | 10 ++++++---- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/__pycache__/config.cpython-38.pyc b/__pycache__/config.cpython-38.pyc index ee342f3..172cadd 100644 Binary files a/__pycache__/config.cpython-38.pyc and b/__pycache__/config.cpython-38.pyc differ diff --git a/__pycache__/plugin.cpython-38.pyc b/__pycache__/plugin.cpython-38.pyc index 37a61da..461f9d9 100644 Binary files a/__pycache__/plugin.cpython-38.pyc and b/__pycache__/plugin.cpython-38.pyc differ diff --git a/config.py b/config.py index cb1084a..9617979 100644 --- a/config.py +++ b/config.py @@ -61,6 +61,9 @@ conf.registerGlobalValue(SnoParser, 'AutoVhost', conf.registerGlobalValue(SnoParser, 'preventHighlight', registry.Boolean(True, ("""Toggles in channel highlights with ZWSP"""))) +### +## WHOIS related settings below: +### conf.registerGroup(SnoParser, 'whois') conf.registerGlobalValue(SnoParser.whois, 'debug', registry.Boolean('false', @@ -69,6 +72,13 @@ conf.registerGlobalValue(SnoParser.whois, 'debug', """ , private=True )) +conf.registerGlobalValue(SnoParser.whois, 'sample', + registry.String('', + """ + SnoParser: This allows to set a testing IP address, if the plugin shall be evaluated on i.e. a local network. This will override all IP addresses from SNOTICES! + """ + , private=True +)) conf.registerGroup(SnoParser.whois, 'redis') conf.registerGlobalValue(SnoParser.whois.redis, 'host', registry.String('127.0.0.1', diff --git a/plugin.py b/plugin.py index 39e2457..10eddb5 100644 --- a/plugin.py +++ b/plugin.py @@ -85,7 +85,7 @@ class SnoParser(callbacks.Plugin): asn = whoisres['asn_registry'] country = whoisres['asn_country_code'] description = whoisres['asn_description'] - whoisout = 'WHOIS ' + asn + ' ' + country + ' ' + description + whoisout = '# ' + asn + ' ' + country + ' ' + description except ipwhois.exceptions.IPDefinedError: whoisout = 'RFC 4291 (Local)' @@ -124,7 +124,7 @@ class SnoParser(callbacks.Plugin): print("DEBUG - ELSE WHOIS_FRESH CALLED") print(data) print(sourceip) - if data.startswith("WHOIS"): + if data.startswith("#"): #data["cache"] = False print("DEBUG - CACHE: FALSE") data = json.dumps(data) @@ -167,8 +167,10 @@ class SnoParser(callbacks.Plugin): nickname = couple.group(1) username = couple.group(2) host = couple.group(3) - #ip = couple.group(4) - ip = '2a03:4000:55:d20::' + if self.registryValue('whois.sample'): + ip = self.registryValue('whois.sample') + else: + ip = couple.group(4) realname = couple.group(5) ip_seen = 0 nick_seen = 0 -- cgit v1.2.3 From 708e4b70755ecb71e63602077f2424965f277049 Mon Sep 17 00:00:00 2001 From: Georg Date: Thu, 26 Aug 2021 17:30:38 +0200 Subject: Improved debug and manual query Signed-off-by: Georg --- __pycache__/plugin.cpython-38.pyc | Bin 8248 -> 8505 bytes plugin.py | 59 +++++++++++++++++++------------------- 2 files changed, 29 insertions(+), 30 deletions(-) diff --git a/__pycache__/plugin.cpython-38.pyc b/__pycache__/plugin.cpython-38.pyc index 461f9d9..7fa3b98 100644 Binary files a/__pycache__/plugin.cpython-38.pyc and b/__pycache__/plugin.cpython-38.pyc differ diff --git a/plugin.py b/plugin.py index 10eddb5..d47440f 100644 --- a/plugin.py +++ b/plugin.py @@ -78,16 +78,17 @@ class SnoParser(callbacks.Plugin): asn = 0 subnet = '' try: - whois = IPWhois(sourceip) - whoisres = whois.lookup_rdap(depth=1,retry_count=0) - results = whoisres - print(results) - asn = whoisres['asn_registry'] - country = whoisres['asn_country_code'] - description = whoisres['asn_description'] - whoisout = '# ' + asn + ' ' + country + ' ' + description + whois = IPWhois(sourceip) + whoisres = whois.lookup_rdap(depth=1,retry_count=0) + results = whoisres + if self.registryValue('whois.debug'): + 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)' + whoisout = 'RFC 4291 (Local)' response = whoisout return response @@ -114,30 +115,30 @@ class SnoParser(callbacks.Plugin): data = self.whois_get_cache(key=sourceip) if data is not None: data = json.loads(data) - #data["cache"] = True - print("DEBUG - CACHE: TRUE") - print(data) - print(sourceip) + if self.registryValue('whois.debug'): + print("SNOPARSER DEBUG - WHOIS_RUN WITH CACHE: TRUE") + print(data) + print(sourceip) return data else: data = self.whois_fresh(sourceip) - print("DEBUG - ELSE WHOIS_FRESH CALLED") - print(data) - print(sourceip) - if data.startswith("#"): - #data["cache"] = False - print("DEBUG - CACHE: FALSE") + if self.registryValue('whois.debug'): + print("SNOPARSER DEBUG - WHOIS_RUN WITH CACHE: FALSE") + print(data) + print(sourceip) + if data.startswith: + if self.registryValue('whois.debug'): + print("SNOPARSER DEBUG - WHOIS_RUN WITH CACHE: FALSE AND CORRECT STARTING CHARACTER") + print(data) data = json.dumps(data) state = self.whois_set_cache(key=sourceip, value=data) - print(data) - print(sourceip) - if state is True: return json.loads(data) else: - print("Data does not start with correct string") - print(data) + if self.registryValue('whois.debug'): + print("SNOPARSER DEBUG _ WHOIS_RUN WITH CACHE: FALSE AND WRONG STARTING CHARACTER") + print(data) return data def query(self, irc, msg, args, ipaddress): @@ -146,11 +147,11 @@ class SnoParser(callbacks.Plugin): """ data = self.whois_get_cache(key=ipaddress) - ttl = self.redis_client.get(ipaddress) + decoded = data.decode('utf-8') + ttl = self.redis_client.ttl(ipaddress) - print(data, ' ', ttl) - #irc.reply(str(data), ' Remaining: ', int(ttl), 's') - irc.reply(data, ttl) + print('SnoParser manual query: ', data, ' ', ttl) + irc.reply(f'{decoded} - Remaining: {ttl}s') query = wrap(query, ['anything']) @@ -174,8 +175,6 @@ class SnoParser(callbacks.Plugin): realname = couple.group(5) ip_seen = 0 nick_seen = 0 - asn = 0 - subnet = '' 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} -- cgit v1.2.3 From 78161bda7f95cfd72b1386ab7bb93c4aad7b741c Mon Sep 17 00:00:00 2001 From: Georg Date: Thu, 26 Aug 2021 19:18:14 +0200 Subject: Added nickname and IP address counting Signed-off-by: Georg --- __pycache__/config.cpython-38.pyc | Bin 2453 -> 2704 bytes __pycache__/plugin.cpython-38.pyc | Bin 8505 -> 10414 bytes config.py | 66 +++++++++++++------ plugin.py | 135 ++++++++++++++++++++++++++++++++------ 4 files changed, 161 insertions(+), 40 deletions(-) diff --git a/__pycache__/config.cpython-38.pyc b/__pycache__/config.cpython-38.pyc index 172cadd..d7e3edd 100644 Binary files a/__pycache__/config.cpython-38.pyc and b/__pycache__/config.cpython-38.pyc differ diff --git a/__pycache__/plugin.cpython-38.pyc b/__pycache__/plugin.cpython-38.pyc index 7fa3b98..1a89f54 100644 Binary files a/__pycache__/plugin.cpython-38.pyc and b/__pycache__/plugin.cpython-38.pyc differ diff --git a/config.py b/config.py index 9617979..983cb6d 100644 --- a/config.py +++ b/config.py @@ -61,62 +61,90 @@ conf.registerGlobalValue(SnoParser, 'AutoVhost', conf.registerGlobalValue(SnoParser, 'preventHighlight', registry.Boolean(True, ("""Toggles in channel highlights with ZWSP"""))) +conf.registerGlobalValue(SnoParser, 'debug', + registry.Boolean('false', + """ + SnoParser: Verbose output. Note: there is a seperate debug option for the `whois` client. + """ + , private=True +)) + ### -## WHOIS related settings below: +# REDIS related settings below: ### -conf.registerGroup(SnoParser, 'whois') -conf.registerGlobalValue(SnoParser.whois, 'debug', - registry.Boolean('false', +conf.registerGroup(SnoParser, 'redis') +conf.registerGlobalValue(SnoParser.redis, 'db1', + registry.Integer('1', """ - SnoParser: True: Very verbose console output. False: Mostly silent operation. + Redis: Database number for counting of NICKNAMES. """ , private=True )) -conf.registerGlobalValue(SnoParser.whois, 'sample', - registry.String('', +conf.registerGlobalValue(SnoParser.redis, 'db2', + registry.Integer('2', """ - SnoParser: This allows to set a testing IP address, if the plugin shall be evaluated on i.e. a local network. This will override all IP addresses from SNOTICES! + Redis: Database number for counting of IP ADDRESSES. """ , private=True )) -conf.registerGroup(SnoParser.whois, 'redis') -conf.registerGlobalValue(SnoParser.whois.redis, 'host', +conf.registerGlobalValue(SnoParser.redis, 'host', registry.String('127.0.0.1', """ Redis: IP address or hostname. """ , private=True )) -conf.registerGlobalValue(SnoParser.whois.redis, 'port', +conf.registerGlobalValue(SnoParser.redis, 'port', registry.Integer('6379', """ Redis: Port. """ , private=True )) -conf.registerGlobalValue(SnoParser.whois.redis, 'username', +conf.registerGlobalValue(SnoParser.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', +conf.registerGlobalValue(SnoParser.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.Integer('0', +conf.registerGlobalValue(SnoParser.redis, 'timeout', + registry.Integer('5', """ - 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. + Redis: Socket Timeout. The developer does not know what to recommend here, but `5` seems to yield good results. """ )) -conf.registerGlobalValue(SnoParser.whois.redis, 'timeout', - registry.Integer('5', + + +### +## WHOIS related settings below: +### +conf.registerGroup(SnoParser, 'whois') +conf.registerGlobalValue(SnoParser.whois, 'debug', + registry.Boolean('false', """ - Redis: Socket Timeout. The developer does not know what to recommend here, but `5` seems to yield good results. + SnoParser: True: Very verbose console output. False: Mostly silent operation. + """ + , private=True +)) +conf.registerGlobalValue(SnoParser.whois, 'sample', + registry.String('', + """ + SnoParser: This allows to set a testing IP address, if the plugin shall be evaluated on i.e. a local network. This will override all IP addresses from SNOTICES! + """ + , private=True +)) +conf.registerGroup(SnoParser.whois, 'redis') +conf.registerGlobalValue(SnoParser.whois.redis, 'db', + registry.Integer('0', + """ + Redis: Database number for WHOIS query caching. """ )) diff --git a/plugin.py b/plugin.py index d47440f..4d9a797 100644 --- a/plugin.py +++ b/plugin.py @@ -53,25 +53,60 @@ class SnoParser(callbacks.Plugin): """Parses the Server Notices from ErgoIRCd""" threaded = True - def redis_connect(self) -> redis.client.Redis: + def redis_connect_whois(self) -> redis.client.Redis: try: - redis_client = redis.Redis( - host = self.registryValue('whois.redis.host'), - port = self.registryValue('whois.redis.port'), - password = self.registryValue('whois.redis.password'), - username = self.registryValue('whois.redis.username'), + redis_client_whois = redis.Redis( + host = self.registryValue('redis.host'), + port = self.registryValue('redis.port'), + password = self.registryValue('redis.password'), + username = self.registryValue('redis.username'), db = self.registryValue('whois.redis.db'), - socket_timeout = int(self.registryValue('whois.redis.timeout')) + socket_timeout = int(self.registryValue('redis.timeout')) ) - ping = redis_client.ping() + ping = redis_client_whois.ping() if ping is True: - return redis_client + return redis_client_whois except redis.AuthenticationError: print("Could not authenticate to Redis backend.") + def redis_connect_nicks(self) -> redis.client.Redis: + try: + redis_client_nicks = redis.Redis( + host = self.registryValue('redis.host'), + port = self.registryValue('redis.port'), + password = self.registryValue('redis.password'), + username = self.registryValue('redis.username'), + db = self.registryValue('redis.db1'), + socket_timeout = int(self.registryValue('redis.timeout')) + ) + ping = redis_client_nicks.ping() + if ping is True: + return redis_client_nicks + except redis.AuthenticationError: + print("Could not authenticate to Redis backend.") + + def redis_connect_ips(self) -> redis.client.Redis: + try: + redis_client_ips = redis.Redis( + host = self.registryValue('redis.host'), + port = self.registryValue('redis.port'), + password = self.registryValue('redis.password'), + username = self.registryValue('redis.username'), + db = self.registryValue('redis.db2'), + socket_timeout = int(self.registryValue('redis.timeout')) + ) + ping = redis_client_ips.ping() + if ping is True: + return redis_client_ips + except redis.AuthenticationError: + print("Could not authenticate to Redis backend.") + + def __init__(self, irc): super().__init__(irc) - self.redis_client = self.redis_connect() + self.redis_client_whois = self.redis_connect_whois() + self.redis_client_nicks = self.redis_connect_nicks() + self.redis_client_ips = self.redis_connect_ips() def whois_fresh(self, sourceip: str) -> dict: """Data from cache.""" @@ -96,17 +131,17 @@ class SnoParser(callbacks.Plugin): def whois_get_cache(self, key: str) -> str: """Data from Redis.""" - k = self.redis_client.get(key) + k = self.redis_client_whois.get(key) # self = SnoParser() -# val = self.redis_client.get(key) +# val = self.redis_client_whois.get(key) val = k return val def whois_set_cache(self, key: str, value: str) -> bool: """Data to Redis.""" - state = self.redis_client.setex(key, timedelta(seconds=3600), value=value,) + state = self.redis_client_whois.setex(key, timedelta(seconds=3600), value=value,) return state def whois_run(self, sourceip: str) -> dict: @@ -141,19 +176,76 @@ class SnoParser(callbacks.Plugin): print(data) return data - def query(self, irc, msg, args, ipaddress): + def nick_run(self, nickname: str) -> dict: + """Tracks nicknames""" + + data = self.redis_client_nicks.get(nickname) + + if data is not None: + if self.registryValue('debug'): + print("SNOPARSER DEBUG - NICK_RUN, SEEN: TRUE") + print(nickname) + print(data) + self.redis_client_nicks.incrby(nickname,amount=1) + if data: + decoded = data.decode('utf-8') + return decoded + else: + return 0 + else: + if self.registryValue('debug'): + print("SNOPARSER DEBUG _ NICK_RUN, SEEN: FALSE") + print(nickname) + print(data) + self.redis_client_nicks.set(nickname,value='1') + if data: + decoded = data.decode('utf-8') + return decoded + else: + return 0 + + def ip_run(self, ipaddress: str) -> dict: + """Tracks IP addresses""" + + data = self.redis_client_ips.get(ipaddress) + + if data is not None: + if self.registryValue('debug'): + print("SNOPARSER DEBUG - IP_RUN, SEEN: TRUE") + print(ipaddress) + print(data) + self.redis_client_ips.incrby(ipaddress,amount=1) + if data: + decoded = data.decode('utf-8') + return decoded + else: + return 0 + else: + if self.registryValue('debug'): + print("SNOPARSER DEBUG _ IP_RUN, SEEN: FALSE") + print(ipaddress) + print(data) + self.redis_client_ips.set(ipaddress,value='1') + if data: + decoded = data.decode('utf-8') + return decoded + else: + return 0 + + def ipquery(self, irc, msg, args, ipaddress): """ Queries the cache for an address. """ data = self.whois_get_cache(key=ipaddress) decoded = data.decode('utf-8') - ttl = self.redis_client.ttl(ipaddress) + ttl = self.redis_client_whois.ttl(ipaddress) + count = self.redis_client_ips.get(ipaddress) print('SnoParser manual query: ', data, ' ', ttl) - irc.reply(f'{decoded} - Remaining: {ttl}s') + irc.reply(f'{decoded} - Count: {count} - Remaining: {ttl}s') - query = wrap(query, ['anything']) + ipquery = wrap(ipquery, ['anything']) def doNotice(self, irc, msg): @@ -173,13 +265,14 @@ class SnoParser(callbacks.Plugin): else: ip = couple.group(4) realname = couple.group(5) - ip_seen = 0 - nick_seen = 0 - whoisout = self.whois_run(sourceip=ip) + ip_seen = self.ip_run(ipaddress=ip) + nick_seen = self.nick_run(nickname=nickname) + whois = 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']}" + 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']} {whois} \x02Realname:\x0F {DictFromSnotice['realname']} \x02IPcount:\x0F {DictFromSnotice['ipCount']} \x02NickCount:\x0F {DictFromSnotice['nickCount']}" self._sendSnotice(irc, msg, repl) if 'XLINE' in text and 'temporary' in text: -- cgit v1.2.3 From c5a37126bc71f8330db54376f46d1ef0d5251c83 Mon Sep 17 00:00:00 2001 From: Georg Date: Thu, 26 Aug 2021 19:32:58 +0200 Subject: Improved `ipcounting` Signed-off-by: Georg --- __pycache__/config.cpython-38.pyc | Bin 2704 -> 2814 bytes __pycache__/plugin.cpython-38.pyc | Bin 10414 -> 10478 bytes config.py | 7 +++++++ plugin.py | 8 +++++--- 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/__pycache__/config.cpython-38.pyc b/__pycache__/config.cpython-38.pyc index d7e3edd..3666f16 100644 Binary files a/__pycache__/config.cpython-38.pyc and b/__pycache__/config.cpython-38.pyc differ diff --git a/__pycache__/plugin.cpython-38.pyc b/__pycache__/plugin.cpython-38.pyc index 1a89f54..2038462 100644 Binary files a/__pycache__/plugin.cpython-38.pyc and b/__pycache__/plugin.cpython-38.pyc differ diff --git a/config.py b/config.py index 983cb6d..90dd9c9 100644 --- a/config.py +++ b/config.py @@ -140,6 +140,13 @@ conf.registerGlobalValue(SnoParser.whois, 'sample', """ , private=True )) +conf.registerGlobalValue(SnoParser.whois, 'ttl', + registry.Integer('3600', + """ + SnoParser: How long to cache WHOIS entries for. + """ + , private=True +)) conf.registerGroup(SnoParser.whois, 'redis') conf.registerGlobalValue(SnoParser.whois.redis, 'db', registry.Integer('0', diff --git a/plugin.py b/plugin.py index 4d9a797..4b16961 100644 --- a/plugin.py +++ b/plugin.py @@ -141,7 +141,8 @@ class SnoParser(callbacks.Plugin): def whois_set_cache(self, key: str, value: str) -> bool: """Data to Redis.""" - state = self.redis_client_whois.setex(key, timedelta(seconds=3600), value=value,) + duration = self.registryValue('whois.ttl') + state = self.redis_client_whois.setex(key, timedelta(seconds=duration), value=value,) return state def whois_run(self, sourceip: str) -> dict: @@ -238,12 +239,13 @@ class SnoParser(callbacks.Plugin): """ data = self.whois_get_cache(key=ipaddress) - decoded = data.decode('utf-8') + decoded_data = data.decode('utf-8') ttl = self.redis_client_whois.ttl(ipaddress) count = self.redis_client_ips.get(ipaddress) + decoded_count = count.decode('utf-8') print('SnoParser manual query: ', data, ' ', ttl) - irc.reply(f'{decoded} - Count: {count} - Remaining: {ttl}s') + irc.reply(f'{decoded_data} - Count: {decoded_count} - Remaining: {ttl}s') ipquery = wrap(ipquery, ['anything']) -- cgit v1.2.3 From 6b6a35cd944f5c9eaabd7c6a8f46efee10fecd94 Mon Sep 17 00:00:00 2001 From: Georg Date: Fri, 27 Aug 2021 14:25:42 +0200 Subject: Whois + Caching + Nickname/IP address counting Signed-off-by: Georg --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 684f813..0bb90ee 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ env/ -.vscode \ No newline at end of file +.vscode +__pycache__/ -- cgit v1.2.3 From 3ada63a085abb9362272ddbaa548907926c574d1 Mon Sep 17 00:00:00 2001 From: Georg Date: Fri, 27 Aug 2021 14:53:38 +0200 Subject: Removing __pycache__ Signed-off-by: Georg --- __pycache__/__init__.cpython-38.pyc | Bin 637 -> 0 bytes __pycache__/config.cpython-38.pyc | Bin 2814 -> 0 bytes __pycache__/plugin.cpython-38.pyc | Bin 10478 -> 0 bytes 3 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 __pycache__/__init__.cpython-38.pyc delete mode 100644 __pycache__/config.cpython-38.pyc delete mode 100644 __pycache__/plugin.cpython-38.pyc diff --git a/__pycache__/__init__.cpython-38.pyc b/__pycache__/__init__.cpython-38.pyc deleted file mode 100644 index de1a3d3..0000000 Binary files a/__pycache__/__init__.cpython-38.pyc and /dev/null differ diff --git a/__pycache__/config.cpython-38.pyc b/__pycache__/config.cpython-38.pyc deleted file mode 100644 index 3666f16..0000000 Binary files a/__pycache__/config.cpython-38.pyc and /dev/null differ diff --git a/__pycache__/plugin.cpython-38.pyc b/__pycache__/plugin.cpython-38.pyc deleted file mode 100644 index 2038462..0000000 Binary files a/__pycache__/plugin.cpython-38.pyc and /dev/null differ -- cgit v1.2.3 From ee7c1610672591302b3bbb068af892530cc6b78e Mon Sep 17 00:00:00 2001 From: Georg Date: Fri, 27 Aug 2021 15:05:25 +0200 Subject: Adding branch info to README.rst Signed-off-by: Georg --- README.rst | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index 7bb4c6a..27ac795 100644 --- a/README.rst +++ b/README.rst @@ -1,8 +1,11 @@ -SnoParser -========= +SnoParser + Redis +================= Parses the Server Notices from ErgoIRCd. +This branch contains Redis functionality: +Performs and caches WHOIS lookups for connecting IP addresses. Counts nicknames and IP addresses. + Assumptions ----------- @@ -24,4 +27,4 @@ Configurations 1. Formatting for 'AutoVhost' post Registration (TODO: Disable) 2. Configure the '#channel' to send the SNO lines (disabled by default) (TODO: Add Exceptions) -3. Use ZWSP to toggle nickhighlights when in channel. \ No newline at end of file +3. Use ZWSP to toggle nickhighlights when in channel. -- cgit v1.2.3 From 9a035edef35d10d5c477a2ec7660d0c69e96729a Mon Sep 17 00:00:00 2001 From: Georg Date: Fri, 27 Aug 2021 15:55:27 +0200 Subject: Fixing timedelta Signed-off-by: Georg --- plugin.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin.py b/plugin.py index 4b16961..1adf2ef 100644 --- a/plugin.py +++ b/plugin.py @@ -109,7 +109,7 @@ class SnoParser(callbacks.Plugin): self.redis_client_ips = self.redis_connect_ips() def whois_fresh(self, sourceip: str) -> dict: - """Data from cache.""" + """Data from WHOIS backend (IANA or respective RIR).""" asn = 0 subnet = '' try: @@ -141,7 +141,7 @@ class SnoParser(callbacks.Plugin): def whois_set_cache(self, key: str, value: str) -> bool: """Data to Redis.""" - duration = self.registryValue('whois.ttl') + duration = int(self.registryValue('whois.ttl')) state = self.redis_client_whois.setex(key, timedelta(seconds=duration), value=value,) return state -- cgit v1.2.3 From 14e7d4e74e77ee92b55e5c117d7e17477b83ef8a Mon Sep 17 00:00:00 2001 From: Georg Date: Sun, 29 Aug 2021 00:13:44 +0200 Subject: OPER and DEOPER notices Signed-off-by: Georg --- plugin.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/plugin.py b/plugin.py index 1adf2ef..cf2e05c 100644 --- a/plugin.py +++ b/plugin.py @@ -358,6 +358,26 @@ class SnoParser(callbacks.Plugin): self._setvhost(irc, msg, account) self._sendSnotice(irc, msg, repl) + if 'OPER' in text and 'Client opered up' in text: + + operregex = "^-OPER- Client opered up \[(.*)\]" + couple = re.match(operregex, text) + account = couple.group(1) + DictFromSnotice = {'notice': 'oper'} + repl = f"\x02\x1FNOTICE:\x0F [{account}] opered up." + + self._sendSnotice(irc, msg, repl) + + if 'OPER' in text and 'Client deopered' in text: + + operregex = "^-OPER- Client deopered \[(.*)\]" + couple = re.match(operregex, text) + account = couple.group(1) + DictFromSnotice = {'notice': 'oper'} + repl = f"\x02\x1FNOTICE:\x0F [{account}] opered down." + + self._sendSnotice(irc, msg, repl) + # Post Registration -- cgit v1.2.3 From 46596e0b33a9a5dc086028f8e0e023a4b599bc95 Mon Sep 17 00:00:00 2001 From: Georg Date: Tue, 31 Aug 2021 16:29:17 +0200 Subject: ipquery IP validation + repaired config integers Signed-off-by: Georg --- config.py | 12 ++++++------ plugin.py | 34 ++++++++++++++++++---------------- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/config.py b/config.py index 90dd9c9..5a9ebbd 100644 --- a/config.py +++ b/config.py @@ -74,14 +74,14 @@ conf.registerGlobalValue(SnoParser, 'debug', ### conf.registerGroup(SnoParser, 'redis') conf.registerGlobalValue(SnoParser.redis, 'db1', - registry.Integer('1', + registry.Integer(1, """ Redis: Database number for counting of NICKNAMES. """ , private=True )) conf.registerGlobalValue(SnoParser.redis, 'db2', - registry.Integer('2', + registry.Integer(2, """ Redis: Database number for counting of IP ADDRESSES. """ @@ -95,7 +95,7 @@ conf.registerGlobalValue(SnoParser.redis, 'host', , private=True )) conf.registerGlobalValue(SnoParser.redis, 'port', - registry.Integer('6379', + registry.Integer(6379, """ Redis: Port. """ @@ -115,7 +115,7 @@ conf.registerGlobalValue(SnoParser.redis, 'password', """ )) conf.registerGlobalValue(SnoParser.redis, 'timeout', - registry.Integer('5', + registry.Integer(5, """ Redis: Socket Timeout. The developer does not know what to recommend here, but `5` seems to yield good results. """ @@ -141,7 +141,7 @@ conf.registerGlobalValue(SnoParser.whois, 'sample', , private=True )) conf.registerGlobalValue(SnoParser.whois, 'ttl', - registry.Integer('3600', + registry.Integer(3600, """ SnoParser: How long to cache WHOIS entries for. """ @@ -149,7 +149,7 @@ conf.registerGlobalValue(SnoParser.whois, 'ttl', )) conf.registerGroup(SnoParser.whois, 'redis') conf.registerGlobalValue(SnoParser.whois.redis, 'db', - registry.Integer('0', + registry.Integer(0, """ Redis: Database number for WHOIS query caching. """ diff --git a/plugin.py b/plugin.py index cf2e05c..d10c8e0 100644 --- a/plugin.py +++ b/plugin.py @@ -168,7 +168,7 @@ class SnoParser(callbacks.Plugin): print(data) data = json.dumps(data) state = self.whois_set_cache(key=sourceip, value=data) - + if state is True: return json.loads(data) else: @@ -243,11 +243,10 @@ class SnoParser(callbacks.Plugin): ttl = self.redis_client_whois.ttl(ipaddress) count = self.redis_client_ips.get(ipaddress) decoded_count = count.decode('utf-8') - print('SnoParser manual query: ', data, ' ', ttl) irc.reply(f'{decoded_data} - Count: {decoded_count} - Remaining: {ttl}s') - - ipquery = wrap(ipquery, ['anything']) + + ipquery = wrap(ipquery, ['ip']) def doNotice(self, irc, msg): @@ -342,7 +341,7 @@ class SnoParser(callbacks.Plugin): ip = couple.group(3) DictFromSnotice = {'notice': 'accreg', 'hostmask': hostmask, 'account': account, 'ip': ip} repl = f"\x02\x1FNOTICE: accreg -> [{account}] was registered by hostmask [{hostmask}] from IP {ip}" - + # Trigger HS SET self._setvhost(irc, msg, account) @@ -354,17 +353,20 @@ class SnoParser(callbacks.Plugin): account = couple.group(2) DictFromSnotice = {'notice': 'sareg', 'oper': oper, 'account': account} repl = f"\x02\x1FNOTICE: sareg -> [{account}] was registered by operator [{oper}]" - + self._setvhost(irc, msg, account) self._sendSnotice(irc, msg, repl) if 'OPER' in text and 'Client opered up' in text: - - operregex = "^-OPER- Client opered up \[(.*)\]" + operregex = "^-OPER- Client opered up \[(.*)\, \ (.*)\]$" couple = re.match(operregex, text) - account = couple.group(1) - DictFromSnotice = {'notice': 'oper'} - repl = f"\x02\x1FNOTICE:\x0F [{account}] opered up." + hostmask = couple.group(1) + oper = couple.group(2) + + print(couple) + + DictFromSnotice = {'notice': 'opered', 'hostmask': hostmask, 'oper': oper} + repl = f"\x02\x1FNOTICE:\x0F [{hostmask}] opered up as [{oper}]." self._sendSnotice(irc, msg, repl) @@ -373,14 +375,14 @@ class SnoParser(callbacks.Plugin): operregex = "^-OPER- Client deopered \[(.*)\]" couple = re.match(operregex, text) account = couple.group(1) - DictFromSnotice = {'notice': 'oper'} + DictFromSnotice = {'notice': 'deopered', 'name': account} repl = f"\x02\x1FNOTICE:\x0F [{account}] opered down." self._sendSnotice(irc, msg, repl) # Post Registration - + def _setvhost(self, irc, msg, account): arg = ['SET'] arg.append(account) @@ -390,18 +392,18 @@ class SnoParser(callbacks.Plugin): args=arg)) - # Send formatted SNO to channel + # Send formatted SNO to channel def _sendSnotice(self, irc, msg, repl): try: channel = self.registryValue('targetChannel') if channel[0] == '#': irc.queueMsg(msg=ircmsgs.IrcMsg(command='NOTICE', - args=(channel, repl))) + args=(channel, repl))) # what sort of exception does one raise except: pass - + Class = SnoParser -- cgit v1.2.3 From ca02bba8c341b00973497e6f37a8ee34d75fcf0a Mon Sep 17 00:00:00 2001 From: Pratyush Desai Date: Tue, 7 Sep 2021 16:33:00 +0200 Subject: TargetChannel config + requirements.txt (#17) Added requirements.txt Updated configuration value errors Reviewed-on: https://git.com.de/LimnoriaPlugins/SnoParser/pulls/17 Co-authored-by: Pratyush Desai Co-committed-by: Pratyush Desai --- config.py | 12 +++++++++--- plugin.py | 2 +- requirements.txt | 3 +++ 3 files changed, 13 insertions(+), 4 deletions(-) create mode 100644 requirements.txt diff --git a/config.py b/config.py index 5a9ebbd..a585def 100644 --- a/config.py +++ b/config.py @@ -28,7 +28,7 @@ ### -from supybot import conf, registry +from supybot import conf, registry, ircutils try: from supybot.i18n import PluginInternationalization _ = PluginInternationalization('SnoParser') @@ -37,6 +37,12 @@ except: # without the i18n module _ = lambda x: x +class ValidChannel(registry.string): + """Value must be a valid channel""" + def setValue(self, v): + if not (ircutils.isChannel(v)): + self.error() + registry.String.setValue(self, v) def configure(advanced): # This will be called by supybot to configure this module. advanced is @@ -52,8 +58,8 @@ SnoParser = conf.registerPlugin('SnoParser') # conf.registerGlobalValue(SnoParser, 'someConfigVariableName', # registry.Boolean(False, _("""Help for someConfigVariableName."""))) -conf.registerGlobalValue(SnoParser, 'targetChannel', - registry.String(None, ("""Sends reformatted snolines to the """))) +conf.regi