certstream_consumer.py 990 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import redis
  2. import json
  3. r = redis.Redis()
  4. count = 0
  5. rules = {}
  6. while True:
  7. if count % 1000 == 0:
  8. count = 0
  9. todel = 1
  10. while (toadd := r.lpop('toadd')) is not None:
  11. toadd = json.loads(toadd.decode('utf-8'))
  12. rules[toadd['id']] = toadd['value']
  13. print("Added rule " + str(toadd['id']))
  14. while (todel := r.lpop('todel')) is not None:
  15. try:
  16. del rules[int(todel.decode('utf-8'))]
  17. print("Delete rule " + str(todel.decode('utf-8')))
  18. except Exception as e:
  19. print(e)
  20. pass
  21. domain = r.blpop('certstream')[1].decode('utf-8')
  22. for rule in rules.values():
  23. notify = False
  24. v = str(rule['v'])
  25. if rule['t'] == 0:
  26. if v in domain:
  27. notify = True
  28. elif rule['t'] == 1:
  29. if domain.startswith(v):
  30. notify = True
  31. elif rule['t'] == 2:
  32. if domain.endswith(v):
  33. notify = True
  34. if notify:
  35. print(domain)
  36. r.rpush('notifications', json.dumps({"domain": domain, "chat": rule['c']}))
  37. count += 1