Skip to content

Commit a41fdce

Browse files
james-callahanzhuizhuhaomeng
authored andcommitted
feature: add api to fetch raw nginx ssl pointer of the downstream request.
1 parent 5f02311 commit a41fdce

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed

src/ngx_http_lua_ssl_certby.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,4 +1479,15 @@ ngx_http_lua_ffi_ssl_verify_client(ngx_http_request_t *r, void *ca_certs,
14791479
}
14801480

14811481

1482+
ngx_ssl_conn_t *
1483+
ngx_http_lua_ffi_get_req_ssl_pointer(ngx_http_request_t *r)
1484+
{
1485+
if (r->connection == NULL || r->connection->ssl == NULL) {
1486+
return NULL;
1487+
}
1488+
1489+
return r->connection->ssl->connection;
1490+
}
1491+
1492+
14821493
#endif /* NGX_HTTP_SSL */

t/140-ssl-c-api.t

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ ffi.cdef[[
5959
int ngx_http_lua_ffi_set_priv_key(void *r,
6060
void *cdata, char **err);
6161
62+
void *ngx_http_lua_ffi_get_req_ssl_pointer(void *r);
63+
6264
void ngx_http_lua_ffi_free_cert(void *cdata);
6365
6466
void ngx_http_lua_ffi_free_priv_key(void *cdata);
@@ -1197,3 +1199,123 @@ lua ssl server name: "test.com"
11971199
--- no_error_log
11981200
[error]
11991201
[alert]
1202+
1203+
1204+
1205+
=== TEST 10: Raw SSL pointer
1206+
--- http_config
1207+
server {
1208+
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl;
1209+
server_name test.com;
1210+
1211+
ssl_certificate_by_lua_block {
1212+
collectgarbage()
1213+
1214+
local ffi = require "ffi"
1215+
require "defines"
1216+
1217+
local r = require "resty.core.base" .get_request()
1218+
if not r then
1219+
ngx.log(ngx.ERR, "no request found")
1220+
return
1221+
end
1222+
1223+
local ssl = ffi.C.ngx_http_lua_ffi_get_req_ssl_pointer(r);
1224+
if ssl == nil then
1225+
ngx.log(ngx.ERR, "failed to retrieve SSL*")
1226+
return
1227+
end
1228+
1229+
ffi.cdef[[
1230+
const char *SSL_get_servername(const void *, const int);
1231+
]]
1232+
local libssl = ffi.load "ssl"
1233+
local TLSEXT_NAMETYPE_host_name = 0
1234+
local sni = ffi.string(libssl.SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name))
1235+
ngx.log(ngx.INFO, "SNI is ", sni)
1236+
}
1237+
1238+
ssl_certificate ../../cert/test.crt;
1239+
ssl_certificate_key ../../cert/test.key;
1240+
1241+
server_tokens off;
1242+
location /foo {
1243+
default_type 'text/plain';
1244+
content_by_lua_block { ngx.status = 201 ngx.say("foo") ngx.exit(201) }
1245+
more_clear_headers Date;
1246+
}
1247+
}
1248+
--- config
1249+
server_tokens off;
1250+
lua_ssl_trusted_certificate ../../cert/test.crt;
1251+
1252+
location /t {
1253+
content_by_lua_block {
1254+
do
1255+
local sock = ngx.socket.tcp()
1256+
1257+
sock:settimeout(2000)
1258+
1259+
local ok, err = sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock")
1260+
if not ok then
1261+
ngx.say("failed to connect: ", err)
1262+
return
1263+
end
1264+
1265+
ngx.say("connected: ", ok)
1266+
1267+
local sess, err = sock:sslhandshake(nil, "test.com", true)
1268+
if not sess then
1269+
ngx.say("failed to do SSL handshake: ", err)
1270+
return
1271+
end
1272+
1273+
ngx.say("ssl handshake: ", type(sess))
1274+
1275+
local req = "GET /foo HTTP/1.0\r\nHost: test.com\r\nConnection: close\r\n\r\n"
1276+
local bytes, err = sock:send(req)
1277+
if not bytes then
1278+
ngx.say("failed to send http request: ", err)
1279+
return
1280+
end
1281+
1282+
ngx.say("sent http request: ", bytes, " bytes.")
1283+
1284+
while true do
1285+
local line, err = sock:receive()
1286+
if not line then
1287+
-- ngx.say("failed to receive response status line: ", err)
1288+
break
1289+
end
1290+
1291+
ngx.say("received: ", line)
1292+
end
1293+
1294+
local ok, err = sock:close()
1295+
ngx.say("close: ", ok, " ", err)
1296+
end -- do
1297+
-- collectgarbage()
1298+
}
1299+
}
1300+
1301+
--- request
1302+
GET /t
1303+
--- response_body
1304+
connected: 1
1305+
ssl handshake: userdata
1306+
sent http request: 56 bytes.
1307+
received: HTTP/1.1 201 Created
1308+
received: Server: nginx
1309+
received: Content-Type: text/plain
1310+
received: Content-Length: 4
1311+
received: Connection: close
1312+
received:
1313+
received: foo
1314+
close: 1 nil
1315+
1316+
--- error_log
1317+
SNI is test.com
1318+
1319+
--- no_error_log
1320+
[error]
1321+
[alert]

0 commit comments

Comments
 (0)