Skip to content

AWS IoT + WSS connection attempt results in "Bad handshake" after upgrade from 1.2.0 to 1.3.1 #479

@Brbb

Description

@Brbb

Hi,

I'm trying to connect AWS IoT through a wss URL created via v4.NewSigner which will result in something like `wss:///X-Amz-Token

wss://<URL>/mqtt?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=<credentials>&X-Amz-Date=20210125T115032Z&X-Amz-Expires=0&X-Amz-SignedHeaders=host&X-Amz-Signature=<signature>&X-Amz-Security-Token=<token>

This worked well all the way to v1.2.0 but then it stopped working with a simple "bad handshake". I've read there might be some issue for the buffered/unbuffered related changes but I'm not sure is related to my issue.

The code I have is super simple:

func NewClientWithConfig(awsSession *session.Session, logger *logger.Logger, clientOpts *pahoMqtt.ClientOptions) (*Client, error) {
	clientOpts.SetClientID(fmt.Sprintf("notifier-%v", time.Now().UnixNano())).
		SetMaxReconnectInterval(1 * time.Second).
		SetAutoReconnect(true).
		SetDefaultPublishHandler(func(client pahoMqtt.Client, msg pahoMqtt.Message) {
			logger.Println(msg)
		}).
		SetConnectionLostHandler(func(client pahoMqtt.Client, reason error) {
			logger.Fatalln("connection lost:", reason.Error())
		})

	wssURL, err := aws.SignedURL(awsSession)
	if err != nil {
		return nil, fmt.Errorf("error generating signed URL: %s", err.Error())
	}
	clientOpts.AddBroker(wssURL)
	clientOpts.SetOnConnectHandler(func(pahoMqtt.Client) {
                // printed up to 1.2.0, then never reached from 1.3.0
		logger.Println("connected")
	})

	return &Client{
		logger: logger,
		client: pahoMqtt.NewClient(clientOpts),
	}, nil
}

I start it like this and it never arrives to the for topic part

// Start establishes the connection with the MQTT broker and subscribe to all the configured topics
func (c *Client) Start() error {
	if token := c.client.Connect(); token.Wait() && token.Error() != nil {
		return token.Error()
	}
	for topic, th := range c.topicHandlers {
		if token := c.client.Subscribe(topic, th.Qos, th.Handler); token.Wait() && token.Error() != nil {
			return token.Error()
		}
	}

	return nil
}

I'm now wondering if the Signed URL is creating issues for some reasons (everything is up and reachable, I can pub/sub to topics in my previous version of the code).

Edit:
I tried with an online test websocket at ws://broker.emqx.io:8083/mqtt and I can connect. I start thinking the wss presigned URL might have some issues with the updated library.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions