moscajs / aedes

Barebone MQTT broker that can run on any stream server, the node way
MIT License
1.77k stars 230 forks source link

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

Open gmnprada opened 10 months ago

gmnprada commented 10 months ago

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 commented 10 months ago

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

gmnprada commented 10 months ago

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 commented 10 months ago

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 commented 10 months ago

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 commented 9 months ago

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.