5396566726b19f6fa22597461a84d726397acfed
Jacob Appelbaum Add a perl script that auto...

Jacob Appelbaum authored 16 years ago

1) #!/usr/bin/perl -w
2) use warnings;
3) use strict;
Andrew Lewman new update mirrors script,...

Andrew Lewman authored 10 years ago

4) use Data::Dumper;
Jacob Appelbaum Add a perl script that auto...

Jacob Appelbaum authored 16 years ago

5) use LWP::Simple;
Andrew Lewman new update mirrors script,...

Andrew Lewman authored 10 years ago

6) use HTML::LinkExtor;
Jacob Appelbaum Add a perl script that auto...

Jacob Appelbaum authored 16 years ago

7) use LWP;
8) use Date::Parse;
Jacob Appelbaum Removed dupe mirror entry,...

Jacob Appelbaum authored 16 years ago

9) use Date::Format;
Andrew Lewman new update mirrors script,...

Andrew Lewman authored 10 years ago

10) use Digest::SHA qw(sha256_hex);
Jacob Appelbaum Add a perl script that auto...

Jacob Appelbaum authored 16 years ago

11) 
Jacob Appelbaum Add credit and some idea of...

Jacob Appelbaum authored 16 years ago

12) # This is Free Software (GPLv3)
13) # http://www.gnu.org/licenses/gpl-3.0.txt
14) 
Jacob Appelbaum Add a perl script that auto...

Jacob Appelbaum authored 16 years ago

15) print "Creating LWP agent ($LWP::VERSION)...\n";
16) my $lua = LWP::UserAgent->new(
17)     keep_alive => 1,
Jon@svn.torproject.org alter timout values (15 -> 30)

Jon@svn.torproject.org authored 14 years ago

18)     timeout => 30,
Jon@svn.torproject.org revert r21050

Jon@svn.torproject.org authored 14 years ago

19)     agent => "Tor MirrorCheck Agent"
Jacob Appelbaum Add a perl script that auto...

Jacob Appelbaum authored 16 years ago

20) );
21) 
22) sub sanitize {
23)     my $taintedData = shift;
24)     my $cleanedData;
25)     my $whitelist = '-a-zA-Z0-9: +';
26) 
27)     # clean the data, return cleaned data
28)     $taintedData =~ s/[^$whitelist]//go;
29)     $cleanedData = $taintedData;
30) 
31)     return $cleanedData;
32) }
Andrew Lewman new update mirrors script,...

Andrew Lewman authored 10 years ago

33) sub ExtractLinks {
34)     my $content = shift; 
35)     my $url     = shift;
36)     my @links;
Jacob Appelbaum Add a perl script that auto...

Jacob Appelbaum authored 16 years ago

37) 
Andrew Lewman new update mirrors script,...

Andrew Lewman authored 10 years ago

38)     my $parser = HTML::LinkExtor->new(undef, $url);
39)     $parser->parse($content);
40)     foreach my $linkarray($parser->links)
41)     {
42)          my ($elt_type, $attr_name, $attr_value) = @$linkarray;
43)          if ($elt_type eq 'a' && $attr_name eq 'href' && $attr_value =~ /\/$/ && $attr_value =~ /^$url/)
44)          {
45)          	push @links, Fetch($attr_value, \&ExtractLinks);
46)          }
47) 	 elsif ($attr_value =~ /\.(xpi|dmg|exe|tar\.gz)$/)
Andrew Lewman update the script some more...

Andrew Lewman authored 10 years ago

48) 	 #elsif ($attr_value =~ /\.(asc)$/) # small pgp files easier to test with
Andrew Lewman new update mirrors script,...

Andrew Lewman authored 10 years ago

49)          {
50)          	push @links, $attr_value;
51)          }
52)     }
53)     return @links;
54) }
55) 
56) sub ExtractDate {
57)     my $content = shift;  
58)     $content    = sanitize($content);
59)     my $date    = str2time($content);
60) 
61)     if ($date) {
Andrew Lewman update the script some more...

Andrew Lewman authored 10 years ago

62)     	print "ExtractDate($content) = $date\n";
Andrew Lewman new update mirrors script,...

Andrew Lewman authored 10 years ago

63)         return $date;
64)     } else {
Andrew Lewman update the script some more...

Andrew Lewman authored 10 years ago

65)     	print "ExtractDate($content) = ?\n";
Andrew Lewman new update mirrors script,...

Andrew Lewman authored 10 years ago

66) 	return undef;
67)     }
68) }
Jacob Appelbaum Add a perl script that auto...

Jacob Appelbaum authored 16 years ago

69) 
Andrew Lewman new update mirrors script,...

Andrew Lewman authored 10 years ago

70) sub ExtractSig {
71)     my $content = shift;
72)     return sha256_hex($content); 
73) }
74) 
75) sub Fetch {
76)     my ($url, $sub) = @_; # Base url for mirror
Jacob Appelbaum Add a perl script that auto...

Jacob Appelbaum authored 16 years ago

77) 
78)     my $request = new HTTP::Request GET => "$url";
79)     my $result = $lua->request($request);
Jacob Appelbaum Fix a date bug.

Jacob Appelbaum authored 16 years ago

80)     my $code = $result->code();
Andrew Lewman update the script some more...

Andrew Lewman authored 10 years ago

81)     print "\nGET $url: $code\n";
Jacob Appelbaum Add a perl script that auto...

Jacob Appelbaum authored 16 years ago

82) 
Jacob Appelbaum Fix a date bug.

Jacob Appelbaum authored 16 years ago

83)     if ($result->is_success && $code eq "200"){
Andrew Lewman new update mirrors script,...

Andrew Lewman authored 10 years ago

84)        my $content = $result->content;
Jacob Appelbaum Add a perl script that auto...

Jacob Appelbaum authored 16 years ago

85)        if ($content) {
Andrew Lewman new update mirrors script,...

Andrew Lewman authored 10 years ago

86) 	    return $sub->($content, $url);
Jacob Appelbaum Add a perl script that auto...

Jacob Appelbaum authored 16 years ago

87)         } else {
Andrew Lewman new update mirrors script,...

Andrew Lewman authored 10 years ago

88)             print "Unable to fetch $url, empty content returned.\n";
Jacob Appelbaum Add a perl script that auto...

Jacob Appelbaum authored 16 years ago

89)         }
90)     }
Jacob Appelbaum Fix a date bug.

Jacob Appelbaum authored 16 years ago

91) 
Andrew Lewman new update mirrors script,...

Andrew Lewman authored 10 years ago

92)     return undef;
Jacob Appelbaum Add a perl script that auto...

Jacob Appelbaum authored 16 years ago

93) }
Andrew Lewman update the script some more...

Andrew Lewman authored 10 years ago

94) my @columns;
95) sub LoadMirrors {
96)     open(CSV, "<", "include/tor-mirrors.csv") or die "Cannot open tor-mirrors.csv: $!"; 
97)     my $line = <CSV>;
98)     chomp($line);
99)     @columns = split(/\s*,\s*/, $line);
100)     my @mirrors;
101)     while ($line = <CSV>)
102)     {
103)         chomp($line);
104) 	my @values = split(/\s*,\s*/, $line);
105) 	my %server;
106) 	for (my $i = 0; $i < scalar(@columns); $i++)
107) 	{
108) 	    $server{$columns[$i]} = $values[$i] || '';
109) 	}
110) 	$server{updateDate} = str2time($server{updateDate}) if ($server{updateDate});
111) 	push @mirrors, {%server};
112)     }
113)     close(CSV);
114)     return @mirrors;
115) }
Jacob Appelbaum Add a perl script that auto...

Jacob Appelbaum authored 16 years ago

116) 
Andrew Lewman update the script some more...

Andrew Lewman authored 10 years ago

117) sub DumpMirrors {
118)     my @m = @_;
119)     open(CSV, ">", "tor-mirrors.csv") or die "Cannot open tor-mirrors.csv: $!";
120)     print CSV join(", ", @columns) . "\n";
121)     foreach my $server(@m) {
122) 	$server->{updateDate} = gmtime($server->{updateDate}) if ($server->{updateDate});
123)         print CSV join(", ", map($server->{$_}, @columns));
124) 	print CSV "\n";
Andrew Lewman add the cloudflare mirror,...

Andrew Lewman authored 12 years ago

125)     }
Jacob Appelbaum Add a perl script that auto...

Jacob Appelbaum authored 16 years ago

126) 
Andrew Lewman update the script some more...

Andrew Lewman authored 10 years ago

127)     close(CSV);
128) }
129) 
130) my @m     = LoadMirrors();
131) my $count = scalar(@m);
Jacob Appelbaum Add a perl script that auto...

Jacob Appelbaum authored 16 years ago

132) print "We have a total of $count mirrors\n";
133) print "Fetching the last updated date for each mirror.\n";
134) 
Andrew Lewman update the script some more...

Andrew Lewman authored 10 years ago

135) my $tortime  = Fetch("https://www.torproject.org/project/trace/www-master.torproject.org", \&ExtractDate);
Andrew Lewman new update mirrors script,...

Andrew Lewman authored 10 years ago

136) my @torfiles = Fetch("https://www.torproject.org/dist/", \&ExtractLinks); 
137) my %randomtorfiles;
138) 
139) for (1 .. 1)
140) {
141) 	my $r = int(rand(scalar(@torfiles)));
142) 	my $suffix = $torfiles[$r];
143) 	$suffix =~ s/^https:\/\/www.torproject.org//;
144) 	$randomtorfiles{$suffix} = Fetch($torfiles[$r], \&ExtractSig);
145) }
146) 
147) print "Using these files for sig matching:\n";
148) print join("\n", keys %randomtorfiles);
149) 
Andrew Lewman update the script some more...

Andrew Lewman authored 10 years ago

150) # Adjust official Tor time by out-of-date offset: number of days * seconds per day
Andrew Lewman update mirrors.

Andrew Lewman authored 12 years ago

151) $tortime -= 1 * 172800;
Andrew Lewman Change mirror script to use...

Andrew Lewman authored 15 years ago

152) print "The official time for Tor is $tortime. \n";
Jacob Appelbaum Add a perl script that auto...

Jacob Appelbaum authored 16 years ago

153) 
Andrew Lewman update the script some more...

Andrew Lewman authored 10 years ago

154) for(my $server = 0; $server < scalar(@m); $server++) {
155)     foreach my $serverType('httpWebsiteMirror', 'httpsWebsiteMirror', 'ftpWebsiteMirror', 'httpDistMirror', 'httpsDistMirror')
Andrew Lewman new update mirrors script,...

Andrew Lewman authored 10 years ago

156)     {
Andrew Lewman update the script some more...

Andrew Lewman authored 10 years ago

157)         if ($m[$server]->{$serverType}) {
158)             my $updateDate = Fetch("$m[$server]->{$serverType}/project/trace/www-master.torproject.org", \&ExtractDate);
159)     				      
160)             if ($updateDate) {
161) 		$m[$server]->{updateDate} = $updateDate;
162) 		$m[$server]->{sigMatched} = 1;
Andrew Lewman new update mirrors script,...

Andrew Lewman authored 10 years ago

163)                 foreach my $randomtorfile(keys %randomtorfiles) {
Andrew Lewman update the script some more...

Andrew Lewman authored 10 years ago

164)                     my $sig = Fetch("$m[$server]->{$serverType}/$randomtorfile", \&ExtractSig);
165)             	    if (!$sig) {
166) 		        print STDERR "Unreadable $randomtorfile on $m[$server]->{$serverType}";
167) 			$m[$server]->{sigMatched} = 0;
168)             	    	last;
169) 		    } elsif ($sig ne $randomtorfiles{$randomtorfile}) {
170) 			$m[$server]->{sigMatched} = 0;
171) 		        print STDERR "Sig mismatch of $randomtorfile on $m[$server]->{$serverType}";
172)             	    	last;
173)             	    }
Andrew Lewman new update mirrors script,...

Andrew Lewman authored 10 years ago

174) 		}
175)             }
176) 	    last;
Andrew Lewman update the script some more...

Andrew Lewman authored 10 years ago

177)         }
Jacob Appelbaum Add a perl script that auto...

Jacob Appelbaum authored 16 years ago

178)     }
Andrew Lewman new update mirrors script,...

Andrew Lewman authored 10 years ago

179) }
180) 
Andrew Lewman update the script some more...

Andrew Lewman authored 10 years ago

181) sub PrintServer {
182)      my $server = shift;
Jacob Appelbaum ensure the date is either p...

Jacob Appelbaum authored 16 years ago

183)      my $time;
Andrew Lewman update the script some more...

Andrew Lewman authored 10 years ago

184)      if ( $server->{'updateDate'} ) {
185) 	  if ( $server->{'updateDate'} > $tortime ) {
Andrew Lewman fix code spacing to avoid t...

Andrew Lewman authored 15 years ago

186) 	    $time = "Up to date";
Andrew Lewman unicorns found by creep.im...

Andrew Lewman authored 11 years ago

187) 	  } else { $time = "DO NOT USE. Out of date."; }
Andrew Lewman revert the last change to u...

Andrew Lewman authored 15 years ago

188)      } else { $time = "Unknown"; }
Jacob Appelbaum Removed dupe mirror entry,...

Jacob Appelbaum authored 16 years ago

189) print OUT <<"END";
190)      \n<tr>\n
Andrew Lewman update the script some more...

Andrew Lewman authored 10 years ago

191)          <td>$server->{'isoCC'}</td>\n
192)          <td>$server->{'orgName'}</td>\n
Jacob Appelbaum Removed dupe mirror entry,...

Jacob Appelbaum authored 16 years ago

193)          <td>$time</td>\n
194) END
195) 
196)      my %prettyNames = (
197)                         httpWebsiteMirror => "http",
198)                         httpsWebsiteMirror => "https",
199)                         ftpWebsiteMirror => "ftp",
200)                         rsyncWebsiteMirror => "rsync",
201)                         httpDistMirror => "http",
202)                         httpsDistMirror => "https",
Andrew Lewman unicorns found by creep.im...

Andrew Lewman authored 11 years ago

203)                         rsyncDistMirror => "rsync", );
Jacob Appelbaum Removed dupe mirror entry,...

Jacob Appelbaum authored 16 years ago

204) 
205)      foreach my $precious ( sort keys %prettyNames )
Jacob Appelbaum Add a perl script that auto...

Jacob Appelbaum authored 16 years ago

206)      {
Andrew Lewman update the script some more...

Andrew Lewman authored 10 years ago

207)         if ($server->{"$precious"}) {
208)             print OUT "    <td><a href=\"" . $server->{$precious} . "\">" .
Jacob Appelbaum Removed dupe mirror entry,...

Jacob Appelbaum authored 16 years ago

209)                       "$prettyNames{$precious}</a></td>\n";
Jacob Appelbaum Add a perl script that auto...

Jacob Appelbaum authored 16 years ago

210)         } else { print OUT "    <td> - </td>\n"; }
211)      }
212) 
213)      print OUT "</tr>\n";
214) }
215) 
Andrew Lewman update the script some more...

Andrew Lewman authored 10 years ago

216) 
217) my $outFile = "include/mirrors-table.wmi";
218) open(OUT, "> $outFile") or die "Can't open $outFile: $!";
219) 
220) # Here's where we open a file and print some wml include goodness
221) # This is sorted from last known recent update to unknown update times
222) foreach my $server ( sort { $b->{'updateDate'} <=> $a->{'updateDate'}} grep {$_->{updateDate} && $_->{sigMatched}} @m ) {
223)     PrintServer($server);
224) }
225) foreach my $server ( grep {!$_->{updateDate} || !$_->{sigMatched}} @m ) {
226)     PrintServer($server);
227) }
228) 
229) DumpMirrors(@m);
230)