You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@trafficserver.apache.org by "shukitchan (via GitHub)" <gi...@apache.org> on 2025/11/09 13:57:09 UTC
[PR] Add support for proxy protocol info for lua plugin [trafficserver]
shukitchan opened a new pull request, #12651:
URL: https://github.com/apache/trafficserver/pull/12651
This pull request adds support for accessing PROXY protocol information in the Lua plugin API, enabling Lua scripts to retrieve details such as version, source/destination addresses and ports, protocol family, and socket type from incoming connections. The changes include new Lua API functions, updates to documentation, and comprehensive tests to ensure correct behavior with and without PROXY protocol.
**Lua API enhancements for PROXY protocol:**
* Added new Lua API functions `ts.client_request.get_pp_info` and `ts.client_request.get_pp_info_int` for retrieving PROXY protocol information as string or integer values, respectively. Constants for key types (e.g., `TS_LUA_PP_INFO_VERSION`, `TS_LUA_PP_INFO_SRC_ADDR`) are also exposed to Lua. (`plugins/lua/ts_lua_client_request.cc`, [[1]](diffhunk://#diff-2ba4d71a58c393bc7b54367d933fcb20e045fa7ac1dd8376fbfee8461e6813dfR23-R40) [[2]](diffhunk://#diff-2ba4d71a58c393bc7b54367d933fcb20e045fa7ac1dd8376fbfee8461e6813dfR97-R100) [[3]](diffhunk://#diff-2ba4d71a58c393bc7b54367d933fcb20e045fa7ac1dd8376fbfee8461e6813dfR121) [[4]](diffhunk://#diff-2ba4d71a58c393bc7b54367d933fcb20e045fa7ac1dd8376fbfee8461e6813dfR1165-R1253)
* Updated the Lua plugin documentation to describe the new API functions, their usage, and example code snippets. (`doc/admin-guide/plugins/lua.en.rst`, [doc/admin-guide/plugins/lua.en.rstR1259-R1327](diffhunk://#diff-0739161b3c546929527676380ebcbd47545e14a305b63013b37f951ffcb63f89R1259-R1327))
**Testing and validation:**
* Added a Lua script (`proxy_protocol.lua`) that demonstrates usage of the new API by logging PROXY protocol details and setting a custom header. (`tests/gold_tests/pluginTest/lua/proxy_protocol.lua`, [tests/gold_tests/pluginTest/lua/proxy_protocol.luaR1-R62](diffhunk://#diff-36103cb1e20949da419da360dbf71d4654a4972817cda842376aa698713a8638R1-R62))
* Introduced a new automated test (`lua_proxy_protocol.test.py`) to verify PROXY protocol support, checking for correct logging and header behavior both when PROXY protocol is present and absent. (`tests/gold_tests/pluginTest/lua/lua_proxy_protocol.test.py`, [tests/gold_tests/pluginTest/lua/lua_proxy_protocol.test.pyR1-R103](diffhunk://#diff-2ccf1c372b6246b6104d4bdfa07c62f3831b4604adbe2833913285c2d4d9f428R1-R103))
* Created a gold file for expected test output. (`tests/gold_tests/pluginTest/lua/gold/lua_proxy_protocol.gold`, [tests/gold_tests/pluginTest/lua/gold/lua_proxy_protocol.goldR1](diffhunk://#diff-308e87db1651bb627afe08d71104dade2ffacc576fe2f186c86f779dabcee5c1R1))
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add support for proxy protocol info for lua plugin [trafficserver]
Posted by "shukitchan (via GitHub)" <gi...@apache.org>.
shukitchan commented on PR #12651:
URL: https://github.com/apache/trafficserver/pull/12651#issuecomment-3517023344
[approve ci osx]
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add support for proxy protocol info for lua plugin [trafficserver]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #12651:
URL: https://github.com/apache/trafficserver/pull/12651#discussion_r2508072886
##########
tests/gold_tests/pluginTest/lua/lua_proxy_protocol.test.py:
##########
@@ -0,0 +1,103 @@
+'''
+Test Lua plugin PROXY protocol support
+'''
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import os
Review Comment:
Import of 'os' is not used.
```suggestion
```
##########
plugins/lua/ts_lua_client_request.cc:
##########
@@ -1139,3 +1162,92 @@ ts_lua_client_request_get_ssl_curve(lua_State *L)
return 1;
}
+
+static void
+ts_lua_inject_client_request_pp_info_api(lua_State *L)
+{
+ size_t i;
+
+ for (i = 0; i < sizeof(ts_lua_pp_info_key_vars) / sizeof(ts_lua_var_item); i++) {
+ lua_pushinteger(L, ts_lua_pp_info_key_vars[i].nvar);
+ lua_setglobal(L, ts_lua_pp_info_key_vars[i].svar);
+ }
+
+ lua_pushcfunction(L, ts_lua_client_request_get_pp_info);
+ lua_setfield(L, -2, "get_pp_info");
+
+ lua_pushcfunction(L, ts_lua_client_request_get_pp_info_int);
+ lua_setfield(L, -2, "get_pp_info_int");
+}
+
+static int
+ts_lua_client_request_get_pp_info(lua_State *L)
+{
+ uint16_t key;
+ const char *value = nullptr;
+ int length;
+ ts_lua_http_ctx *http_ctx;
+ TSHttpSsn ssnp;
+ TSVConn client_conn;
+
+ GET_HTTP_CONTEXT(http_ctx, L);
+
+ key = static_cast<uint16_t>(luaL_checkinteger(L, 1));
+
+ ssnp = TSHttpTxnSsnGet(http_ctx->txnp);
+ client_conn = TSHttpSsnClientVConnGet(ssnp);
+
+ if (TSVConnPPInfoGet(client_conn, key, &value, &length) == TS_SUCCESS) {
+ if (key == TS_PP_INFO_SRC_ADDR || key == TS_PP_INFO_DST_ADDR) {
+ // For addresses, convert sockaddr to string
+ char ip_str[INET6_ADDRSTRLEN];
+ const sockaddr *addr = reinterpret_cast<const sockaddr *>(value);
+ const sockaddr_in *addr_in;
+ const sockaddr_in6 *addr_in6;
+
+ if (addr->sa_family == AF_INET) {
+ addr_in = reinterpret_cast<const sockaddr_in *>(addr);
+ inet_ntop(AF_INET, &addr_in->sin_addr, ip_str, sizeof(ip_str));
+ } else if (addr->sa_family == AF_INET6) {
+ addr_in6 = reinterpret_cast<const sockaddr_in6 *>(addr);
Review Comment:
[nitpick] The variables `addr_in` and `addr_in6` are declared but not initialized. While they are assigned before use, modern C++ best practices recommend declaring variables at the point of initialization or as close to their use as possible. Consider moving the declarations to lines 1209 and 1212 respectively, combining declaration with initialization:
```cpp
const sockaddr_in *addr_in = reinterpret_cast<const sockaddr_in *>(addr);
```
and
```cpp
const sockaddr_in6 *addr_in6 = reinterpret_cast<const sockaddr_in6 *>(addr);
```
```suggestion
if (addr->sa_family == AF_INET) {
const sockaddr_in *addr_in = reinterpret_cast<const sockaddr_in *>(addr);
inet_ntop(AF_INET, &addr_in->sin_addr, ip_str, sizeof(ip_str));
} else if (addr->sa_family == AF_INET6) {
const sockaddr_in6 *addr_in6 = reinterpret_cast<const sockaddr_in6 *>(addr);
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add support for proxy protocol info for lua plugin [trafficserver]
Posted by "shukitchan (via GitHub)" <gi...@apache.org>.
shukitchan merged PR #12651:
URL: https://github.com/apache/trafficserver/pull/12651
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add support for proxy protocol info for lua plugin [trafficserver]
Posted by "shukitchan (via GitHub)" <gi...@apache.org>.
shukitchan commented on PR #12651:
URL: https://github.com/apache/trafficserver/pull/12651#issuecomment-3508708694
[approve ci]
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org