Skip to content

Commit fb70407

Browse files
feature: makes outgoing connections to a proxied server originate from the specified local IP address with an optional port.
1 parent d981e1b commit fb70407

File tree

4 files changed

+505
-2
lines changed

4 files changed

+505
-2
lines changed

lib/ngx/balancer.lua

+42
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ local ngx_lua_ffi_balancer_set_more_tries
2424
local ngx_lua_ffi_balancer_get_last_failure
2525
local ngx_lua_ffi_balancer_set_timeouts -- used by both stream and http
2626
local ngx_lua_ffi_balancer_set_upstream_tls
27+
local ngx_lua_ffi_balancer_bind_to_local_addr
2728

2829

2930
if subsystem == 'http' then
@@ -48,8 +49,13 @@ if subsystem == 'http' then
4849

4950
int ngx_http_lua_ffi_balancer_recreate_request(ngx_http_request_t *r,
5051
char **err);
52+
5153
int ngx_http_lua_ffi_balancer_set_upstream_tls(ngx_http_request_t *r,
5254
int on, char **err);
55+
56+
int ngx_http_lua_ffi_balancer_bind_to_local_addr(ngx_http_request_t *r,
57+
const u_char *addr, size_t addr_len,
58+
u_char *errbuf, size_t *errbuf_size);
5359
]]
5460

5561
ngx_lua_ffi_balancer_set_current_peer =
@@ -70,6 +76,9 @@ if subsystem == 'http' then
7076
ngx_lua_ffi_balancer_set_upstream_tls =
7177
C.ngx_http_lua_ffi_balancer_set_upstream_tls
7278

79+
ngx_lua_ffi_balancer_bind_to_local_addr =
80+
C.ngx_http_lua_ffi_balancer_bind_to_local_addr
81+
7382
elseif subsystem == 'stream' then
7483
ffi.cdef[[
7584
int ngx_stream_lua_ffi_balancer_set_current_peer(
@@ -353,5 +362,38 @@ if subsystem == 'http' then
353362
end
354363
end
355364

365+
if subsystem == "http" then
366+
function _M.bind_to_local_addr(addr)
367+
local r = get_request()
368+
if not r then
369+
error("no request found")
370+
end
371+
372+
if type(addr) ~= "string" then
373+
error("bad argument #1 to 'bind_to_local_addr' "
374+
.. "(string expected, got " .. type(addr) .. ")")
375+
end
376+
377+
local errbuf_size = 1024
378+
local errbuf = get_string_buf(errbuf_size)
379+
local sizep = get_size_ptr()
380+
sizep[0] = errbuf_size
381+
local rc = ngx_lua_ffi_balancer_bind_to_local_addr(r, addr, #addr,
382+
errbuf,
383+
sizep)
384+
if rc == FFI_OK then
385+
return true
386+
end
387+
388+
return nil, ffi_str(errbuf, sizep[0])
389+
end
390+
391+
else
392+
function _M.bind_to_local_addr(addr)
393+
error("'bind_to_local_addr' not yet implemented in " .. subsystem ..
394+
" subsystem", 2)
395+
end
396+
end
397+
356398

357399
return _M

lib/ngx/balancer.md

+15
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Table of Contents
1616
* [get_last_failure](#get_last_failure)
1717
* [recreate_request](#recreate_request)
1818
* [set_current_peer](#set_current_peer)
19+
* [bind_to_local_addr](#bind_to_local_addr)
1920
* [enable_keepalive](#enable_keepalive)
2021
* [set_more_tries](#set_more_tries)
2122
* [set_timeouts](#set_timeouts)
@@ -174,6 +175,20 @@ In case of an error, this function returns `nil` and a string describing the err
174175

175176
[Back to TOC](#table-of-contents)
176177

178+
bind_to_local_addr
179+
--------------
180+
**syntax:** *ok, err = balancer.bind_to_local_addr(addr)*
181+
182+
**context:** *balancer_by_lua**
183+
184+
Makes outgoing connections to a proxied server originate from the specified local IP address with an optional port.
185+
186+
`addr` is a string value of the IP address with optional port. For example: 127.0.0.1, 127.0.0.1:12345.
187+
188+
In case of an error, this function returns `nil` and a string describing the error.
189+
190+
[Back to TOC](#table-of-contents)
191+
177192
enable_keepalive
178193
----------------
179194
**syntax:** `ok, err = balancer.enable_keepalive(idle_timeout?, max_requests?)`

0 commit comments

Comments
 (0)