Skip to content

Instantly share code, notes, and snippets.

@halfcrazy
Created February 28, 2019 08:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halfcrazy/96a3fb84d44d0e0d6a0759890466773b to your computer and use it in GitHub Desktop.
Save halfcrazy/96a3fb84d44d0e0d6a0759890466773b to your computer and use it in GitHub Desktop.
ssl_certificate_by_lua server_port
diff -ruN openresty-1.13.6.2/bundle/lua-resty-core-0.1.15/lib/ngx/ssl.lua openresty-1.13.6.2-patched/bundle/lua-resty-core-0.1.15/lib/ngx/ssl.lua
--- openresty-1.13.6.2/bundle/lua-resty-core-0.1.15/lib/ngx/ssl.lua 2018-04-23 02:47:22.000000000 +0800
+++ openresty-1.13.6.2-patched/bundle/lua-resty-core-0.1.15/lib/ngx/ssl.lua 2019-02-28 16:13:56.422720600 +0800
@@ -38,6 +38,9 @@
int ngx_http_lua_ffi_ssl_server_name(ngx_http_request_t *r, char **name,
size_t *namelen, char **err);
+int ngx_http_lua_ffi_ssl_server_port(ngx_http_request_t *r,
+ int *port, char **err);
+
int ngx_http_lua_ffi_ssl_raw_client_addr(ngx_http_request_t *r, char **addr,
size_t *addrlen, int *addrtype, char **err);
@@ -162,6 +165,25 @@
end
if rc == FFI_DECLINED then
+ return nil
+ end
+
+ return nil, ffi_str(errmsg[0])
+end
+
+
+function _M.server_port()
+ local r = getfenv(0).__ngx_req
+ if not r then
+ error("no request found")
+ end
+
+ local rc = C.ngx_http_lua_ffi_ssl_server_port(r, intp, errmsg)
+ if rc == FFI_OK then
+ return intp[0]
+ end
+
+ if rc == FFI_DECLINED then
return nil
end
diff -ruN openresty-1.13.6.2/bundle/ngx_lua-0.10.13/src/ngx_http_lua_ssl_certby.c openresty-1.13.6.2-patched/bundle/ngx_lua-0.10.13/src/ngx_http_lua_ssl_certby.c
--- openresty-1.13.6.2/bundle/ngx_lua-0.10.13/src/ngx_http_lua_ssl_certby.c 2018-04-23 02:41:56.000000000 +0800
+++ openresty-1.13.6.2-patched/bundle/ngx_lua-0.10.13/src/ngx_http_lua_ssl_certby.c 2019-02-28 16:34:09.310299735 +0800
@@ -880,6 +880,39 @@
int
+ngx_http_lua_ffi_ssl_server_port(ngx_http_request_t *r,
+ int *port, char **err)
+{
+ ngx_ssl_conn_t *ssl_conn;
+ ngx_connection_t *c;
+ int p;
+ if (r->connection == NULL || r->connection->ssl == NULL) {
+ *err = "bad request";
+ return NGX_ERROR;
+ }
+ ssl_conn = r->connection->ssl->connection;
+ if (ssl_conn == NULL) {
+ *err = "bad ssl conn";
+ return NGX_ERROR;
+ }
+
+ c = ngx_ssl_get_connection(ssl_conn);
+
+ if (ngx_connection_local_sockaddr(c, NULL, 0) != NGX_OK) {
+ return 0;
+ }
+
+ p = ngx_inet_get_port(c->local_sockaddr);
+
+ if (p > 0 && p < 65536) {
+ *port = p;
+ }
+
+ return NGX_OK;
+}
+
+
+int
ngx_http_lua_ffi_ssl_raw_client_addr(ngx_http_request_t *r, char **addr,
size_t *addrlen, int *addrtype, char **err)
{
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment