commit 42e1a0e539c4616f5a6ecfe4b2fffe039823aad1 Author: Giulio Date: Wed Apr 22 01:36:22 2020 +0200 Initial commit diff --git a/certstream_consumer.py b/certstream_consumer.py new file mode 100755 index 0000000..8022be2 --- /dev/null +++ b/certstream_consumer.py @@ -0,0 +1,43 @@ +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: + toadd = json.loads(toadd.decode('ascii')) + rules[toadd['id']] = toadd['value'] + while (todel := r.lpop('todel')) is not None: + try: + del rules[todel.decode('ascii')] + except: + pass + + + domain = r.blpop('certstream')[1].decode('ascii') + + for rule in rules.values(): + notify = False + if rule['t'] == 0: + if rule['v'] in domain: + notify = True + elif rule['t'] == 1: + if domain.startswith(rule['v']): + notify = True + elif rule['t'] == 2: + if domain.endswith(rule['v']): + notify = True + if notify: + print(domain) + r.rpush('notifications', json.dumps({"domain": domain, "chats": rule['c']})) + + + count += 1 + + + diff --git a/certstream_producer.py b/certstream_producer.py new file mode 100755 index 0000000..7753fe5 --- /dev/null +++ b/certstream_producer.py @@ -0,0 +1,11 @@ +import certstream +import redis +import sys + +r = redis.Redis() + +def enqueue(message, context): + for domain in message['data']['leaf_cert']['all_domains']: + r.rpush('certstream', domain) + +certstream.listen_for_events(enqueue, url='wss://certstream.calidog.io/') \ No newline at end of file diff --git a/notifications_consumer.py b/notifications_consumer.py new file mode 100755 index 0000000..ab5c641 --- /dev/null +++ b/notifications_consumer.py @@ -0,0 +1,12 @@ +import redis +import json +import os + +r = redis.Redis() +token = os.environ.get('token') + +while True: + notification = json.loads(r.blpop('notifications')[1].decode('ascii')) + print(notification) + for chat in notification['chats']: + requests.get("https://api.telegram.org/bot{}/sendMessage".format(token), params={"chatid": chat, "text": "New cert for *{}*".format(domain), "parse_mode": "Markdown"}) diff --git a/rules_test.py b/rules_test.py new file mode 100755 index 0000000..b2d3f7f --- /dev/null +++ b/rules_test.py @@ -0,0 +1,15 @@ +import redis +import json + +r = redis.Redis() + +rules = [ + {"id": "22", "value": {"t": 0, "v": ".edu", "c": 1234567}}, + {"id": "27", "value": {"t": 2, "v": ".int", "c": 1234567}}, + {"id": "232", "value": {"t": 0, "v": ".sy", "c": 1234567}} +] + +for rule in rules: + r.rpush("toadd", json.dumps(rule)) + +r.rpush("todel", "22") \ No newline at end of file