|
| 1 | +#!/usr/bin/perl |
| 2 | + |
| 3 | +# (C) Sergey Kandaurov |
| 4 | +# (C) Nginx, Inc. |
| 5 | + |
| 6 | +# Tests for stream realip module, server side proxy protocol. |
| 7 | + |
| 8 | +############################################################################### |
| 9 | + |
| 10 | +use warnings; |
| 11 | +use strict; |
| 12 | + |
| 13 | +use Test::More; |
| 14 | + |
| 15 | +use IO::Select; |
| 16 | +use Socket qw/ $CRLF /; |
| 17 | + |
| 18 | +BEGIN { use FindBin; chdir($FindBin::Bin); } |
| 19 | + |
| 20 | +use lib 'lib'; |
| 21 | +use Test::Nginx; |
| 22 | +use Test::Nginx::Stream qw/ stream /; |
| 23 | + |
| 24 | +############################################################################### |
| 25 | + |
| 26 | +select STDERR; $| = 1; |
| 27 | +select STDOUT; $| = 1; |
| 28 | + |
| 29 | +my $t = Test::Nginx->new()->has(qw/stream stream_return stream_realip ipv6/) |
| 30 | + ->write_file_expand('nginx.conf', <<'EOF'); |
| 31 | +
|
| 32 | +%%TEST_GLOBALS%% |
| 33 | +
|
| 34 | +daemon off; |
| 35 | +
|
| 36 | +events { |
| 37 | +} |
| 38 | +
|
| 39 | +stream { |
| 40 | + server { |
| 41 | + listen 127.0.0.1:8083 proxy_protocol; |
| 42 | + listen 127.0.0.1:8084; |
| 43 | + return $proxy_protocol_addr:$proxy_protocol_port; |
| 44 | + } |
| 45 | +
|
| 46 | + server { |
| 47 | + listen 127.0.0.1:8085 proxy_protocol; |
| 48 | + proxy_pass 127.0.0.1:8081; |
| 49 | + } |
| 50 | +
|
| 51 | + server { |
| 52 | + listen 127.0.0.1:8086 proxy_protocol; |
| 53 | + listen [::1]:%%PORT_8086%% proxy_protocol; |
| 54 | + return "$remote_addr:$remote_port: |
| 55 | + $realip_remote_addr:$realip_remote_port"; |
| 56 | +
|
| 57 | + set_real_ip_from ::1; |
| 58 | + set_real_ip_from 127.0.0.2; |
| 59 | + } |
| 60 | +
|
| 61 | + server { |
| 62 | + listen 127.0.0.1:8087; |
| 63 | + proxy_pass [::1]:%%PORT_8086%%; |
| 64 | + } |
| 65 | +
|
| 66 | + server { |
| 67 | + listen 127.0.0.1:8088 proxy_protocol; |
| 68 | + listen [::1]:%%PORT_8088%% proxy_protocol; |
| 69 | + return "$remote_addr:$remote_port: |
| 70 | + $realip_remote_addr:$realip_remote_port"; |
| 71 | +
|
| 72 | + set_real_ip_from 127.0.0.1; |
| 73 | + set_real_ip_from ::2; |
| 74 | + } |
| 75 | +
|
| 76 | + server { |
| 77 | + listen 127.0.0.1:8089; |
| 78 | + proxy_pass [::1]:%%PORT_8088%%; |
| 79 | + } |
| 80 | +} |
| 81 | +
|
| 82 | +EOF |
| 83 | + |
| 84 | +$t->run_daemon(\&stream_daemon); |
| 85 | +$t->try_run('no stream proxy_protocol and/or inet6 support')->plan(8); |
| 86 | +$t->waitforsocket('127.0.0.1:' . port(8081)); |
| 87 | + |
| 88 | +############################################################################### |
| 89 | + |
| 90 | +is(stream('127.0.0.1:' . port(8083)) |
| 91 | + ->io("PROXY TCP4 192.0.2.1 192.0.2.2 1234 5678${CRLF}"), |
| 92 | + '192.0.2.1:1234', 'server'); |
| 93 | + |
| 94 | +is(stream('127.0.0.1:' . port(8084)) |
| 95 | + ->io("PROXY TCP4 192.0.2.1 192.0.2.2 1234 5678${CRLF}"), |
| 96 | + ':', 'server off'); |
| 97 | + |
| 98 | +is(stream('127.0.0.1:' . port(8085)) |
| 99 | + ->io("PROXY TCP4 192.0.2.1 192.0.2.2 1234 5678${CRLF}close"), |
| 100 | + 'close', 'server payload'); |
| 101 | + |
| 102 | +like(stream('127.0.0.1:' . port(8086)) |
| 103 | + ->io("PROXY TCP4 192.0.2.1 192.0.2.2 1234 5678${CRLF}"), |
| 104 | + qr/^(\Q127.0.0.1:\E\d+):\s+\1$/, 'server ipv6 realip - no match'); |
| 105 | + |
| 106 | +like(stream('127.0.0.1:' . port(8087)) |
| 107 | + ->io("PROXY TCP4 192.0.2.1 192.0.2.2 1234 5678${CRLF}"), |
| 108 | + qr/\Q192.0.2.1:1234:\E\s+\Q::1:\E\d+/, 'server ipv6 realip'); |
| 109 | + |
| 110 | +like(stream('127.0.0.1:' . port(8088)) |
| 111 | + ->io("PROXY TCP4 192.0.2.1 192.0.2.2 1234 5678${CRLF}"), |
| 112 | + qr/\Q192.0.2.1:1234:\E\s+\Q127.0.0.1:\E\d+/, 'server ipv4 realip'); |
| 113 | + |
| 114 | +like(stream('127.0.0.1:' . port(8089)) |
| 115 | + ->io("PROXY TCP4 192.0.2.1 192.0.2.2 1234 5678${CRLF}"), |
| 116 | + qr/^(::1:\d+):\s+\1$/, 'server ipv4 realip - no match'); |
| 117 | + |
| 118 | +like(stream('127.0.0.1:' . port(8088)) |
| 119 | + ->io("PROXY UNKNOWN TCP4 192.0.2.1 192.0.2.2 1234 5678${CRLF}"), |
| 120 | + qr/^(\Q127.0.0.1:\E\d+):\s+\1$/, 'server unknown'); |
| 121 | + |
| 122 | +############################################################################### |
| 123 | + |
| 124 | +sub stream_daemon { |
| 125 | + my $server = IO::Socket::INET->new( |
| 126 | + Proto => 'tcp', |
| 127 | + LocalAddr => '127.0.0.1:' . port(8081), |
| 128 | + Listen => 5, |
| 129 | + Reuse => 1 |
| 130 | + ) |
| 131 | + or die "Can't create listening socket: $!\n"; |
| 132 | + |
| 133 | + my $sel = IO::Select->new($server); |
| 134 | + |
| 135 | + local $SIG{PIPE} = 'IGNORE'; |
| 136 | + |
| 137 | + while (my @ready = $sel->can_read) { |
| 138 | + foreach my $fh (@ready) { |
| 139 | + if ($server == $fh) { |
| 140 | + my $new = $fh->accept; |
| 141 | + $new->autoflush(1); |
| 142 | + $sel->add($new); |
| 143 | + |
| 144 | + } elsif (stream_handle_client($fh)) { |
| 145 | + $sel->remove($fh); |
| 146 | + $fh->close; |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +sub stream_handle_client { |
| 153 | + my ($client) = @_; |
| 154 | + |
| 155 | + log2c("(new connection $client)"); |
| 156 | + |
| 157 | + $client->sysread(my $buffer, 65536) or return 1; |
| 158 | + |
| 159 | + log2i("$client $buffer"); |
| 160 | + |
| 161 | + log2o("$client $buffer"); |
| 162 | + |
| 163 | + $client->syswrite($buffer); |
| 164 | + |
| 165 | + return $buffer =~ /close/; |
| 166 | +} |
| 167 | + |
| 168 | +sub log2i { Test::Nginx::log_core('|| <<', @_); } |
| 169 | +sub log2o { Test::Nginx::log_core('|| >>', @_); } |
| 170 | +sub log2c { Test::Nginx::log_core('||', @_); } |
| 171 | + |
| 172 | +############################################################################### |
0 commit comments