forked from nginx/ngx-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttporigdst.rs
314 lines (281 loc) · 10.3 KB
/
httporigdst.rs
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
use std::ffi::{c_char, c_int, c_void};
use std::ptr::addr_of;
use ngx::core;
use ngx::ffi::{
in_port_t, nginx_version, ngx_conf_t, ngx_connection_local_sockaddr, ngx_http_add_variable, ngx_http_module_t,
ngx_http_variable_t, ngx_inet_get_port, ngx_int_t, ngx_module_t, ngx_sock_ntop, ngx_str_t, ngx_uint_t,
ngx_variable_value_t, sockaddr, sockaddr_storage, INET_ADDRSTRLEN, NGX_HTTP_MODULE, NGX_RS_MODULE_SIGNATURE,
};
use ngx::http::{self, HTTPModule};
use ngx::{http_variable_get, ngx_http_null_variable, ngx_log_debug_http, ngx_null_string, ngx_string};
const IPV4_STRLEN: usize = INET_ADDRSTRLEN as usize;
#[derive(Debug)]
struct NgxHttpOrigDstCtx {
orig_dst_addr: ngx_str_t,
orig_dst_port: ngx_str_t,
}
impl Default for NgxHttpOrigDstCtx {
fn default() -> NgxHttpOrigDstCtx {
NgxHttpOrigDstCtx {
orig_dst_addr: ngx_null_string!(),
orig_dst_port: ngx_null_string!(),
}
}
}
impl NgxHttpOrigDstCtx {
pub fn save(&mut self, addr: &str, port: in_port_t, pool: &mut core::Pool) -> core::Status {
let addr_data = pool.alloc_unaligned(addr.len());
if addr_data.is_null() {
return core::Status::NGX_ERROR;
}
unsafe { libc::memcpy(addr_data, addr.as_ptr() as *const c_void, addr.len()) };
self.orig_dst_addr.len = addr.len();
self.orig_dst_addr.data = addr_data as *mut u8;
let port_str = port.to_string();
let port_data = pool.alloc_unaligned(port_str.len());
if port_data.is_null() {
return core::Status::NGX_ERROR;
}
unsafe { libc::memcpy(port_data, port_str.as_bytes().as_ptr() as *const c_void, port_str.len()) };
self.orig_dst_port.len = port_str.len();
self.orig_dst_port.data = port_data as *mut u8;
core::Status::NGX_OK
}
pub unsafe fn bind_addr(&self, v: *mut ngx_variable_value_t) {
if self.orig_dst_addr.len == 0 {
(*v).set_not_found(1);
return;
}
(*v).set_valid(1);
(*v).set_no_cacheable(0);
(*v).set_not_found(0);
(*v).set_len(self.orig_dst_addr.len as u32);
(*v).data = self.orig_dst_addr.data;
}
pub unsafe fn bind_port(&self, v: *mut ngx_variable_value_t) {
if self.orig_dst_port.len == 0 {
(*v).set_not_found(1);
return;
}
(*v).set_valid(1);
(*v).set_no_cacheable(0);
(*v).set_not_found(0);
(*v).set_len(self.orig_dst_port.len as u32);
(*v).data = self.orig_dst_port.data;
}
}
static NGX_HTTP_ORIG_DST_MODULE_CTX: ngx_http_module_t = ngx_http_module_t {
preconfiguration: Some(Module::preconfiguration),
postconfiguration: Some(Module::postconfiguration),
create_main_conf: Some(Module::create_main_conf),
init_main_conf: Some(Module::init_main_conf),
create_srv_conf: Some(Module::create_srv_conf),
merge_srv_conf: Some(Module::merge_srv_conf),
create_loc_conf: Some(Module::create_loc_conf),
merge_loc_conf: Some(Module::merge_loc_conf),
};
// Generate the `ngx_modules` table with exported modules.
// This feature is required to build a 'cdylib' dynamic module outside of the NGINX buildsystem.
#[cfg(feature = "export-modules")]
ngx::ngx_modules!(ngx_http_orig_dst_module);
#[used]
#[allow(non_upper_case_globals)]
#[cfg_attr(not(feature = "export-modules"), no_mangle)]
pub static mut ngx_http_orig_dst_module: ngx_module_t = ngx_module_t {
ctx_index: ngx_uint_t::MAX,
index: ngx_uint_t::MAX,
name: std::ptr::null_mut(),
spare0: 0,
spare1: 0,
version: nginx_version as ngx_uint_t,
signature: NGX_RS_MODULE_SIGNATURE.as_ptr() as *const c_char,
ctx: &NGX_HTTP_ORIG_DST_MODULE_CTX as *const _ as *mut _,
commands: std::ptr::null_mut(),
type_: NGX_HTTP_MODULE as ngx_uint_t,
init_master: None,
init_module: None,
init_process: None,
init_thread: None,
exit_thread: None,
exit_process: None,
exit_master: None,
spare_hook0: 0,
spare_hook1: 0,
spare_hook2: 0,
spare_hook3: 0,
spare_hook4: 0,
spare_hook5: 0,
spare_hook6: 0,
spare_hook7: 0,
};
static mut NGX_HTTP_ORIG_DST_VARS: [ngx_http_variable_t; 3] = [
// ngx_str_t name
// ngx_http_set_variable_pt set_handler
// ngx_http_get_variable_pt get_handler
// uintptr_t data
// ngx_uint_t flags
// ngx_uint_t index
ngx_http_variable_t {
name: ngx_string!("server_orig_addr"),
set_handler: None,
get_handler: Some(ngx_http_orig_dst_addr_variable),
data: 0,
flags: 0,
index: 0,
},
ngx_http_variable_t {
name: ngx_string!("server_orig_port"),
set_handler: None,
get_handler: Some(ngx_http_orig_dst_port_variable),
data: 0,
flags: 0,
index: 0,
},
ngx_http_null_variable!(),
];
unsafe fn ngx_get_origdst(request: &mut http::Request) -> Result<(String, in_port_t), core::Status> {
let c = request.connection();
if (*c).type_ != libc::SOCK_STREAM {
ngx_log_debug_http!(request, "httporigdst: connection is not type SOCK_STREAM");
return Err(core::Status::NGX_DECLINED);
}
if ngx_connection_local_sockaddr(c, std::ptr::null_mut(), 0) != core::Status::NGX_OK.into() {
ngx_log_debug_http!(request, "httporigdst: no local sockaddr from connection");
return Err(core::Status::NGX_ERROR);
}
let level: c_int;
let optname: c_int;
match (*(*c).local_sockaddr).sa_family as i32 {
libc::AF_INET => {
level = libc::SOL_IP;
optname = libc::SO_ORIGINAL_DST;
}
_ => {
ngx_log_debug_http!(request, "httporigdst: only support IPv4");
return Err(core::Status::NGX_DECLINED);
}
}
let mut addr: sockaddr_storage = { std::mem::zeroed() };
let mut addrlen: libc::socklen_t = std::mem::size_of_val(&addr) as libc::socklen_t;
let rc = libc::getsockopt(
(*c).fd,
level,
optname,
&mut addr as *mut _ as *mut _,
&mut addrlen as *mut u32,
);
if rc == -1 {
ngx_log_debug_http!(request, "httporigdst: getsockopt failed");
return Err(core::Status::NGX_DECLINED);
}
let mut ip: Vec<u8> = vec![0; IPV4_STRLEN];
let e = unsafe {
ngx_sock_ntop(
std::ptr::addr_of_mut!(addr) as *mut sockaddr,
std::mem::size_of::<sockaddr>() as u32,
ip.as_mut_ptr(),
IPV4_STRLEN,
0,
)
};
if e == 0 {
ngx_log_debug_http!(request, "httporigdst: ngx_sock_ntop failed to convert sockaddr");
return Err(core::Status::NGX_ERROR);
}
ip.truncate(e);
let port = unsafe { ngx_inet_get_port(std::ptr::addr_of_mut!(addr) as *mut sockaddr) };
Ok((String::from_utf8(ip).unwrap(), port))
}
http_variable_get!(
ngx_http_orig_dst_addr_variable,
|request: &mut http::Request, v: *mut ngx_variable_value_t, _: usize| {
let ctx = request.get_module_ctx::<NgxHttpOrigDstCtx>(&*addr_of!(ngx_http_orig_dst_module));
if let Some(obj) = ctx {
ngx_log_debug_http!(request, "httporigdst: found context and binding variable",);
obj.bind_addr(v);
return core::Status::NGX_OK;
}
// lazy initialization:
// get original dest information
// create context
// set context
// bind address
ngx_log_debug_http!(request, "httporigdst: context not found, getting address");
let r = ngx_get_origdst(request);
match r {
Err(e) => {
return e;
}
Ok((ip, port)) => {
// create context,
// set context
let new_ctx = request.pool().allocate::<NgxHttpOrigDstCtx>(Default::default());
if new_ctx.is_null() {
return core::Status::NGX_ERROR;
}
ngx_log_debug_http!(request, "httporigdst: saving ip - {:?}, port - {}", ip, port,);
(*new_ctx).save(&ip, port, &mut request.pool());
(*new_ctx).bind_addr(v);
request.set_module_ctx(new_ctx as *mut c_void, &*addr_of!(ngx_http_orig_dst_module));
}
}
core::Status::NGX_OK
}
);
http_variable_get!(
ngx_http_orig_dst_port_variable,
|request: &mut http::Request, v: *mut ngx_variable_value_t, _: usize| {
let ctx = request.get_module_ctx::<NgxHttpOrigDstCtx>(&*addr_of!(ngx_http_orig_dst_module));
if let Some(obj) = ctx {
ngx_log_debug_http!(request, "httporigdst: found context and binding variable",);
obj.bind_port(v);
return core::Status::NGX_OK;
}
// lazy initialization:
// get original dest information
// create context
// set context
// bind port
ngx_log_debug_http!(request, "httporigdst: context not found, getting address");
let r = ngx_get_origdst(request);
match r {
Err(e) => {
return e;
}
Ok((ip, port)) => {
// create context,
// set context
let new_ctx = request.pool().allocate::<NgxHttpOrigDstCtx>(Default::default());
if new_ctx.is_null() {
return core::Status::NGX_ERROR;
}
ngx_log_debug_http!(request, "httporigdst: saving ip - {:?}, port - {}", ip, port,);
(*new_ctx).save(&ip, port, &mut request.pool());
(*new_ctx).bind_port(v);
request.set_module_ctx(new_ctx as *mut c_void, &*addr_of!(ngx_http_orig_dst_module));
}
}
core::Status::NGX_OK
}
);
struct Module;
impl HTTPModule for Module {
type MainConf = ();
type SrvConf = ();
type LocConf = ();
// static ngx_int_t ngx_http_orig_dst_add_variables(ngx_conf_t *cf)
unsafe extern "C" fn preconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t {
for mut v in NGX_HTTP_ORIG_DST_VARS {
if v.name.len == 0 {
break;
}
let var = ngx_http_add_variable(cf, &mut v.name, v.flags);
if var.is_null() {
return core::Status::NGX_ERROR.into();
}
(*var).get_handler = v.get_handler;
(*var).data = v.data;
}
core::Status::NGX_OK.into()
}
}