line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::Domain::Match; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
54946
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
73
|
|
4
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
1797
|
use version; our $VERSION = qv('0.2.3'); |
|
2
|
|
|
|
|
5740
|
|
|
2
|
|
|
|
|
14
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $LOCAL = '/tmp/effective_tld_names.dat'; |
8
|
|
|
|
|
|
|
our $CACHE = '/tmp/effective_tld_names.dat.cache'; |
9
|
|
|
|
|
|
|
our $SOURCE = 'http://mxr.mozilla.org/mozilla-central/source/netwerk/dns/effective_tld_names.dat?raw=1'; |
10
|
|
|
|
|
|
|
|
11
|
2
|
|
|
2
|
|
791176
|
use LWP::UserAgent; |
|
2
|
|
|
|
|
1570740
|
|
|
2
|
|
|
|
|
588
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub import { |
14
|
2
|
50
|
|
2
|
|
21
|
if( grep { /:pdata/ } @_ ){ |
|
2
|
|
|
|
|
4309
|
|
15
|
0
|
|
|
|
|
0
|
$SOURCE = 'https://raw.github.com/petermblair/Perl-CPAN/master/Net-Domain-Regex/misc/tld.txt'; |
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub new { |
20
|
2
|
|
|
2
|
1
|
29
|
my $class = shift; |
21
|
|
|
|
|
|
|
|
22
|
2
|
|
|
|
|
15
|
my $args = { |
23
|
|
|
|
|
|
|
local => $LOCAL, |
24
|
|
|
|
|
|
|
source => $SOURCE, |
25
|
|
|
|
|
|
|
cache => $CACHE, |
26
|
|
|
|
|
|
|
@_, |
27
|
|
|
|
|
|
|
}; |
28
|
|
|
|
|
|
|
|
29
|
2
|
|
|
|
|
6
|
my $o = bless $args => $class; |
30
|
|
|
|
|
|
|
|
31
|
2
|
50
|
|
|
|
60
|
unless( -e $o->{local} ){ |
32
|
2
|
|
|
|
|
9
|
$o->pull; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
0
|
|
|
|
|
0
|
$o->refresh; |
36
|
|
|
|
|
|
|
|
37
|
0
|
|
|
|
|
0
|
return $o; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub insert { |
41
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
42
|
0
|
|
|
|
|
0
|
my $domain = shift; |
43
|
|
|
|
|
|
|
|
44
|
0
|
|
|
|
|
0
|
my @a = split( /\./, $domain ); |
45
|
|
|
|
|
|
|
|
46
|
0
|
0
|
|
|
|
0
|
if( scalar @a == 2 ){ |
47
|
0
|
|
|
|
|
0
|
$self->{tld}->{"$a[-1]"}++; |
48
|
0
|
|
|
|
|
0
|
$self->{sld}->{"$domain"}++; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub refresh { |
53
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
54
|
|
|
|
|
|
|
|
55
|
2
|
|
|
2
|
|
1838
|
use open qw(:std :utf8); |
|
2
|
|
|
|
|
2794
|
|
|
2
|
|
|
|
|
11
|
|
56
|
0
|
|
|
|
|
0
|
open FD, "<$self->{local}"; |
57
|
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
0
|
my $tlds = {}; |
59
|
0
|
|
|
|
|
0
|
my $slds = {}; |
60
|
|
|
|
|
|
|
|
61
|
0
|
|
|
|
|
0
|
while( ){ |
62
|
0
|
|
|
|
|
0
|
chomp; |
63
|
|
|
|
|
|
|
|
64
|
0
|
0
|
0
|
|
|
0
|
if(/^(\S[^\.\s]+)$/){ |
|
|
0
|
|
|
|
|
|
65
|
0
|
|
|
|
|
0
|
$tlds->{$1}++; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
elsif ( /^\S[^\.\s]+\.(.+)$/ && exists $tlds->{$1} ) { |
68
|
0
|
|
|
|
|
0
|
$slds->{$_}++; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
0
|
|
|
|
|
0
|
$self->{tld} = $tlds; |
73
|
0
|
|
|
|
|
0
|
$self->{sld} = $slds; |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
# any manual overrides - not all ccSLD are in the external file |
76
|
0
|
|
|
|
|
0
|
for( qw/ co.uk / ){ |
77
|
0
|
|
|
|
|
0
|
$self->insert( $_ ); |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub pull { |
83
|
2
|
|
|
2
|
1
|
4
|
my $self = shift; |
84
|
|
|
|
|
|
|
|
85
|
2
|
|
|
|
|
23
|
my $ua = LWP::UserAgent->new; |
86
|
2
|
|
|
|
|
7127
|
my $req = HTTP::Request->new( GET => $self->{source} ); |
87
|
2
|
|
|
|
|
20612
|
my $res = $ua->request( $req ); |
88
|
|
|
|
|
|
|
|
89
|
2
|
50
|
|
|
|
1692518
|
if( $res->is_success ){ |
90
|
0
|
|
|
|
|
0
|
open FD, ">$self->{local}"; |
91
|
0
|
|
|
|
|
0
|
local $/; |
92
|
0
|
|
|
|
|
0
|
print FD $res->content; |
93
|
|
|
|
|
|
|
} else { |
94
|
2
|
|
|
|
|
32
|
die $res->status_line; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
0
|
|
|
0
|
0
|
|
sub generate_regex { |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub generate_map { |
102
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
103
|
|
|
|
|
|
|
|
104
|
0
|
|
|
|
|
|
my $map = {}; |
105
|
|
|
|
|
|
|
|
106
|
0
|
|
|
|
|
|
for( keys %{$self->{tld}} ){ |
|
0
|
|
|
|
|
|
|
107
|
0
|
|
|
|
|
|
$map->{$_} = {}; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
0
|
|
|
|
|
|
for( keys %{$self->{sld}} ){ |
|
0
|
|
|
|
|
|
|
111
|
0
|
|
|
|
|
|
my @a = split( /\./, $_ ); |
112
|
0
|
|
|
|
|
|
my $t = $a[-1]; |
113
|
|
|
|
|
|
|
|
114
|
0
|
0
|
|
|
|
|
if( defined $map->{$t} ){ |
115
|
0
|
|
|
|
|
|
$map->{$t}->{$_}++; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
0
|
|
|
|
|
|
return $map; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
sub match { |
123
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
124
|
0
|
|
|
|
|
|
my $target = shift; |
125
|
|
|
|
|
|
|
|
126
|
0
|
|
|
|
|
|
$self->match_map( $target ); |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
sub match_map { |
130
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
131
|
0
|
|
|
|
|
|
my $target = shift; |
132
|
0
|
|
|
|
|
|
my $orig = $target; |
133
|
|
|
|
|
|
|
|
134
|
2
|
|
|
2
|
|
3498
|
use Data::Dumper; |
|
2
|
|
|
|
|
17318
|
|
|
2
|
|
|
|
|
857
|
|
135
|
|
|
|
|
|
|
|
136
|
0
|
|
|
|
|
|
my $map = $self->generate_map(); |
137
|
|
|
|
|
|
|
|
138
|
0
|
|
|
|
|
|
my @tok = split /[^\w\.\-]/, $target; |
139
|
|
|
|
|
|
|
|
140
|
0
|
|
|
|
|
|
my @results; |
141
|
|
|
|
|
|
|
|
142
|
0
|
|
|
|
|
|
TARGET:for my $target ( @tok ){ |
143
|
0
|
|
|
|
|
|
my ( $tld, $domain, $hostname ) = ( undef, undef, '' ); |
144
|
|
|
|
|
|
|
|
145
|
0
|
|
|
|
|
|
my @tokens = split( /\./, $target ); |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
# Check 1: Is the TLD found |
148
|
0
|
0
|
|
|
|
|
if( defined $map->{"$tokens[-1]"} ){ |
149
|
|
|
|
|
|
|
# Check if the SLD is found |
150
|
0
|
0
|
|
|
|
|
if( defined $map->{"$tokens[-1]"}->{"$tokens[-2].$tokens[-1]"} ){ |
151
|
0
|
|
|
|
|
|
$tld = "$tokens[-2].$tokens[-1]"; |
152
|
|
|
|
|
|
|
} else { |
153
|
0
|
|
|
|
|
|
$tld = $tokens[-1]; |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
# bail out, if we don't have the TLD defined |
158
|
0
|
0
|
|
|
|
|
next TARGET unless $tld; |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
# Strip out the $tld from the target |
161
|
0
|
|
|
|
|
|
$target =~ s/^(.*?)\.$tld$/$1/; |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
# Re-split it, to get the domain |
164
|
0
|
|
|
|
|
|
my @a = split( /\./, $target ); |
165
|
|
|
|
|
|
|
|
166
|
0
|
0
|
|
|
|
|
next TARGET unless scalar @a > 0; |
167
|
|
|
|
|
|
|
|
168
|
0
|
|
|
|
|
|
$domain = $a[-1]; |
169
|
|
|
|
|
|
|
|
170
|
0
|
|
|
|
|
|
pop( @a ); |
171
|
|
|
|
|
|
|
|
172
|
0
|
0
|
|
|
|
|
$hostname = join( ".", @a ) |
173
|
|
|
|
|
|
|
if scalar @a > 0; |
174
|
|
|
|
|
|
|
|
175
|
0
|
|
|
|
|
|
push( @results, { match => $orig, hostname => $hostname, domain => $domain, tld => $tld } ); |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
|
178
|
0
|
|
|
|
|
|
return @results; |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
1; |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
__END__ |