@@ -143,13 +143,16 @@ static void ngx_stream_lua_ssl_handshake_handler(ngx_connection_t *c);
143143static int ngx_stream_lua_ssl_free_session (lua_State * L );
144144#endif
145145static void ngx_stream_lua_socket_tcp_close_connection (ngx_connection_t * c );
146-
146+ #if (NGX_HAVE_TRANSPARENT_PROXY )
147+ static void ngx_stream_lua_inject_socket_option_consts (lua_State * L );
148+ #endif
147149
148150enum {
149151 SOCKET_CTX_INDEX = 1 ,
150152 SOCKET_TIMEOUT_INDEX = 2 ,
151153 SOCKET_KEY_INDEX = 3 ,
152- SOCKET_BIND_INDEX = 4 /* only in upstream cosocket */
154+ SOCKET_BIND_INDEX = 4 , /* only in upstream cosocket */
155+ SOCKET_IP_TRANSPARENT_INDEX = 5
153156};
154157
155158
@@ -205,11 +208,24 @@ static char ngx_stream_lua_ssl_session_metatable_key;
205208#endif
206209
207210
211+ static void
212+ ngx_stream_lua_inject_socket_option_consts (lua_State * L )
213+ {
214+ /* {{{ socket option constants */
215+ #if (NGX_HAVE_TRANSPARENT_PROXY )
216+ lua_pushinteger (L , NGX_STREAM_LUA_SOCKET_OPTION_TRANSPARENT );
217+ lua_setfield (L , -2 , "IP_TRANSPARENT" );
218+ #endif
219+ }
220+
221+
208222void
209223ngx_stream_lua_inject_socket_tcp_api (ngx_log_t * log , lua_State * L )
210224{
211225 ngx_int_t rc ;
212226
227+ ngx_stream_lua_inject_socket_option_consts (L );
228+
213229 lua_createtable (L , 0 , 3 /* nrec */ ); /* ngx.socket */
214230
215231 lua_pushcfunction (L , ngx_stream_lua_socket_tcp );
@@ -641,6 +657,17 @@ ngx_stream_lua_socket_tcp_connect(lua_State *L)
641657 u -> peer .local = local ;
642658 }
643659
660+ #if (NGX_HAVE_TRANSPARENT_PROXY )
661+ lua_rawgeti (L , 1 , SOCKET_IP_TRANSPARENT_INDEX );
662+
663+ if (lua_tointeger (L , -1 ) > 0 ) {
664+ pc -> transparent = 1 ;
665+ ngx_log_debug0 (NGX_LOG_DEBUG_STREAM , s -> connection -> log , 0 ,
666+ "stream lua set TCP upstream with IP_TRANSPARENT" );
667+ }
668+ lua_pop (L , 1 );
669+ #endif
670+
644671 lua_rawgeti (L , 1 , SOCKET_TIMEOUT_INDEX );
645672 timeout = (ngx_int_t ) lua_tointeger (L , -1 );
646673 lua_pop (L , 1 );
@@ -2567,8 +2594,49 @@ ngx_stream_lua_socket_tcp_close(lua_State *L)
25672594static int
25682595ngx_stream_lua_socket_tcp_setoption (lua_State * L )
25692596{
2570- /* TODO */
2571- return 0 ;
2597+ ngx_stream_session_t * s ;
2598+ ngx_stream_lua_ctx_t * ctx ;
2599+ int n ;
2600+ int option ;
2601+
2602+ n = lua_gettop (L );
2603+
2604+ if (n < 2 ) {
2605+ return luaL_error (L , "ngx.socket setoption: expecting 2 or 3 "
2606+ "arguments (including the object) but seen %d" ,
2607+ lua_gettop (L ));
2608+ }
2609+
2610+ s = ngx_stream_lua_get_session (L );
2611+ if (s == NULL ) {
2612+ return luaL_error (L , "no request found" );
2613+ }
2614+
2615+ ctx = ngx_stream_get_module_ctx (s , ngx_stream_lua_module );
2616+ if (ctx == NULL ) {
2617+ return luaL_error (L , "no ctx found" );
2618+ }
2619+
2620+ ngx_stream_lua_check_context (L , ctx , NGX_STREAM_LUA_CONTEXT_CONTENT
2621+ | NGX_STREAM_LUA_CONTEXT_TIMER );
2622+
2623+ luaL_checktype (L , 1 , LUA_TTABLE );
2624+
2625+ option = luaL_checkint (L , 2 );
2626+
2627+ switch (option ) {
2628+ #if (NGX_HAVE_TRANSPARENT_PROXY )
2629+ case NGX_STREAM_LUA_SOCKET_OPTION_TRANSPARENT :
2630+ lua_rawseti (L , 1 , SOCKET_IP_TRANSPARENT_INDEX );
2631+ lua_pushboolean (L , 1 );
2632+ break ;
2633+ #endif
2634+ default :
2635+ return luaL_error (L , "invalid tcp socket option: %d" , option );
2636+
2637+ }
2638+
2639+ return 1 ;
25722640}
25732641
25742642
0 commit comments