Skip to content

Commit f139b09

Browse files
feature: add new API: tcpsock:getsslsession.
1 parent 75dff4c commit f139b09

1 file changed

Lines changed: 26 additions & 1 deletion

File tree

lib/resty/core/socket.lua

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ ngx_http_lua_ffi_ssl_free_session(void *sess);
7171
int
7272
ngx_http_lua_ffi_socket_tcp_getfd(ngx_http_request_t *r,
7373
ngx_http_lua_socket_tcp_upstream_t *u, char **errmsg);
74+
int
75+
ngx_http_lua_socket_tcp_get_ssl_session(ngx_http_request_t *r,
76+
ngx_http_lua_socket_tcp_upstream_t *u, void **sess,
77+
char **errmsg);
7478
]]
7579

7680
ngx_lua_ffi_socket_tcp_getoption = C.ngx_http_lua_ffi_socket_tcp_getoption
@@ -102,6 +106,7 @@ end
102106

103107

104108
local output_value_buf = ffi_new("int[1]")
109+
local session_ptr = ffi_new("void *[1]")
105110
local ERR_BUF_SIZE = 4096
106111

107112
local FFI_OK = base.FFI_OK
@@ -211,7 +216,6 @@ end
211216

212217

213218
if subsystem == 'http' then
214-
local session_ptr = ffi_new("void *[1]")
215219
local server_name_str = ffi_new("ngx_str_t[1]")
216220
local openssl_error_code = ffi_new("int[1]")
217221

@@ -334,6 +338,26 @@ local function sslhandshake(cosocket, reused_session, server_name, ssl_verify,
334338
end
335339

336340

341+
local function getsslsession(cosocket)
342+
if not cosocket then
343+
error("ngx.socket getfd: expecting the cosocket object, but seen none")
344+
end
345+
346+
local r = get_request()
347+
if not r then
348+
error("no request found")
349+
end
350+
351+
local u = get_tcp_socket(cosocket)
352+
local rc = C.ngx_http_lua_socket_tcp_get_ssl_session(r, u,
353+
session_ptr, errmsg)
354+
if rc == FFI_ERROR then
355+
return nil, ffi_str(errmsg[0])
356+
end
357+
358+
return ffi_gc(session_ptr[0], C.ngx_http_lua_ffi_ssl_free_session)
359+
end
360+
337361
do
338362
local method_table = registry.__tcp_cosocket_mt
339363
method_table.getoption = getoption
@@ -343,6 +367,7 @@ do
343367
method_table.getfd = getfd
344368
method_table.getoption = getoption
345369
method_table.setoption = setoption
370+
method_table.getsslsession = getsslsession
346371

347372
method_table = registry.__tcp_req_cosocket_mt
348373
method_table.getfd = getfd

0 commit comments

Comments
 (0)