From f7466fd9683a22f81a786088ba10f7a3779bbdb6 Mon Sep 17 00:00:00 2001 From: andsel Date: Tue, 22 Sep 2015 15:29:02 +0200 Subject: [PATCH] Added check to avoid creation of LostConnectionEvents for not yet completed connects (fix issue #88) --- ChangeLog.txt | 1 + broker/config/password_file.conf | 2 +- .../server/netty/NettyMQTTHandler.java | 7 ++- broker/src/test/resources/log4j.properties | 1 - ...e88_reconnect_after_bad_credentials.groovy | 57 +++++++++++++++++++ 5 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 tools_scripts/integration/issue88_reconnect_after_bad_credentials.groovy diff --git a/ChangeLog.txt b/ChangeLog.txt index 48e6d0c8..cef57fa2 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -9,6 +9,7 @@ Version 0.8: Switched from commit persistence on every change to time based to fix delay in handling many msgs/sec (fix issue #79) Changed willMessage and password in CONNECT message from String to byte[] (fix issue #94 and #95) Fixed ugly empty session storage, avoid fake NullObject empty subscription subscription + Added check to avoid creation of LostConnectionEvents for not yet completed connects (fix issue #88) Version 0.7: Fix issue #38, added aligned to MQTT 3.1.1 diff --git a/broker/config/password_file.conf b/broker/config/password_file.conf index d7033982..ee2ee592 100644 --- a/broker/config/password_file.conf +++ b/broker/config/password_file.conf @@ -1,2 +1,2 @@ #the password is sha256(passwd) -testuser:36767030e79073783c8e9b299d9f8f1d4cd66baa82cd8869caad8dbb55204099 \ No newline at end of file +testuser:0d6be69b264717f2dd33652e212b173104b4a647b7c11ae72e9885f11cd312fb diff --git a/broker/src/main/java/org/eclipse/moquette/server/netty/NettyMQTTHandler.java b/broker/src/main/java/org/eclipse/moquette/server/netty/NettyMQTTHandler.java index be3b04ea..7c43a82f 100644 --- a/broker/src/main/java/org/eclipse/moquette/server/netty/NettyMQTTHandler.java +++ b/broker/src/main/java/org/eclipse/moquette/server/netty/NettyMQTTHandler.java @@ -22,6 +22,7 @@ import java.util.HashMap; import java.util.Map; import io.netty.handler.codec.CorruptedFrameException; +import org.apache.commons.codec.binary.StringUtils; import org.eclipse.moquette.spi.IMessaging; import org.eclipse.moquette.proto.Utils; import org.eclipse.moquette.proto.messages.AbstractMessage; @@ -70,7 +71,11 @@ public class NettyMQTTHandler extends ChannelInboundHandlerAdapter { @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { String clientID = (String) NettyUtils.getAttribute(ctx, NettyChannel.ATTR_KEY_CLIENTID); - m_messaging.lostConnection(clientID); + if (clientID != null && !clientID.isEmpty()) { + //if the channel was of a correctly connected client, inform messagin + //else it was of a not completed CONNECT message + m_messaging.lostConnection(clientID); + } ctx.close(/*false*/); } diff --git a/broker/src/test/resources/log4j.properties b/broker/src/test/resources/log4j.properties index 62fec888..38274c8a 100644 --- a/broker/src/test/resources/log4j.properties +++ b/broker/src/test/resources/log4j.properties @@ -1,5 +1,4 @@ # Set root logger level to DEBUG and its only appender to A1. -#log4j.rootLogger=ERROR, A1 log4j.rootLogger=ERROR, stdout, file #log4j.logger.org.eclipse.moquette=WARN diff --git a/tools_scripts/integration/issue88_reconnect_after_bad_credentials.groovy b/tools_scripts/integration/issue88_reconnect_after_bad_credentials.groovy new file mode 100644 index 00000000..329c9551 --- /dev/null +++ b/tools_scripts/integration/issue88_reconnect_after_bad_credentials.groovy @@ -0,0 +1,57 @@ +//@Grab(group='org.fusesource.mqtt-client', module='mqtt-client', version='1.10') +@GrabResolver(name='Paho', root="https://repo.eclipse.org/content/repositories/paho-releases/") +@Grab(group='org.eclipse.paho', module='org.eclipse.paho.client.mqttv3', version='1.0.2') + +import org.eclipse.paho.client.mqttv3.MqttClient +import org.eclipse.paho.client.mqttv3.MqttConnectOptions +import org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence +import org.eclipse.paho.client.mqttv3.MqttException + +MqttDefaultFilePersistence subDataStore = new MqttDefaultFilePersistence("/tmp/sub") +MqttClient subClient = new MqttClient("tcp://localhost:1883", "ConnectTestCLI", subDataStore) +MqttConnectOptions connectOptions = new MqttConnectOptions() +connectOptions.setUserName("gino") +connectOptions.setPassword("fake".toCharArray()) +connectOptions.setKeepAliveInterval(5) +println "Before connect with bad" +try { + subClient.connect(connectOptions) +} catch (MqttException mex) { + println "OK, catched connect with bad credentials, wait 4x keepalive" + sleep(20 * 1000) +} +println "Before connect with ok credentials" +connectOptions = new MqttConnectOptions() +connectOptions.setUserName("testuser") +connectOptions.setPassword("passwd".toCharArray()) +subClient.connect(connectOptions) +println "Successfull connected" + +subClient.disconnect() +println "Finished!" + +//import org.fusesource.mqtt.client.BlockingConnection +//import org.fusesource.mqtt.client.MQTT +//"Usage: groovy issue88_reconnect_after_bad_credentials.groovy" + +//MQTT mqtt = new MQTT() +//def host = "localhost" +//mqtt.setCleanSession(true) +//mqtt.setHost(host, 1883) +//mqtt.setUserName("gino") +//mqtt.setPassword("fake") +// +//mqtt.setClientId("BadConnecter_client") +//BlockingConnection connection = mqtt.blockingConnection() +//connection.connect() +// +////try a valid conn +//mqtt.setUserName("testuser") +//mqtt.setPassword("passwd") +//connection = mqtt.blockingConnection() +//connection.connect() +// +//connection.disconnect() +//println "exited" + +