2010-06-04 05:12:19 -04:00
|
|
|
#!/usr/bin/env perl
|
2014-11-03 05:27:33 -05:00
|
|
|
# Simulate a tunneling proxy to a HTTPS URL that needs authentication.
|
|
|
|
# Use two connections (Connection: close)
|
2008-11-12 16:54:49 -05:00
|
|
|
|
2008-04-27 00:28:35 -04:00
|
|
|
use strict;
|
2008-11-12 16:54:49 -05:00
|
|
|
use warnings;
|
2008-04-27 00:28:35 -04:00
|
|
|
|
2009-09-05 16:54:05 -04:00
|
|
|
use WgetFeature qw(https);
|
2014-09-24 10:40:03 -04:00
|
|
|
use WgetTests; # For $WGETPATH.
|
2008-06-22 20:52:57 -04:00
|
|
|
|
2009-06-12 03:06:10 -04:00
|
|
|
my $cert_path;
|
|
|
|
my $key_path;
|
2014-09-24 10:40:03 -04:00
|
|
|
my $srcdir;
|
2009-06-12 03:06:10 -04:00
|
|
|
|
|
|
|
if (@ARGV) {
|
2014-09-24 10:40:03 -04:00
|
|
|
$srcdir = shift @ARGV;
|
|
|
|
} elsif (defined $ENV{srcdir}) {
|
|
|
|
$srcdir = $ENV{srcdir};
|
2009-06-12 03:06:10 -04:00
|
|
|
}
|
2014-10-01 10:42:04 -04:00
|
|
|
|
2014-09-24 10:40:03 -04:00
|
|
|
if (defined $srcdir) {
|
|
|
|
$key_path = "$srcdir/certs/server-key.pem";
|
|
|
|
$cert_path = "$srcdir/certs/server-cert.pem";
|
|
|
|
} else {
|
|
|
|
$key_path = "certs/server-key.pem";
|
|
|
|
$cert_path = "certs/server-cert.pem";
|
|
|
|
}
|
|
|
|
|
2009-06-12 03:06:10 -04:00
|
|
|
|
2008-04-27 00:28:35 -04:00
|
|
|
use HTTP::Daemon;
|
|
|
|
use HTTP::Request;
|
2008-06-22 15:58:03 -04:00
|
|
|
use IO::Socket::SSL;
|
|
|
|
|
|
|
|
my $SOCKET = HTTP::Daemon->new (LocalAddr => 'localhost',
|
|
|
|
ReuseAddr => 1) or die "Cannot create server!!!";
|
2008-04-27 00:28:35 -04:00
|
|
|
|
|
|
|
sub get_request {
|
|
|
|
my $conn = shift;
|
|
|
|
my $content = '';
|
|
|
|
my $line;
|
|
|
|
|
|
|
|
while (defined ($line = <$conn>)) {
|
|
|
|
$content .= $line;
|
|
|
|
last if $line eq "\r\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
my $rqst = HTTP::Request->parse($content)
|
|
|
|
or die "Couldn't parse request:\n$content\n";
|
|
|
|
|
|
|
|
return $rqst;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub do_server {
|
2014-10-28 06:40:34 -04:00
|
|
|
my ($synch_callback) = @_;
|
2008-06-22 15:58:03 -04:00
|
|
|
my $s = $SOCKET;
|
2008-04-27 00:28:35 -04:00
|
|
|
my $conn;
|
|
|
|
my $rqst;
|
|
|
|
my $rspn;
|
2014-10-28 06:40:34 -04:00
|
|
|
|
2014-11-03 05:27:33 -05:00
|
|
|
my %options = (
|
|
|
|
SSL_server => 1,
|
|
|
|
SSL_passwd_cb => sub { return "Hello"; });
|
|
|
|
$options{SSL_cert_file} = $cert_path if ($cert_path);
|
|
|
|
$options{SSL_key_file} = $key_path if ($key_path);
|
|
|
|
my @options = %options;
|
|
|
|
|
|
|
|
# sync with the parent
|
2014-10-28 06:40:34 -04:00
|
|
|
$synch_callback->();
|
|
|
|
|
2014-11-03 05:27:33 -05:00
|
|
|
# Simulate a HTTPS proxy server with tunneling.
|
|
|
|
|
2008-04-27 00:28:35 -04:00
|
|
|
for my $expect_inner_auth (0, 1) {
|
|
|
|
$conn = $s->accept;
|
|
|
|
$rqst = $conn->get_request;
|
|
|
|
die "Method not CONNECT\n" if ($rqst->method ne 'CONNECT');
|
|
|
|
$rspn = HTTP::Response->new(200, 'OK');
|
|
|
|
$conn->send_response($rspn);
|
|
|
|
|
2014-11-03 05:27:33 -05:00
|
|
|
# Now switch from plain to SSL (for simulating a transparent tunnel
|
|
|
|
# to an HTTPS server).
|
2009-06-12 03:06:10 -04:00
|
|
|
|
|
|
|
$conn = IO::Socket::SSL->new_from_fd($conn->fileno, @options)
|
2008-04-27 00:28:35 -04:00
|
|
|
or die "Couldn't initiate SSL";
|
|
|
|
|
|
|
|
$rqst = &get_request($conn)
|
|
|
|
or die "Didn't get proxied request\n";
|
|
|
|
|
|
|
|
unless ($expect_inner_auth) {
|
|
|
|
die "Early proxied auth\n" if $rqst->header('Authorization');
|
|
|
|
|
|
|
|
$rspn = HTTP::Response->new(401, 'Unauthorized', [
|
|
|
|
'WWW-Authenticate' => 'Basic realm="gondor"',
|
|
|
|
Connection => 'close'
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
die "No proxied auth\n" unless $rqst->header('Authorization');
|
|
|
|
|
|
|
|
$rspn = HTTP::Response->new(200, 'OK', [
|
|
|
|
'Content-Type' => 'text/plain',
|
|
|
|
'Connection' => 'close',
|
|
|
|
], "foobarbaz\n");
|
|
|
|
}
|
2014-11-03 05:27:33 -05:00
|
|
|
|
|
|
|
$rspn->protocol('HTTP/1.0');
|
|
|
|
print STDERR "=====\n";
|
|
|
|
print STDERR $rspn->as_string;
|
|
|
|
print STDERR "\n=====\n";
|
|
|
|
print $conn $rspn->as_string;
|
|
|
|
|
2008-04-27 00:28:35 -04:00
|
|
|
$conn->close;
|
|
|
|
}
|
2014-11-03 05:27:33 -05:00
|
|
|
|
2008-04-27 00:28:35 -04:00
|
|
|
undef $conn;
|
|
|
|
undef $s;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub fork_server {
|
2014-10-28 06:40:34 -04:00
|
|
|
pipe(FROM_CHILD, TO_PARENT) or die "Cannot create pipe!";
|
|
|
|
select((select(TO_PARENT), $| = 1)[0]);
|
|
|
|
|
|
|
|
my $pid = fork();
|
|
|
|
if ($pid < 0) {
|
|
|
|
die "Cannot fork";
|
|
|
|
} elsif ($pid == 0) {
|
|
|
|
# child
|
|
|
|
close FROM_CHILD;
|
|
|
|
do_server(sub { print TO_PARENT "SYNC\n"; close TO_PARENT });
|
2014-11-03 05:27:33 -05:00
|
|
|
exit 0;
|
2014-10-28 06:40:34 -04:00
|
|
|
} else {
|
|
|
|
# parent
|
|
|
|
close TO_PARENT;
|
|
|
|
chomp(my $line = <FROM_CHILD>);
|
|
|
|
close FROM_CHILD;
|
|
|
|
}
|
2008-04-27 00:28:35 -04:00
|
|
|
|
2014-10-28 06:40:34 -04:00
|
|
|
return $pid;
|
2008-04-27 00:28:35 -04:00
|
|
|
}
|
|
|
|
|
2014-11-03 05:27:33 -05:00
|
|
|
unlink "needs-auth.txt";
|
2008-06-12 04:31:54 -04:00
|
|
|
my $pid = &fork_server;
|
2008-04-27 00:28:35 -04:00
|
|
|
|
|
|
|
my $cmdline = $WgetTest::WGETPATH . " --user=fiddle-dee-dee"
|
2008-06-12 05:18:35 -04:00
|
|
|
. " --password=Dodgson -e https_proxy=localhost:{{port}}"
|
2008-04-27 00:28:35 -04:00
|
|
|
. " --no-check-certificate"
|
|
|
|
. " https://no.such.domain/needs-auth.txt";
|
2008-06-22 15:58:03 -04:00
|
|
|
$cmdline =~ s/{{port}}/$SOCKET->sockport()/e;
|
2008-04-27 00:28:35 -04:00
|
|
|
|
2014-11-03 05:27:33 -05:00
|
|
|
my $valgrind = $ENV{VALGRIND_TESTS};
|
|
|
|
if (!defined $valgrind || $valgrind eq "" || $valgrind == 0) {
|
|
|
|
# Valgrind not requested - leave $cmdline as it is
|
|
|
|
} elsif ($valgrind == 1) {
|
|
|
|
$cmdline = "valgrind --error-exitcode=301 --leak-check=yes --track-origins=yes " . $cmdline;
|
|
|
|
} else {
|
|
|
|
$cmdline = $valgrind . " " . $cmdline;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $code = system($cmdline . " 2>&1") >> 8;
|
|
|
|
unlink "needs-auth.txt";
|
2008-04-27 00:28:35 -04:00
|
|
|
|
|
|
|
warn "Got code: $code\n" if $code;
|
2008-06-12 04:31:54 -04:00
|
|
|
kill ('TERM', $pid);
|
2014-11-03 05:27:33 -05:00
|
|
|
exit ($code != 0);
|