-
Notifications
You must be signed in to change notification settings - Fork 9
/
check_urls.pl
executable file
·397 lines (309 loc) · 9.62 KB
/
check_urls.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#!/usr/bin/perl -W
# (P) & (C) 2006 by Peter Bieringer <pb at bieringer dot de>
# This program extracts URLs from a Lyx file and checks them
# 20060822/PB: major improvement, add support for persistent XML database
# 20081109/PB: enhancement to detect URLs in newer lyx file format
# 20090214/PB: detect IPv6 addresses in brackets and remove brackets, otherwise it won't work
# 20131112/PB: support newer Perl
use strict;
use Net::HTTP;
use Net::FTP;
use Net::NNTP;
use Crypt::SSLeay;
use LWP::UserAgent;
use XML::Dumper;
use Socket qw(NI_NUMERICSERV NI_NUMERICHOST getaddrinfo inet_pton);
my $debug = 0xffff & ~(0x20);
my %urls;
my $p_urls = \%urls;
my %hosts;
my $time = time;
my $dbfile;
my $dbfile_suffix = ".url-database.xml";
sub quote($) {
$_[0] =~ s/\`/#60/g;
$_[0] =~ s/[\200-\377]/\?/g;
return $_[0];
};
sub extract_urls($) {
my ($url, $desc);
print STDERR "DEBUG/extract_urls: open file: $_[0]\n";
open FILE, "<" . $_[0] || die "ERROR : can't open file: " . $_[0];
my $linecounter = 0;
while (<FILE>) {
$linecounter++;
chomp $_;
if ($_ =~ /LatexCommand \\url\[([^]]*)\]{([^}]*)}/) {
$desc = $1;
$url = $2;
print STDERR "DEBUG/extract_urls: desc='$desc' URL=$url\n" if ($debug & 0x10);
if (defined $$p_urls{$url}->{'line'}) {
print STDERR "DEBUG/extract_urls: URL already found earlier - skip\n" if ($debug & 0x10);
if ($$p_urls{$url}->{'time'} == $time) {
} else {
# from database, update now
$$p_urls{$url}->{'time'} = $time;
$$p_urls{$url}->{'line'} = $linecounter;
$$p_urls{$url}->{'desc'} = quote($desc);
};
next;
} else {
$$p_urls{$url}->{'desc'} = quote($desc);
$$p_urls{$url}->{'time'} = $time;
$$p_urls{$url}->{'line'} = $linecounter;
};
$url = "";
$desc = "";
} elsif ($_ =~ /name \"([^"]*)\"/) {
# name "IPv6 & Linux - HowTo"
$desc = $1;
} elsif ($_ =~ /target \"([^"]*)\"/) {
# target "http://www.bieringer.de/linux/IPv6/"
$url = $1;
print STDERR "DEBUG/extract_urls: desc='$desc' URL=$url\n" if ($debug & 0x10);
if (defined $$p_urls{$url}->{'line'}) {
print STDERR "DEBUG/extract_urls: URL already found earlier - skip\n" if ($debug & 0x10);
if ($$p_urls{$url}->{'time'} == $time) {
} else {
# from database, update now
$$p_urls{$url}->{'time'} = $time;
$$p_urls{$url}->{'line'} = $linecounter;
$$p_urls{$url}->{'desc'} = quote($desc);
};
next;
} else {
$$p_urls{$url}->{'desc'} = quote($desc);
$$p_urls{$url}->{'time'} = $time;
$$p_urls{$url}->{'line'} = $linecounter;
};
};
};
close(FILE);
};
sub load_urls() {
if (! -f $dbfile) {
print STDERR "DEBUG/load_urls: database file doesn't exist, skip load: $dbfile\n" if ($debug & 0x10);
return 2;
};
my $dump = new XML::Dumper;
print STDERR "DEBUG/load_urls: load database file: $dbfile\n" if ($debug & 0x10);
$p_urls = $dump->xml2pl($dbfile);
};
sub store_urls() {
my $dump = new XML::Dumper;
$dump->pl2xml($p_urls, $dbfile);
};
sub cleanup_old_urls() {
for my $url (keys %$p_urls) {
if ($$p_urls{$url}->{'time'} < $time) {
print STDERR "DEBUG/cleanup_old_urls: remove old URL from database: $url\n" if ($debug & 0x10);
my $p_h = $$p_urls{$url};
delete $$p_urls{$url};
};
};
};
sub check_ipv6only($$) {
print STDERR "DEBUG/check_ipv6only: begin\n" if ($debug & 0x10);
print STDERR "DEBUG/check_ipv6only: check: " . $_[0] . " on port " . $_[1] . "\n" if ($debug & 0x10);
my ($family, $socktype, $proto, $saddr, $canonname, @res, $err);
my ($host, $port);
$family = -1;
if ($_[0] =~ /^\[([0-9a-fA-F:]+)\]$/) {
# Strip [...]
$host = $1;
$port = $_[1];
print STDERR "DEBUG/check_ipv6only: host: " . $host . "\n" if ($debug & 0x10);
socket(Socket_Handle, PF_INET6, SOCK_STREAM, 0) || return 1;
$saddr = pack_sockaddr_in6($port, inet_pton(AF_INET6, $host));
connect(Socket_Handle, $saddr) && return 0;
close(Socket_Handle);
} else {
my %hints = ( socktype => SOCK_STREAM, family => AF_INET6);
($err, @res) = getaddrinfo($_[0], "echo", \%hints);
if (defined $err) {
print STDERR "ERROR/check_ipv6only: getaddrinfo fails: $err\n" if ($debug & 0x10);
return 1;
};
if (scalar(@res) < 5) {
print STDERR "ERROR/check_ipv6only: getaddrinfo fails\n" if ($debug & 0x10);
return 1;
};
$family = -1;
while (scalar(@res) >= 5) {
($family, $socktype, $proto, $saddr, $canonname, @res) = @res;
($host, $port) = getnameinfo($saddr, NI_NUMERICHOST | NI_NUMERICSERV);
print STDERR "Trying to connect to $host port $port...\n";
socket(Socket_Handle, $family, $socktype, $proto) || next;
connect(Socket_Handle, $saddr) && last;
close(Socket_Handle);
$family = -1;
};
if ($family != -1) {
print STDERR "connected to $host port $port\n";
close(Socket_Handle);
return 0;
} else {
warn "connect attempt failed\n";
return 1;
};
};
};
sub check_urls() {
print STDERR "DEBUG/check_urls: begin\n" if ($debug & 0x10);
for my $url (sort keys %$p_urls) {
if (defined $$p_urls{$url}->{'checktime'}) {
if ($$p_urls{$url}->{'checktime'} > $time - 60*60*24*7) {
if (defined $$p_urls{$url}->{'checkresult'} && $$p_urls{$url}->{'checkresult'} =~ /^ok/) {
# Checked during last 7 days - skip
print STDERR "DEBUG/check_urls: checked during last 7 days - skip: $url\n" if ($debug & 0x10);
next;
};
};
};
print STDERR "DEBUG/check_urls: check now: $url\n" if ($debug & 0x10);
my ($host, $port);
my $desc = $$p_urls{$url}->{'desc'};
my $status = "undef";
# Extract host
my ($proto, $hostport, $uri) = $url =~ /^([^:]+):\/\/([^\/]+)(.*)$/;
if ($hostport =~ /^([^:]):([0-9]+)$/) {
$host = $1;
$port = $2;
} else {
$host = $hostport;
if ($proto eq "http") {
$port = 80;
} elsif ($proto eq "ftp") {
$port = 21;
} elsif ($proto eq "nntp") {
$port = 119;
} elsif ($proto eq "https") {
$port = 443;
};
};
# Strip trailing #
$uri =~ s/#.*//;
if (length($uri) == 0) {
$status = "URI is empty";
goto ("LABEL_PRINT");
};
my $s;
if ($proto eq "ftp") {
# Check FTP
print STDERR "DEBUG/check_urls: open FTP connection: $host:$port\n" if ($debug & 0x20);
$s = Net::FTP->new(Host => $host, Port => $port, Timeout => 30, Passive => 1);
if (! defined $s) {
$status = "Host not found";
if (! check_ipv6only($host,$port)) {
$status = "ok (IPv6 only)";
};
goto ("LABEL_PRINT");
};
if (!$s->login("anonymous",'-anonymous@')) {
$status = "FTP anonymous login failed";
goto ("LABEL_PRINT");
};
if (!$s->cwd($uri)) {
$status = "FTP can't change to directory $uri";
goto ("LABEL_PRINT");
};
$status = "ok";
$s->quit;
} elsif ($proto eq "nntp") {
my $s = Net::NNTP->new(Host => $host, Timeout => 30);
if (! defined $s) {
$status = "Host not found";
if (! check_ipv6only($host,$port)) {
$status = "ok (IPv6 only)";
};
goto ("LABEL_PRINT");
};
$status = "ok";
$s->quit;
} elsif ($proto eq "https") {
my $ua = new LWP::UserAgent;
my $req = new HTTP::Request('HEAD', $url);
my $res = $ua->request($req);
my $code = $res->code;
if ($code !~ /^[23]/) {
$status = "HTTPS reports: $code";
} else {
$status = "ok";
};
} elsif ($proto eq "http") {
# Check HTTP
print STDERR "DEBUG/check_urls: open HTTP connection: $host:$port\n" if ($debug & 0x20);
$s = Net::HTTP->new(Host => $host, PeerPort => $port, Timeout => 30);
if (! defined $s) {
$status = "Host not found";
if (! check_ipv6only($host,$port)) {
$status = "ok (IPv6 only)";
};
goto ("LABEL_PRINT");
};
print STDERR "DEBUG/check_urls: send HEAD request: $uri\n" if ($debug & 0x20);
if ($s->write_request(HEAD => $uri, 'User-Agent' => "Mozilla/5.0") == 0) {
$status = "trouble with uri";
goto ("LABEL_PRINT");
};
print STDERR "DEBUG/check_urls: wait for response\n" if ($debug & 0x20);
my($code, $mess, %h) = $s->read_response_headers;
print STDERR "DEBUG/check_urls: check response\n" if ($debug & 0x10);
if ($code !~ /^[23]/) {
$status = "HTTP reports: $code";
} else {
$status = "ok";
};
};
LABEL_PRINT:
if ($status ne "ok") {
print "desc='$desc' URL=$url proto=$proto host=$host port=$port uri='$uri'";
print " status=$status\n\n";
die;
};
LABEL_END:
$$p_urls{$url}->{'checktime'} = $time;
$$p_urls{$url}->{'checkresult'} = $status;
undef $s;
store_urls();
};
};
sub report_urls() {
print STDERR "DEBUG/report_urls: begin\n" if ($debug & 0x10);
for my $url (sort { $$p_urls{$a}->{'line'} <=> $$p_urls{$b}->{'line'} } ( keys %$p_urls)) {
if ($$p_urls{$url}->{'checkresult'} =~ /^ok/) {
next;
};
print "NOTICE: URL has a problem: $url\n";
print " Description : " . $$p_urls{$url}->{'desc'} . "\n";
print " Line number : " . $$p_urls{$url}->{'line'} . "\n";
print " Result : " . $$p_urls{$url}->{'checkresult'} . "\n";
print "\n";
};
};
sub check_rfc_urls() {
print STDERR "DEBUG/check_rfc_urls: begin\n" if ($debug & 0x10);
for my $url (sort { $$p_urls{$a}->{'line'} <=> $$p_urls{$b}->{'line'} } ( keys %$p_urls)) {
if ($url =~ /rfc[0-9]{1,4}/) {
print "NOTICE: URL points to RFC: $url\n";
print " Description : " . $$p_urls{$url}->{'desc'} . "\n";
print " Line number : " . $$p_urls{$url}->{'line'} . "\n";
print "\n";
};
};
};
##### Main
if (! defined $ARGV[0] || $ARGV[0] eq "") {
die "Missing file name (arg1)";
};
if (! -f $ARGV[0]) {
die "Argument 1 is not an existing file: " . $ARGV[0];
};
$dbfile = $ARGV[0] . $dbfile_suffix;
load_urls();
extract_urls($ARGV[0]);
cleanup_old_urls();
check_urls();
store_urls();
check_rfc_urls();
report_urls();