lua-nginx-module
lua-nginx-module copied to clipboard
feature: add tcpsock:bind api
Just like the standard proxy_bind directive, this api makes the outgoing connection to a upstream server originate from the specified local IP address.
I complied the patch and used some simple scripts to test them, everything looks good.
(Not included in files) Additionally, I tested binding to a bad address, test failed (correct behavior)
I'll look into this as soon as I can manage :)
I do have one comment, but it is not directly related to sock:bind() but I found it while testing sock:bind(), so I am posting it here. This applies to all sock:* calls and is related to the luaL_error() call.
There is a different behavior for different types of errors.
For instance if I try to bind to the ip "A.B.C.D" I get a error as the 2nd return value of the call e.g. local ok, err = sock:bind("A.B.C.D");
Whereas if I put some garbage arguments on the end of the sock:bind() call e.g. local ok, err = sock:bind("127.0.0.1", 8888, 1, 2, 3); The error propagates directly to the logs/error.log level and the script is killed logs/error.log: 2016/05/11 13:24:26 [error] 3023#0: *1 lua entry thread aborted: runtime error: nginx/./locations/test.lua:23: ngx.socket bind: expecting at least 2 arguments (including the object) but seen 6 stack traceback: coroutine 0: [C]: in function 'bind'
It just seems like there should be only one error path
Anyways, not a big deal, but it's a problem I ran into, and this is my (lazy) way of documenting it :)
@agentzh @JakSprats
I'd like to add the IP_TRANSPARENT socket option.
Per my understanding I just need to set pc->transparent and nginx will take of the rest
where would be a better location to set this flag? adding a parameter to udp.setpeername() / tcp.connect() or to this new bind() api ?
@JakSprats, just for my understanding, why did you choose to implement a separate bind() and not do it all in one go with additional args to 'connect()` ?
Thanks!
@JakSprats We distinguish between bad argument errors due to API misuse and other kinds of errors. The former usually results in Lua exceptions so that the user cannot (easily) ignore it.
@alonbg I think we'd better add support for the transparent argument of the setoption method, similar to LuaSocket's same-name method:
http://w3.impa.br/~diego/software/luasocket/tcp.html#setoption
@agentzh Thanks! btw, for your reference, some time ago, I opened an issue@LuaSockets for this.
@agentzh Please, review and add this code into main branch. Almost two years have passed.
love to see the bind option merged as well, it is really usefull
What is the plan with this pull request? We would like to leverage bind() to ensure correct source IP for our requests.
This pull request is now in conflict :(