Skip to content

Commit 719d638

Browse files
authored
feat: add GM C API enable/disable ntls (#69)
1 parent acda31f commit 719d638

File tree

5 files changed

+343
-21
lines changed

5 files changed

+343
-21
lines changed

lib/resty/apisix/ssl.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ ffi.cdef[[
1414
typedef intptr_t ngx_flag_t;
1515
int ngx_http_apisix_set_gm_cert(void *r, void *cdata, char **err, ngx_flag_t type);
1616
int ngx_http_apisix_set_gm_priv_key(void *r, void *cdata, char **err, ngx_flag_t type);
17+
int ngx_http_apisix_enable_ntls(void *r, int enabled);
1718
]]
1819

1920

@@ -62,4 +63,24 @@ function _M.set_gm_priv_key(enc_pkey, sign_pkey)
6263
end
6364

6465

66+
function _M.enable_ntls()
67+
local r = get_request()
68+
if not r then
69+
error("no request found")
70+
end
71+
72+
C.ngx_http_apisix_enable_ntls(r, 1)
73+
end
74+
75+
76+
function _M.disable_ntls()
77+
local r = get_request()
78+
if not r then
79+
error("no request found")
80+
end
81+
82+
C.ngx_http_apisix_enable_ntls(r, 0)
83+
end
84+
85+
6586
return _M

patch/1.21.4/nginx-enable_ntls.patch

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
diff --git src/http/ngx_http_request.c src/http/ngx_http_request.c
2-
index 013b715..a729693 100644
2+
index 013b715..96be553 100644
33
--- src/http/ngx_http_request.c
44
+++ src/http/ngx_http_request.c
5-
@@ -754,6 +754,11 @@ ngx_http_ssl_handshake(ngx_event_t *rev)
5+
@@ -8,6 +8,9 @@
6+
#include <ngx_config.h>
7+
#include <ngx_core.h>
8+
#include <ngx_http.h>
9+
+#if (NGX_HTTP_APISIX)
10+
+#include <ngx_http_apisix_module.h>
11+
+#endif
12+
13+
14+
static void ngx_http_wait_request_handler(ngx_event_t *ev);
15+
@@ -754,6 +757,12 @@ ngx_http_ssl_handshake(ngx_event_t *rev)
616
return;
717
}
818

919
+#if (TONGSUO_VERSION_NUMBER && NGX_HTTP_APISIX)
10-
+ // FIXME: add option later
11-
+ SSL_enable_ntls(c->ssl->connection);
20+
+ if (ngx_http_apisix_is_ntls_enabled(hc->conf_ctx)) {
21+
+ SSL_enable_ntls(c->ssl->connection);
22+
+ }
1223
+#endif
1324
+
1425
ngx_reusable_connection(c, 0);

src/ngx_http_apisix_module.c

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,18 @@
99
#define NGX_HTTP_APISIX_SSL_SIGN 2
1010

1111

12+
typedef struct {
13+
ngx_flag_t enable_ntls;
14+
} ngx_http_apisix_main_conf_t;
15+
16+
1217
static ngx_str_t remote_addr = ngx_string("remote_addr");
1318
static ngx_str_t remote_port = ngx_string("remote_port");
1419
static ngx_str_t realip_remote_addr = ngx_string("realip_remote_addr");
1520
static ngx_str_t realip_remote_port = ngx_string("realip_remote_port");
1621

1722

23+
static void *ngx_http_apisix_create_main_conf(ngx_conf_t *cf);
1824
static void *ngx_http_apisix_create_loc_conf(ngx_conf_t *cf);
1925
static char *ngx_http_apisix_merge_loc_conf(ngx_conf_t *cf, void *parent,
2026
void *child);
@@ -36,7 +42,7 @@ static ngx_http_module_t ngx_http_apisix_module_ctx = {
3642
NULL, /* preconfiguration */
3743
NULL, /* postconfiguration */
3844

39-
NULL, /* create main configuration */
45+
ngx_http_apisix_create_main_conf, /* create main configuration */
4046
NULL, /* init main configuration */
4147

4248
NULL, /* create server configuration */
@@ -63,6 +69,26 @@ ngx_module_t ngx_http_apisix_module = {
6369
};
6470

6571

72+
static void *
73+
ngx_http_apisix_create_main_conf(ngx_conf_t *cf)
74+
{
75+
ngx_http_apisix_main_conf_t *acf;
76+
77+
acf = ngx_pcalloc(cf->pool, sizeof(ngx_http_apisix_main_conf_t));
78+
if (acf == NULL) {
79+
return NULL;
80+
}
81+
82+
/*
83+
* set by ngx_pcalloc():
84+
*
85+
* acf->enable_ntls = 0;
86+
*/
87+
88+
return acf;
89+
}
90+
91+
6692
static void *
6793
ngx_http_apisix_create_loc_conf(ngx_conf_t *cf)
6894
{
@@ -778,3 +804,24 @@ ngx_http_apisix_set_gm_priv_key(ngx_http_request_t *r,
778804

779805
#endif
780806
}
807+
808+
809+
int
810+
ngx_http_apisix_enable_ntls(ngx_http_request_t *r, int enabled)
811+
{
812+
ngx_http_apisix_main_conf_t *acf;
813+
814+
acf = ngx_http_get_module_main_conf(r, ngx_http_apisix_module);
815+
acf->enable_ntls = enabled;
816+
return NGX_OK;
817+
}
818+
819+
820+
ngx_flag_t
821+
ngx_http_apisix_is_ntls_enabled(ngx_http_conf_ctx_t *conf_ctx)
822+
{
823+
ngx_http_apisix_main_conf_t *acf;
824+
825+
acf = ngx_http_get_module_main_conf(conf_ctx, ngx_http_apisix_module);
826+
return acf->enable_ntls;
827+
}

src/ngx_http_apisix_module.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,7 @@ void ngx_http_apisix_mark_request_header_set(ngx_http_request_t *r);
5555
ngx_int_t ngx_http_apisix_is_header_filter_by_lua_skipped(ngx_http_request_t *r);
5656
ngx_int_t ngx_http_apisix_is_body_filter_by_lua_skipped(ngx_http_request_t *r);
5757

58+
ngx_flag_t ngx_http_apisix_is_ntls_enabled(ngx_http_conf_ctx_t *conf_ctx);
59+
5860

5961
#endif /* _NGX_HTTP_APISIX_H_INCLUDED_ */

0 commit comments

Comments
 (0)