Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add TLS support#33

Open
cyril-barragan wants to merge 4 commits into
php8-smpp:mainfrom
cyril-barragan:feature/addTlsSupport
Open

Add TLS support#33
cyril-barragan wants to merge 4 commits into
php8-smpp:mainfrom
cyril-barragan:feature/addTlsSupport

Conversation

@cyril-barragan
Copy link
Copy Markdown

@cyril-barragan cyril-barragan commented Aug 25, 2025

Hello,

In this PR, I’ve added TLS support through a new StreamTransport implementation.

I hope this enhancement will be helpful, and I’d be glad if you consider merging it!

Comment thread src/Transport/StreamTransport.php Outdated
Comment thread src/Transport/StreamTransport.php Outdated
Comment thread src/Transport/StreamTransport.php Outdated
@cyril-barragan cyril-barragan changed the title Feature/add tls support Add TLS support Aug 26, 2025
Copy link
Copy Markdown
Collaborator

@alexandr-mironov alexandr-mironov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that StreamTransport has many differences from SocketTransport. Because of this, they require a separate Entry to store connection data, and probably a separate Resolver (in the form of a class inheritor). This solves some problems with choosing a protocol, for example. I mean that you explicitly realize that either your connection is secure and use one type of transport that will have its own nuances (in the form of a certificate file for verification) or you use a regular connection (port 2775) and access via IP addresses (accordingly, you get rid of the host explicitly)

Comment on lines +27 to +56
## Secured TLS (port 2776)

```php
<?php

declare(strict_types=1);

use Smpp\ClientBuilder;
use Smpp\Pdu\Address;
use Smpp\Smpp;


$config = new StreamTransportConfig();
$config->setUseTls(true)
->setCertificateFile('/etc/ssl/certs/<ca-root-certificate>.pem')
;

$client = ClientBuilder::createForStream(['smpp.host.domain:2776'], $config)
->setCredentials(getenv('SYSTEM_ID'), getenv('PASSWORD'))
->buildClient();

$client->bindTransceiver();

$client->sendSMS(
from: new Address("php8-smpp", Smpp::TON_ALPHANUMERIC),
to: new Address("79000000000"),
message: "Some kind of message"
);

$client->close();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a default use case. Should be added as separete example file.

Comment on lines +7 to +14
enum ProtocolEnum: string
{
case SSL = 'ssl';
case TCP = 'tcp';
case TCP4 = 'tcp4';
case TCP6 = 'tcp6';
case TLS = 'tls';
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Supported in version >= 8.1, this is not reverse compatible changes for this library. Disallowed.

Comment on lines +43 to +51
if ($this->config->getUseTls()) {
return ProtocolEnum::TLS;
} elseif ($this->config->isForceIpv6()) {
return ProtocolEnum::TCP6;
} elseif ($this->config->isForceIpv4()) {
return ProtocolEnum::TCP4;
} else {
return ProtocolEnum::TCP;
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not use "match"?

Comment on lines +20 to +21
/** @var ?resource */
protected $socket = null;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +54 to +65
public function getHost(Entry $entry): string
{
if ($this->config->getUseTls()) {
return $entry->getHost();
} elseif ($this->config->isForceIpv6()) {
return $entry->getIpv6();
} elseif ($this->config->isForceIpv4()) {
return $entry->getIpv4();
} else {
return $entry->getIpv4();
}
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not use "match"?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why public?

];

if ($this->config->getUseTls() && $this->config->getCertificateFile()) {
$ctx['ssl'] = [...$ctx['ssl'], 'cafile' => $this->config->getCertificateFile()];
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe just

$ctx['ssl']['cafile'] = $this->config->getCertificateFile();

?

Comment on lines +67 to +81
private function getStreamContext(): array
{
$ctx = [
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
]
];

if ($this->config->getUseTls() && $this->config->getCertificateFile()) {
$ctx['ssl'] = [...$ctx['ssl'], 'cafile' => $this->config->getCertificateFile()];
}

return $ctx;
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

array is not enough strict structure - maybe better replace to some special object with __toArray method which incapsulate this logic ?


private function getStreamContext(): array
{
$ctx = [
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not $context?

$e = [$this->socket];
$res = stream_select($r, $w, $e, 0);

if (false === $res) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls w/o yoda things

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants