Browse Source

Initial commit

Giulio 3 years ago
commit
42e1a0e539
4 changed files with 81 additions and 0 deletions
  1. 43 0
      certstream_consumer.py
  2. 11 0
      certstream_producer.py
  3. 12 0
      notifications_consumer.py
  4. 15 0
      rules_test.py

+ 43 - 0
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
+
+
+	    

+ 11 - 0
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/')

+ 12 - 0
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"})

+ 15 - 0
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")