Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filter available? #213

Open
MageekChiu opened this issue Oct 19, 2020 · 5 comments
Open

filter available? #213

MageekChiu opened this issue Oct 19, 2020 · 5 comments

Comments

@MageekChiu
Copy link

MageekChiu commented Oct 19, 2020

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!

@toremick
Copy link

toremick commented Jun 8, 2023

Any solution to this?

@oowl
Copy link
Contributor

oowl commented Jun 8, 2023

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

@toremick
Copy link

toremick commented Jun 8, 2023

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.

@oowl
Copy link
Contributor

oowl commented Jun 8, 2023

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.

@toremick
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants