Skip to content

Commit

Permalink
add IPv6 Support (#174)
Browse files Browse the repository at this point in the history
* add IPv6 Support

* skip IPv6 test because Github Actions don't support it

* try another method of skipping github actions
  • Loading branch information
LordSimal authored Jun 16, 2022
1 parent 0c8e227 commit 89596d2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ SslCertificate::download()
->forHost($hostName);
```

This also works with IPv6 addresses
```php
SslCertificate::download()
->fromIpAddress('2a00:1450:4001:80e::200e')
->forHost('google.com');
```

You can specify [socket context options](https://www.php.net/manual/en/context.socket.php).
```php
SslCertificate::download()
Expand Down
17 changes: 13 additions & 4 deletions src/Downloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class Downloader

protected ?string $ipAddress = null;

protected bool $isIPv6 = false;

protected bool $usingIpAddress = false;

protected int $timeout = 30;
Expand Down Expand Up @@ -85,9 +87,14 @@ public function setFollowLocation(int $followLocation): self

public function fromIpAddress(string $ipAddress): self
{
if (! filter_var($ipAddress, FILTER_VALIDATE_IP)) {
$isValidIPv4 = filter_var($ipAddress, FILTER_VALIDATE_IP) !== false;
$isValidIPv6 = filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false;
if (!$isValidIPv4 && !$isValidIPv6) {
throw InvalidIpAddress::couldNotValidate($ipAddress);
}
if($isValidIPv6) {
$this->isIPv6 = true;
}

$this->ipAddress = $ipAddress;
$this->usingIpAddress = true;
Expand Down Expand Up @@ -164,9 +171,11 @@ protected function fetchCertificates(string $hostName): array
'ssl' => $sslOptions,
]);

$connectTo = ($this->usingIpAddress)
? $this->ipAddress
: $hostName;
if($this->usingIpAddress) {
$connectTo = ($this->isIPv6) ? "[" . $this->ipAddress . ']' : $this->ipAddress;
} else {
$connectTo = $hostName;
}

$client = @stream_socket_client(
"ssl://{$connectTo}:{$this->port}",
Expand Down
17 changes: 15 additions & 2 deletions tests/DownloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function it_can_download_a_certificate_from_a_host_name_with_strange_char
public function it_can_download_a_certificate_for_a_host_name_from_an_ip_address()
{
$sslCertificate = SslCertificate::download()
->fromIpAddress('138.197.187.74')
->fromIpAddress('164.92.244.169')
->forHost('spatie.be');

$this->assertInstanceOf(SslCertificate::class, $sslCertificate);
Expand All @@ -75,6 +75,19 @@ public function it_can_download_a_certificate_for_a_host_name_from_an_ip_address
$this->assertInstanceOf(SslCertificate::class, $sslCertificate);
}

/** @test */
public function it_can_download_a_certificate_for_a_host_name_from_an_ipv6_address()
{
if(getenv('GITHUB_ACTIONS')) {
$this->markTestSkipped('Github Actions have no IPv6 Support');
}
$sslCertificate = SslCertificate::download()
->fromIpAddress('2a00:1450:4001:80e::200e')
->forHost('google.com');

$this->assertInstanceOf(SslCertificate::class, $sslCertificate);
}

/** @test */
public function it_sets_a_fingerprint_on_the_downloaded_certificate()
{
Expand Down Expand Up @@ -142,7 +155,7 @@ public function it_can_retrieve_the_ip_address_of_the_server_that_served_the_cer
{
$sslCertificate = Downloader::downloadCertificateFromUrl('spatie.be');

$this->assertEquals('138.197.187.74:443', $sslCertificate->getRemoteAddress());
$this->assertEquals('164.92.244.169:443', $sslCertificate->getRemoteAddress());
}

/** @test */
Expand Down

0 comments on commit 89596d2

Please sign in to comment.