Sergey Kandaurov | b794464 | 2021-02-08 17:09:23 +0300 | [diff] [blame] | 1 | #!/usr/bin/perl |
| 2 | |
| 3 | # (C) Sergey Kandaurov |
| 4 | # (C) Nginx, Inc. |
| 5 | |
| 6 | # Tests for haproxy protocol with unix socket. |
| 7 | |
| 8 | ############################################################################### |
| 9 | |
| 10 | use warnings; |
| 11 | use strict; |
| 12 | |
| 13 | use Test::More; |
| 14 | |
| 15 | BEGIN { use FindBin; chdir($FindBin::Bin); } |
| 16 | |
| 17 | use lib 'lib'; |
| 18 | use Test::Nginx; |
| 19 | use Test::Nginx::Stream; |
| 20 | |
| 21 | ############################################################################### |
| 22 | |
| 23 | select STDERR; $| = 1; |
| 24 | select STDOUT; $| = 1; |
| 25 | |
| 26 | my $t = Test::Nginx->new() |
| 27 | ->has(qw/http realip stream stream_realip stream_return unix/) |
| 28 | ->plan(5); |
| 29 | |
| 30 | $t->write_file_expand('nginx.conf', <<'EOF'); |
| 31 | |
| 32 | %%TEST_GLOBALS%% |
| 33 | |
| 34 | daemon off; |
| 35 | |
| 36 | events { |
| 37 | } |
| 38 | |
| 39 | http { |
| 40 | %%TEST_GLOBALS_HTTP%% |
| 41 | |
| 42 | server { |
| 43 | listen unix:%%TESTDIR%%/unix.sock proxy_protocol; |
| 44 | server_name localhost; |
| 45 | |
| 46 | add_header X-IP $remote_addr; |
| 47 | add_header X-PP $proxy_protocol_addr; |
| 48 | real_ip_header proxy_protocol; |
| 49 | |
| 50 | location / { } |
| 51 | location /pp { |
| 52 | set_real_ip_from unix:; |
| 53 | error_page 404 =200 /t; |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | stream { |
| 59 | %%TEST_GLOBALS_STREAM%% |
| 60 | |
| 61 | server { |
| 62 | listen unix:%%TESTDIR%%/unix1.sock proxy_protocol; |
| 63 | return $remote_addr:$proxy_protocol_addr; |
| 64 | } |
| 65 | |
| 66 | server { |
| 67 | listen unix:%%TESTDIR%%/unix2.sock proxy_protocol; |
| 68 | return $remote_addr:$proxy_protocol_addr; |
| 69 | |
| 70 | set_real_ip_from unix:; |
| 71 | } |
| 72 | |
| 73 | server { |
| 74 | listen 127.0.0.1:8080; |
| 75 | proxy_pass unix:%%TESTDIR%%/unix.sock; |
| 76 | |
| 77 | proxy_protocol on; |
| 78 | } |
| 79 | |
| 80 | server { |
| 81 | listen 127.0.0.1:8081; |
| 82 | proxy_pass unix:%%TESTDIR%%/unix1.sock; |
| 83 | |
| 84 | proxy_protocol on; |
| 85 | } |
| 86 | |
| 87 | server { |
| 88 | listen 127.0.0.1:8082; |
| 89 | proxy_pass unix:%%TESTDIR%%/unix2.sock; |
| 90 | |
| 91 | proxy_protocol on; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | EOF |
| 96 | |
| 97 | $t->write_file('t', 'SEE-THIS'); |
| 98 | $t->run(); |
| 99 | |
| 100 | ############################################################################### |
| 101 | |
| 102 | my $r = http_get('/t'); |
| 103 | like($r, qr/X-IP: unix/, 'remote_addr'); |
| 104 | like($r, qr/X-PP: 127.0.0.1/, 'proxy_protocol_addr'); |
| 105 | |
| 106 | $r = http_get('/pp'); |
| 107 | like($r, qr/X-IP: 127.0.0.1/, 'remote_addr realip'); |
| 108 | |
| 109 | # listen proxy_protocol in stream |
| 110 | |
| 111 | is(get(8081), 'unix::127.0.0.1', 'stream proxy_protocol'); |
| 112 | is(get(8082), '127.0.0.1:127.0.0.1', 'stream proxy_protocol realip'); |
| 113 | |
| 114 | ############################################################################### |
| 115 | |
| 116 | sub get { |
| 117 | Test::Nginx::Stream->new(PeerPort => port(shift))->read(); |
| 118 | } |
| 119 | |
| 120 | ############################################################################### |