certalert-bot/certstream_consumer.py

48 lines
990 B
Python
Raw Permalink Normal View History

2020-04-22 01:36:22 +02:00
import redis
import json
r = redis.Redis()
count = 0
rules = {}
while True:
if count % 1000 == 0:
count = 0
todel = 1
while (toadd := r.lpop('toadd')) is not None:
2020-05-13 11:53:58 +02:00
toadd = json.loads(toadd.decode('utf-8'))
2020-04-22 01:36:22 +02:00
rules[toadd['id']] = toadd['value']
2020-05-13 11:53:58 +02:00
print("Added rule " + str(toadd['id']))
2020-04-22 01:36:22 +02:00
while (todel := r.lpop('todel')) is not None:
try:
2020-05-13 11:53:58 +02:00
del rules[int(todel.decode('utf-8'))]
print("Delete rule " + str(todel.decode('utf-8')))
except Exception as e:
print(e)
2020-04-22 01:36:22 +02:00
pass
2020-05-13 11:53:58 +02:00
domain = r.blpop('certstream')[1].decode('utf-8')
2020-04-22 01:36:22 +02:00
for rule in rules.values():
notify = False
2020-05-13 11:53:58 +02:00
v = str(rule['v'])
2020-04-22 01:36:22 +02:00
if rule['t'] == 0:
2020-05-13 11:53:58 +02:00
if v in domain:
2020-04-22 01:36:22 +02:00
notify = True
elif rule['t'] == 1:
2020-05-13 11:53:58 +02:00
if domain.startswith(v):
2020-04-22 01:36:22 +02:00
notify = True
elif rule['t'] == 2:
2020-05-13 11:53:58 +02:00
if domain.endswith(v):
2020-04-22 01:36:22 +02:00
notify = True
if notify:
print(domain)
2020-05-13 11:53:58 +02:00
r.rpush('notifications', json.dumps({"domain": domain, "chat": rule['c']}))
2020-04-22 01:36:22 +02:00
count += 1