ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/myhttpd/netgeo.pl
(Generate patch)

Comparing Coro/myhttpd/netgeo.pl (file contents):
Revision 1.8 by root, Fri Sep 28 16:35:00 2001 UTC vs.
Revision 1.18 by root, Sat Dec 8 21:01:16 2007 UTC

4 4
5use Socket; 5use Socket;
6use Fcntl; 6use Fcntl;
7 7
8use Coro; 8use Coro;
9use Coro::Event; 9use Coro::EV;
10use Coro::Semaphore; 10use Coro::Semaphore;
11use Coro::SemaphoreSet; 11use Coro::SemaphoreSet;
12use Coro::Socket; 12use Coro::Socket;
13use Coro::Timer;
13 14
14use BerkeleyDB; 15use BerkeleyDB;
15
16$Event::DIED = sub {
17 Event::verbose_exception_handler(@_);
18 #Event::unloop_all();
19};
20 16
21tie %netgeo::whois, BerkeleyDB::Btree, 17tie %netgeo::whois, BerkeleyDB::Btree,
22 -Env => $db_env, 18 -Env => $db_env,
23 -Filename => "whois", 19 -Filename => "whois",
24 -Flags => DB_CREATE, 20 -Flags => DB_CREATE,
29 -Flags => DB_CREATE, 25 -Flags => DB_CREATE,
30 or die "unable to create/open iprange table"; 26 or die "unable to create/open iprange table";
31 27
32package Whois; 28package Whois;
33 29
34use Coro::Event; 30use Coro::EV;
35 31
36sub new { 32sub new {
37 my $class = shift; 33 my $class = shift;
38 my $name = shift; 34 my $name = shift;
39 my $ip = shift; 35 my $ip = shift;
63 my $timeout = 5; 59 my $timeout = 5;
64 60
65 while () { 61 while () {
66 my $fh = new Coro::Socket 62 my $fh = new Coro::Socket
67 PeerAddr => $self->ip, 63 PeerAddr => $self->ip,
68 PeerPort => 'whois', 64 PeerPort => $self->{port} || "whois",
69 Timeout => 30; 65 Timeout => 30;
70 if ($fh) { 66 if ($fh) {
71 print $fh "$query\n"; 67 print $fh "$query\n";
72 $fh->read($whois, 16*1024); # max 16k. whois stored 68 $fh->read($whois, 16*1024); # max 16k. whois stored
73 close $fh; 69 close $fh;
83 $timeout *= 2; 79 $timeout *= 2;
84 $timeout = 1 if $timeout > 600; 80 $timeout = 1 if $timeout > 600;
85 } else { 81 } else {
86 last; 82 last;
87 } 83 }
84 } else {
85 # only retry once a minute
86 print STDERR "unable to connect to $self->{ip} ($self->{name}), retrying...\n";
87 Coro::Timer::sleep 300;
88 } 88 }
89 } 89 }
90 90
91 $netgeo::whois{$id} = $whois; 91 $netgeo::whois{$id} = $whois;
92 } 92 }
115sub ip_request { 115sub ip_request {
116 my ($self, $ip) = @_; 116 my ($self, $ip) = @_;
117 117
118 my $whois = $self->whois_request($ip); 118 my $whois = $self->whois_request($ip);
119 119
120 return () if $whois =~ /^No match/; 120 return if $whois =~ /^No match/;
121 121
122 if ($whois =~ /^To single out one record/m) { 122 if ($whois =~ /^To single out one record/m) {
123 my $handle; 123 my $handle;
124 while ($whois =~ /\G\S.*\(([A-Z0-9\-]+)\).*\n/mg) { 124 while ($whois =~ /\G\S.*\(([A-Z0-9\-]+)\).*\n/mg) {
125 $handle = $1; 125 $handle = $1;
126 #return if $handle =~ /-(RIPE|APNIC)/; # heuristic, bbut bad because ripe might not have better info 126 #return if $handle =~ /-(RIPE|APNIC)/; # heuristic, but bad because ripe might not have better info
127 } 127 }
128 $handle or die "$whois ($ip): unparseable multimatch\n"; 128 $handle or die "$whois ($ip): unparseable multimatch\n";
129 $whois = $self->whois_request("!$handle"); 129 $whois = $self->whois_request("!$handle");
130 } 130 }
131 131
167 $whois; 167 $whois;
168} 168}
169 169
170package Whois::RIPE; 170package Whois::RIPE;
171 171
172use Socket;
172use base Whois; 173use base Whois;
173 174
174sub sanitize { 175sub sanitize {
175 local $_ = $_[1]; 176 local $_ = $_[1];
177
176 s/^%.*\n//gm; 178 s/^%.*\n//gm;
177 s/^\n+//; 179 s/^\n+//;
178 s/\n*$/\n/; 180 s/\n*$/\n/;
181
182 s/^inetnum:\s+/*in: /gm;
183 s/^admin-c:\s+/*ac: /gm;
184 s/^tech-c:\s+/*tc: /gm;
185 s/^owner-c:\s+/*oc: /gm;
186 s/^country:\s+/*cy: /gm;
187 s/^phone:\s+/*ph: /gm;
188 s/^remarks:\s+/*rm: /gm;
189 s/^changed:\s+/*ch: /gm;
190 s/^created:\s+/*cr: /gm;
191 s/^address:\s+/*ad: /gm;
192 s/^status:\s+/*st: /gm;
193 s/^inetrev:\s+/*ir: /gm;
194 s/^nserver:\s+/*ns: /gm;
195
179 $_; 196 $_;
180} 197}
181 198
182sub ip_request { 199sub ip_request {
183 my ($self, $ip) = @_; 200 my ($self, $ip) = @_;
184 201
185 my $whois = $self->whois_request("-FSTin $ip"); 202 my $whois = $self->whois_request("$self->{rflags}$ip");
203
204 $whois =~ s{
205 (2[0-5][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])
206 (?:\.
207 (2[0-5][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])
208 (?:\.
209 (2[0-5][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])
210 (?:\.
211 (2[0-5][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])
212 )?
213 )?
214 )?
215 /
216 ([0-9]+)
217 }{
218 my $ip = inet_aton sprintf "%d.%d.%d.%d", $1, $2, $3, $4;
219 my $net = 1 << (31 - $5);
220 my $mask = inet_aton 2 ** 32 - $net;
221
222 my $ip1 = $ip & $mask;
223 my $ip2 = $ip1 | inet_aton $net * 2 - 1;
224 (inet_ntoa $ip1) . " - " . (inet_ntoa $ip2);
225 }gex;
186 226
187 $whois =~ /^\*in: 0\.0\.0\.0 - 255\.255\.255\.255/ 227 $whois =~ /^\*in: 0\.0\.0\.0 - 255\.255\.255\.255/
188 and return; 228 and return;
189 229
230 $whois =~ /^\*de: This network range is not allocated to /m # APINIC e.g. 24.0.0.0
231 and return;
232
190 $whois =~ /^\*ac: XXX0/m # 192.0.0.0 233 $whois =~ /^\*ac: XXX0/m # 192.0.0.0
234 and return;
235
236 $whois =~ /^\*st: (?:ALLOCATED )?UNSPECIFIED/m
191 and return; 237 and return;
192 238
193 $whois =~ /^%ERROR:/m 239 $whois =~ /^%ERROR:/m
194 and return; 240 and return;
195 241
196 #while ($whois =~ s/^\*(?:ac|tc):\s+(\S+)\n//m) { 242 #while ($whois =~ s/^\*(?:ac|tc):\s+(\S+)\n//m) {
197 # $whois .= $self->whois_request("-FSTpn $1"); 243 # $whois .= $self->whois_request("-FSTpn $1");
198 #} 244 #}
199 245
246 #$whois =~ s/^\*(?:pn|nh|mb|ch|so|rz|ny|st|rm):.*\n//mg;
247
248 $whois =~ s/\n+$//;
249
250 $whois;
251}
252
253package Whois::RWHOIS;
254
255use base Whois;
256
257sub sanitize {
258 local $_ = $_[1];
259 s/^%referral\s+/referral:/gm;
260 s/^network://gm;
261 s/^%.*\n//gm;
262 s/^\n+//m;
263 s/\n*$/\n/m;
264
265 s/^(\S+):\s*/\L$1: /gm;
266 s/^ip-network-block:/*in:/gm;
267 s/^country-code:/*cy:/gm;
268 s/^tech-contact;i:/*tc:/gm;
269 s/^updated:/*ch:/gm;
270 s/^street-address:/*ad:/gm;
271 s/^org-name:/*rm:/gm;
272 s/^created:/*cr:/gm;
273
274 $_;
275}
276
277sub ip_request {
278 my ($self, $ip) = @_;
279
280 my $whois = $self->whois_request("$ip");
281
282 $whois =~ /^\*in: 0\.0\.0\.0 - 255\.255\.255\.255/
283 and return;
284
285 $whois =~ /^\*ac: XXX0/m # 192.0.0.0
286 and return;
287
288 $whois =~ /^%ERROR:/m
289 and return;
290
291 #while ($whois =~ s/^\*(?:ac|tc):\s+(\S+)\n//m) {
292 # $whois .= $self->whois_request("-FSTpn $1");
293 #}
294
200 $whois =~ s/^\*(?:pn|nh|mb|ch|so|rz|ny|st|rm):.*\n//mg; 295 $whois =~ s/^\*(?:pn|nh|mb|ch|so|rz|ny|st|rm):.*\n//mg;
201 296
202 $whois =~ s/\n+$//; 297 $whois =~ s/\n+$//;
203 298
204 $whois; 299 $whois;
217 inet_ntoa pack "N", $_[0]; 312 inet_ntoa pack "N", $_[0];
218} 313}
219 314
220our %WHOIS; 315our %WHOIS;
221 316
222$WHOIS{ARIN} = new Whois::ARIN ARIN => "whois.arin.net", maxjobs => 12; 317#$WHOIS{ARIN} = new Whois::ARIN ARIN => "whois.arin.net", port => 43, maxjobs => 12;
318$WHOIS{ARIN} = new Whois::RWHOIS ARIN => "rwhois.arin.net", port => 4321, maxjobs => 12;
223$WHOIS{RIPE} = new Whois::RIPE RIPE => "whois.ripe.net", maxjobs => 20; 319$WHOIS{RIPE} = new Whois::RIPE RIPE => "whois.ripe.net", port => 43, rflags => "-FTin ", maxjobs => 20;
320$WHOIS{APNIC} = new Whois::RIPE APNIC => "whois.apnic.net", port => 43, rflags => "-FTin ", maxjobs => 20;
224$WHOIS{APNIC} = new Whois::RIPE APNIC => "whois.apnic.net", maxjobs => 20; 321$WHOIS{LACNIC} = new Whois::RIPE LACNIC => "whois.lacnic.net", port => 43, maxjobs => 20;
225 322
226$whoislock = new Coro::SemaphoreSet; 323$whoislock = new Coro::SemaphoreSet;
227 324
228sub ip_request { 325sub ip_request {
229 my $ip = $_[0]; 326 my $ip = $_[0];
241 } 338 }
242 } 339 }
243 340
244 my ($arin, $ripe, $apnic); 341 my ($arin, $ripe, $apnic);
245 342
246 $whois = $WHOIS{APNIC}->ip_request($ip) 343 $whois = $WHOIS{RIPE}->ip_request($ip)
247 || $WHOIS{RIPE} ->ip_request($ip) 344 || $WHOIS{APNIC} ->ip_request($ip)
248 || $WHOIS{ARIN} ->ip_request($ip); 345 || $WHOIS{ARIN} ->ip_request($ip)
346# || $WHOIS{LACNIC}->ip_request($ip)
347 ;
249 348
250 $whois =~ /^\*in: ([0-9.]+)\s+-\s+([0-9.]+)\s*$/mi 349 $whois =~ /^\*in: ([0-9.]+)\s+-\s+([0-9.]+)\s*$/mi
251 or do { warn "$whois($ip): no addresses found\n", last }; 350 or do { warn "$whois($ip): no addresses found\n", last };
252 351
253 my ($ip0, $ip1) = ($1, $2); 352 my ($ip0, $ip1) = ($1, $2);
267 $iprange->db_sync; 366 $iprange->db_sync;
268 367
269 $whois; 368 $whois;
270} 369}
271 370
371if (0) {
372 #print ip_request "68.52.164.8"; # goof
373 #print "\n\n";
374 #print ip_request "200.202.220.222"; # lacnic
375 #print "\n\n";
376 #print ip_request "62.116.167.250";
377 #print "\n\n";
378 #print ip_request "133.11.128.254"; # jp
379 #print "\n\n";
380 print ip_request "80.131.153.93";
381 print "\n\n";
382}
272 383
3841;
273 385
386

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines