Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ use RealtimeRegister\IsProxy;
$isProxy = new IsProxy('my-secret-api-key');

foreach ($isProxy->checkMany('example', ['nl', 'com', 'net', 'org']) as $result) {
echo $result->getDomain() . $result->isAvailable() ? ' ✅' : ' ❌';
echo $result->getDomain() . ($result->isAvailable() ? ' ✅' : ' ❌') . PHP_EOL;
}
// example.nl ✅
// example.com ❌
Expand Down
4 changes: 2 additions & 2 deletions src/IsProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ final class IsProxy
{
private IsProxyConnection $connection;

public function __construct(string $apiKey, string $host = 'is.yoursrs.com', int $port = 2001)
public function __construct(string $apiKey, string $host = 'is.yoursrs.com', int $port = 2001, bool $tls = true)
{
$this->setConnection(new IsProxyConnection($apiKey, $host, $port));
$this->setConnection(new IsProxyConnection($apiKey, $host, $port, $tls));
}

public function setConnection(IsProxyConnection $connection): void
Expand Down
41 changes: 40 additions & 1 deletion src/Support/IsProxyConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ class IsProxyConnection
/** @var resource */
protected $socket;

public function __construct(string $apiKey, string $host = 'is.yoursrs.com', int $port = 2001)
protected bool $tls;

public function __construct(string $apiKey, string $host = 'is.yoursrs.com', int $port = 2001, bool $tls = true)
{
$this->apiKey = $apiKey;
$this->host = $host;
$this->port = $port;
$this->tls = $tls;
}

public function __destruct()
Expand All @@ -34,11 +37,23 @@ public function connect(): bool
}

$this->socket = $socket;

// Try to set up tls, if enabled;
if ($this->tls) {
if ($this->upgradeToTls() === false) {
return false;
}
}

return $this->login();
}

public function disconnect(): void
{
if (! $this->isConnected()) {
return;
}

$this->write('CLOSE');

@fclose($this->socket);
Expand Down Expand Up @@ -84,4 +99,28 @@ protected function login(): bool

return (bool) preg_match('#^100\sLogin\sok#', $response);
}

protected function upgradeToTls(): bool
{
if (! $this->write('STARTTLS')) {
return false;
}

$response = $this->read();
if (! preg_match('#^100\sOK#', $response)) {
return true;
}

$methods = STREAM_CRYPTO_METHOD_TLS_CLIENT;

if (defined('STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT')) {
$methods |= STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT;
}

if (defined('STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT')) {
$methods |= STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT;
}

return stream_socket_enable_crypto($this->socket, true, $methods) === true;
}
}
3 changes: 1 addition & 2 deletions tests/Clients/Domains/DomainsApiTransferTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public function test_transfer_quote(): void
authcode: '123123123',
autoRenew: true,
ns: [],
transferContacts: ['REGISTRANT'],
designatedAgent: 'OLD',
zone: Zone::fromArray(include __DIR__ . '/../../Domain/data/dns/zones/zone_valid.php'),
contacts: DomainContactCollection::fromArray([include __DIR__ . '/../../Domain/data/domains/domain_contact_valid.php']),
Expand Down Expand Up @@ -64,7 +63,7 @@ public function test_transfer(): void
'123123123',
true,
[],
['ADMIN'],
null,
'OLD',
Zone::fromArray(include __DIR__ . '/../../Domain/data/dns/zones/zone_valid.php'),
DomainContactCollection::fromArray([include __DIR__ . '/../../Domain/data/domains/domain_contact_valid.php']),
Expand Down
26 changes: 24 additions & 2 deletions tests/Helpers/MockedIsProxyConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class MockedIsProxyConnection extends IsProxyConnection
/** @var bool */
private $connected = false;

public function __construct(string $apiKey, TestCase $testCase)
public function __construct(string $apiKey, TestCase $testCase, bool $tls = true)
{
parent::__construct($apiKey);
parent::__construct($apiKey, tls: $tls);

$this->testCase = $testCase;
}
Expand Down Expand Up @@ -55,6 +55,13 @@ public function expectWrite(string $message): MockedIsProxyConnection
public function connect(): bool
{
$this->connected = true;

if ($this->tls) {
if ($this->upgradeToTls() === false) {
return false;
}
}

return $this->login();
}

Expand All @@ -81,4 +88,19 @@ public function isConnected(): bool
{
return $this->connected;
}

protected function upgradeToTls(): bool
{
if (! $this->write('STARTTLS')) {
return false;
}

$response = $this->read();

if ($response !== '100 OK') {
return false;
}

return true;
Comment thread
zbrag marked this conversation as resolved.
}
}
32 changes: 32 additions & 0 deletions tests/IsProxyConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class IsProxyConnectionTest extends TestCase
public function test_check(): void
{
$connection = (new MockedIsProxyConnection('secret', $this))
->expectWrite('STARTTLS')
->expectRead('100 OK')
->expectWrite('LOGIN secret')
->expectRead('100 Login ok')
->expectWrite('IS example.com')
Expand All @@ -33,6 +35,8 @@ public function test_check(): void
public function test_connection_error(): void
{
$connection = (new MockedIsProxyConnection('secret', $this))
->expectWrite('STARTTLS')
->expectRead('100 OK')
->expectWrite('LOGIN secret')
->expectRead('400 Login failed');

Expand All @@ -46,6 +50,8 @@ public function test_connection_error(): void
public function test_check_wrong_answer(): void
{
$connection = (new MockedIsProxyConnection('secret', $this))
->expectWrite('STARTTLS')
->expectRead('100 OK')
->expectWrite('LOGIN secret')
->expectRead('100 Login ok')
->expectWrite('IS example.com')
Expand All @@ -63,6 +69,8 @@ public function test_check_wrong_answer(): void
public function test_check_many(): void
{
$connection = (new MockedIsProxyConnection('secret', $this))
->expectWrite('STARTTLS')
->expectRead('100 OK')
->expectWrite('LOGIN secret')
->expectRead('100 Login ok')
->expectWrite('IS example.com')
Expand Down Expand Up @@ -110,6 +118,8 @@ public function test_check_many(): void
public function test_check_many_connection_fail(): void
{
$connection = (new MockedIsProxyConnection('secret', $this))
->expectWrite('STARTTLS')
->expectRead('100 OK')
->expectWrite('LOGIN secret')
->expectRead('400 Login failed');

Expand All @@ -123,6 +133,8 @@ public function test_check_many_connection_fail(): void
public function test_check_premium(): void
{
$connection = (new MockedIsProxyConnection('secret', $this))
->expectWrite('STARTTLS')
->expectRead('100 OK')
->expectWrite('LOGIN secret')
->expectRead('100 Login ok')
->expectWrite('ENABLE premium')
Expand Down Expand Up @@ -182,4 +194,24 @@ public function test_check_premium(): void
$this->assertEmpty($availableDomain->getExtras());
$this->assertTrue($invalidDomain->isInvalid());
}

public function test_check_without_ssl_support(): void
{
$connection = (new MockedIsProxyConnection('secret', $this, false))
->expectWrite('LOGIN secret')
->expectRead('100 Login ok')
->expectWrite('IS example.com')
->expectRead('example.com available')
->expectWrite('CLOSE');

$isProxy = new IsProxy(apiKey:'secret', tls: false);
$isProxy->setConnection($connection);

$result = $isProxy->check('example', 'com');

$this->assertInstanceOf(IsProxyDomain::class, $result);
$this->assertSame(IsProxyDomain::STATUS_AVAILABLE, $result->getStatus());
$this->assertSame('example.com', $result->getDomain());
$this->assertTrue($result->isAvailable());
}
}