|
| 1 | +#!/usr/bin/python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +import csv |
| 5 | +import re |
| 6 | +import requests |
| 7 | + |
| 8 | +src_auth_dirs = 'https://gitweb.torproject.org/tor.git/plain/src/app/config/auth_dirs.inc' |
| 9 | +src_fallback_dirs = 'https://gitweb.torproject.org/tor.git/plain/src/app/config/fallback_dirs.inc' |
| 10 | +src_blutmagie = 'https://torstatus.blutmagie.de/query_export.php' |
| 11 | + |
| 12 | +destination = '/var/www/html/tor.rsc' |
| 13 | + |
| 14 | +ips_dir = [] |
| 15 | +ips_guard = [] |
| 16 | +ips_exit = [] |
| 17 | + |
| 18 | +ip_re = re.compile(r'(?<![0-9])(?:(?:[0-1]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])[.](?:[0-1]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])[.](?:[0-1]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])[.](?:[0-1]?[0-9]{1,2}|2[0-4][0-9]|25[0-5]))(?![0-9])') |
| 19 | + |
| 20 | +try: |
| 21 | + # Get directory nodes |
| 22 | + for src in (src_auth_dirs, src_fallback_dirs): |
| 23 | + r = requests.get(src) |
| 24 | + for line in r.text.splitlines(): |
| 25 | + m = ip_re.search(line) |
| 26 | + if m: |
| 27 | + ips_dir.append(m.group(0)) |
| 28 | + |
| 29 | + # Get guard and exit nodes |
| 30 | + r = requests.get(src_blutmagie) |
| 31 | + reader = csv.DictReader(r.text.splitlines()) |
| 32 | + for row in reader: |
| 33 | + if row['Flag - Guard'] == '1': |
| 34 | + ips_guard.append(row['IP Address']) |
| 35 | + if row['Flag - Exit'] == '1': |
| 36 | + ips_exit.append(row['IP Address']) |
| 37 | + |
| 38 | + # Create RouterOS lists |
| 39 | + with open(destination, 'w') as f: |
| 40 | + f.write('/ip firewall address-list\n') |
| 41 | + f.write('remove [find list=Tor-Dir]\n') |
| 42 | + f.write('remove [find list=Tor-Guard]\n') |
| 43 | + f.write('remove [find list=Tor-Exit]\n') |
| 44 | + for ip in sorted(set(ips_dir)): |
| 45 | + f.write('add list=Tor-Dir address={}\n'.format(ip)) |
| 46 | + for ip in sorted(set(ips_guard)): |
| 47 | + f.write('add list=Tor-Guard address={}\n'.format(ip)) |
| 48 | + for ip in sorted(set(ips_exit)): |
| 49 | + f.write('add list=Tor-Exit address={}\n'.format(ip)) |
| 50 | +except: |
| 51 | + pass |
0 commit comments