Skip to content

Commit e309d53

Browse files
authored
feat: add variable connection_original_dst (#45)
1 parent e842c96 commit e309d53

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
diff --git auto/os/linux auto/os/linux
2+
index 5e280eca..086e5372 100644
3+
--- auto/os/linux
4+
+++ auto/os/linux
5+
@@ -190,6 +190,21 @@ ngx_feature_test="struct __user_cap_data_struct data;
6+
(void) SYS_capset"
7+
. auto/feature
8+
9+
+# netfilter_ipv4
10+
+
11+
+ngx_feature="netfilter_ipv4"
12+
+ngx_feature_name="NGX_HAVE_NETFILTER_IPV4"
13+
+ngx_feature_run=no
14+
+ngx_feature_incs="#include <linux/netfilter_ipv4.h>"
15+
+ngx_feature_path=
16+
+ngx_feature_libs=
17+
+ngx_feature_test="int so_original_dst;
18+
+
19+
+ so_original_dst = SO_ORIGINAL_DST;
20+
+
21+
+ (void) so_original_dst;"
22+
+. auto/feature
23+
+
24+
25+
# crypt_r()
26+
27+
diff --git src/http/ngx_http_variables.c src/http/ngx_http_variables.c
28+
index c2113c84..1c2a06a7 100644
29+
--- src/http/ngx_http_variables.c
30+
+++ src/http/ngx_http_variables.c
31+
@@ -130,6 +130,11 @@ static ngx_int_t ngx_http_variable_connection(ngx_http_request_t *r,
32+
static ngx_int_t ngx_http_variable_connection_requests(ngx_http_request_t *r,
33+
ngx_http_variable_value_t *v, uintptr_t data);
34+
35+
+#if (NGX_HAVE_NETFILTER_IPV4)
36+
+static ngx_int_t ngx_http_variable_connection_dst(ngx_http_request_t *r,
37+
+ ngx_http_variable_value_t *v, uintptr_t data);
38+
+#endif
39+
+
40+
static ngx_int_t ngx_http_variable_nginx_version(ngx_http_request_t *r,
41+
ngx_http_variable_value_t *v, uintptr_t data);
42+
static ngx_int_t ngx_http_variable_hostname(ngx_http_request_t *r,
43+
@@ -342,6 +347,11 @@ static ngx_http_variable_t ngx_http_core_variables[] = {
44+
{ ngx_string("connection_requests"), NULL,
45+
ngx_http_variable_connection_requests, 0, 0, 0 },
46+
47+
+#if (NGX_HAVE_NETFILTER_IPV4)
48+
+ { ngx_string("connection_original_dst"), NULL,
49+
+ ngx_http_variable_connection_dst, 0, 0, 0 },
50+
+#endif
51+
+
52+
{ ngx_string("nginx_version"), NULL, ngx_http_variable_nginx_version,
53+
0, 0, 0 },
54+
55+
@@ -2252,6 +2262,43 @@ ngx_http_variable_connection_requests(ngx_http_request_t *r,
56+
}
57+
58+
59+
+#if (NGX_HAVE_NETFILTER_IPV4)
60+
+static ngx_int_t
61+
+ngx_http_variable_connection_dst(ngx_http_request_t *r,
62+
+ ngx_http_variable_value_t *v, uintptr_t data)
63+
+{
64+
+ struct sockaddr_in dst;
65+
+ socklen_t socklen;
66+
+ int rn;
67+
+ u_char *p;
68+
+
69+
+ socklen = sizeof(struct sockaddr_in);
70+
+
71+
+ rn = getsockopt(r->connection->fd, SOL_IP, SO_ORIGINAL_DST, (void *) &dst,
72+
+ &socklen);
73+
+ if (rn < 0) {
74+
+ ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_socket_errno,
75+
+ "getsockopt(SO_ORIGINAL_DST) failed");
76+
+ return NGX_ERROR;
77+
+ }
78+
+
79+
+ p = ngx_pnalloc(r->pool, NGX_SOCKADDR_STRLEN);
80+
+ if (p == NULL) {
81+
+ return NGX_ERROR;
82+
+ }
83+
+
84+
+ v->len = ngx_sock_ntop((struct sockaddr *) &dst, socklen, p,
85+
+ NGX_SOCKADDR_STRLEN, dst.sin_port);
86+
+ v->valid = 1;
87+
+ v->no_cacheable = 0;
88+
+ v->not_found = 0;
89+
+ v->data = p;
90+
+
91+
+ return NGX_OK;
92+
+}
93+
+#endif
94+
+
95+
+
96+
static ngx_int_t
97+
ngx_http_variable_nginx_version(ngx_http_request_t *r,
98+
ngx_http_variable_value_t *v, uintptr_t data)
99+
diff --git src/os/unix/ngx_linux_config.h src/os/unix/ngx_linux_config.h
100+
index 3036caeb..2353e9b3 100644
101+
--- src/os/unix/ngx_linux_config.h
102+
+++ src/os/unix/ngx_linux_config.h
103+
@@ -103,6 +103,9 @@ typedef struct iocb ngx_aiocb_t;
104+
#include <linux/capability.h>
105+
#endif
106+
107+
+#if (NGX_HAVE_NETFILTER_IPV4)
108+
+#include <linux/netfilter_ipv4.h>
109+
+#endif
110+
111+
#define NGX_LISTEN_BACKLOG 511
112+

0 commit comments

Comments
 (0)