-
Notifications
You must be signed in to change notification settings - Fork 2
/
dl_query.pl
executable file
·202 lines (161 loc) · 5.45 KB
/
dl_query.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
#!/usr/bin/env perl
use strict;
use warnings;
use POE;
use POE::Component::Client::DNS;
use JSON::XS;
# GetOpts::Long doesn't seem to work for me at the moment..
open CONF, '<', 'lists.json' or die "Missing lists.json!\n";
my $conf_raw = join "\n", <CONF>;
close CONF;
my $conf = JSON::XS->new()->ascii->decode($conf_raw);
my $ip = shift;
die "Syntax: $0 IPADDRESS\n" unless $ip;
chomp $ip;
my $type = shift || '(?:blacklist|)';
chomp $type;
my $fail_on = '(?:blacklisted)';
my $rev_ip = join '.', reverse split /\./, $ip;
my @hosts = ();
my $response_handler = {};
my $retcode = 0;
# restructure conf
foreach my $service (keys %{$conf}) {
foreach my $zone (keys %{$conf->{$service}}) {
next if $zone =~ /^comments?$/o;
if (defined $conf->{$service}{$zone}{'type'}) {
next unless grep {/^$type$/o} @{$conf->{$service}{$zone}{'type'}};
}
push @hosts, "$rev_ip.$zone";
$response_handler->{$zone} = $conf->{$service}{$zone};
}
}
sub responder {
my ($host, $response) = @_;
# Scrape off IP
$host =~ s/^$rev_ip\.//o;
if (defined $response_handler->{$host}{$response}) {
# Query returned a known "noop", nothing to see here
if ($response_handler->{$host}{$response} eq 'noop') {
return 'ok';
}
return $response_handler->{$host}{$response};
}
elsif ($response eq 'none') {
return 'ok';
}
elsif (defined $response_handler->{$host}{'default'}) {
return $response_handler->{$host}{'default'};
}
elsif ($response eq '127.0.0.2') {
return 'blacklisted';
}
else {
return "FAILED! No response defined for '$response' from $host";
}
}
POE::Component::Client::DNS->spawn(
Alias => 'resolver',
Timeout => '20',
);
POE::Session->create(
inline_states => {
_start => sub {
my ($kernel, $heap) = @_[KERNEL, HEAP];
$heap->{answers} = 0;
$heap->{timeouts} = 0;
$heap->{results} = {};
$heap->{start_time} = time();
for (1 .. 20) {
$kernel->yield('query_next');
}
},
_stop => sub {
my $heap = $_[HEAP];
my $elapsed_time = time() - $heap->{start_time};
warn(
"Elapsed time: $elapsed_time second(s).\n",
"$heap->{timeouts} resolver timeouts.\n",
"$heap->{answers} resolver answers.\n"
);
foreach my $type (keys %{$heap->{results}}) {
if ($type =~ /$fail_on/o) {
$retcode = 1;
}
foreach my $host (@{$heap->{results}{$type}}) {
print "$type\t$host";
if (defined $heap->{result_notes}{$host}) {
print "\t$heap->{result_notes}{$host}";
}
print "\n";
}
}
},
query_txt => sub {
my ($kernel, $heap) = @_[KERNEL, HEAP];
my ($hostname) = $_[ARG0];
return unless defined $hostname;
$kernel->post(
'resolver' =>
'resolve' =>
'response' =>
$hostname => 'TXT'
);
},
query_next => sub {
my ($kernel, $heap) = @_[KERNEL, HEAP];
my $hostname = pop @hosts;
return unless defined $hostname;
$kernel->post(
'resolver' =>
'resolve' =>
'response' =>
$hostname => 'A'
);
},
response => sub {
my ($kernel, $heap) = @_[KERNEL, HEAP];
my ($request, $response) = @_[ARG0, ARG1];
my ($query, $error) = @$response;
my $request_address = $request->[0];
unless (defined $query) {
warn ("$request_address: error ($error)\n");
$heap->{timeouts}++;
$kernel->yield('query_next');
return;
}
my @answers = $query->answer();
unless (@answers) {
my $result = responder($request_address, 'none');
push @{$heap->{results}{$result}}, $request_address;
$heap->{answers}++;
$kernel->yield('query_next');
return;
}
foreach my $answer (@answers) {
my $rdatastr = $answer->rdatastr();
if ($answer->type eq 'TXT') {
if (defined $rdatastr) {
if ($rdatastr =~ /v=spf\d/) {
$rdatastr = "DEAD? Service responded with SPF: '${rdatastr}'";
}
$heap->{'result_notes'}{$request_address} = $rdatastr;
}
next;
}
elsif ($answer->type ne 'A') {
warn "Unexpected type: ".$answer->type."\n";
}
$heap->{answers}++;
my $result = responder($request_address, $rdatastr);
push @{$heap->{results}{$result}}, $request_address;
if ($result !~ /ok$/o) {
$kernel->yield("query_txt", $request_address);
}
}
$kernel->yield('query_next');
},
}
);
$poe_kernel->run();
exit $retcode;