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.17 by root, Sat Sep 17 20:27:47 2005 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines