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 (.+)$"