Skip to content

Commit 8258141

Browse files
authored
Added support for TLS ALPN to connect via MQTT protocol to a TLS encrypted HTTP port 443 (#181)
* Update ConnectionSettings.php added TLS ALPN option * Update MqttClient.php Added TLS ALPN option to the TLS options * Update MqttClient.php * Update README.md Added TLS ALPN to the ConnectSettings options
1 parent 0d9f829 commit 8258141

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,10 @@ $connectionSettings = (new \PhpMqtt\Client\ConnectionSettings)
243243
// This option requires ConnectionSettings::setTlsClientCertificateFile() and
244244
// ConnectionSettings::setTlsClientCertificateKeyFile() to be used as well.
245245
->setTlsClientCertificateKeyPassphrase(null);
246+
247+
// The TLS ALPN is used to establish a TLS encrypted mqtt connection on port 443,
248+
// which usually is reserved for TLS encrypted HTTP traffic.
249+
->setTlsAlpn(null);
246250
```
247251

248252
## Features

src/ConnectionSettings.php

+22
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class ConnectionSettings
3737
private ?string $tlsClientCertificateFile = null;
3838
private ?string $tlsClientCertificateKeyFile = null;
3939
private ?string $tlsClientCertificateKeyPassphrase = null;
40+
private ?string $tlsAlpn = null;
4041

4142
/**
4243
* The username used for authentication when connecting to the broker.
@@ -531,4 +532,25 @@ public function getTlsClientCertificateKeyPassphrase(): ?string
531532
{
532533
return $this->tlsClientCertificateKeyPassphrase;
533534
}
535+
536+
/**
537+
* The TLS ALPN is used to establish a TLS encrypted mqtt connection on port 443,
538+
* which usually is reserved for TLS encrypted HTTP traffic.
539+
*
540+
* @return ConnectionSettings A copy of the original object with the new setting applied.
541+
*/
542+
public function setTlsAlpn(?string $tlsAlpn): ConnectionSettings
543+
{
544+
$copy = clone $this;
545+
546+
$copy->tlsAlpn = $tlsAlpn;
547+
548+
return $copy;
549+
}
550+
551+
public function getTlsAlpn(): ?string
552+
{
553+
return $this->tlsAlpn;
554+
}
555+
534556
}

src/MqttClient.php

+4
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ protected function establishSocketConnection(): void
197197
$tlsOptions['passphrase'] = $this->settings->getTlsClientCertificateKeyPassphrase();
198198
}
199199

200+
if ($this->settings->getTlsAlpn() !== null) {
201+
$tlsOptions['alpn_protocols'] = $this->settings->getTlsAlpn();
202+
}
203+
200204
$contextOptions['ssl'] = $tlsOptions;
201205
}
202206

0 commit comments

Comments
 (0)