|
| 1 | +#!/usr/bin/perl |
| 2 | + |
| 3 | +# (C) Eugene Grebenschikov |
| 4 | +# (C) Nginx, Inc. |
| 5 | + |
| 6 | +# Tests for http tunnel module with auth_basic. |
| 7 | + |
| 8 | +############################################################################### |
| 9 | + |
| 10 | +use warnings; |
| 11 | +use strict; |
| 12 | + |
| 13 | +use Test::More; |
| 14 | + |
| 15 | +use MIME::Base64 qw/ encode_base64 /; |
| 16 | + |
| 17 | +BEGIN { use FindBin; chdir($FindBin::Bin); } |
| 18 | + |
| 19 | +use lib 'lib'; |
| 20 | +use Test::Nginx; |
| 21 | + |
| 22 | +############################################################################### |
| 23 | + |
| 24 | +select STDERR; $| = 1; |
| 25 | +select STDOUT; $| = 1; |
| 26 | + |
| 27 | +my $t = Test::Nginx->new()->has(qw/http tunnel auth_basic rewrite/) |
| 28 | + ->write_file_expand('nginx.conf', <<'EOF'); |
| 29 | +
|
| 30 | +%%TEST_GLOBALS%% |
| 31 | +
|
| 32 | +daemon off; |
| 33 | +
|
| 34 | +events { |
| 35 | +} |
| 36 | +
|
| 37 | +http { |
| 38 | + %%TEST_GLOBALS_HTTP%% |
| 39 | +
|
| 40 | + server { |
| 41 | + listen 127.0.0.1:8080; |
| 42 | + server_name localhost; |
| 43 | +
|
| 44 | + auth_basic "closed proxy"; |
| 45 | + auth_basic_user_file %%TESTDIR%%/htpasswd; |
| 46 | + tunnel_pass; |
| 47 | + } |
| 48 | +
|
| 49 | + server { |
| 50 | + listen 127.0.0.1:8081; |
| 51 | + server_name localhost; |
| 52 | +
|
| 53 | + location / { |
| 54 | + return 200 "SEE-THIS"; |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | +
|
| 59 | +EOF |
| 60 | + |
| 61 | +$t->write_file('htpasswd', 'plain:' . '{PLAIN}password' . "\n"); |
| 62 | + |
| 63 | +$t->try_run('no tunnel')->plan(2); |
| 64 | + |
| 65 | +############################################################################### |
| 66 | + |
| 67 | +like(proxy_get('/', '127.0.0.1:' . port(8081), port(8080)), qr/ 407/, |
| 68 | + 'CONNECT proxy auth required'); |
| 69 | +like(proxy_get('/', '127.0.0.1:' . port(8081), port(8080), user => 'plain', |
| 70 | + pass => 'password'), qr/SEE-THIS/, 'CONNECT proxy auth success'); |
| 71 | + |
| 72 | +############################################################################### |
| 73 | + |
| 74 | +sub proxy_get { |
| 75 | + my ($uri, $host, $proxy_port, %extra) = @_; |
| 76 | + my $reply = ''; |
| 77 | + |
| 78 | + my $s = IO::Socket::INET->new( |
| 79 | + Proto => 'tcp', |
| 80 | + PeerAddr => '127.0.0.1:' . $proxy_port, |
| 81 | + ) |
| 82 | + or die "Can't connect to proxy 127.0.0.1:$proxy_port $!\n"; |
| 83 | + |
| 84 | + http_connect($host, socket => $s, start => 1, %extra); |
| 85 | + |
| 86 | + while (<$s>) { |
| 87 | + $reply .= $_; |
| 88 | + last if /^\r?\n$/; |
| 89 | + } |
| 90 | + |
| 91 | + if ($reply =~ /200 OK/) { |
| 92 | + log_in($reply); |
| 93 | + return http_get($uri, socket => $s, %extra); |
| 94 | + |
| 95 | + } elsif ($reply =~ /Content-Length:\s*(\d+)/i) { |
| 96 | + read ($s, $_, $1); |
| 97 | + $reply .= $_; |
| 98 | + } |
| 99 | + |
| 100 | + $s->close; |
| 101 | + log_in($reply); |
| 102 | + return $reply; |
| 103 | +} |
| 104 | + |
| 105 | +sub http_connect { |
| 106 | + my ($host, %extra) = @_; |
| 107 | + my $auth_header = ''; |
| 108 | + |
| 109 | + $auth_header = 'Proxy-Authorization: Basic ' |
| 110 | + . encode_base64($extra{user} . ':' . $extra{pass}, '') . "\n" |
| 111 | + if defined $extra{user} && defined $extra{pass}; |
| 112 | + |
| 113 | + return http(<<EOF, %extra); |
| 114 | +CONNECT $host HTTP/1.1 |
| 115 | +Host: $host |
| 116 | +$auth_header |
| 117 | +EOF |
| 118 | +} |
| 119 | + |
| 120 | +############################################################################### |
0 commit comments