Created
December 16, 2014 16:19
-
-
Save DonnchaC/5df239b664ebafca0700 to your computer and use it in GitHub Desktop.
Simple log file poller for Tor HS logs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| """Polls the Tor HS log file and records all INTRODUCTION | |
| cells seen to a database. | |
| """ | |
| import re | |
| import time | |
| import datetime | |
| def parse_introduce_from_log(log_line): | |
| match = re.match( | |
| '^(?P<timestamp>[A-Za-z0-9:. ]*) \[(?P<log_level>[a-z]*)\] Received ' + | |
| 'INTRODUCE2 cell for service "(?P<service_id>[a-z2-7]*)" on circ ' + | |
| '(?P<circuit_id>\d*).$', log_line) | |
| if match: | |
| return match.groupdict() | |
| else: | |
| return None | |
| def parse_log_timestamp(timestamp): | |
| timestamp = datetime.datetime.strptime(timestamp, '%b %d %H:%M:%S.%f') | |
| # No year in the log file, default to the current year. | |
| return timestamp.replace(year=datetime.datetime.now().year) | |
| def main(): | |
| import argparse | |
| from database import db_session | |
| from models import INTRODUCERequest | |
| from database import init_db | |
| init_db() | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("-f", "--log-file", type=argparse.FileType('r'), | |
| default='/var/log/tor/notices.log', | |
| help="Log file for the Tor hidden service process " | |
| "(default: %(default)s)") | |
| parser.add_argument("-w", "--wait", type=int, default=5, | |
| help="How often to poll the log file for updates " | |
| "(default: %(default)s seconds)") | |
| parser.add_argument("-v", "--verbose", action='store_true') | |
| args = parser.parse_args() | |
| log_file = args.log_file | |
| request_count = 0 | |
| # Read to the end of the current log file | |
| log_file.read() | |
| # Begin poll the log file | |
| print 'Starting polling' | |
| try: | |
| while True: | |
| # Read in any new log entries line-by-line | |
| while True: | |
| log_line = log_file.readline() | |
| if not log_line: | |
| break | |
| parsed_request = parse_introduce_from_log(log_line) | |
| if parsed_request: | |
| # Got an INTRODUCE cell, store it in the DB. | |
| logged_request = INTRODUCERequest( | |
| service_id=parsed_request.get('service_id'), | |
| timestamp=parse_log_timestamp( | |
| parsed_request.get('timestamp')) | |
| ) | |
| db_session.add(logged_request) | |
| db_session.commit() | |
| print '{}: Got INTRODUCE cell for service {}'.format( | |
| logged_request.timestamp, | |
| logged_request.service_id | |
| ) | |
| request_count += 1 | |
| time.sleep(args.wait) | |
| except KeyboardInterrupt: | |
| print '\nFinishing. Received {} INTRODUCE cells.'.format(request_count) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment