Mercurial > hg > nginx-tests
comparison proxy_protocol2.t @ 1309:3eb1de7e3b41
Tests: proxy protocol v2 tests.
author | Sergey Kandaurov <pluknet@nginx.com> |
---|---|
date | Tue, 27 Mar 2018 13:12:50 +0300 |
parents | |
children | 97c8280de681 |
comparison
equal
deleted
inserted
replaced
1308:58fdd9515f81 | 1309:3eb1de7e3b41 |
---|---|
1 #!/usr/bin/perl | |
2 | |
3 # (C) Sergey Kandaurov | |
4 # (C) Nginx, Inc. | |
5 | |
6 # Tests for haproxy protocol. | |
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 | |
20 ############################################################################### | |
21 | |
22 select STDERR; $| = 1; | |
23 select STDOUT; $| = 1; | |
24 | |
25 my $t = Test::Nginx->new()->has(qw/http access realip/); | |
26 | |
27 plan(skip_all => 'no proxy protocol v2') unless $t->has_version('1.13.11'); | |
28 | |
29 $t->write_file_expand('nginx.conf', <<'EOF')->plan(21); | |
30 | |
31 %%TEST_GLOBALS%% | |
32 | |
33 daemon off; | |
34 | |
35 events { | |
36 } | |
37 | |
38 http { | |
39 %%TEST_GLOBALS_HTTP%% | |
40 | |
41 log_format pp '$remote_addr $request'; | |
42 | |
43 server { | |
44 listen 127.0.0.1:8080 proxy_protocol; | |
45 server_name localhost; | |
46 | |
47 set_real_ip_from 127.0.0.1/32; | |
48 add_header X-IP $remote_addr; | |
49 add_header X-PP $proxy_protocol_addr; | |
50 | |
51 location /pp { | |
52 real_ip_header proxy_protocol; | |
53 error_page 404 =200 /t1; | |
54 access_log %%TESTDIR%%/pp.log pp; | |
55 | |
56 location /pp_4 { | |
57 deny 192.0.2.1/32; | |
58 } | |
59 location /pp_6 { | |
60 deny 2001:DB8::1/128; | |
61 } | |
62 } | |
63 } | |
64 } | |
65 | |
66 EOF | |
67 | |
68 $t->write_file('t1', 'SEE-THIS'); | |
69 $t->run(); | |
70 | |
71 ############################################################################### | |
72 | |
73 my $p = pack("N3C", 0x0D0A0D0A, 0x000D0A51, 0x5549540A, 0x21); | |
74 my $tcp4 = $p . pack("CnN2n2", 0x11, 12, 0xc0000201, 0xc0000202, 1234, 5678); | |
75 my $tcp6 = $p . pack("CnNx8NNx8Nn2", 0x21, 36, | |
76 0x20010db8, 0x00000001, 0x20010db8, 0x00000002, 1234, 5678); | |
77 my $tlv = $p . pack("CnN2n2x9", 0x11, 21, 0xc0000201, 0xc0000202, 1234, 5678); | |
78 my $unk1 = $p . pack("Cxx", 0x01); | |
79 my $unk2 = $p . pack("CnC4", 0x41, 4, 1, 2, 3, 4); | |
80 my $r; | |
81 | |
82 # no realip, just PROXY header parsing | |
83 | |
84 $r = pp_get('/t1', $tcp4); | |
85 like($r, qr/SEE-THIS/, 'tcp4 request'); | |
86 like($r, qr/X-PP: 192.0.2.1/, 'tcp4 proxy'); | |
87 unlike($r, qr/X-IP: 192.0.2.1/, 'tcp4 client'); | |
88 | |
89 $r = pp_get('/t1', $tcp6); | |
90 like($r, qr/SEE-THIS/, 'tcp6 request'); | |
91 like($r, qr/X-PP: 2001:DB8::1/i, 'tcp6 proxy'); | |
92 unlike($r, qr/X-IP: 2001:DB8::1/i, 'tcp6 client'); | |
93 | |
94 $r = pp_get('/t1', $tlv); | |
95 like($r, qr/SEE-THIS/, 'tlv request'); | |
96 like($r, qr/X-PP: 192.0.2.1/, 'tlv proxy'); | |
97 unlike($r, qr/X-IP: 192.0.2.1/, 'tlv client'); | |
98 | |
99 like(pp_get('/t1', $unk1), qr/SEE-THIS/, 'unknown request 1'); | |
100 like(pp_get('/t1', $unk2), qr/SEE-THIS/, 'unknown request 2'); | |
101 | |
102 # realip | |
103 | |
104 $r = pp_get('/pp', $tcp4); | |
105 like($r, qr/SEE-THIS/, 'tcp4 request realip'); | |
106 like($r, qr/X-PP: 192.0.2.1/, 'tcp4 proxy realip'); | |
107 like($r, qr/X-IP: 192.0.2.1/, 'tcp4 client realip'); | |
108 | |
109 $r = pp_get('/pp', $tcp6); | |
110 like($r, qr/SEE-THIS/, 'tcp6 request realip'); | |
111 like($r, qr/X-PP: 2001:DB8::1/i, 'tcp6 proxy realip'); | |
112 like($r, qr/X-IP: 2001:DB8::1/i, 'tcp6 client realip'); | |
113 | |
114 # access | |
115 | |
116 $r = pp_get('/pp_4', $tcp4); | |
117 like($r, qr/403 Forbidden/, 'tcp4 access'); | |
118 | |
119 $r = pp_get('/pp_6', $tcp6); | |
120 like($r, qr/403 Forbidden/, 'tcp6 access'); | |
121 | |
122 # client address in access.log | |
123 | |
124 $t->stop(); | |
125 | |
126 my $log = $t->read_file('pp.log'); | |
127 like($log, qr!^192\.0\.2\.1 GET /pp_4!m, 'tcp4 access log'); | |
128 like($log, qr!^2001:DB8::1 GET /pp_6!mi, 'tcp6 access log'); | |
129 | |
130 ############################################################################### | |
131 | |
132 sub pp_get { | |
133 my ($url, $proxy) = @_; | |
134 return http($proxy . <<EOF); | |
135 GET $url HTTP/1.0 | |
136 Host: localhost | |
137 | |
138 EOF | |
139 } | |
140 | |
141 ############################################################################### |