stream-lua-nginx-module icon indicating copy to clipboard operation
stream-lua-nginx-module copied to clipboard

filter available?

Open MageekChiu opened this issue 5 years ago • 5 comments

is there anything like a filter in stream-lua-nginx-module?

like this: https://nginx.org/en/docs/stream/ngx_stream_js_module.html#js_filter

in this filter we can do something with each packet.

thank you!

MageekChiu avatar Oct 19 '20 03:10 MageekChiu

Any solution to this?

toremick avatar Jun 08 '23 07:06 toremick

Openresty gives a lua API https://github.com/openresty/stream-lua-nginx-module/blob/master/README.md?plain=1#L82 which can get raw downstream in lua coroutine, So maybe it's a better way to meet your need. @toremick @MageekChiu

oowl avatar Jun 08 '23 08:06 oowl

Openresty gives a lua API https://github.com/openresty/stream-lua-nginx-module/blob/master/README.md?plain=1#L82 which can get raw downstream in lua coroutine, So maybe it's a better way to meet your need. @toremick @MageekChiu

Hi @oowl That's not what i at least am trying to accomplish. I need to send a string upstream on the first connect. No use for the downstream.

in njs i can do this like:

function inject_string(s)  
{
  s.on('upstream', function(data, flags) 
  {
    s.sendUpstream("My string\n");
    s.off('upstream');
  });
}

and then use js_filter inject_string; in nginx it will on a new connection send my string by using filter, then turn off filter so the next data in the stream will continue as usual.

toremick avatar Jun 08 '23 10:06 toremick

I have read njs doc. Yeah, it seems openresty can not inject some content in proxy, But openresty ngx.req.socket can get raw downstream socket in lua coroutine, this means you can combine openresty cosocket to do anything you want

local downstream = ngx.tcp.socket(true)
local upstream = ngx.tcp.socket()
upstream:connect("1.1.1.1:8080")
local first_process = false
while true do
  local line, err, partial = downstream:receive()
  if not line then
    return
  end
  if not first_process then
      upstream.send("My string\n")
      first_process = true
  end
  upstream.send(line)
end

Of course, this bypasses a lot of configurations in nginx, such as the upstream, but it is feasible in openresty.

oowl avatar Jun 08 '23 15:06 oowl

I have read njs doc. Yeah, it seems openresty can not inject some content in proxy, But openresty ngx.req.socket can get raw downstream socket in lua coroutine, this means you can combine openresty cosocket to do anything you want

local downstream = ngx.tcp.socket(true)
local upstream = ngx.tcp.socket()
upstream:connect("1.1.1.1:8080")
local first_process = false
while true do
  local line, err, partial = downstream:receive()
  if not line then
    return
  end
  if not first_process then
      upstream.send("My string\n")
      first_process = true
  end
  upstream.send(line)
end

Of course, this bypasses a lot of configurations in nginx, such as the upstream, but it is feasible in openresty.

Thanks for the input, but it will not work very good i guess, since i need traffic both ways etc. I think proxy_pass is a bit more too it than that.

toremick avatar Jun 12 '23 10:06 toremick