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.15 by root, Wed May 7 19:00:43 2003 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 }
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
190 $whois =~ /^\*ac: XXX0/m # 192.0.0.0 235 $whois =~ /^\*ac: XXX0/m # 192.0.0.0
236 and return;
237
238 $whois =~ /^\*st: (?:ALLOCATED )?UNSPECIFIED/m
191 and return; 239 and return;
192 240
193 $whois =~ /^%ERROR:/m 241 $whois =~ /^%ERROR:/m
194 and return; 242 and return;
195 243
196 #while ($whois =~ s/^\*(?:ac|tc):\s+(\S+)\n//m) { 244 #while ($whois =~ s/^\*(?:ac|tc):\s+(\S+)\n//m) {
197 # $whois .= $self->whois_request("-FSTpn $1"); 245 # $whois .= $self->whois_request("-FSTpn $1");
198 #} 246 #}
199 247
248 #$whois =~ s/^\*(?:pn|nh|mb|ch|so|rz|ny|st|rm):.*\n//mg;
249
250 $whois =~ s/\n+$//;
251
252 $whois;
253}
254
255package Whois::RWHOIS;
256
257use base Whois;
258
259sub sanitize {
260 local $_ = $_[1];
261 s/^%referral\s+/referral:/gm;
262 s/^network://gm;
263 s/^%.*\n//gm;
264 s/^\n+//m;
265 s/\n*$/\n/m;
266
267 s/^(\S+):\s*/\L$1: /gm;
268 s/^ip-network-block:/*in:/gm;
269 s/^country-code:/*cy:/gm;
270 s/^tech-contact;i:/*tc:/gm;
271 s/^updated:/*ch:/gm;
272 s/^street-address:/*ad:/gm;
273 s/^org-name:/*rm:/gm;
274 s/^created:/*cr:/gm;
275
276 $_;
277}
278
279sub ip_request {
280 my ($self, $ip) = @_;
281
282 my $whois = $self->whois_request("$ip");
283
284 $whois =~ /^\*in: 0\.0\.0\.0 - 255\.255\.255\.255/
285 and return;
286
287 $whois =~ /^\*ac: XXX0/m # 192.0.0.0
288 and return;
289
290 $whois =~ /^%ERROR:/m
291 and return;
292
293 #while ($whois =~ s/^\*(?:ac|tc):\s+(\S+)\n//m) {
294 # $whois .= $self->whois_request("-FSTpn $1");
295 #}
296
200 $whois =~ s/^\*(?:pn|nh|mb|ch|so|rz|ny|st|rm):.*\n//mg; 297 $whois =~ s/^\*(?:pn|nh|mb|ch|so|rz|ny|st|rm):.*\n//mg;
201 298
202 $whois =~ s/\n+$//; 299 $whois =~ s/\n+$//;
203 300
204 $whois; 301 $whois;
217 inet_ntoa pack "N", $_[0]; 314 inet_ntoa pack "N", $_[0];
218} 315}
219 316
220our %WHOIS; 317our %WHOIS;
221 318
222$WHOIS{ARIN} = new Whois::ARIN ARIN => "whois.arin.net", maxjobs => 12; 319#$WHOIS{ARIN} = new Whois::ARIN ARIN => "whois.arin.net", port => 43, maxjobs => 12;
320$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; 321$WHOIS{RIPE} = new Whois::RIPE RIPE => "whois.ripe.net", port => 43, rflags => "-FTin ", maxjobs => 20;
322$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; 323$WHOIS{LACNIC} = new Whois::RIPE LACNIC => "whois.lacnic.net", port => 43, maxjobs => 20;
225 324
226$whoislock = new Coro::SemaphoreSet; 325$whoislock = new Coro::SemaphoreSet;
227 326
228sub ip_request { 327sub ip_request {
229 my $ip = $_[0]; 328 my $ip = $_[0];
241 } 340 }
242 } 341 }
243 342
244 my ($arin, $ripe, $apnic); 343 my ($arin, $ripe, $apnic);
245 344
246 $whois = $WHOIS{APNIC}->ip_request($ip) 345 $whois = $WHOIS{RIPE}->ip_request($ip)
247 || $WHOIS{RIPE} ->ip_request($ip) 346 || $WHOIS{APNIC} ->ip_request($ip)
248 || $WHOIS{ARIN} ->ip_request($ip); 347 || $WHOIS{ARIN} ->ip_request($ip)
348# || $WHOIS{LACNIC}->ip_request($ip)
349 ;
249 350
250 $whois =~ /^\*in: ([0-9.]+)\s+-\s+([0-9.]+)\s*$/mi 351 $whois =~ /^\*in: ([0-9.]+)\s+-\s+([0-9.]+)\s*$/mi
251 or do { warn "$whois($ip): no addresses found\n", last }; 352 or do { warn "$whois($ip): no addresses found\n", last };
252 353
253 my ($ip0, $ip1) = ($1, $2); 354 my ($ip0, $ip1) = ($1, $2);
267 $iprange->db_sync; 368 $iprange->db_sync;
268 369
269 $whois; 370 $whois;
270} 371}
271 372
373if (0) {
374 #print ip_request "68.52.164.8"; # goof
375 #print "\n\n";
376 #print ip_request "200.202.220.222"; # lacnic
377 #print "\n\n";
378 #print ip_request "62.116.167.250";
379 #print "\n\n";
380 #print ip_request "133.11.128.254"; # jp
381 #print "\n\n";
382 print ip_request "80.131.153.93";
383 print "\n\n";
384}
272 385
3861;
273 387
388

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines