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

Server factory integration #5

Merged
merged 25 commits into from
Oct 27, 2020

Conversation

getlarge
Copy link
Member

@getlarge getlarge commented Oct 14, 2020

  • Refactor the protocol decoder

    • change expected arguments ( to be used by aedes-server-factory instead of aedes)
    • split in several sub functions
    • export extractSocketDetails function, usable when not using / or trusting proxy
  • Update test, docs, examples and dependancies

related to aedes-changes and aedes-server-factory

README.md Outdated Show resolved Hide resolved
README.md Outdated
The purpose of this module is to be used inside [aedes](https://github.com/moscajs/aedes) `decodeProtocol` hook, which is called when aedes instance receives a first valid buffer from client ( before CONNECT packet). The client object state is in default and its connected state is false.
The function extract socket details and if aedes `trustProxy` option is set to true, it will first parse http headers (x-real-ip | x-forwarded-for) and proxy protocol (v1 and v2) to retrieve information in client.connDetails.
The purpose of this module is to be used inside [aedes-server-factory](https://github.com/moscajs/aedes-server-factory) `bindConnection` function, which is called when the server receives a connection from client ( before CONNECT packet). The client object state is in default and its connected state is false.
The function extract socket details and if aedes-server-factory `trustProxy` option is set to true, it will first parse http headers (x-real-ip | x-forwarded-for) and proxy protocol (v1 and v2) to retrieve information in client.connDetails.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The function extract socket details and if aedes-server-factory `trustProxy` option is set to true, it will first parse http headers (x-real-ip | x-forwarded-for) and proxy protocol (v1 and v2) to retrieve information in client.connDetails.
The function extract socket details and if aedes-server-factory `trustProxy` option is set to true, it will first parse http headers (x-real-ip | x-forwarded-for) and proxy protocol (v1 and v2) to retrieve information in `client.connDetails`.

README.md Outdated Show resolved Hide resolved
test.js Outdated Show resolved Hide resolved
@robertsLando
Copy link
Member

robertsLando commented Oct 15, 2020

@getlarge Something like:

function setup(options){
var broker;
var server;
var client;

 if(options.broker) {
 broker = aedes(options.broker)
}

if(options.client) {
 client = mqtt.connect(options.client)
}

if(server) {
  server = createServer(broker,options.server)
}

return {broker, client, server};
}

function close(setup) {
 if(client) client.end(true)
 if(broker) broker.close()
 if(server) server.close()
}

test('tcp clients have access to the ipAddress from the socket', function (t) {
  t.plan(2)

var setup = setup(....)

// code here 

t.tearDown(close.bind(t, setup);
})

@robertsLando
Copy link
Member

@getlarge seems tests are endless on nodejs 12 /14

@getlarge
Copy link
Member Author

@robertsLando yes i noticed, but what i don't understand is that prior to this commit, it was only node v10 which was hanging.

@getlarge
Copy link
Member Author

Also i tried to run tests locally with node v12 and this issue does not appear, so i'm kind of stuck with this right now.

@robertsLando
Copy link
Member

@getlarge From my experience with github actions, when weird things happen in tests the best is to disable parallel. LEt's see how this goes :) (already submitted a commit)

@robertsLando
Copy link
Member

@getlarge Seems stucked anyway. It stops here

# tcp proxied (protocol v1) clients buffer contains MQTT packet and proxy header
ok 15 no error
ok 16 no error
ok 17 should be equal

@robertsLando
Copy link
Member

robertsLando commented Oct 26, 2020

@mcollina Is there any change in nodejs 10 to 12-14 that could cause this stuck in tests?

It stucks in this test:

test('tcp proxied (protocol v1) clients buffer contains MQTT packet and proxy header', function (t) {

@getlarge
Copy link
Member Author

The last test in this suite is rather tricky because there are "many" sockets opened, since i tried to reproduce the behavior of a setup with a "real" proxy server.
So maybe a client | server connection could not be closed properly when test is ending ?

@robertsLando
Copy link
Member

robertsLando commented Oct 26, 2020

@getlarge Maybe I got it. I think it could be caused by some leaked handler (open socket). I suggest you to edit your tests in a way that close waits for close callbacks before calling end.

function close(setup, t) {

var toClose = 0
var closed = 0
 if(client) {
toClose++
client.end(true, onDone)
}
 if(broker) {
toClose++
broker.close(onDone)
}
 if(server) {
toClose++
server.close(onDone)
}

function onDone() {
if(toClose === ++closed)
t.end()
}

}

@getlarge
Copy link
Member Author

I will try.
What's really curious, is that the tests work locally with node v12.3.0 but not 12.19.0 (which is the version used in the CI).

@robertsLando
Copy link
Member

Also what you could try to do is to create a custom server destroy method as server.close never triggers the callback if there are opened sockets. Or you could you this module: https://github.com/isaacs/server-destroy

@robertsLando
Copy link
Member

What's really curious, is that the tests work locally with node v12.3.0 but not 12.19.0 (which is the version used in the CI).

No clue why but if test never ends just means some socket is left opened. So use this strategy and we will for sure find what's wrong here :)

@getlarge
Copy link
Member Author

The destroy method did not help, but it seems the error is caused by this block

  socket.on('end', function (data) {
    proxyClient.end(data, function () {
      proxyClient.connected = false
    })
  })

When commented the test end properly.

@getlarge
Copy link
Member Author

@robertsLando Any idea what that error means at coverall parallel phase ?

@robertsLando
Copy link
Member

No clue but try to re enable parallel, that wasn't the problem

@robertsLando
Copy link
Member

@getlarge lol, a typo :)

@robertsLando
Copy link
Member

When commented the test end properly.

If it's not needed, remove it

index.js Show resolved Hide resolved
@robertsLando
Copy link
Member

Aedes 0.44.0 is on npm

@robertsLando robertsLando requested a review from mcollina October 27, 2020 08:18
package.json Outdated Show resolved Hide resolved
package.json Show resolved Hide resolved
Copy link
Member

@robertsLando robertsLando left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Collaborator

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@robertsLando robertsLando merged commit cda32b3 into moscajs:master Oct 27, 2020
@robertsLando
Copy link
Member

@getlarge Should I release a 2.0.0?

@getlarge
Copy link
Member Author

@robertsLando If we strictly follow the semver, then yes you should probably go straight to 2.0.0.

@robertsLando
Copy link
Member

@getlarge Sorry for the delay, published on npm now

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

Successfully merging this pull request may close these issues.

3 participants