@@ -143,13 +143,16 @@ static void ngx_stream_lua_ssl_handshake_handler(ngx_connection_t *c);
143
143
static int ngx_stream_lua_ssl_free_session (lua_State * L );
144
144
#endif
145
145
static 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
147
149
148
150
enum {
149
151
SOCKET_CTX_INDEX = 1 ,
150
152
SOCKET_TIMEOUT_INDEX = 2 ,
151
153
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
153
156
};
154
157
155
158
@@ -205,11 +208,24 @@ static char ngx_stream_lua_ssl_session_metatable_key;
205
208
#endif
206
209
207
210
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
+
208
222
void
209
223
ngx_stream_lua_inject_socket_tcp_api (ngx_log_t * log , lua_State * L )
210
224
{
211
225
ngx_int_t rc ;
212
226
227
+ ngx_stream_lua_inject_socket_option_consts (L );
228
+
213
229
lua_createtable (L , 0 , 3 /* nrec */ ); /* ngx.socket */
214
230
215
231
lua_pushcfunction (L , ngx_stream_lua_socket_tcp );
@@ -641,6 +657,17 @@ ngx_stream_lua_socket_tcp_connect(lua_State *L)
641
657
u -> peer .local = local ;
642
658
}
643
659
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
+
644
671
lua_rawgeti (L , 1 , SOCKET_TIMEOUT_INDEX );
645
672
timeout = (ngx_int_t ) lua_tointeger (L , -1 );
646
673
lua_pop (L , 1 );
@@ -2567,8 +2594,49 @@ ngx_stream_lua_socket_tcp_close(lua_State *L)
2567
2594
static int
2568
2595
ngx_stream_lua_socket_tcp_setoption (lua_State * L )
2569
2596
{
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 ;
2572
2640
}
2573
2641
2574
2642
0 commit comments