Open raschiapedane opened 2 years ago
hi . I will review on this🧐.
Hello, i'm encountering (i guess) the same problem. My app crashes reporting:
CocoaMQTT(debug): Call the SSL/TLS manually validating function
2022-07-15 17:43:23.560407+0200 BookIt[8527:276038] API MISUSE: NSURLSession delegate _TtCC18CocoaMQTTWebSocket18CocoaMQTTWebSocket20FoundationConnection: <_TtCC18CocoaMQTTWebSocket18CocoaMQTTWebSocket20FoundationConnection: 0x60000114fa80> (0x60000114fa80)
2022-07-15 17:43:23.560928+0200 BookIt[8527:276038] API MISUSE: task:didReceiveChallenge:completionHandler: completion handler not called
In my setup, i'm using nginx as a reverse proxy to forward the mqtt websocket request to my internal container of a docker stack. Nginx is secured with SSL.
nginx.conf
...
listen 443;
location / mqtt{
proxy_pass http://mosquitto:8083;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
}
...
My setup should be fine as i can succesfully connect to the mosquitto broker using a javascript client (mqtt.js) with the following session:
mqttClient = mqtt.connect(`wss://myserver:443/mqtt`, options)
Finally i attach my instance of mqtt session in swift:
let websocket = CocoaMQTTWebSocket(uri: "/mqtt")
let topic = "my_topic"
let clientID = "cocoamqtt_\(String(ProcessInfo().processIdentifier))"
self.mqtt5 = CocoaMQTT5(clientID: clientID, host: GlobalConfiguration.apiUrl, port: UInt16(GlobalConfiguration.apiPort) ?? 8083, socket: websocket)
let connectProperties = MqttConnectProperties()
connectProperties.topicAliasMaximum = 0
connectProperties.sessionExpiryInterval = 30 * 1000
connectProperties.receiveMaximum = 100
connectProperties.maximumPacketSize = 500
if let mqtt5 = self.mqtt5 {
mqtt5.connectProperties = connectProperties
mqtt5.autoReconnect = true
mqtt5.autoReconnectTimeInterval = 1
mqtt5.enableSSL = true
mqtt5.logLevel = .debug
let _ = mqtt5.connect()
mqtt5.didConnectAck = { cocoaMQTT5, cocoaMQTTCONNACKReasonCode, mqttDecodeConnAck in
mqtt5.subscribe(topic, qos: CocoaMQTTQoS.qos1)
mqtt5.didReceiveMessage = { mqtt, message, id, publishData in
print(message)
}
}
}else{
print("An error arose while setting up mqtt5")
}
The bug is still present in 2.0.5 and manual override of func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge) is still needed to avoid app crashing!
Bug still present in 2.1.0
Bug still present in 2.1.3
Still present in 2.1.6. Is there a plan to fix this?
This worked for me without any manual override
import Foundation
import CocoaMQTT
import CocoaMQTTWebSocket
struct MyMqttConfig {
let clientID: String
let username: String
let password: String
let host: String
let port: Int
}
class MyMqttConnector {
let config: MyMqttConfig
init(_ config: MyMqttConfig) {
self.config = config
}
func connect() {
let websocket = CocoaMQTTWebSocket(uri: "/mqtt")
websocket.enableSSL = true
let mqtt = CocoaMQTT(clientID: config.clientID, host: config.host, port: UInt16(config.port), socket: websocket)
mqtt.username = config.username
mqtt.password = config.password
mqtt.enableSSL = true
mqtt.keepAlive = 30
mqtt.delegate = self
mqtt.cleanSession = true
let connect = mqtt.connect()
Print.d("connect: \(connect)")
}
}
extension MyMqttConnector: CocoaMQTTDelegate {
func mqtt(_ mqtt: CocoaMQTT, didReceive trust: SecTrust, completionHandler: @escaping (Bool) -> Void) {
completionHandler(true)
}
func mqtt(_ mqtt: CocoaMQTT, didConnectAck ack: CocoaMQTTConnAck) {
Print.d()
}
func mqtt(_ mqtt: CocoaMQTT, didPublishMessage message: CocoaMQTTMessage, id: UInt16) {
Print.d()
}
func mqtt(_ mqtt: CocoaMQTT, didPublishAck id: UInt16) {
Print.d()
}
func mqtt(_ mqtt: CocoaMQTT, didReceiveMessage message: CocoaMQTTMessage, id: UInt16) {
Print.d()
}
func mqtt(_ mqtt: CocoaMQTT, didSubscribeTopics success: NSDictionary, failed: [String]) {
Print.d()
}
func mqtt(_ mqtt: CocoaMQTT, didUnsubscribeTopics topics: [String]) {
Print.d()
}
func mqttDidPing(_ mqtt: CocoaMQTT) {
Print.d()
}
func mqttDidReceivePong(_ mqtt: CocoaMQTT) {
Print.d()
}
func mqttDidDisconnect(_ mqtt: CocoaMQTT, withError err: Error?) {
Print.d()
}
}
Still present in version "2.1.6"
Tested on:
let mqtt5 = CocoaMQTT5(clientID: options.clientId, host: options.host, port: options.port, socket: socket)
mqtt5.enableSSL = isSsl // true in my tests
mqtt5.allowUntrustCACertificate = options.allowUntrustCACertificate // false in my tests
mqtt5.sslSettings = [kCFStreamSSLPeerName as String: options.host as NSObject]
mqtt5.username = options.username // String?
mqtt5.password = options.password // String?
mqtt5.delegate = self
_ = mqtt5.connect()
API MISUSE: NSURLSession delegate _TtCC9CocoaMQTT18CocoaMQTTWebSocket20FoundationConnection: <_TtCC9CocoaMQTT18CocoaMQTTWebSocket20FoundationConnection: 0x600000d34d80> (0x600000d34d80)
API MISUSE: task:didReceiveChallenge:completionHandler: completion handler not called
nw_connection_add_timestamp_locked_on_nw_queue [C3] Hit maximum timestamp count, will start dropping events
nw_connection_add_timestamp_locked_on_nw_queue [C5] Hit maximum timestamp count, will start dropping events
nw_connection_add_timestamp_locked_on_nw_queue [C6] Hit maximum timestamp count, will start dropping events
Connection 7: received failure notification
Connection 7: failed to connect 3:-9816, reason -1
Connection 7: encountered error(3:-9816)
Task <79B81D1A-E5D8-4524-8B3D-9C0F881351E3>.<1> HTTP load failed, 0/0 bytes (error code: -1200 [3:-9816])
I'm having issues connecting to AWS IoT Core using wss and CocoaMQTTWebSocket. Log reports the following error messages:
Implementation of the following CocoaMQTTDelegate method seems to be useless...
...method is called but the connection cannot be established after calling completionHandler both with true/false value passed as parameter. After the implementation, log doesn't display the "API MISUSE" warning but connection can't be established.
I changed CocoaMQTTWebSocket.swift file to manually implement
and connection is working fine
I believe that a manual override of func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge) is a barely acceptable workaround but I'm stuck figuring out a definitive solution. Can you please help?