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

[question] Parsing a packet that come from old mqtt client version #902

Open
gmnprada opened this issue Oct 15, 2023 · 5 comments
Open

[question] Parsing a packet that come from old mqtt client version #902

gmnprada opened this issue Oct 15, 2023 · 5 comments
Labels

Comments

@gmnprada
Copy link

Dunno this may be result on bug that can be produced or not.

today I refactor my old server code that previously use mqtt.js connected into mosquitto broker

the old client protocol should be set to protocolVersion : 3 ,(the default is 4) on mqtt.js to be able parse the packet correctly.
previously it use to broke a connection from tcp socket into frontend ws socket its working fine.

when I finished the aedes broker to run it the connection established fine and the broker can subscribe to some topic.

until I notice a log in some line inform unknown packet,
when I debug that the packet.payload is truncated (Some header message is gone)

the usual server code if mosquitto as a broker , the server code just a regular mqtt.js client.

// read the opcode at first index 
const opcode = packet.payload.readUint(0);

if(opcode == 1){
   doSomething();
}

// ignore the client and broadcast to other client and server
let dcbuffer = Buffer.from(dcopcode);
publish("DisconnectBroadcast",dcbuffer);

my question is how I can handle properly the client that still using protocol version 3 in aedes?
I try to use mqtt-packet which aedes-packet use under the hood to identify the client
something like this hooks in pre-connect.

    function pre_connect(client, packet, cb) {
        info_log(`[Pre_Connect] Client`,client);
        info_log(`[Pre_Connect] Packet`,packet);

        // mqtt packet version 3 handle the legacy client , make a session , inform them , and close the socket later
        const parser = Parser({ protocolVersion: 3 });
        packet = parser.parse(packet);

        // This One Incorrectly Parse my old users that still using old mosquitto clients 
        // some buffer header is truncated and looks like If I see aedes-packet source its used the default too from mqtt-packet 
        const parser4 = Parser({ protocolVersion: 4 });
        packet = parser.parse(packet);

        cb(null,true);
    }  

or is there a opts function that I can pass to aedes like on mqtt-packet?

@robertsLando
Copy link
Member

I think the question is more related to mqtt-packet parser then aedes itself, would you mind to open a PR there?

@gmnprada
Copy link
Author

no the question is more related to aedes after I do much digging on this huge separated module I end up seeing how aedes parse packet in aedes-protocol-decoder in top of tcp that use the mqtt-packet parser. there is nothing to with mqtt-packet its can parse it.

if I do pass a trust proxy and seeing the raw buffer on top of tcp I do had that complete header packet , but aedes side like missing message-id that crash the whole program at node runtime , what I want to ask here is how the current flow aedes do broking the packet from its huge repo.

the aedes protocol decoder its says

if the object contains data property, it will be parsed as an [mqtt-packet](https://github.com/mqttjs/mqtt-packet).

having a handle and opts in that decoder for each mqtt protocol version on handling the flow of data in aedes is what I need here to not crash the entire flow of callback / middleware that's next need to do the chain of calls handling the stream.

its will be convenience also if the broker handle for that different version of mqtt.

something like this

TCP Stream -> MQTT -> parseAsV3, parseAsV4, parseAsV5 -> Packet (To High level handler) or subscription function handler.

@robertsLando
Copy link
Member

Try to submit a PR on aedes that implements what you want because it's not 100% clear to me what you want to do here

@gmnprada
Copy link
Author

I fork and create a commit on some test at aedes-protocol-decoder , cannot submit a PR now (need to read some mqtt specification clearly) before doing that implementation to solve current running problems.

the commit link https://github.com/madepriambhada/aedes-protocol-decoder/commit/f33a7e80f1e0ce9b278dff7b12a26da8ef10eb13

will do that soon after I correctly can identify the old client program some user use to connect into broker.

@getlarge
Copy link
Member

getlarge commented Nov 11, 2023

Hi @madepriambhada, I am not certain that I got what you are asking for ( don't want to be rude, but maybe you can use a good translation app ? ) but I will give it a try.
If you check this part of Aedes when connection packet is handled, you can observe how the packet .protocolVersion is assigned to client.version. In preConnect hook this assignment is not yet made, but you can still check the packet.protocolVersion at that stage and add your custom logic.

For the aedes-protocol-decoder integration, you can have a look at aedes-server-factory to see how to use it or even simpler directly use that library!

Hopefully that will help.

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

No branches or pull requests

3 participants