| 18 | parser.add_argument('--url', default="wss://certstream.calidog.io", dest='url', help='Connect to a certstream server.') |
| 19 | |
| 20 | def main(): |
| 21 | args = parser.parse_args() |
| 22 | |
| 23 | # Ignore broken pipes |
| 24 | signal(SIGPIPE, SIG_DFL) |
| 25 | |
| 26 | log_level = logging.INFO |
| 27 | if args.verbose: |
| 28 | log_level = logging.DEBUG |
| 29 | |
| 30 | logging.basicConfig(format='[%(levelname)s:%(name)s] %(asctime)s - %(message)s', level=log_level) |
| 31 | |
| 32 | def _handle_messages(message, context): |
| 33 | if args.json: |
| 34 | sys.stdout.flush() |
| 35 | sys.stdout.write(json.dumps(message) + "\n") |
| 36 | sys.stdout.flush() |
| 37 | else: |
| 38 | if args.disable_colors: |
| 39 | logging.debug("Starting normal output.") |
| 40 | payload = "{} {} - {} {}\n".format( |
| 41 | "[{}]".format(datetime.datetime.fromtimestamp(message['data']['seen']).isoformat()), |
| 42 | message['data']['source']['url'], |
| 43 | message['data']['leaf_cert']['subject']['CN'], |
| 44 | "[{}]".format(", ".join(message['data']['leaf_cert']['all_domains'])) if args.full else "" |
| 45 | ) |
| 46 | |
| 47 | sys.stdout.write(payload) |
| 48 | else: |
| 49 | logging.debug("Starting colored output.") |
| 50 | payload = "{} {} - {} {}\n".format( |
| 51 | termcolor.colored("[{}]".format(datetime.datetime.fromtimestamp(message['data']['seen']).isoformat()), 'cyan', attrs=["bold", ]), |
| 52 | termcolor.colored(message['data']['source']['url'], 'blue', attrs=["bold",]), |
| 53 | termcolor.colored(message['data']['leaf_cert']['subject']['CN'], 'green', attrs=["bold",]), |
| 54 | termcolor.colored("[", 'blue') + "{}".format( |
| 55 | termcolor.colored(", ", 'blue').join( |
| 56 | [termcolor.colored(x, 'white', attrs=["bold",]) for x in message['data']['leaf_cert']['all_domains']] |
| 57 | ) |
| 58 | ) + termcolor.colored("]", 'blue') if args.full else "", |
| 59 | ) |
| 60 | sys.stdout.write(payload) |
| 61 | |
| 62 | sys.stdout.flush() |
| 63 | |
| 64 | certstream.listen_for_events(_handle_messages, args.url, skip_heartbeats=True) |
| 65 | |
| 66 | if __name__ == "__main__": |
| 67 | main() |