Skip to content

Commit c7b161f

Browse files
feature: add new API: tcpsock:get_ssl_session.
1 parent 196a55f commit c7b161f

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

src/ngx_stream_lua_socket_tcp.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ static int ngx_stream_lua_socket_tcp_connect(lua_State *L);
3333
#if (NGX_STREAM_SSL)
3434
static int ngx_stream_lua_socket_tcp_sslhandshake(lua_State *L);
3535
static int ngx_stream_lua_socket_tcp_serversslhandshake(lua_State *L);
36+
static int ngx_stream_lua_socket_tcp_get_ssl_session(lua_State *L);
3637
#endif
3738
static int ngx_stream_lua_socket_tcp_receive(lua_State *L);
3839
static int ngx_stream_lua_socket_tcp_receiveany(lua_State *L);
@@ -373,6 +374,9 @@ ngx_stream_lua_inject_socket_tcp_api(ngx_log_t *log, lua_State *L)
373374
lua_pushcfunction(L, ngx_stream_lua_socket_tcp_sslhandshake);
374375
lua_setfield(L, -2, "sslhandshake");
375376

377+
lua_pushcfunction(L, ngx_stream_lua_socket_tcp_get_ssl_session);
378+
lua_setfield(L, -2, "getsslsession");
379+
376380
#endif
377381

378382
lua_pushcfunction(L, ngx_stream_lua_socket_tcp_receive);
@@ -1659,6 +1663,82 @@ ngx_stream_lua_socket_conn_error_retval_handler(ngx_stream_lua_request_t *r,
16591663

16601664

16611665
#if (NGX_STREAM_SSL)
1666+
static int
1667+
ngx_stream_lua_socket_tcp_get_ssl_session(lua_State *L)
1668+
{
1669+
int n;
1670+
1671+
ngx_connection_t *c;
1672+
ngx_ssl_session_t *ssl_session, **ud;
1673+
ngx_stream_lua_request_t *r;
1674+
ngx_stream_lua_socket_tcp_upstream_t *u;
1675+
1676+
1677+
n = lua_gettop(L);
1678+
if (n < 1) {
1679+
return luaL_error(L, "getsslsession: expecting 1 "
1680+
"argument, but seen %d", n);
1681+
}
1682+
1683+
r = ngx_stream_lua_get_req(L);
1684+
if (r == NULL) {
1685+
return luaL_error(L, "no request found");
1686+
}
1687+
1688+
ngx_log_debug0(NGX_LOG_DEBUG_STREAM, r->connection->log, 0,
1689+
"stream lua tcp socket getsslsession");
1690+
1691+
luaL_checktype(L, 1, LUA_TTABLE);
1692+
1693+
lua_rawgeti(L, 1, SOCKET_CTX_INDEX);
1694+
u = lua_touserdata(L, -1);
1695+
1696+
if (u == NULL
1697+
|| u->peer.connection == NULL
1698+
|| u->read_closed
1699+
|| u->write_closed)
1700+
{
1701+
lua_pushnil(L);
1702+
lua_pushliteral(L, "closed");
1703+
return 2;
1704+
}
1705+
1706+
if (u->request != r) {
1707+
return luaL_error(L, "bad request");
1708+
}
1709+
1710+
c = u->peer.connection;
1711+
if (c == NULL) {
1712+
lua_pushnil(L);
1713+
lua_pushliteral(L, "connection not found");
1714+
return 2;
1715+
}
1716+
1717+
ssl_session = ngx_ssl_get_session(c);
1718+
if (ssl_session == NULL) {
1719+
lua_pushnil(L);
1720+
lua_pushliteral(L, "no session");
1721+
return 2;
1722+
}
1723+
1724+
if (!SSL_SESSION_is_resumable(ssl_session)) {
1725+
ngx_ssl_free_session(ssl_session);
1726+
lua_pushnil(L);
1727+
lua_pushliteral(L, "not resumable session");
1728+
return 2;
1729+
}
1730+
1731+
ud = lua_newuserdata(L, sizeof(ngx_ssl_session_t *));
1732+
*ud = ssl_session;
1733+
/* set up the __gc metamethod */
1734+
lua_pushlightuserdata(L, ngx_stream_lua_lightudata_mask(
1735+
ssl_session_metatable_key));
1736+
lua_rawget(L, LUA_REGISTRYINDEX);
1737+
lua_setmetatable(L, -2);
1738+
1739+
return 1;
1740+
}
1741+
16621742

16631743
static int
16641744
ngx_stream_lua_socket_tcp_sslhandshake(lua_State *L)

t/062-count.t

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ worker: 5
251251
ngx.say("n = ", n)
252252
}
253253
--- stream_response
254-
n = 18
254+
n = 19
255255
--- no_error_log
256256
[error]
257257
@@ -312,6 +312,6 @@ n = 13
312312
ngx.say("n = ", n)
313313
}
314314
--- stream_response
315-
n = 18
315+
n = 19
316316
--- no_error_log
317317
[error]

0 commit comments

Comments
 (0)