| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package App::HTTP_Proxy_IMP::Relay; | ||
| 5 | use fields ( | ||
| 6 | 'fds', # file descriptors | ||
| 7 | 'conn', # App::HTTP_Proxy_IMP::HTTPConn object | ||
| 8 | 'acct', # collect accounting | ||
| 9 | ); | ||
| 10 | |||
| 11 | use App::HTTP_Proxy_IMP::Debug; | ||
| 12 | use Scalar::Util 'weaken'; | ||
| 13 | use IO::Socket::SSL; | ||
| 14 | use AnyEvent; | ||
| 15 | |||
| 16 | # active relay, inserted in new, removed in $idlet timer | ||
| 17 | my @relays; | ||
| 18 | sub relays { return grep { $_ } @relays } | ||
| 19 | |||
| 20 | # creates new relay and puts it into @relays as weak reference | ||
| 21 | sub new { | ||
| 22 | my ($class,$cfd,$upstream,$conn) = @_; | ||
| 23 | my $self = fields::new($class); | ||
| 24 | debug("create relay $self"); | ||
| 25 | |||
| 26 | if ( $upstream && ! ref($upstream)) { | ||
| 27 | $upstream =~m{\A(?:\[([a-f\d:.]+)\]|([\da-z_\-.]+)):(\d+)\Z} or | ||
| 28 | die "invalid upstream specification: $upstream"; | ||
| 29 | $upstream = [ $1||$2, $3 ]; | ||
| 30 | } | ||
| 31 | |||
| 32 | my $cobj = $conn->new_connection({ | ||
| 33 | daddr => $cfd->sockhost, | ||
| 34 | dport => $cfd->sockport, | ||
| 35 | saddr => $cfd->peerhost, | ||
| 36 | sport => $cfd->peerport, | ||
| 37 | upstream => $upstream, | ||
| 38 | },$self); | ||
| 39 | |||
| 40 | #debug("create connection $cobj"); | ||
| 41 | $self->{conn} = $cobj; | ||
| 42 | my $cfo = $self->{fds}[0] = App::HTTP_Proxy_IMP::Relay::FD->new(0,$cfd,$self,1); | ||
| 43 | $cfo->mask( r => 1 ); # enable read | ||
| 44 | |||
| 45 | push @relays, $self; | ||
| 46 | weaken($relays[-1]); | ||
| 47 | |||
| 48 | return $self; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub DESTROY { | ||
| 52 | my $self = shift; | ||
| 53 | $self->account('destroy'); | ||
| 54 | $self->xdebug("destroy relay $self"); | ||
| 55 | } | ||
| 56 | |||
| 57 | sub acctinfo { | ||
| 58 | my ($self,$acct) = @_; | ||
| 59 | $self->{acct} = $acct; | ||
| 60 | } | ||
| 61 | sub account { | ||
| 62 | my ($self,$what,%args) = @_; | ||
| 63 | my $acct = $self->{acct}; | ||
| 64 | $acct = $acct ? { %$acct,%args } : \%args if %args; | ||
| 65 | $acct or return; | ||
| 66 | $self->{acct} = undef; | ||
| 67 | if ( my $t = delete $acct->{start} ) { | ||
| 68 | $acct->{duration} = AnyEvent->now - $t; | ||
| 69 | } | ||
| 70 | my @msg; | ||
| 71 | for( sort keys %$acct ) { | ||
| 72 | my $t; | ||
| 73 | my $v = $acct->{$_}; | ||
| 74 | if ( ! defined $v ) { | ||
| 75 | next; | ||
| 76 | } elsif ( ref($v) eq 'ARRAY') { | ||
| 77 | $t = "$_=[".join(',',map { _quote($_) } @$v)."]"; | ||
| 78 | } elsif ( defined $v ) { | ||
| 79 | $t = "$_="._quote($v); | ||
| 80 | } | ||
| 81 | push @msg,$t; | ||
| 82 | } | ||
| 83 | print STDERR "ACCT @msg\n"; | ||
| 84 | } | ||
| 85 | |||
| 86 | sub _quote { | ||
| 87 | my $text = shift; | ||
| 88 | $text =~s{([\000-\037\\"\377-\777])}{ sprintf("\\%03o",ord($1)) }eg; | ||
| 89 | return $text =~m{ } ? qq["$text"]:$text; | ||
| 90 | } | ||
| 91 | |||
| 92 | sub xdebug { | ||
| 93 | my $self = shift; | ||
| 94 | my $conn = $self->{conn}; | ||
| 95 | if ( my $xdebug = UNIVERSAL::can($conn,'xdebug') ) { | ||
| 96 | unshift @_,$conn; | ||
| 97 | goto &$xdebug; | ||
| 98 | } else { | ||
| 99 | goto &debug; | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | |||
| 104 | # non-fatal problem | ||
| 105 | sub error { | ||
| 106 | my ($self,$reason) = @_; | ||
| 107 | warn "[error] ".( $self->{conn} && $self->{conn}->id || 'noid')." $reason\n"; | ||
| 108 | return 0; | ||
| 109 | } | ||
| 110 | |||
| 111 | # fatal problem - close connection | ||
| 112 | sub fatal { | ||
| 113 | my ($self,$reason) = @_; | ||
| 114 | warn "[fatal] ".( $self->{conn} && $self->{conn}->id || 'noid')." $reason\n"; | ||
| 115 | $self->close; | ||
| 116 | return 0; | ||
| 117 | } | ||
| 118 | |||
| 119 | sub connect:method { | ||
| 120 | my ($self,$to,$host,$port,$callback,$reconnect) = @_; | ||
| 121 | my $fo = $self->{fds}[$to] ||= App::HTTP_Proxy_IMP::Relay::FD->new($to,undef,$self); | ||
| 122 | $fo->connect($host,$port,$callback,$reconnect); | ||
| 123 | } | ||
| 124 | |||
| 125 | # masks/unmasks fd for dir, rw = r|w | ||
| 126 | sub mask { | ||
| 127 | my ($self,$dir,$rw,$v) = @_; | ||
| 128 | my $fd = $self->{fds}[$dir] or do { | ||
| 129 | warn "fd dir=$dir does not exists\n"; | ||
| 130 | return; | ||
| 131 | }; | ||
| 132 | $fd->mask($rw,$v); | ||
| 133 | } | ||
| 134 | |||
| 135 | sub fd { | ||
| 136 | my ($self,$dir) = @_; | ||
| 137 | return $self->{fds}[$dir]; | ||
| 138 | } | ||
| 139 | |||
| 140 | # send some data via fd dir | ||
| 141 | sub forward { | ||
| 142 | my ($self,$from,$to,$data) = @_; | ||
| 143 | my $fo = $self->{fds}[$to] or return | ||
| 144 | $self->fatal("cannot write to $to - no such fo"); | ||
| 145 | $self->xdebug("$to>$from - forward %d bytes",length($data)); | ||
| 146 | $fo->write($data,$from); | ||
| 147 | } | ||
| 148 | |||
| 149 | # ssl interception, e.g. upgrade both client and server to SSL sockets, | ||
| 150 | # where I can read/write unencrypted data | ||
| 151 | sub sslify { | ||
| 152 | my ($self,$from,$to,$hostname,$callback) = @_; | ||
| 153 | my $conn = $self->{conn} or return; | ||
| 154 | my $mitm = $conn->{mitm} or return; # no MITM needed | ||
| 155 | |||
| 156 | # destroy the current connection object and create a new obne | ||
| 157 | $conn = $self->{conn} = $conn->clone; | ||
| 158 | $conn->{intunnel} = 1; | ||
| 159 | |||
| 160 | my $sfo = $self->{fds}[$from] or return | ||
| 161 | $self->fatal("cannot startssl $from - no such fo"); | ||
| 162 | |||
| 163 | # stop handling all data | ||
| 164 | $self->mask($to,r=>0); | ||
| 165 | $self->mask($from,r=>0); | ||
| 166 | weaken( my $wself = $self ); | ||
| 167 | |||
| 168 | my %sslargs = ( | ||
| 169 | SSL_verifycn_name => $hostname, | ||
| 170 | SSL_verifycn_schema => 'http', | ||
| 171 | SSL_hostname => $hostname, # SNI | ||
| 172 | $conn->{capath} ? ( | ||
| 173 | SSL_verify_mode => SSL_VERIFY_PEER, | ||
| 174 | ( -d $conn->{capath} ? 'SSL_ca_path' : 'SSL_ca_file' ), | ||
| 175 | $conn->{capath} | ||
| 176 | ):( | ||
| 177 | SSL_verify_mode => SSL_VERIFY_NONE | ||
| 178 | ) | ||
| 179 | ); | ||
| 180 | $sfo->startssl( %sslargs, sub { | ||
| 181 | my $sfo = shift; | ||
| 182 | my ($cert,$key) = $mitm->clone_cert($sfo->{fd}->peer_certificate); | ||
| 183 | my $cfo = $wself->{fds}[$to] or return | ||
| 184 | $wself->fatal("cannot startssl $to - no such fo"); | ||
| 185 | $cfo->startssl( | ||
| 186 | SSL_server => 1, | ||
| 187 | SSL_cert => $cert, | ||
| 188 | SSL_key => $key, | ||
| 189 | sub { | ||
| 190 | # allow data again | ||
| 191 | $self->mask($to,r=>1); | ||
| 192 | $self->mask($from,r=>1); | ||
| 193 | $callback->() if $callback; | ||
| 194 | } | ||
| 195 | ); | ||
| 196 | }); | ||
| 197 | } | ||
| 198 | |||
| 199 | # closes relay | ||
| 200 | sub close:method { | ||
| 201 | my $self = shift; | ||
| 202 | #debug("close $self"); | ||
| 203 | undef $self->{conn}; | ||
| 204 | @relays = grep { !$_ or $_ != $self } @relays; | ||
| 205 | $_ && $_->close for @{$self->{fds}}; | ||
| 206 | @{$self->{fds}} = (); | ||
| 207 | } | ||
| 208 | |||
| 209 | # shutdown part of relay | ||
| 210 | sub shutdown:method { | ||
| 211 | my ($self,$dir,$rw,$force) = @_; | ||
| 212 | my $fo = $self->{fds}[$dir] or return; | ||
| 213 | $fo->shutdown($rw,$force); | ||
| 214 | } | ||
| 215 | |||
| 216 | # check for condition, where we cannot transfer anymore data: | ||
| 217 | # - nowhere to read | ||
| 218 | # - nowhere to write too | ||
| 219 | sub closeIfDone { | ||
| 220 | my $self = shift; | ||
| 221 | my $sink = my $src = ''; | ||
| 222 | for my $fo (@{$self->{fds}}) { | ||
| 223 | $fo && $fo->{fd} or next; | ||
| 224 | $sink .= $fo->{dir} if $fo->{status} & 0b010; # not write-closed | ||
| 225 | $src .= $fo->{dir} if $fo->{status} & 0b100; # not read-closed | ||
| 226 | return if $fo->{wbuf} ne ''; # still has data to write | ||
| 227 | } | ||
| 228 | |||
| 229 | if ( $sink eq '' # nowhere to write | ||
| 230 | or $src eq '' # nowhere to read from new data | ||
| 231 | ) { | ||
| 232 | # close relay | ||
| 233 | return $self->close; | ||
| 234 | } | ||
| 235 | |||
| 236 | return; | ||
| 237 | } | ||
| 238 | |||
| 239 | |||
| 240 | # dump state to debug | ||
| 241 | sub dump_state { | ||
| 242 | my $self = shift; | ||
| 243 | my $conn = $self->{conn}; | ||
| 244 | if ( my $fds = $self->{fds} ) { | ||
| 245 | my @st; | ||
| 246 | for( my $i=0;$i<@$fds;$i++) { | ||
| 247 | push @st, sprintf("%d=%03b",$i,$fds->[$i]{status} || 0); | ||
| 248 | } | ||
| 249 | $conn->xdebug("status @st"); | ||
| 250 | } | ||
| 251 | $conn->dump_state(); | ||
| 252 | } | ||
| 253 | |||
| 254 | |||
| 255 | my $idlet = AnyEvent->timer( | ||
| 256 | after => 5, | ||
| 257 | interval => 5, cb => sub { | ||
| 258 | @relays = grep { $_ } @relays or return; | ||
| 259 | #debug("check timeouts for %d conn",+@relays); | ||
| 260 | my $now = AnyEvent->now; | ||
| 261 | RELAY: for my $r (@relays) { | ||
| 262 | for my $fo (@{$r->{fds}}) { | ||
| 263 | next RELAY if $_->{didit} + 30 > $now; | ||
| 264 | } | ||
| 265 | $r->xdebug("close because of timeout"); | ||
| 266 | $r->close | ||
| 267 | } | ||
| 268 | } | ||
| 269 | ); | ||
| 270 | |||
| 271 | ############################################################################ | ||
| 272 | # Filehandle | ||
| 273 | ############################################################################ | ||
| 274 | |||
| 275 | package App::HTTP_Proxy_IMP::Relay::FD; | ||
| 276 | use Carp 'croak'; | ||
| 277 | use Scalar::Util 'weaken'; | ||
| 278 | use App::HTTP_Proxy_IMP::Debug; | ||
| 279 | use AnyEvent::Socket qw(tcp_connect format_address); | ||
| 280 | use IO::Socket::SSL; | ||
| 281 | |||
| 282 | use fields ( | ||
| 283 | 'dir', # direction 0,1 | ||
| 284 | 'fd', # file descriptor | ||
| 285 | 'host', # destination hostname | ||
| 286 | 'status', # bitmap of read_shutdown|write_shutdown|connected | ||
| 287 | 'relay', # weak link to relay | ||
| 288 | 'didit', # time of last activity (read/write) | ||
| 289 | 'rbuf', # read buffer (read but not processed) | ||
| 290 | 'rsub', # read handler | ||
| 291 | 'rwatch', # AnyEvent watcher - undef if read is disabled | ||
| 292 | 'wbuf', # write buffer (not yet written to socket) | ||
| 293 | 'wsub', # write handler | ||
| 294 | 'wwatch', # AnyEvent watcher - undef if write is disabled | ||
| 295 | 'wsrc', # source of writes for stalled handling | ||
| 296 | ); | ||
| 297 | |||
| 298 | sub new { | ||
| 299 | my ($class,$dir,$fd,$relay,$connected) = @_; | ||
| 300 | my $self = fields::new($class); | ||
| 301 | $self->{dir} = $dir; | ||
| 302 | $self->{fd} = $fd; | ||
| 303 | $self->{status} = $connected ? 0b001 : 0; | ||
| 304 | #weaken( $self->{relay} = $relay ); | ||
| 305 | $self->{relay} = $relay; | ||
| 306 | $self->{rbuf} = $self->{wbuf} = ''; | ||
| 307 | return $self; | ||
| 308 | } | ||
| 309 | |||
| 310 | sub xdebug { | ||
| 311 | my $self = shift; | ||
| 312 | my $conn = $self->{relay}{conn}; | ||
| 313 | if ( my $xdebug = UNIVERSAL::can($conn,'xdebug') ) { | ||
| 314 | my $msg = "[$self->{dir}] ".shift(@_); | ||
| 315 | unshift @_,$conn,$msg; | ||
| 316 | goto &$xdebug; | ||
| 317 | } else { | ||
| 318 | goto &debug; | ||
| 319 | } | ||
| 320 | } | ||
| 321 | |||
| 322 | sub close:method { | ||
| 323 | my $self = shift; | ||
| 324 | $self->xdebug("close"); | ||
| 325 | if ( $self->{fd} ) { | ||
| 326 | $self->{fd} = undef; | ||
| 327 | delete $self->{relay}{fds}[$self->{dir}]; | ||
| 328 | $self->{relay}->closeIfDone; | ||
| 329 | } | ||
| 330 | %$self = (); | ||
| 331 | } | ||
| 332 | |||
| 333 | sub reset { | ||
| 334 | my $self = shift; | ||
| 335 | $self->xdebug("reset"); | ||
| 336 | close($self->{fd}) if $self->{fd}; | ||
| 337 | $self->{fd} = | ||
| 338 | $self->{rwatch} = $self->{rsub} = | ||
| 339 | $self->{wwatch} = $self->{wsub} = | ||
| 340 | $self->{host} = | ||
| 341 | $self->{wsrc} = | ||
| 342 | undef; | ||
| 343 | $self->{status} = $self->{didit} = 0; | ||
| 344 | $self->{rbuf} = $self->{wbuf} = ''; | ||
| 345 | return 1; | ||
| 346 | } | ||
| 347 | |||
| 348 | # attempt to shutdown fd. | ||
| 349 | # don't shutdown(1) if wbuf ne '' && ! $force | ||
| 350 | sub shutdown:method { | ||
| 351 | my ($self,$rw,$force) = @_; | ||
| 352 | my $what = $rw eq 'r' ? 0 : $rw eq 'w' ? 1 : $rw; | ||
| 353 | my $stat = $what ? 0b010 : 0b100; | ||
| 354 | return if $self->{status} & $stat && ! $force; # no change | ||
| 355 | |||
| 356 | $self->xdebug("shutdown $rw fn=".fileno($self->{fd})); | ||
| 357 | |||
| 358 | $self->{status} |= $stat; | ||
| 359 | if ( $rw && $self->{wbuf} ne '' ) { | ||
| 360 | return if ! $force; # will shutdown once all is written | ||
| 361 | $self->{wbuf} = ''; # drop rest | ||
| 362 | undef $self->{wsrc}; # don't re-enable, unclear state | ||
| 363 | undef $self->{wwatch}; | ||
| 364 | } | ||
| 365 | |||
| 366 | shutdown($self->{fd},$what); | ||
| 367 | # shutdown on both sides -> close | ||
| 368 | return $self->close if $self->{status} & 0b110 == 0b110; | ||
| 369 | |||
| 370 | # if all fd are closed, close the relay too | ||
| 371 | $self->{relay}->closeIfDone; | ||
| 372 | |||
| 373 | return 1; | ||
| 374 | } | ||
| 375 | |||
| 376 | |||
| 377 | sub mask { | ||
| 378 | my ($self,$rw,$val) = @_; | ||
| 379 | #debug("$self->{dir} $self->{fd} fn=".fileno($self->{fd})." $rw=>$val"); | ||
| 380 | if ( $rw eq 'r' ) { | ||
| 381 | if ( ! $val ) { | ||
| 382 | # disable read | ||
| 383 | undef $self->{rwatch}; | ||
| 384 | } else { | ||
| 385 | $self->{status} & 0b100 and return 0; # read shutdown already | ||
| 386 | $self->{rsub} ||= sub { _read($self) }; | ||
| 387 | $self->{rwatch} = AnyEvent->io( | ||
| 388 | fh => $self->{fd}, | ||
| 389 | poll => 'r', | ||
| 390 | cb => ref($val) ? $val : $self->{rsub} | ||
| 391 | ); | ||
| 392 | } | ||
| 393 | } elsif ( $rw eq 'w' ) { | ||
| 394 | if ( ! $val ) { | ||
| 395 | # disable write | ||
| 396 | undef $self->{wwatch}; | ||
| 397 | } else { | ||
| 398 | $self->{status} & 0b010 and return 0; # write shutdown already | ||
| 399 | $self->{wsub} ||= sub { _writebuf($self) }; | ||
| 400 | $self->{wwatch} = AnyEvent->io( | ||
| 401 | fh => $self->{fd}, | ||
| 402 | poll => 'w', | ||
| 403 | cb => ref($val) ? $val : $self->{wsub} | ||
| 404 | ); | ||
| 405 | } | ||
| 406 | } else { | ||
| 407 | croak("cannot set mask for $rw"); | ||
| 408 | } | ||
| 409 | return 1; | ||
| 410 | } | ||
| 411 | |||
| 412 | # write data, gets written from relay->send | ||
| 413 | sub write:method { | ||
| 414 | my ($self,$data,$from) = @_; | ||
| 415 | my $n = 0; | ||
| 416 | if ( $self->{wbuf} eq '' ) { | ||
| 417 | # no buffered data, set as buffer and try to write immediately | ||
| 418 | $self->{wbuf} = $data; | ||
| 419 | $n = _writebuf($self,$from) // return; # fatal? | ||
| 420 | } else { | ||
| 421 | # only append to buffer, will be written on write ready | ||
| 422 | $self->{wbuf} .= $data; | ||
| 423 | } | ||
| 424 | |||
| 425 | if ( $self->{wbuf} ne '' | ||
| 426 | && ! $self->{wsrc}{$from}++ ) { | ||
| 427 | # newly stalled, disable reads on $from for now | ||
| 428 | $self->{relay}->mask($from, r=>0); | ||
| 429 | } | ||
| 430 | return $n; | ||
| 431 | } | ||
| 432 | |||
| 433 | # gets called if wbuf is not empty, either from write or from callback | ||
| 434 | # when fd is writable again | ||
| 435 | sub _writebuf { | ||
| 436 | my $self = shift; | ||
| 437 | #debug("write $self fn=".fileno($self->{fd})); | ||
| 438 | my $n = syswrite($self->{fd},$self->{wbuf}); | ||
| 439 | #debug("write(%s,%d) -> %s", $self->{dir},length($self->{wbuf}), (defined $n ? $n : $!)); | ||
| 440 | if ( ! defined $n ) { | ||
| 441 | $self->{relay}->fatal("write($self->{dir}) failed: $!") | ||
| 442 | unless $!{EINTR} or $!{EAGAIN}; | ||
| 443 | return; | ||
| 444 | } | ||
| 445 | |||
| 446 | substr($self->{wbuf},0,$n,''); | ||
| 447 | $self->{didit} = AnyEvent->now; | ||
| 448 | |||
| 449 | if ( $self->{wbuf} eq '' ) { | ||
| 450 | # wrote everything | ||
| 451 | #debug("all written to $self->{dir}"); | ||
| 452 | undef $self->{wwatch}; | ||
| 453 | |||
| 454 | if ( $self->{status} & 0b100 ) { | ||
| 455 | # was marked for shutdown | ||
| 456 | shutdown($self->{fd},1); | ||
| 457 | # if all fd are closed, close the relay too | ||
| 458 | $self->{relay}->closeIfDone; | ||
| 459 | } | ||
| 460 | # enable read again on stalled fd | ||
| 461 | if ( my $src = $self->{wsrc} ) { | ||
| 462 | $self->{relay}->mask($_, r=>1) for (keys %$src); | ||
| 463 | } | ||
| 464 | } else { | ||
| 465 | # need to write more later | ||
| 466 | #debug("need to write more"); | ||
| 467 | mask($self,w=>1); | ||
| 468 | } | ||
| 469 | return $n; | ||
| 470 | } | ||
| 471 | |||
| 472 | # gets called if data are available on the socket | ||
| 473 | # but only, if we don't have unsent data in wbuf | ||
| 474 | # reads data into rbuf and calls connection->in | ||
| 475 | sub _read:method { | ||
| 476 | my $self = shift; | ||
| 477 | #debug("read $self fn=".fileno($self->{fd})); | ||
| 478 | my $n = sysread($self->{fd},$self->{rbuf},2**15,length($self->{rbuf})); | ||
| 479 | #debug("read done: ". (defined $n ? $n : $!)); | ||
| 480 | if ( ! defined $n ) { | ||
| 481 | if ( ! $!{EINTR} and ! $!{EAGAIN} ) { | ||
| 482 | # complain only if we are inside a request | ||
| 483 | # timeouts after inactivity are normal | ||
| 484 | return $self->{relay}->fatal("read($self->{dir}) failed: $!") | ||
| 485 | if $self->{relay}{conn}->open_requests; | ||
| 486 | |||
| 487 | # close connection | ||
| 488 | $self->xdebug("closing relay because of read error on $self->{dir}"); | ||
| 489 | return $self->{relay}->close; | ||
| 490 | } | ||
| 491 | return; | ||
| 492 | } | ||
| 493 | |||
| 494 | $self->{didit} = AnyEvent->now; | ||
| 495 | my $bytes = $self->{relay}{conn} | ||
| 496 | ->in($self->{dir},$self->{rbuf},!$n,$self->{didit}); | ||
| 497 | |||
| 498 | # fd/relay closed from within in() ? | ||
| 499 | defined $self->{fd} or return; | ||
| 500 | |||
| 501 | if ( $bytes ) { | ||
| 502 | # connection accepted $bytes | ||
| 503 | substr($self->{rbuf},0,$bytes,''); | ||
| 504 | } | ||
| 505 | |||
| 506 | return $self->{relay}->fatal( | ||
| 507 | "connection should have taken all remaining bytes on eof") | ||
| 508 | if !$n && $self->{rbuf} ne ''; | ||
| 509 | |||
| 510 | $self->shutdown('r') if ! $n; | ||
| 511 | } | ||
| 512 | |||
| 513 | sub connect:method { | ||
| 514 | my ($self,$host,$port,$callback,$reconnect) = @_; | ||
| 515 | |||
| 516 | # down existing connection if we should connect to another host | ||
| 517 | $self->reset if $self->{fd} and | ||
| 518 | ( $reconnect or $self->{host}||'' ne "$host.$port" ); | ||
| 519 | |||
| 520 | # if we have a connection already, keep it | ||
| 521 | if ( $self->{status} & 0b001 ) { # already connected | ||
| 522 | $callback->(); | ||
| 523 | return 1; | ||
| 524 | } | ||
| 525 | |||
| 526 | # (re)connect | ||
| 527 | $self->xdebug("connecting to $host.$port"); | ||
| 528 | # async dns lookup + connect | ||
| 529 | App::HTTP_Proxy_IMP::Relay::DNS::lookup($host, sub { | ||
| 530 | $self->{relay} or return; # relay already closed | ||
| 531 | if ( my $addr = shift ) { | ||
| 532 | tcp_connect($addr,$port, sub { | ||
| 533 | if ( my $fd = shift ) { | ||
| 534 | $self->{relay} or return; # relay already closed | ||
| 535 | $self->{fd} = $fd; | ||
| 536 | $self->{status} = 0b001; | ||
| 537 | $self->{host} = "$host.$port"; | ||
| 538 | $self->xdebug("connect done"); | ||
| 539 | $self->mask( r => 1 ); | ||
| 540 | $callback->(); | ||
| 541 | } else { | ||
| 542 | App::HTTP_Proxy_IMP::Relay::DNS::uncache($host,$addr); | ||
| 543 | $self->{relay} or return; # relay already closed | ||
| 544 | $self->{relay}->fatal("connect to $host.$port failed: $!"); | ||
| 545 | } | ||
| 546 | }); | ||
| 547 | } else { | ||
| 548 | $self->{relay}->fatal( | ||
| 549 | "connect to $host.$port failed: no such host (DNS)"); | ||
| 550 | } | ||
| 551 | }); | ||
| 552 | return -1; | ||
| 553 | } | ||
| 554 | |||
| 555 | sub startssl { | ||
| 556 | my $self = shift; | ||
| 557 | $self->{rbuf} eq '' or return | ||
| 558 | $self->{relay}->fatal("read buf $self->{dir} not empty before starting SSL: '$self->{rbuf}'"); | ||
| 559 | $self->{wbuf} eq '' or return | ||
| 560 | $self->{relay}->fatal("write buf $self->{dir} not empty before starting SSL: '$self->{wbuf}'"); | ||
| 561 | |||
| 562 | my $callback = @_%2 ? pop(@_):undef; | ||
| 563 | my %sslargs = @_; | ||
| 564 | IO::Socket::SSL->start_SSL( $self->{fd}, | ||
| 565 | %sslargs, | ||
| 566 | SSL_startHandshake => 0, | ||
| 567 | ) or die "failed to upgrade socket to SSL"; | ||
| 568 | my $sub = $sslargs{SSL_server} | ||
| 569 | ? \&IO::Socket::SSL::accept_SSL | ||
| 570 | : \&IO::Socket::SSL::connect_SSL; | ||
| 571 | _ssl($self,$sub,$callback,\%sslargs); | ||
| 572 | } | ||
| 573 | |||
| 574 | sub _ssl { | ||
| 575 | my ($self,$sub,$cb,$sslargs) = @_; | ||
| 576 | if ( $sub->($self->{fd}) ) { | ||
| 577 | $self->xdebug("ssl handshake success"); | ||
| 578 | $cb->($self) if $cb; | ||
| 579 | } elsif ( $!{EAGAIN} ) { | ||
| 580 | # retry | ||
| 581 | my $dir = | ||
| 582 | $SSL_ERROR == SSL_WANT_READ ? 'r' : | ||
| 583 | $SSL_ERROR == SSL_WANT_WRITE ? 'w' : | ||
| 584 | return $self->{relay}->fatal( "unhandled $SSL_ERROR on EAGAIN" ); | ||
| 585 | $self->mask( $dir => sub { _ssl($self,$sub,$cb,$sslargs) }); | ||
| 586 | } elsif ( $sslargs->{SSL_server} ) { | ||
| 587 | return $self->{relay}->fatal( "error on accept_SSL: $SSL_ERROR|$!" ); | ||
| 588 | } else { | ||
| 589 | return $self->{relay}->fatal( | ||
| 590 | "error on connect_SSL to $sslargs->{SSL_verifycn_name}: $SSL_ERROR|$!" ); | ||
| 591 | } | ||
| 592 | } | ||
| 593 | |||
| 594 | |||
| 595 | ############################################################################ | ||
| 596 | # DNS cache | ||
| 597 | ############################################################################ | ||
| 598 | |||
| 599 | package App::HTTP_Proxy_IMP::Relay::DNS; | ||
| 600 | use AnyEvent::DNS; | ||
| 601 | use Socket qw(AF_INET AF_INET6 inet_pton); | ||
| 602 | |||
| 603 | my %cache; | ||
| 604 | sub uncache { | ||
| 605 | my ($host,$addr) = @_; | ||
| 606 | my $e = $cache{lc($host)} or return; | ||
| 607 | @$e = grep { $_ ne $addr } @$e; | ||
| 608 | delete $cache{lc($host)} if !@$e; | ||
| 609 | } | ||
| 610 | |||
| 611 | sub lookup { | ||
| 612 | my ($host,$cb) = @_; | ||
| 613 | $host = lc($host); | ||
| 614 | |||
| 615 | if ( my $e = $cache{$host} ) { | ||
| 616 | return $cb->(@$e); | ||
| 617 | } elsif ( inet_pton(AF_INET,$host) || inet_pton(AF_INET6,$host) ) { | ||
| 618 | return $cb->($host); | ||
| 619 | } | ||
| 620 | |||
| 621 | AnyEvent::DNS::a($host,sub { | ||
| 622 | if ( @_ ) { | ||
| 623 | $cache{$host} = [ @_ ]; | ||
| 624 | return $cb->(@_); | ||
| 625 | } | ||
| 626 | |||
| 627 | # try AAAA | ||
| 628 | AnyEvent::DNS::aaaa($host,sub { | ||
| 629 | $cache{$host} = [ @_ ] if @_; | ||
| 630 | return $cb->(@_); | ||
| 631 | }); | ||
| 632 | }); | ||
| 633 | } | ||
| 634 | |||
| 635 | 1; |