| 1 |
package Proxy::Proxy; |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
use POPFile::Module; |
|---|
| 29 |
@ISA = ( "POPFile::Module" ); |
|---|
| 30 |
|
|---|
| 31 |
use IO::Handle; |
|---|
| 32 |
use IO::Socket; |
|---|
| 33 |
use IO::Select; |
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
my $eol = "\015\012"; |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
sub new |
|---|
| 47 |
{ |
|---|
| 48 |
my $type = shift; |
|---|
| 49 |
my $self = POPFile::Module->new(); |
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
$self->{classifier__} = 0; |
|---|
| 54 |
$self->{history__} = 0; |
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
$self->{child_} = 0; |
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
$self->{pipe_cache__} = {}; |
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
$self->{api_session__} = ''; |
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
$self->{ssl_not_supported_error_} = '-ERR SSL connection is not supported since required modules are not installed'; |
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
$self->{connect_banner__} = ''; |
|---|
| 90 |
|
|---|
| 91 |
return bless $self, $type; |
|---|
| 92 |
} |
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
sub initialize |
|---|
| 103 |
{ |
|---|
| 104 |
my ( $self ) = @_; |
|---|
| 105 |
|
|---|
| 106 |
$self->config_( 'enabled', 1 ); |
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
$self->config_( 'socks_server', '' ); |
|---|
| 112 |
$self->config_( 'socks_port', 1080 ); |
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
$self->config_( 'unix_socket', '' ); |
|---|
| 119 |
|
|---|
| 120 |
return 1; |
|---|
| 121 |
} |
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
sub start |
|---|
| 134 |
{ |
|---|
| 135 |
my ( $self ) = @_; |
|---|
| 136 |
|
|---|
| 137 |
my $name = $self->name(); |
|---|
| 138 |
my $unix_socket = $self->config_( 'unix_socket' ); |
|---|
| 139 |
|
|---|
| 140 |
if ( !$unix_socket ) { |
|---|
| 141 |
|
|---|
| 142 |
my $port = $self->config_( 'port' ); |
|---|
| 143 |
my $local = ( ( $self->config_( 'local' ) || 0 ) == 1 ); |
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 |
$self->log_( 1, "Opening listening socket for $name proxy on port $port." ); |
|---|
| 148 |
|
|---|
| 149 |
$self->{server__} = IO::Socket::INET->new( |
|---|
| 150 |
Proto => 'tcp', |
|---|
| 151 |
( $local ? ( LocalAddr => 'localhost' ) : () ), |
|---|
| 152 |
LocalPort => $port, |
|---|
| 153 |
Listen => SOMAXCONN, |
|---|
| 154 |
Reuse => 1 |
|---|
| 155 |
); |
|---|
| 156 |
|
|---|
| 157 |
if ( !defined( $self->{server__} ) ) { |
|---|
| 158 |
print STDERR <<EOM; |
|---|
| 159 |
|
|---|
| 160 |
\nCouldn\'t start the $name proxy because POPFile could not bind to the |
|---|
| 161 |
listen port $port. This could be because there is another service |
|---|
| 162 |
using that port or because you do not have the right privileges on |
|---|
| 163 |
your system (On Unix systems this can happen if you are not root |
|---|
| 164 |
and the port you specified is less than 1024). |
|---|
| 165 |
The original error message: $! |
|---|
| 166 |
|
|---|
| 167 |
EOM |
|---|
| 168 |
|
|---|
| 169 |
return 0; |
|---|
| 170 |
} |
|---|
| 171 |
} else { |
|---|
| 172 |
|
|---|
| 173 |
unlink $unix_socket if ( -e $unix_socket ); |
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 |
$self->log_( 1, "Opening listening socket for $name proxy on unix socket $unix_socket." ); |
|---|
| 178 |
|
|---|
| 179 |
$self->{server__} = IO::Socket::UNIX->new( |
|---|
| 180 |
Type => SOCK_STREAM, |
|---|
| 181 |
Local => $unix_socket, |
|---|
| 182 |
Listen => SOMAXCONN, |
|---|
| 183 |
); |
|---|
| 184 |
|
|---|
| 185 |
if ( !defined( $self->{server__} ) ) { |
|---|
| 186 |
print STDERR <<EOM; |
|---|
| 187 |
|
|---|
| 188 |
\nCouldn\'t start the $name proxy because POPFile could not bind to the |
|---|
| 189 |
unix socket $unix_socket. This could be because there is another |
|---|
| 190 |
service using that socket or because you do not have the right |
|---|
| 191 |
privileges on your system. |
|---|
| 192 |
The original error message: $! |
|---|
| 193 |
|
|---|
| 194 |
EOM |
|---|
| 195 |
|
|---|
| 196 |
return 0; |
|---|
| 197 |
} |
|---|
| 198 |
} |
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 201 |
|
|---|
| 202 |
|
|---|
| 203 |
$self->{selector__} = new IO::Select( $self->{server__} ); |
|---|
| 204 |
|
|---|
| 205 |
|
|---|
| 206 |
|
|---|
| 207 |
$self->register_configuration_item_( 'configuration', |
|---|
| 208 |
$name . '_socks_configuration', |
|---|
| 209 |
'socks-widget.thtml', |
|---|
| 210 |
$self ); |
|---|
| 211 |
|
|---|
| 212 |
return 1; |
|---|
| 213 |
} |
|---|
| 214 |
|
|---|
| 215 |
|
|---|
| 216 |
|
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 |
|
|---|
| 220 |
|
|---|
| 221 |
|
|---|
| 222 |
|
|---|
| 223 |
|
|---|
| 224 |
sub stop |
|---|
| 225 |
{ |
|---|
| 226 |
my ( $self ) = @_; |
|---|
| 227 |
|
|---|
| 228 |
if ( $self->{api_session__} ne '' ) { |
|---|
| 229 |
$self->{classifier__}->release_session_key( $self->{api_session__} ); |
|---|
| 230 |
} |
|---|
| 231 |
|
|---|
| 232 |
|
|---|
| 233 |
|
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 |
close $self->{server__} if ( defined( $self->{server__} ) ); |
|---|
| 237 |
} |
|---|
| 238 |
|
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 |
|
|---|
| 242 |
|
|---|
| 243 |
|
|---|
| 244 |
|
|---|
| 245 |
|
|---|
| 246 |
|
|---|
| 247 |
|
|---|
| 248 |
|
|---|
| 249 |
|
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 |
sub service |
|---|
| 253 |
{ |
|---|
| 254 |
my ( $self ) = @_; |
|---|
| 255 |
|
|---|
| 256 |
|
|---|
| 257 |
|
|---|
| 258 |
|
|---|
| 259 |
|
|---|
| 260 |
|
|---|
| 261 |
|
|---|
| 262 |
|
|---|
| 263 |
|
|---|
| 264 |
if ( ( defined( $self->{selector__}->can_read(0) ) ) && |
|---|
| 265 |
( $self->{alive_} ) ) { |
|---|
| 266 |
if ( my $client = $self->{server__}->accept() ) { |
|---|
| 267 |
|
|---|
| 268 |
|
|---|
| 269 |
|
|---|
| 270 |
if ( $self->{api_session__} eq '' ) { |
|---|
| 271 |
$self->{api_session__} = |
|---|
| 272 |
$self->{classifier__}->get_session_key( 'admin', '' ); |
|---|
| 273 |
} |
|---|
| 274 |
|
|---|
| 275 |
|
|---|
| 276 |
|
|---|
| 277 |
|
|---|
| 278 |
|
|---|
| 279 |
|
|---|
| 280 |
my $acceptable = 0; |
|---|
| 281 |
|
|---|
| 282 |
if ( ref $client eq 'IO::Socket::UNIX' ) { |
|---|
| 283 |
|
|---|
| 284 |
|
|---|
| 285 |
|
|---|
| 286 |
|
|---|
| 287 |
|
|---|
| 288 |
$acceptable = 1; |
|---|
| 289 |
} else { |
|---|
| 290 |
|
|---|
| 291 |
my ( $remote_port, $remote_host ) = sockaddr_in( |
|---|
| 292 |
$client->peername() ); |
|---|
| 293 |
|
|---|
| 294 |
$acceptable = ( ( ( $self->config_( 'local' ) || 0 ) == 0 ) || |
|---|
| 295 |
( $remote_host eq inet_aton( "127.0.0.1" ) ) ); |
|---|
| 296 |
} |
|---|
| 297 |
|
|---|
| 298 |
if ( $acceptable ) { |
|---|
| 299 |
|
|---|
| 300 |
|
|---|
| 301 |
|
|---|
| 302 |
|
|---|
| 303 |
|
|---|
| 304 |
|
|---|
| 305 |
binmode( $client ); |
|---|
| 306 |
|
|---|
| 307 |
if ( $self->config_( 'force_fork' ) ) { |
|---|
| 308 |
my ( $pid, $pipe ) = &{$self->{forker_}}; |
|---|
| 309 |
|
|---|
| 310 |
|
|---|
| 311 |
|
|---|
| 312 |
|
|---|
| 313 |
if ( !defined( $pid ) || ( $pid == 0 ) ) { |
|---|
| 314 |
$self->{child_}( $self, $client, |
|---|
| 315 |
$self->{api_session__} ); |
|---|
| 316 |
if ( defined( $pid ) ) { |
|---|
| 317 |
&{$self->{childexit_}}( 0 ); |
|---|
| 318 |
} |
|---|
| 319 |
} |
|---|
| 320 |
} else { |
|---|
| 321 |
pipe my $reader, my $writer; |
|---|
| 322 |
|
|---|
| 323 |
$self->{child_}( $self, $client, $self->{api_session__} ); |
|---|
| 324 |
close $reader; |
|---|
| 325 |
} |
|---|
| 326 |
} |
|---|
| 327 |
|
|---|
| 328 |
close $client; |
|---|
| 329 |
} |
|---|
| 330 |
} |
|---|
| 331 |
|
|---|
| 332 |
return 1; |
|---|
| 333 |
} |
|---|
| 334 |
|
|---|
| 335 |
|
|---|
| 336 |
|
|---|
| 337 |
|
|---|
| 338 |
|
|---|
| 339 |
|
|---|
| 340 |
|
|---|
| 341 |
|
|---|
| 342 |
|
|---|
| 343 |
|
|---|
| 344 |
|
|---|
| 345 |
|
|---|
| 346 |
sub forked |
|---|
| 347 |
{ |
|---|
| 348 |
my ( $self ) = @_; |
|---|
| 349 |
|
|---|
| 350 |
close $self->{server__}; |
|---|
| 351 |
} |
|---|
| 352 |
|
|---|
| 353 |
|
|---|
| 354 |
|
|---|
| 355 |
|
|---|
| 356 |
|
|---|
| 357 |
|
|---|
| 358 |
|
|---|
| 359 |
|
|---|
| 360 |
|
|---|
| 361 |
|
|---|
| 362 |
|
|---|
| 363 |
sub tee_ |
|---|
| 364 |
{ |
|---|
| 365 |
my ( $self, $socket, $text ) = @_; |
|---|
| 366 |
|
|---|
| 367 |
|
|---|
| 368 |
$self->log_( 1, $text ); |
|---|
| 369 |
print $socket $text; |
|---|
| 370 |
} |
|---|
| 371 |
|
|---|
| 372 |
|
|---|
| 373 |
|
|---|
| 374 |
|
|---|
| 375 |
|
|---|
| 376 |
|
|---|
| 377 |
|
|---|
| 378 |
|
|---|
| 379 |
|
|---|
| 380 |
|
|---|
| 381 |
|
|---|
| 382 |
|
|---|
| 383 |
|
|---|
| 384 |
|
|---|
| 385 |
sub echo_to_regexp_ |
|---|
| 386 |
{ |
|---|
| 387 |
my ( $self, $mail, $client, $regexp, $log, $suppress ) = @_; |
|---|
| 388 |
|
|---|
| 389 |
$log = 0 if (!defined($log)); |
|---|
| 390 |
|
|---|
| 391 |
while ( my $line = $self->slurp_( $mail ) ) { |
|---|
| 392 |
if (!defined($suppress) || !( $line =~ $suppress )) { |
|---|
| 393 |
if ( !$log ) { |
|---|
| 394 |
print $client $line; |
|---|
| 395 |
} else { |
|---|
| 396 |
$self->tee_( $client, $line ); |
|---|
| 397 |
} |
|---|
| 398 |
} else { |
|---|
| 399 |
$self->log_( 2, "Suppressed: $line" ); |
|---|
| 400 |
} |
|---|
| 401 |
|
|---|
| 402 |
if ( $line =~ $regexp ) { |
|---|
| 403 |
last; |
|---|
| 404 |
} |
|---|
| 405 |
} |
|---|
| 406 |
} |
|---|
| 407 |
|
|---|
| 408 |
|
|---|
| 409 |
|
|---|
| 410 |
|
|---|
| 411 |
|
|---|
| 412 |
|
|---|
| 413 |
|
|---|
| 414 |
|
|---|
| 415 |
|
|---|
| 416 |
|
|---|
| 417 |
|
|---|
| 418 |
sub echo_to_dot_ |
|---|
| 419 |
{ |
|---|
| 420 |
my ( $self, $mail, $client ) = @_; |
|---|
| 421 |
|
|---|
| 422 |
|
|---|
| 423 |
|
|---|
| 424 |
|
|---|
| 425 |
|
|---|
| 426 |
$self->echo_to_regexp_( $mail, $client, qr/^\.(\r\n|\r|\n)$/); |
|---|
| 427 |
} |
|---|
| 428 |
|
|---|
| 429 |
|
|---|
| 430 |
|
|---|
| 431 |
|
|---|
| 432 |
|
|---|
| 433 |
|
|---|
| 434 |
|
|---|
| 435 |
|
|---|
| 436 |
|
|---|
| 437 |
|
|---|
| 438 |
|
|---|
| 439 |
|
|---|
| 440 |
|
|---|
| 441 |
|
|---|
| 442 |
|
|---|
| 443 |
sub get_response_ |
|---|
| 444 |
{ |
|---|
| 445 |
my ( $self, $mail, $client, $command, $null_resp, $suppress ) = @_; |
|---|
| 446 |
|
|---|
| 447 |
$null_resp = 0 if (!defined $null_resp); |
|---|
| 448 |
$suppress = 0 if (!defined $suppress); |
|---|
| 449 |
|
|---|
| 450 |
unless ( defined($mail) && $mail->connected ) { |
|---|
| 451 |
|
|---|
| 452 |
$self->tee_( $client, "$self->{connection_timeout_error_}$eol" ); |
|---|
| 453 |
return ( $self->{connection_timeout_error_}, 0 ); |
|---|
| 454 |
} |
|---|
| 455 |
|
|---|
| 456 |
|
|---|
| 457 |
$self->tee_( $mail, $command. $eol ); |
|---|
| 458 |
|
|---|
| 459 |
my $response; |
|---|
| 460 |
|
|---|
| 461 |
|
|---|
| 462 |
|
|---|
| 463 |
my $can_read = 0; |
|---|
| 464 |
if ( $mail =~ /ssl/i ) { |
|---|
| 465 |
$can_read = ( $mail->pending() > 0 ); |
|---|
| 466 |
} |
|---|
| 467 |
if ( !$can_read ) { |
|---|
| 468 |
my $selector = new IO::Select( $mail ); |
|---|
| 469 |
my ( $ready ) = $selector->can_read( |
|---|
| 470 |
( !$null_resp ? $self->global_config_( 'timeout' ) : .5 ) ); |
|---|
| 471 |
$can_read = defined( $ready ) && ( $ready == $mail ); |
|---|
| 472 |
} |
|---|
| 473 |
|
|---|
| 474 |
if ( $can_read ) { |
|---|
| 475 |
$response = $self->slurp_( $mail ); |
|---|
| 476 |
|
|---|
| 477 |
if ( $response ) { |
|---|
| 478 |
|
|---|
| 479 |
|
|---|
| 480 |
|
|---|
| 481 |
$self->tee_( $client, $response ) if ( !$suppress ); |
|---|
| 482 |
return ( $response, 1 ); |
|---|
| 483 |
} |
|---|
| 484 |
} |
|---|
| 485 |
|
|---|
| 486 |
if ( !$null_resp ) { |
|---|
| 487 |
|
|---|
| 488 |
|
|---|
| 489 |
$self->tee_( $client, "$self->{connection_timeout_error_}$eol" ); |
|---|
| 490 |
return ( $self->{connection_timeout_error_}, 0 ); |
|---|
| 491 |
} else { |
|---|
| 492 |
$self->tee_($client, ""); |
|---|
| 493 |
return ( "", 1 ); |
|---|
| 494 |
} |
|---|
| 495 |
} |
|---|
| 496 |
|
|---|
| 497 |
|
|---|
| 498 |
|
|---|
| 499 |
|
|---|
| 500 |
|
|---|
| 501 |
|
|---|
| 502 |
|
|---|
| 503 |
|
|---|
| 504 |
|
|---|
| 505 |
|
|---|
| 506 |
|
|---|
| 507 |
|
|---|
| 508 |
|
|---|
| 509 |
|
|---|
| 510 |
|
|---|
| 511 |
|
|---|
| 512 |
|
|---|
| 513 |
|
|---|
| 514 |
|
|---|
| 515 |
|
|---|
| 516 |
sub echo_response_ |
|---|
| 517 |
{ |
|---|
| 518 |
my ( $self, $mail, $client, $command, $suppress ) = @_; |
|---|
| 519 |
|
|---|
| 520 |
|
|---|
| 521 |
|
|---|
| 522 |
|
|---|
| 523 |
my ( $response, $ok ) = $self->get_response_( $mail, $client, $command, 0, $suppress ); |
|---|
| 524 |
|
|---|
| 525 |
if ( $ok == 1 ) { |
|---|
| 526 |
if ( $response =~ /$self->{good_response_}/ ) { |
|---|
| 527 |
return 0; |
|---|
| 528 |
} else { |
|---|
| 529 |
return 1; |
|---|
| 530 |
} |
|---|
| 531 |
} else { |
|---|
| 532 |
return 2; |
|---|
| 533 |
} |
|---|
| 534 |
} |
|---|
| 535 |
|
|---|
| 536 |
|
|---|
| 537 |
|
|---|
| 538 |
|
|---|
| 539 |
|
|---|
| 540 |
|
|---|
| 541 |
|
|---|
| 542 |
|
|---|
| 543 |
|
|---|
| 544 |
|
|---|
| 545 |
|
|---|
| 546 |
|
|---|
| 547 |
|
|---|
| 548 |
|
|---|
| 549 |
|
|---|
| 550 |
|
|---|
| 551 |
|
|---|
| 552 |
sub verify_connected_ |
|---|
| 553 |
{ |
|---|
| 554 |
my ( $self, $mail, $client, $hostname, $port, $ssl, $unix_socket ) = @_; |
|---|
| 555 |
|
|---|
| 556 |
$ssl = 0 if ( !defined( $ssl ) ); |
|---|
| 557 |
|
|---|
| 558 |
|
|---|
| 559 |
|
|---|
| 560 |
return $mail if ( $mail && $mail->connected ); |
|---|
| 561 |
|
|---|
| 562 |
|
|---|
| 563 |
|
|---|
| 564 |
|
|---|
| 565 |
if ( $self->config_( 'socks_server' ) ne '' ) { |
|---|
| 566 |
require IO::Socket::Socks; |
|---|
| 567 |
$self->log_( 0, "Attempting to connect to socks server at " |
|---|
| 568 |
. $self->config_( 'socks_server' ) . ":" |
|---|
| 569 |
. $self->config_( 'socks_port' ) ); |
|---|
| 570 |
|
|---|
| 571 |
$mail = IO::Socket::Socks->new( |
|---|
| 572 |
ProxyAddr => $self->config_( 'socks_server' ), |
|---|
| 573 |
ProxyPort => $self->config_( 'socks_port' ), |
|---|
| 574 |
ConnectAddr => $hostname, |
|---|
| 575 |
ConnectPort => $port ); |
|---|
| 576 |
} else { |
|---|
| 577 |
if ( $ssl ) { |
|---|
| 578 |
eval { |
|---|
| 579 |
require IO::Socket::SSL; |
|---|
| 580 |
}; |
|---|
| 581 |
if ( $@ ) { |
|---|
| 582 |
|
|---|
| 583 |
|
|---|
| 584 |
$self->tee_( $client, "$self->{ssl_not_supported_error_}$eol" ); |
|---|
| 585 |
return undef; |
|---|
| 586 |
} |
|---|
| 587 |
|
|---|
| 588 |
$self->log_( 0, "Attempting to connect to SSL server at " |
|---|
| 589 |
. "$hostname:$port" ); |
|---|
| 590 |
|
|---|
| 591 |
if ( $^O eq 'MSWin32' ) { |
|---|
| 592 |
|
|---|
| 593 |
|
|---|
| 594 |
|
|---|
| 595 |
|
|---|
| 596 |
|
|---|
| 597 |
|
|---|
| 598 |
|
|---|
| 599 |
|
|---|
| 600 |
my $timeout = $self->global_config_( 'timeout' ); |
|---|
| 601 |
my $tt = time + $timeout; |
|---|
| 602 |
|
|---|
| 603 |
$mail = IO::Socket::INET->new( |
|---|
| 604 |
Proto => "tcp", |
|---|
| 605 |
PeerAddr => $hostname, |
|---|
| 606 |
PeerPort => $port, |
|---|
| 607 |
Timeout => $timeout, |
|---|
| 608 |
); |
|---|
| 609 |
|
|---|
| 610 |
if ( $mail ) { |
|---|
| 611 |
|
|---|
| 612 |
|
|---|
| 613 |
my $non_blocking = 1; |
|---|
| 614 |
ioctl( $mail, 0x8004667e, pack( 'L!', $non_blocking ) ); |
|---|
| 615 |
|
|---|
| 616 |
$self->log_( 2, "Trying to upgrade socket $mail to SSL" ); |
|---|
| 617 |
|
|---|
| 618 |
while ( $tt > time ) { |
|---|
| 619 |
|
|---|
| 620 |
|
|---|
| 621 |
IO::Socket::SSL->start_SSL( |
|---|
| 622 |
$mail, |
|---|
| 623 |
Timeout => $timeout, |
|---|
| 624 |
); |
|---|
| 625 |
|
|---|
| 626 |
my $err = IO::Socket::SSL->errstr; |
|---|
| 627 |
last if ( $err eq '' ); |
|---|
| 628 |
|
|---|
| 629 |
$self->log_( 1, "Got an error $err from start_SSL" ); |
|---|
| 630 |
|
|---|
| 631 |
last if ( !defined $mail ); |
|---|
| 632 |
|
|---|
| 633 |
my $vec = ''; |
|---|
| 634 |
vec( $vec, $mail->fileno, 1 ) = 1; |
|---|
| 635 |
my $rv = |
|---|
| 636 |
( $err eq IO::Socket::SSL->SSL_WANT_READ ) ? select( $vec, undef, undef, $timeout ) : |
|---|
| 637 |
( $err eq IO::Socket::SSL->SSL_WANT_WRITE ) ? select( undef, $vec, undef, $timeout ) : |
|---|
| 638 |
undef; |
|---|
| 639 |
|
|---|
| 640 |
last if ( !$rv ); |
|---|
| 641 |
} |
|---|
| 642 |
|
|---|
| 643 |
if ( !defined $mail || ( ref $mail ne 'IO::Socket::SSL' ) ) { |
|---|
| 644 |
$self->log_( 0, "Failed to upgrade the socket to SSL" ); |
|---|
| 645 |
$mail->close if defined $mail; |
|---|
| 646 |
undef $mail; |
|---|
| 647 |
} else { |
|---|
| 648 |
$self->log_( 2, "The socket $mail has successfully been upgraded" ); |
|---|
| 649 |
|
|---|
| 650 |
|
|---|
| 651 |
|
|---|
| 652 |
$non_blocking = 0; |
|---|
| 653 |
ioctl( $mail, 0x8004667e, pack( 'L!', $non_blocking ) ); |
|---|
| 654 |
} |
|---|
| 655 |
} |
|---|
| 656 |
} |
|---|
| 657 |
else { |
|---|
| 658 |
$mail = IO::Socket::SSL->new( |
|---|
| 659 |
Proto => "tcp", |
|---|
| 660 |
PeerAddr => $hostname, |
|---|
| 661 |
PeerPort => $port, |
|---|
| 662 |
Timeout => $self->global_config_( 'timeout' ), |
|---|
| 663 |
Domain => AF_INET, |
|---|
| 664 |
); |
|---|
| 665 |
} |
|---|
| 666 |
|
|---|
| 667 |
} else { |
|---|
| 668 |
if ( $unix_socket ) { |
|---|
| 669 |
|
|---|
| 670 |
$self->log_( 0, "Attempting to connect to unix socket at $unix_socket" ); |
|---|
| 671 |
|
|---|
| 672 |
$mail = IO::Socket::UNIX->new( |
|---|
| 673 |
Type => SOCK_STREAM, |
|---|
| 674 |
Peer => $unix_socket, |
|---|
| 675 |
Timeout => $self->global_config_( 'timeout' ), |
|---|
| 676 |
); |
|---|
| 677 |
|
|---|
| 678 |
} else { |
|---|
| 679 |
|
|---|
| 680 |
$self->log_( 0, "Attempting to connect to POP server at " |
|---|
| 681 |
. "$hostname:$port" ); |
|---|
| 682 |
|
|---|
| 683 |
$mail = IO::Socket::INET->new( |
|---|
| 684 |
Proto => "tcp", |
|---|
| 685 |
PeerAddr => $hostname, |
|---|
| 686 |
PeerPort => $port, |
|---|
| 687 |
Timeout => $self->global_config_( 'timeout' ), |
|---|
| 688 |
); |
|---|
| 689 |
|
|---|
| 690 |
} |
|---|
| 691 |
} |
|---|
| 692 |
} |
|---|
| 693 |
|
|---|
| 694 |
|
|---|
| 695 |
if ( $mail ) { |
|---|
| 696 |
if ( $mail->connected ) { |
|---|
| 697 |
|
|---|
| 698 |
if ( $unix_socket ) { |
|---|
| 699 |
$self->log_( 0, "Connected to $unix_socket timeout " . $self->global_config_( 'timeout' ) ); |
|---|
| 700 |
} else { |
|---|
| 701 |
$self->log_( 0, "Connected to $hostname:$port timeout " . $self->global_config_( 'timeout' ) ); |
|---|
| 702 |
} |
|---|
| 703 |
|
|---|
| 704 |
|
|---|
| 705 |
|
|---|
| 706 |
|
|---|
| 707 |
if ( !$ssl ) { |
|---|
| 708 |
binmode( $mail ); |
|---|
| 709 |
} |
|---|
| 710 |
|
|---|
| 711 |
if ( !$ssl || ( $mail->pending() == 0 ) ) { |
|---|
| 712 |
|
|---|
| 713 |
|
|---|
| 714 |
|
|---|
| 715 |
my $selector = new IO::Select( $mail ); |
|---|
| 716 |
last unless $selector->can_read($self->global_config_( 'timeout' )); |
|---|
| 717 |
} |
|---|
| 718 |
|
|---|
| 719 |
|
|---|
| 720 |
|
|---|
| 721 |
my $buf = ''; |
|---|
| 722 |
my $max_length = 8192; |
|---|
| 723 |
my $n = sysread( $mail, $buf, $max_length, length $buf ); |
|---|
| 724 |
|
|---|
| 725 |
if ( !( $buf =~ /[\r\n]/ ) ) { |
|---|
| 726 |
my $hit_newline = 0; |
|---|
| 727 |
my $temp_buf; |
|---|
| 728 |
|
|---|
| 729 |
|
|---|
| 730 |
|
|---|
| 731 |
my $wait = ( ($^O eq 'MSWin32') && !($mail =~ /socket/i) ) ? 1 : 0; |
|---|
| 732 |
|
|---|
| 733 |
|
|---|
| 734 |
|
|---|
| 735 |
for my $i ( 0..($self->global_config_( 'timeout' ) * 100) ) { |
|---|
| 736 |
if ( !$hit_newline ) { |
|---|
| 737 |
$temp_buf = $self->flush_extra_( $mail, $client, 1 ); |
|---|
| 738 |
$hit_newline = ( $temp_buf =~ /[\r\n]/ ); |
|---|
| 739 |
$buf .= $temp_buf; |
|---|
| 740 |
if ( $wait && ! length $temp_buf ) { |
|---|
| 741 |
select undef, undef, undef, 0.01; |
|---|
| 742 |
} |
|---|
| 743 |
} |
|---|
| 744 |
else { |
|---|
| 745 |
last; |
|---|
| 746 |
} |
|---|
| 747 |
} |
|---|
| 748 |
} |
|---|
| 749 |
|
|---|
| 750 |
$self->log_( 1, "Connection returned: $buf" ); |
|---|
| 751 |
|
|---|
| 752 |
|
|---|
| 753 |
|
|---|
| 754 |
if ( $buf eq '' ) { |
|---|
| 755 |
close $mail; |
|---|
| 756 |
last; |
|---|
| 757 |
} |
|---|
| 758 |
|
|---|
| 759 |
$self->{connect_banner__} = $buf; |
|---|
| 760 |
|
|---|
| 761 |
|
|---|
| 762 |
|
|---|
| 763 |
for my $i ( 0..4 ) { |
|---|
| 764 |
$self->flush_extra_( $mail, $client, 1 ); |
|---|
| 765 |
} |
|---|
| 766 |
|
|---|
| 767 |
return $mail; |
|---|
| 768 |
} |
|---|
| 769 |
} |
|---|
| 770 |
|
|---|
| 771 |
$self->log_( 0, "IO::Socket::* gets an error: $!" ); |
|---|
| 772 |
|
|---|
| 773 |
|
|---|
| 774 |
if ( $unix_socket ) { |
|---|
| 775 |
$self->tee_( $client, "$self->{connection_failed_error_} $unix_socket$eol" ); |
|---|
| 776 |
} else { |
|---|
| 777 |
$self->tee_( $client, "$self->{connection_failed_error_} $hostname:$port$eol" ); |
|---|
| 778 |
} |
|---|
| 779 |
|
|---|
| 780 |
return undef; |
|---|
| 781 |
} |
|---|
| 782 |
|
|---|
| 783 |
|
|---|
| 784 |
|
|---|
| 785 |
|
|---|
| 786 |
|
|---|
| 787 |
|
|---|
| 788 |
|
|---|
| 789 |
|
|---|
| 790 |
|
|---|
| 791 |
|
|---|
| 792 |
|
|---|
| 793 |
sub configure_item |
|---|
| 794 |
{ |
|---|
| 795 |
my ( $self, $name, $templ ) = @_; |
|---|
| 796 |
|
|---|
| 797 |
$templ->param( 'Socks_Widget_Name' => $self->name() ); |
|---|
| 798 |
$templ->param( 'Socks_Server' => $self->config_( 'socks_server' ) ); |
|---|
| 799 |
$templ->param( 'Socks_Port' => $self->config_( 'socks_port' ) ); |
|---|
| 800 |
} |
|---|
| 801 |
|
|---|
| 802 |
|
|---|
| 803 |
|
|---|
| 804 |
|
|---|
| 805 |
|
|---|
| 806 |
|
|---|
| 807 |
|
|---|
| 808 |
|
|---|
| 809 |
|
|---|
| 810 |
|
|---|
| 811 |
|
|---|
| 812 |
|
|---|
| 813 |
|
|---|
| 814 |
sub validate_item |
|---|
| 815 |
{ |
|---|
| 816 |
my ( $self, $name, $templ, $language, $form ) = @_; |
|---|
| 817 |
|
|---|
| 818 |
my $me = $self->name(); |
|---|
| 819 |
|
|---|
| 820 |
if ( defined($$form{"$me" . "_socks_port"}) ) { |
|---|
| 821 |
if ( ( $$form{"$me" . "_socks_port"} >= 1 ) && ( $$form{"$me" . "_socks_port"} < 65536 ) ) { |
|---|
| 822 |
$self->config_( 'socks_port', $$form{"$me" . "_socks_port"} ); |
|---|
| 823 |
$templ->param( 'Socks_Widget_If_Port_Updated' => 1 ); |
|---|
| 824 |
$templ->param( 'Socks_Widget_Port_Updated' => sprintf( $$language{Configuration_SOCKSPortUpdate}, $self->config_( 'socks_port' ) ) ); |
|---|
| 825 |
} else { |
|---|
| 826 |
$templ->param( 'Socks_Widget_If_Port_Error' => 1 ); |
|---|
| 827 |
} |
|---|
| 828 |
} |
|---|
| 829 |
|
|---|
| 830 |
if ( defined($$form{"$me" . "_socks_server"}) ) { |
|---|
| 831 |
$self->config_( 'socks_server', $$form{"$me" . "_socks_server"} ); |
|---|
| 832 |
$templ->param( 'Socks_Widget_If_Server_Updated' => 1 ); |
|---|
| 833 |
$templ->param( 'Socks_Widget_Server_Updated' => sprintf( $$language{Configuration_SOCKSServerUpdate}, $self->config_( 'socks_server' ) ) ); |
|---|
| 834 |
} |
|---|
| 835 |
} |
|---|
| 836 |
|
|---|
| 837 |
|
|---|
| 838 |
|
|---|
| 839 |
sub classifier |
|---|
| 840 |
{ |
|---|
| 841 |
my ( $self, $classifier ) = @_; |
|---|
| 842 |
|
|---|
| 843 |
$self->{classifier__} = $classifier; |
|---|
| 844 |
} |
|---|
| 845 |
|
|---|
| 846 |
sub history |
|---|
| 847 |
{ |
|---|
| 848 |
my ( $self, $history ) = @_; |
|---|
| 849 |
|
|---|
| 850 |
$self->{history__} = $history; |
|---|
| 851 |
} |
|---|
| 852 |
|
|---|
| 853 |
1; |
|---|