From 9fc57423db0cc291809dbd62ffa8842d3d319f56 Mon Sep 17 00:00:00 2001 From: Scott Joseph Spitler II Date: Mon, 11 Dec 2023 22:53:41 -0500 Subject: [PATCH 1/2] Fixed pubMsg when WillTopic is null Previously the broker would run and compile, but would throw a runtime panic if the will was null because of GoLang's inline struct operator. Should REALLY consider adding a unit test --- broker/broker.go | 12 ++++++++---- broker/client.go | 14 +++++++++----- broker/http.go | 10 ++++++---- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/broker/broker.go b/broker/broker.go index f798818..267be0e 100644 --- a/broker/broker.go +++ b/broker/broker.go @@ -407,16 +407,20 @@ func (b *Broker) handleConnection(typ int, conn net.Conn) error{ } } b.clients.Store(cid, c) + log.Warn("trying to build publish packet..\n") + + var pubPack = PubPacket{} + if willmsg != nil { + pubPack.TopicName = info.willMsg.TopicName + pubPack.Payload = info.willMsg.Payload + } pubInfo := Info{ ClientID: info.clientID, Username: info.username, Password: info.password, Keepalive: info.keepalive, - WillMsg: &PubPacket{ - TopicName: info.willMsg.TopicName, - Payload: info.willMsg.Payload, - }, + WillMsg: pubPack, } b.OnlineOfflineNotification(pubInfo, true, c.lastMsgTime) diff --git a/broker/client.go b/broker/client.go index 5926466..223efc9 100644 --- a/broker/client.go +++ b/broker/client.go @@ -122,7 +122,7 @@ type Info struct { Username string `json:"username"` Password []byte `json:"password"` Keepalive uint16 `json:"keepalive"` - WillMsg *PubPacket `json:"willMsg"` + WillMsg PubPacket `json:"willMsg"` } type route struct { @@ -859,15 +859,19 @@ func (c *client) Close() { if c.typ == CLIENT { b.BroadcastUnSubscribe(unSubTopics) + + var pubPack = PubPacket{} + if c.info.willMsg != nil { + pubPack.TopicName = c.info.willMsg.TopicName + pubPack.Payload = c.info.willMsg.Payload + } + pubInfo := Info{ ClientID: c.info.clientID, Username: c.info.username, Password: c.info.password, Keepalive: c.info.keepalive, - WillMsg: &PubPacket{ - TopicName: c.info.willMsg.TopicName, - Payload: c.info.willMsg.Payload, - }, + WillMsg: pubPack, } //offline notification b.OnlineOfflineNotification(pubInfo, false, c.lastMsgTime) diff --git a/broker/http.go b/broker/http.go index 5055f10..c99f4ff 100644 --- a/broker/http.go +++ b/broker/http.go @@ -37,6 +37,11 @@ func InitHTTPMoniter(b *Broker) { conns := make([]ConnClient, 0) b.clients.Range(func (k, v interface{}) bool { cl, _ := v.(*client) + var pubPack = PubPacket{} + if cl.info.willMsg != nil { + pubPack.TopicName = cl.info.willMsg.TopicName + pubPack.Payload = cl.info.willMsg.Payload + } msg := ConnClient{ Info: Info{ @@ -44,10 +49,7 @@ func InitHTTPMoniter(b *Broker) { Username: cl.info.username, Password: cl.info.password, Keepalive: cl.info.keepalive, - WillMsg: &PubPacket{ - TopicName: cl.info.willMsg.TopicName, - Payload: cl.info.willMsg.Payload, - }, + WillMsg: pubPack, }, LastMsgTime: cl.lastMsgTime, } From cdff42698ac1111749a6840e8cf2b42e8dd19fb4 Mon Sep 17 00:00:00 2001 From: Scott Joseph Spitler II Date: Mon, 11 Dec 2023 23:04:24 -0500 Subject: [PATCH 2/2] Removed un-needed log line --- broker/broker.go | 1 - 1 file changed, 1 deletion(-) diff --git a/broker/broker.go b/broker/broker.go index 267be0e..cf772c9 100644 --- a/broker/broker.go +++ b/broker/broker.go @@ -407,7 +407,6 @@ func (b *Broker) handleConnection(typ int, conn net.Conn) error{ } } b.clients.Store(cid, c) - log.Warn("trying to build publish packet..\n") var pubPack = PubPacket{} if willmsg != nil {