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.6 by root, Thu Aug 30 03:35:56 2001 UTC vs.
Revision 1.14 by root, Sat Oct 5 12:42:22 2002 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;
14
15use BerkeleyDB;
13 16
14$Event::DIED = sub { 17$Event::DIED = sub {
15 Event::verbose_exception_handler(@_); 18 Event::verbose_exception_handler(@_);
16 #Event::unloop_all(); 19 #Event::unloop_all();
17}; 20};
61 my $timeout = 5; 64 my $timeout = 5;
62 65
63 while () { 66 while () {
64 my $fh = new Coro::Socket 67 my $fh = new Coro::Socket
65 PeerAddr => $self->ip, 68 PeerAddr => $self->ip,
66 PeerPort => 'whois', 69 PeerPort => $self->{port} || "whois",
67 Timeout => 30; 70 Timeout => 30;
68 if ($fh) { 71 if ($fh) {
69 print $fh "$query\n"; 72 print $fh "$query\n";
70 $fh->read($whois, 16*1024); # max 16k. whois stored 73 $fh->read($whois, 16*1024); # max 16k. whois stored
71 close $fh; 74 close $fh;
81 $timeout *= 2; 84 $timeout *= 2;
82 $timeout = 1 if $timeout > 600; 85 $timeout = 1 if $timeout > 600;
83 } else { 86 } else {
84 last; 87 last;
85 } 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;
86 } 93 }
87 } 94 }
88 95
89 $netgeo::whois{$id} = $whois; 96 $netgeo::whois{$id} = $whois;
90 } 97 }
165 $whois; 172 $whois;
166} 173}
167 174
168package Whois::RIPE; 175package Whois::RIPE;
169 176
177use Socket;
170use base Whois; 178use base Whois;
171 179
172sub sanitize { 180sub sanitize {
173 local $_ = $_[1]; 181 local $_ = $_[1];
182
174 s/^%.*\n//gm; 183 s/^%.*\n//gm;
175 s/^\n+//; 184 s/^\n+//;
176 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
177 $_; 201 $_;
178} 202}
179 203
180sub ip_request { 204sub ip_request {
181 my ($self, $ip) = @_; 205 my ($self, $ip) = @_;
182 206
183 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;
184 231
185 $whois =~ /^\*in: 0\.0\.0\.0 - 255\.255\.255\.255/ 232 $whois =~ /^\*in: 0\.0\.0\.0 - 255\.255\.255\.255/
186 and return; 233 and return;
187 234
188 $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
189 and return; 239 and return;
190 240
191 $whois =~ /^%ERROR:/m 241 $whois =~ /^%ERROR:/m
192 and return; 242 and return;
193 243
194 #while ($whois =~ s/^\*(?:ac|tc):\s+(\S+)\n//m) { 244 #while ($whois =~ s/^\*(?:ac|tc):\s+(\S+)\n//m) {
195 # $whois .= $self->whois_request("-FSTpn $1"); 245 # $whois .= $self->whois_request("-FSTpn $1");
196 #} 246 #}
197 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
198 $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;
199 298
200 $whois =~ s/\n+$//; 299 $whois =~ s/\n+$//;
201 300
202 $whois; 301 $whois;
203} 302}
204 303
205package netgeo; 304package netgeo;
206 305
306use Socket;
207use BerkeleyDB; 307use BerkeleyDB;
208use Socket;
209 308
210sub ip2int($) { 309sub ip2int($) {
211 unpack "N", inet_aton $_[0]; 310 unpack "N", inet_aton $_[0];
212} 311}
213 312
215 inet_ntoa pack "N", $_[0]; 314 inet_ntoa pack "N", $_[0];
216} 315}
217 316
218our %WHOIS; 317our %WHOIS;
219 318
220$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;
221$WHOIS{RIPE} = new Whois::RIPE RIPE => "whois.ripe.net", maxjobs => 20; 321$WHOIS{RIPE} = new Whois::RIPE RIPE => "whois.ripe.net", port => 43, rflags => "-FSTin ", maxjobs => 20;
322$WHOIS{APNIC} = new Whois::RIPE APNIC => "whois.apnic.net", port => 43, rflags => "-FSTin ", maxjobs => 20;
222$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;
223 324
224$whoislock = new Coro::SemaphoreSet; 325$whoislock = new Coro::SemaphoreSet;
225 326
226sub ip_request { 327sub ip_request {
227 my $ip = $_[0]; 328 my $ip = $_[0];
239 } 340 }
240 } 341 }
241 342
242 my ($arin, $ripe, $apnic); 343 my ($arin, $ripe, $apnic);
243 344
244 $whois = $WHOIS{APNIC}->ip_request($ip) 345 $whois = $WHOIS{RIPE}->ip_request($ip)
245 || $WHOIS{RIPE} ->ip_request($ip) 346 || $WHOIS{APNIC} ->ip_request($ip)
246 || $WHOIS{ARIN} ->ip_request($ip); 347 || $WHOIS{ARIN} ->ip_request($ip)
348 || $WHOIS{LACNIC}->ip_request($ip)
349 ;
247 350
248 $whois =~ /^\*in: ([0-9.]+)\s+-\s+([0-9.]+)\s*$/mi 351 $whois =~ /^\*in: ([0-9.]+)\s+-\s+([0-9.]+)\s*$/mi
249 or do { warn "$whois($ip): no addresses found\n", last }; 352 or do { warn "$whois($ip): no addresses found\n", last };
250 353
251 my ($ip0, $ip1) = ($1, $2); 354 my ($ip0, $ip1) = ($1, $2);
265 $iprange->db_sync; 368 $iprange->db_sync;
266 369
267 $whois; 370 $whois;
268} 371}
269 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 "151.197.52.251";
383 #print "\n\n";
384}
270 385
3861;
271 387
388

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines