diff options
| author | Georg | 2021-08-26 11:41:41 +0200 |
|---|---|---|
| committer | Georg | 2021-08-26 11:41:41 +0200 |
| commit | c4941a26c1f1d127a7eaa315705aedc439edb15d (patch) | |
| tree | 3ef1d3393cad35b739957d7586e600e524ab0ac9 | |
| parent | e2a713e2994c6d8c83c4223e8b5f4e1adba9c3fd (diff) | |
| download | snoparser-c4941a26c1f1d127a7eaa315705aedc439edb15d.tar.gz snoparser-c4941a26c1f1d127a7eaa315705aedc439edb15d.tar.bz2 snoparser-c4941a26c1f1d127a7eaa315705aedc439edb15d.zip | |
Init
Signed-off-by: Georg <georg@lysergic.dev>
| -rw-r--r-- | __pycache__/__init__.cpython-38.pyc | bin | 0 -> 637 bytes | |||
| -rw-r--r-- | __pycache__/config.cpython-38.pyc | bin | 0 -> 1008 bytes | |||
| -rw-r--r-- | __pycache__/plugin.cpython-38.pyc | bin | 0 -> 7286 bytes | |||
| -rw-r--r-- | plugin.py | 98 |
4 files changed, 94 insertions, 4 deletions
diff --git a/__pycache__/__init__.cpython-38.pyc b/__pycache__/__init__.cpython-38.pyc Binary files differnew file mode 100644 index 0000000..de1a3d3 --- /dev/null +++ b/__pycache__/__init__.cpython-38.pyc diff --git a/__pycache__/config.cpython-38.pyc b/__pycache__/config.cpython-38.pyc Binary files differnew file mode 100644 index 0000000..2c3345f --- /dev/null +++ b/__pycache__/config.cpython-38.pyc diff --git a/__pycache__/plugin.cpython-38.pyc b/__pycache__/plugin.cpython-38.pyc Binary files differnew file mode 100644 index 0000000..70c3898 --- /dev/null +++ b/__pycache__/plugin.cpython-38.pyc @@ -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 (.+)$" |
