-
Notifications
You must be signed in to change notification settings - Fork 3
/
stream-proxy-protocol-v2-release-1.19.8.patch
626 lines (588 loc) · 19.3 KB
/
stream-proxy-protocol-v2-release-1.19.8.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
diff --git a/src/core/ngx_proxy_protocol.c b/src/core/ngx_proxy_protocol.c
index 7a9e7f9..eba8bc7 100644
--- a/src/core/ngx_proxy_protocol.c
+++ b/src/core/ngx_proxy_protocol.c
@@ -13,6 +13,40 @@
#define NGX_PROXY_PROTOCOL_AF_INET6 2
+#define NGX_PROXY_PROTOCOL_V2_SIG "\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A"
+#define NGX_PROXY_PROTOCOL_V2_SIG_LEN 12
+#define NGX_PROXY_PROTOCOL_V2_HDR_LEN 16
+#define NGX_PROXY_PROTOCOL_V2_HDR_LEN_INET \
+ (NGX_PROXY_PROTOCOL_V2_HDR_LEN + (4 + 4 + 2 + 2))
+#define NGX_PROXY_PROTOCOL_V2_HDR_LEN_INET6 \
+ (NGX_PROXY_PROTOCOL_V2_HDR_LEN + (16 + 16 + 2 + 2))
+
+#define NGX_PROXY_PROTOCOL_V2_CMD_PROXY (0x20 | 0x01)
+
+#define NGX_PROXY_PROTOCOL_V2_TRANS_STREAM 0x01
+
+#define NGX_PROXY_PROTOCOL_V2_FAM_UNSPEC 0x00
+#define NGX_PROXY_PROTOCOL_V2_FAM_INET 0x10
+#define NGX_PROXY_PROTOCOL_V2_FAM_INET6 0x20
+
+#define NGX_PROXY_PROTOCOL_V2_TYPE_ALPN 0x01
+#define NGX_PROXY_PROTOCOL_V2_TYPE_AUTHORITY 0x02 # Not implemented
+#define NGX_PROXY_PROTOCOL_V2_TYPE_CRC32C 0x03 # Not implemented
+#define NGX_PROXY_PROTOCOL_V2_TYPE_NOOP 0x04 # Not implemented
+#define NGX_PROXY_PROTOCOL_V2_TYPE_UNIQUE_ID 0x05 # Not implemented
+#define NGX_PROXY_PROTOCOL_V2_TYPE_SSL 0x20
+#define NGX_PROXY_PROTOCOL_V2_SUBTYPE_SSL_VERSION 0x21
+#define NGX_PROXY_PROTOCOL_V2_SUBTYPE_SSL_CN 0x22
+#define NGX_PROXY_PROTOCOL_V2_SUBTYPE_SSL_CIPHER 0x23
+#define NGX_PROXY_PROTOCOL_V2_SUBTYPE_SSL_SIG_ALG 0x24
+#define NGX_PROXY_PROTOCOL_V2_SUBTYPE_SSL_KEY_ALG 0x25
+#define NGX_PROXY_PROTOCOL_V2_TYPE_NETNS 0x30 # Not implemented
+
+#define NGX_PROXY_PROTOCOL_V2_CLIENT_SSL 0x01
+#define NGX_PROXY_PROTOCOL_V2_CLIENT_CERT_CONN 0x02
+#define NGX_PROXY_PROTOCOL_V2_CLIENT_CERT_SESS 0x04
+
+
#define ngx_proxy_protocol_parse_uint16(p) ((p)[0] << 8 | (p)[1])
@@ -40,12 +74,68 @@ typedef struct {
} ngx_proxy_protocol_inet6_addrs_t;
+typedef union {
+ struct {
+ uint32_t src_addr;
+ uint32_t dst_addr;
+ uint16_t src_port;
+ uint16_t dst_port;
+ } ip4;
+ struct {
+ uint8_t src_addr[16];
+ uint8_t dst_addr[16];
+ uint16_t src_port;
+ uint16_t dst_port;
+ } ip6;
+} ngx_proxy_protocol_addrs_t;
+
+
+typedef struct {
+ u_char signature[12];
+ uint8_t version_command;
+ uint8_t family_transport;
+ uint16_t len;
+ ngx_proxy_protocol_addrs_t addr;
+} ngx_proxy_protocol_v2_header_t;
+
+
+struct ngx_tlv_s {
+ uint8_t type;
+ uint8_t length_hi;
+ uint8_t length_lo;
+ uint8_t value[0];
+} __attribute__((packed));
+
+typedef struct ngx_tlv_s ngx_tlv_t;
+
+
+#if (NGX_STREAM_SSL)
+struct ngx_tlv_ssl_s {
+ ngx_tlv_t tlv;
+ uint8_t client;
+ uint32_t verify;
+ uint8_t sub_tlv[];
+} __attribute__((packed));
+
+typedef struct ngx_tlv_ssl_s ngx_tlv_ssl_t;
+#endif
+
+
static u_char *ngx_proxy_protocol_read_addr(ngx_connection_t *c, u_char *p,
u_char *last, ngx_str_t *addr);
static u_char *ngx_proxy_protocol_read_port(u_char *p, u_char *last,
in_port_t *port, u_char sep);
static u_char *ngx_proxy_protocol_v2_read(ngx_connection_t *c, u_char *buf,
u_char *last);
+static u_char *ngx_proxy_protocol_v2_write(ngx_connection_t *c, u_char *buf,
+ u_char *last);
+#if (NGX_HAVE_INET6)
+static void ngx_v4tov6(struct in6_addr *sin6_addr, struct sockaddr *addr);
+#endif
+#if (NGX_STREAM_SSL)
+static u_char *ngx_copy_tlv(u_char *pos, u_char *last, u_char type,
+ u_char *value, uint16_t value_len);
+#endif
u_char *
@@ -223,7 +313,8 @@ ngx_proxy_protocol_read_port(u_char *p, u_char *last, in_port_t *port,
u_char *
-ngx_proxy_protocol_write(ngx_connection_t *c, u_char *buf, u_char *last)
+ngx_proxy_protocol_write(ngx_connection_t *c, u_char *buf, u_char *last,
+ ngx_uint_t pp_version)
{
ngx_uint_t port, lport;
@@ -235,6 +326,10 @@ ngx_proxy_protocol_write(ngx_connection_t *c, u_char *buf, u_char *last)
return NULL;
}
+ if (pp_version == 2) {
+ return ngx_proxy_protocol_v2_write(c, buf, last);
+ }
+
switch (c->sockaddr->sa_family) {
case AF_INET:
@@ -420,3 +515,367 @@ ngx_proxy_protocol_v2_read(ngx_connection_t *c, u_char *buf, u_char *last)
return end;
}
+
+
+static u_char *
+ngx_proxy_protocol_v2_write(ngx_connection_t *c, u_char *buf, u_char *last)
+{
+ struct sockaddr *src, *dst;
+ ngx_proxy_protocol_v2_header_t *header;
+#if (NGX_HAVE_INET6)
+ struct in6_addr v6_tmp;
+ ngx_int_t v6_used;
+#endif
+#if (NGX_STREAM_SSL)
+ ngx_tlv_ssl_t *tlv;
+ u_char *value, *pos;
+ u_char kbuf[100];
+ const unsigned char *data;
+ unsigned int data_len;
+
+ X509 *crt;
+ EVP_PKEY *key;
+ const ASN1_OBJECT *algorithm;
+ const char *s;
+
+ long rc;
+ size_t tlv_len;
+#endif
+ size_t len;
+
+ header = (ngx_proxy_protocol_v2_header_t *) buf;
+
+ header->len = 0;
+
+ src = c->sockaddr;
+ dst = c->local_sockaddr;
+
+ len = 0;
+
+#if (NGX_HAVE_INET6)
+ v6_used = 0;
+#endif
+
+ ngx_memcpy(header->signature, NGX_PROXY_PROTOCOL_V2_SIG,
+ NGX_PROXY_PROTOCOL_V2_SIG_LEN);
+
+ header->version_command = NGX_PROXY_PROTOCOL_V2_CMD_PROXY;
+ header->family_transport = NGX_PROXY_PROTOCOL_V2_TRANS_STREAM;
+
+ /** Addrs */
+
+ switch (src->sa_family) {
+
+ case AF_INET:
+
+ if (dst->sa_family == AF_INET) {
+
+ header->addr.ip4.src_addr =
+ ((struct sockaddr_in *) src)->sin_addr.s_addr;
+ header->addr.ip4.src_port = ((struct sockaddr_in *) src)->sin_port;
+ }
+#if (NGX_HAVE_INET6)
+ else /** dst == AF_INET6 */{
+
+ ngx_v4tov6(&v6_tmp, src);
+ ngx_memcpy(header->addr.ip6.src_addr, &v6_tmp, 16);
+ header->addr.ip6.src_port = ((struct sockaddr_in *) src)->sin_port;
+ }
+#endif
+ break;
+
+#if (NGX_HAVE_INET6)
+ case AF_INET6:
+ v6_used = 1;
+
+ ngx_memcpy(header->addr.ip6.src_addr,
+ &((struct sockaddr_in6 *) src)->sin6_addr, 16);
+ header->addr.ip6.src_port = ((struct sockaddr_in6 *) src)->sin6_port;
+
+ break;
+#endif
+
+ default:
+ ngx_log_debug1(NGX_LOG_DEBUG_CORE, c->log, 0,
+ "PROXY protocol v2 unsupported src address family %ui",
+ src->sa_family);
+ goto unspec;
+ };
+
+ switch (dst->sa_family) {
+ case AF_INET:
+
+ if (src->sa_family == AF_INET) {
+
+ header->addr.ip4.dst_addr =
+ ((struct sockaddr_in *) dst)->sin_addr.s_addr;
+ header->addr.ip4.dst_port = ((struct sockaddr_in *) dst)->sin_port;
+ }
+#if (NGX_HAVE_INET6)
+ else /** src == AF_INET6 */{
+
+ ngx_v4tov6(&v6_tmp, dst);
+ ngx_memcpy(header->addr.ip6.dst_addr, &v6_tmp, 16);
+ header->addr.ip6.dst_port = ((struct sockaddr_in *) dst)->sin_port;
+
+ }
+#endif
+ break;
+
+#if (NGX_HAVE_INET6)
+ case AF_INET6:
+ v6_used = 1;
+
+ ngx_memcpy(header->addr.ip6.dst_addr,
+ &((struct sockaddr_in6 *) dst)->sin6_addr, 16);
+ header->addr.ip6.dst_port = ((struct sockaddr_in6 *) dst)->sin6_port;
+
+ break;
+#endif
+
+ default:
+ ngx_log_debug1(NGX_LOG_DEBUG_CORE, c->log, 0,
+ "PROXY protocol v2 unsupported dest address family %ui",
+ dst->sa_family);
+ goto unspec;
+ }
+
+#if (NGX_HAVE_INET6)
+ if (!v6_used) {
+ header->family_transport |= NGX_PROXY_PROTOCOL_V2_FAM_INET;
+ len = NGX_PROXY_PROTOCOL_V2_HDR_LEN_INET;
+
+ } else {
+ header->family_transport |= NGX_PROXY_PROTOCOL_V2_FAM_INET6;
+ len = NGX_PROXY_PROTOCOL_V2_HDR_LEN_INET6;
+
+ }
+#else
+ header->family_transport |= NGX_PROXY_PROTOCOL_V2_FAM_INET;
+ len = NGX_PROXY_PROTOCOL_V2_HDR_LEN_INET;
+#endif
+
+ /** SSL TLVs */
+#if (NGX_STREAM_SSL)
+
+ if (c->ssl != NULL) {
+
+ data = NULL;
+ data_len = 0;
+
+ tlv = (ngx_tlv_ssl_t *) (buf + len);
+ ngx_memzero(tlv, sizeof(ngx_tlv_ssl_t));
+
+ tlv->tlv.type = NGX_PROXY_PROTOCOL_V2_TYPE_SSL;
+ pos = buf + len + sizeof(ngx_tlv_ssl_t);
+
+ tlv->client |= NGX_PROXY_PROTOCOL_V2_CLIENT_SSL;
+
+#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
+ SSL_get0_alpn_selected(c->ssl->connection, &data, &data_len);
+
+#ifdef TLSEXT_TYPE_next_proto_neg
+ if (data_len == 0) {
+ SSL_get0_next_proto_negotiated(c->ssl->connection,
+ &data, &data_len);
+ }
+#endif
+
+#else /* TLSEXT_TYPE_next_proto_neg */
+ SSL_get0_next_proto_negotiated(c->ssl->connection, &data, &data_len);
+#endif
+
+ if (data_len) {
+
+ pos = ngx_copy_tlv(pos, last,
+ NGX_PROXY_PROTOCOL_V2_TYPE_ALPN,
+ (u_char *) data, (uint16_t) data_len);
+ if (pos == NULL) {
+ return NULL;
+ }
+ }
+
+ value = (u_char *) SSL_get_version(c->ssl->connection);
+ if (value != NULL) {
+
+ pos = ngx_copy_tlv(pos, last,
+ NGX_PROXY_PROTOCOL_V2_SUBTYPE_SSL_VERSION,
+ value, ngx_strlen(value));
+ if (pos == NULL) {
+ return NULL;
+ }
+ }
+
+ crt = SSL_get_peer_certificate(c->ssl->connection);
+ if (crt != NULL) {
+
+ tlv->client |= NGX_PROXY_PROTOCOL_V2_CLIENT_CERT_SESS;
+
+ rc = SSL_get_verify_result(c->ssl->connection);
+ tlv->verify = htonl(rc);
+
+ if (rc == X509_V_OK) {
+
+ if (ngx_ssl_ocsp_get_status(c, &s) == NGX_OK) {
+ tlv->client |= NGX_PROXY_PROTOCOL_V2_CLIENT_CERT_CONN;
+ }
+ }
+
+ X509_NAME *subject_name_value = X509_get_subject_name(crt);
+ if(subject_name_value != NULL) {
+ int nid = OBJ_txt2nid("CN");
+ int index = X509_NAME_get_index_by_NID(subject_name_value, nid, -1);
+
+ X509_NAME_ENTRY *subject_name_cn_entry = X509_NAME_get_entry(subject_name_value, index);
+ if (subject_name_cn_entry) {
+ ASN1_STRING *subject_name_cn_data_asn1 = X509_NAME_ENTRY_get_data(subject_name_cn_entry);
+
+ if (subject_name_cn_data_asn1 != NULL) {
+ value = (u_char *) ASN1_STRING_get0_data(subject_name_cn_data_asn1);
+ if(value != NULL) {
+ pos = ngx_copy_tlv(pos, last,
+ NGX_PROXY_PROTOCOL_V2_SUBTYPE_SSL_CN,
+ value, ngx_strlen(value));
+ if (pos == NULL) {
+ return NULL;
+ }
+ }
+ }
+ }
+ }
+
+ X509_free(crt);
+ }
+
+
+ crt = SSL_get_certificate(c->ssl->connection);
+ if (crt != NULL) {
+
+ key = X509_get_pubkey(crt);
+
+ /** Key */
+ if (key != NULL) {
+
+ switch (EVP_PKEY_base_id(key)) {
+ case EVP_PKEY_RSA:
+ value = (u_char *) "RSA";
+ break;
+ case EVP_PKEY_EC:
+ value = (u_char *) "EC";
+ break;
+ case EVP_PKEY_DSA:
+ value = (u_char *) "DSA";
+ break;
+ default:
+ value = NULL;
+ break;
+ }
+
+ if (value != NULL) {
+
+ value = ngx_snprintf(kbuf, sizeof(kbuf) - 1, "%s%d%Z",
+ value, EVP_PKEY_bits(key));
+
+ pos = ngx_copy_tlv(pos, last,
+ NGX_PROXY_PROTOCOL_V2_SUBTYPE_SSL_KEY_ALG,
+ kbuf, ngx_strlen(kbuf));
+ }
+
+ EVP_PKEY_free(key);
+
+ if (pos == NULL) {
+ return NULL;
+ }
+ }
+
+ /* ALG */
+ X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
+ value = (u_char *) OBJ_nid2sn(OBJ_obj2nid(algorithm));
+
+ if (value != NULL) {
+
+ pos = ngx_copy_tlv(pos, last,
+ NGX_PROXY_PROTOCOL_V2_SUBTYPE_SSL_SIG_ALG,
+ value, ngx_strlen(value));
+ if (pos == NULL) {
+ return NULL;
+ }
+ }
+ }
+
+ value = (u_char *) SSL_get_cipher_name(c->ssl->connection);
+ if (value != NULL) {
+
+ pos = ngx_copy_tlv(pos, last,
+ NGX_PROXY_PROTOCOL_V2_SUBTYPE_SSL_CIPHER,
+ value, ngx_strlen(value));
+ if (pos == NULL) {
+ return NULL;
+ }
+ }
+
+ tlv_len = pos - (buf + len);
+
+ tlv->tlv.length_hi = (uint16_t) (tlv_len - sizeof(ngx_tlv_t)) >> 8;
+ tlv->tlv.length_lo = (uint16_t) (tlv_len - sizeof(ngx_tlv_t)) & 0x00ff;
+
+ len = len + tlv_len;
+ }
+
+#endif
+
+ header->len = htons(len - NGX_PROXY_PROTOCOL_V2_HDR_LEN);
+ return buf + len;
+
+unspec:
+ header->family_transport |= NGX_PROXY_PROTOCOL_V2_FAM_UNSPEC;
+ header->len = 0;
+
+ return buf + NGX_PROXY_PROTOCOL_V2_HDR_LEN;
+}
+
+
+#if (NGX_HAVE_INET6)
+static void
+ngx_v4tov6(struct in6_addr *sin6_addr, struct sockaddr *addr)
+{
+ static const char rfc4291[] = { 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0xFF, 0xFF };
+
+ struct in_addr tmp_addr, *sin_addr;
+
+ sin_addr = &((struct sockaddr_in *) addr)->sin_addr;
+
+ tmp_addr.s_addr = sin_addr->s_addr;
+ ngx_memcpy(sin6_addr->s6_addr, rfc4291, sizeof(rfc4291));
+ ngx_memcpy(sin6_addr->s6_addr + 12, &tmp_addr.s_addr, 4);
+}
+#endif
+
+
+#if (NGX_STREAM_SSL)
+
+static u_char *
+ngx_copy_tlv(u_char *pos, u_char *last, u_char type,
+ u_char *value, uint16_t value_len)
+{
+ ngx_tlv_t *tlv;
+
+ if (last - pos < (long) sizeof(*tlv)) {
+ return NULL;
+ }
+
+ tlv = (ngx_tlv_t *) pos;
+
+ tlv->type = type;
+ tlv->length_hi = (uint16_t) value_len >> 8;
+ tlv->length_lo = (uint16_t) value_len & 0x00ff;
+ ngx_memcpy(tlv->value, value, value_len);
+
+ return pos + (value_len + sizeof(*tlv));
+}
+
+#endif
+
+
diff --git a/src/core/ngx_proxy_protocol.h b/src/core/ngx_proxy_protocol.h
index b716220..190ec03 100644
--- a/src/core/ngx_proxy_protocol.h
+++ b/src/core/ngx_proxy_protocol.h
@@ -13,7 +13,7 @@
#include <ngx_core.h>
-#define NGX_PROXY_PROTOCOL_MAX_HEADER 107
+#define NGX_PROXY_PROTOCOL_MAX_HEADER 214
struct ngx_proxy_protocol_s {
@@ -27,7 +27,7 @@ struct ngx_proxy_protocol_s {
u_char *ngx_proxy_protocol_read(ngx_connection_t *c, u_char *buf,
u_char *last);
u_char *ngx_proxy_protocol_write(ngx_connection_t *c, u_char *buf,
- u_char *last);
+ u_char *last, ngx_uint_t pp_version);
#endif /* _NGX_PROXY_PROTOCOL_H_INCLUDED_ */
diff --git a/src/stream/ngx_stream_proxy_module.c b/src/stream/ngx_stream_proxy_module.c
index 01cda7a..faec264 100644
--- a/src/stream/ngx_stream_proxy_module.c
+++ b/src/stream/ngx_stream_proxy_module.c
@@ -30,7 +30,7 @@ typedef struct {
ngx_uint_t responses;
ngx_uint_t next_upstream_tries;
ngx_flag_t next_upstream;
- ngx_flag_t proxy_protocol;
+ ngx_uint_t proxy_protocol;
ngx_stream_upstream_local_t *local;
ngx_flag_t socket_keepalive;
@@ -121,6 +121,14 @@ static ngx_conf_post_t ngx_stream_proxy_ssl_conf_command_post =
#endif
+static ngx_conf_enum_t ngx_stream_proxy_protocol[] = {
+ { ngx_string("off"), 0 },
+ { ngx_string("on"), 1 },
+ { ngx_string("v2"), 2 },
+ { ngx_null_string, 0 }
+};
+
+
static ngx_conf_deprecated_t ngx_conf_deprecated_proxy_downstream_buffer = {
ngx_conf_deprecated, "proxy_downstream_buffer", "proxy_buffer_size"
};
@@ -239,10 +247,10 @@ static ngx_command_t ngx_stream_proxy_commands[] = {
{ ngx_string("proxy_protocol"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_FLAG,
- ngx_conf_set_flag_slot,
+ ngx_conf_set_enum_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_proxy_srv_conf_t, proxy_protocol),
- NULL },
+ &ngx_stream_proxy_protocol },
#if (NGX_STREAM_SSL)
@@ -891,7 +899,8 @@ ngx_stream_proxy_init_upstream(ngx_stream_session_t *s)
cl->buf->pos = p;
- p = ngx_proxy_protocol_write(c, p, p + NGX_PROXY_PROTOCOL_MAX_HEADER);
+ p = ngx_proxy_protocol_write(c, p, p + NGX_PROXY_PROTOCOL_MAX_HEADER,
+ u->proxy_protocol);
if (p == NULL) {
ngx_stream_proxy_finalize(s, NGX_STREAM_INTERNAL_SERVER_ERROR);
return;
@@ -942,14 +951,15 @@ ngx_stream_proxy_send_proxy_protocol(ngx_stream_session_t *s)
ngx_log_debug0(NGX_LOG_DEBUG_STREAM, c->log, 0,
"stream proxy send PROXY protocol header");
- p = ngx_proxy_protocol_write(c, buf, buf + NGX_PROXY_PROTOCOL_MAX_HEADER);
+ u = s->upstream;
+
+ p = ngx_proxy_protocol_write(c, buf, buf + NGX_PROXY_PROTOCOL_MAX_HEADER,
+ u->proxy_protocol);
if (p == NULL) {
ngx_stream_proxy_finalize(s, NGX_STREAM_INTERNAL_SERVER_ERROR);
return NGX_ERROR;
}
- u = s->upstream;
-
pc = u->peer.connection;
size = p - buf;
@@ -1998,7 +2008,7 @@ ngx_stream_proxy_create_srv_conf(ngx_conf_t *cf)
conf->responses = NGX_CONF_UNSET_UINT;
conf->next_upstream_tries = NGX_CONF_UNSET_UINT;
conf->next_upstream = NGX_CONF_UNSET;
- conf->proxy_protocol = NGX_CONF_UNSET;
+ conf->proxy_protocol = NGX_CONF_UNSET_UINT;
conf->local = NGX_CONF_UNSET_PTR;
conf->socket_keepalive = NGX_CONF_UNSET;
@@ -2053,7 +2063,7 @@ ngx_stream_proxy_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
ngx_conf_merge_value(conf->next_upstream, prev->next_upstream, 1);
- ngx_conf_merge_value(conf->proxy_protocol, prev->proxy_protocol, 0);
+ ngx_conf_merge_uint_value(conf->proxy_protocol, prev->proxy_protocol, 0);
ngx_conf_merge_ptr_value(conf->local, prev->local, NULL);
diff --git a/src/stream/ngx_stream_upstream.h b/src/stream/ngx_stream_upstream.h
index 9857e0b..494af4a 100644
--- a/src/stream/ngx_stream_upstream.h
+++ b/src/stream/ngx_stream_upstream.h
@@ -141,7 +141,7 @@ typedef struct {
ngx_stream_upstream_resolved_t *resolved;
ngx_stream_upstream_state_t *state;
unsigned connected:1;
- unsigned proxy_protocol:1;
+ unsigned proxy_protocol:2;
} ngx_stream_upstream_t;