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.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;
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 }
113sub ip_request { 120sub ip_request {
114 my ($self, $ip) = @_; 121 my ($self, $ip) = @_;
115 122
116 my $whois = $self->whois_request($ip); 123 my $whois = $self->whois_request($ip);
117 124
118 return () if $whois =~ /^No match/; 125 return if $whois =~ /^No match/;
119 126
120 if ($whois =~ /^To single out one record/m) { 127 if ($whois =~ /^To single out one record/m) {
121 my $handle; 128 my $handle;
122 while ($whois =~ /\G\S.*\(([A-Z0-9\-]+)\).*\n/mg) { 129 while ($whois =~ /\G\S.*\(([A-Z0-9\-]+)\).*\n/mg) {
123 $handle = $1; 130 $handle = $1;
124 #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
125 } 132 }
126 $handle or die "$whois ($ip): unparseable multimatch\n"; 133 $handle or die "$whois ($ip): unparseable multimatch\n";
127 $whois = $self->whois_request("!$handle"); 134 $whois = $self->whois_request("!$handle");
128 } 135 }
129 136
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
235 $whois =~ /^\*de: This network range is not allocated to /m # APINIC e.g. 24.0.0.0
236 and return;
237
188 $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
189 and return; 242 and return;
190 243
191 $whois =~ /^%ERROR:/m 244 $whois =~ /^%ERROR:/m
192 and return; 245 and return;
193 246
194 #while ($whois =~ s/^\*(?:ac|tc):\s+(\S+)\n//m) { 247 #while ($whois =~ s/^\*(?:ac|tc):\s+(\S+)\n//m) {
195 # $whois .= $self->whois_request("-FSTpn $1"); 248 # $whois .= $self->whois_request("-FSTpn $1");
196 #} 249 #}
197 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
198 $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;
199 301
200 $whois =~ s/\n+$//; 302 $whois =~ s/\n+$//;
201 303
202 $whois; 304 $whois;
203} 305}
204 306
205package netgeo; 307package netgeo;
206 308
309use Socket;
207use BerkeleyDB; 310use BerkeleyDB;
208use Socket;
209 311
210sub ip2int($) { 312sub ip2int($) {
211 unpack "N", inet_aton $_[0]; 313 unpack "N", inet_aton $_[0];
212} 314}
213 315
215 inet_ntoa pack "N", $_[0]; 317 inet_ntoa pack "N", $_[0];
216} 318}
217 319
218our %WHOIS; 320our %WHOIS;
219 321
220$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;
221$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;
222$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;
223 327
224$whoislock = new Coro::SemaphoreSet; 328$whoislock = new Coro::SemaphoreSet;
225 329
226sub ip_request { 330sub ip_request {
227 my $ip = $_[0]; 331 my $ip = $_[0];
239 } 343 }
240 } 344 }
241 345
242 my ($arin, $ripe, $apnic); 346 my ($arin, $ripe, $apnic);
243 347
244 $whois = $WHOIS{APNIC}->ip_request($ip) 348 $whois = $WHOIS{RIPE}->ip_request($ip)
245 || $WHOIS{RIPE} ->ip_request($ip) 349 || $WHOIS{APNIC} ->ip_request($ip)
246 || $WHOIS{ARIN} ->ip_request($ip); 350 || $WHOIS{ARIN} ->ip_request($ip)
351# || $WHOIS{LACNIC}->ip_request($ip)
352 ;
247 353
248 $whois =~ /^\*in: ([0-9.]+)\s+-\s+([0-9.]+)\s*$/mi 354 $whois =~ /^\*in: ([0-9.]+)\s+-\s+([0-9.]+)\s*$/mi
249 or do { warn "$whois($ip): no addresses found\n", last }; 355 or do { warn "$whois($ip): no addresses found\n", last };
250 356
251 my ($ip0, $ip1) = ($1, $2); 357 my ($ip0, $ip1) = ($1, $2);
265 $iprange->db_sync; 371 $iprange->db_sync;
266 372
267 $whois; 373 $whois;
268} 374}
269 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}
270 388
3891;
271 390
391

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines