fix 5.16.1 errors
[spider.git] / perl / create_master_badip_files.pl
1 #!/usr/bin/env perl
2 #
3 # Get the TOR exit and relay lists from the net, extract the exit and relay
4 # node ip addresses and store them, one per line, in the standard places
5 # in /spider/local_data. 
6 #
7
8 use 5.16.1;
9
10 # search local then perl directories
11 use strict;
12
13 BEGIN {
14         # root of directory tree for this system
15         our $root = "/spider"; 
16         $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
17
18         mkdir "$root/local_data", 02777 unless -d "$root/local_data";
19
20         unshift @INC, "$root/perl";     # this IS the right way round!
21         unshift @INC, "$root/local";
22         our $data = "$root/data";
23 }
24
25
26 use DXVars;
27 use SysVar;
28
29 use DXDebug;
30 use DXUtil;
31
32 use LWP::Simple;
33 use JSON;
34 use Date::Parse;
35 use File::Copy;
36
37 DXDebug::dbginit();
38
39 $ENV{PERL_JSON_BACKEND} = "JSON::XS,JSON::PP";
40
41
42 my $debug;
43
44 if (@ARGV && $ARGV[0] eq '-x') {
45         shift;
46         $debug = 1;
47 }
48 my $url = "https://onionoo.torproject.org/details";
49 my $relayfn = localdata('badip.torrelay');
50 my $exitfn = localdata('badip.torexit');
51
52 my $last_seen_window = 10800;
53 my $content;
54
55 if (@ARGV) {
56         local $/ = undef;
57         my $fn = shift;
58         open IN, $fn or die "$0 cannot open file $fn, $!";
59         $content = <IN>;
60         close IN;
61 } else {
62         $content = get($url) or die "$0: connect error on $url, $!\n";
63 }
64
65 die "No TOR content available $!\n" unless $content;
66
67 my $l = length $content;
68 my $data = decode_json($content);
69 my $now = time;
70 my $ecount = 0;
71 my $rcount = 0;
72
73 my $rand = rand;
74 open RELAY, ">$relayfn.$rand" or die "$0: cannot open $relayfn $!";
75 open EXIT, ">$exitfn.$rand" or die "$0: cannot open $exitfn $1";
76
77 foreach my $e (@{$data->{relays}}) {
78
79         my $seen = str2time($e->{last_seen});
80         next unless $seen >= $now - $last_seen_window;
81         
82         my @or = clean_addr(@{$e->{or_addresses}}) if exists $e->{or_addresses};
83         my @exit = clean_addr(@{$e->{exit_addresses}}) if exists $e->{exit_addresses} ;
84         my $ors = join ', ', @or;
85         my $es = join ', ', @exit;
86         dbg "$0: $e->{nickname} $e->{last_seen} relays: [$ors] exits: [$es]" if $debug;
87         for (@or) {
88                 print RELAY "$_\n";
89                 ++$rcount;
90         }
91         for (@exit) {
92                 print EXIT "$_\n";
93                 ++$ecount;
94         }
95 }
96
97 close RELAY;
98 close EXIT;
99
100 dbg("$0: $rcount relays $ecount exits found");
101 move "$relayfn.$rand", $relayfn if $rcount;
102 move "$exitfn.$rand", $exitfn if $ecount;
103 unlink "$relayfn.$rand";
104 unlink "$exitfn.$rand";
105
106 exit 0;
107
108 sub clean_addr
109 {
110         my @out;
111         foreach (@_) {
112                 my ($ipv4) = /^((?:\d+\.){3}\d+)/;
113                 if ($ipv4) {
114                         push @out, $ipv4;
115                         next;
116                 }
117                 my ($ipv6) = /^\[([:a-f\d]+)\]/;
118                 push @out, $ipv6 if $ipv6;
119         }
120         return @out;
121 }