Skip to content

Server factory integration - #5

Merged
robertsLando merged 25 commits into
moscajs:masterfrom
getlarge:server-factory-integration
Oct 27, 2020
Merged

Server factory integration#5
robertsLando merged 25 commits into
moscajs:masterfrom
getlarge:server-factory-integration

Conversation

@getlarge

@getlarge getlarge commented Oct 14, 2020

Copy link
Copy Markdown
Member
  • 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

Comment thread README.md Outdated
Comment thread 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
Copy Markdown
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`.

Comment thread README.md Outdated
Comment thread test.js Outdated
@robertsLando

robertsLando commented Oct 15, 2020

Copy link
Copy Markdown
Member

@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
Copy Markdown
Member

@getlarge seems tests are endless on nodejs 12 /14

@getlarge

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

robertsLando commented Oct 26, 2020

Copy link
Copy Markdown
Member

@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
Copy Markdown
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

robertsLando commented Oct 26, 2020

Copy link
Copy Markdown
Member

@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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member Author

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

@robertsLando

Copy link
Copy Markdown
Member

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

@robertsLando

Copy link
Copy Markdown
Member

@getlarge lol, a typo :)

@robertsLando

Copy link
Copy Markdown
Member

When commented the test end properly.

If it's not needed, remove it

Comment thread index.js
@robertsLando

Copy link
Copy Markdown
Member

Aedes 0.44.0 is on npm

@robertsLando
robertsLando requested a review from mcollina October 27, 2020 08:18
Comment thread package.json Outdated
Comment thread package.json

@robertsLando robertsLando left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@mcollina mcollina left a comment

Copy link
Copy Markdown
Collaborator

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
Copy Markdown
Member

@getlarge Should I release a 2.0.0?

@getlarge

Copy link
Copy Markdown
Member Author

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

@robertsLando

Copy link
Copy Markdown
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