Skip to content

Commit

Permalink
Merge pull request #1460 from rana01645/master
Browse files Browse the repository at this point in the history
Update "fetch-share-url" to pull correct URL for HTTPS tunnels, not just HTTP
  • Loading branch information
mattstauffer authored Dec 21, 2023
2 parents 3f2b629 + cd099f6 commit 9c922e5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
21 changes: 16 additions & 5 deletions cli/Valet/Ngrok.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,33 @@ public function currentTunnelUrl(?string $domain = null): string
}

/**
* Find the HTTP tunnel URL from the list of tunnels.
* Find the HTTP/HTTPS tunnel URL from the list of tunnels.
*/
public function findHttpTunnelUrl(array $tunnels, string $domain): ?string
{
$httpTunnel = null;
$httpsTunnel = null;

// If there are active tunnels on the Ngrok instance we will spin through them and
// find the one responding on HTTP. Each tunnel has an HTTP and a HTTPS address
// but for local dev purposes we just desire the plain HTTP URL endpoint.
// if no HTTP tunnel is found we will return the HTTPS tunnel as a fallback.

// Iterate through tunnels to find both HTTP and HTTPS tunnels
foreach ($tunnels as $tunnel) {
if ($tunnel->proto === 'http' && strpos($tunnel->config->addr, strtolower($domain))) {
return $tunnel->public_url;
if (stripos($tunnel->config->addr, $domain)) {
if ($tunnel->proto === 'http') {
$httpTunnel = $tunnel->public_url;
} elseif ($tunnel->proto === 'https') {
$httpsTunnel = $tunnel->public_url;
}
}
}

return null;
// Return HTTP tunnel if available, otherwise return HTTPS tunnel
return $httpTunnel ?? $httpsTunnel;
}


/**
* Set the Ngrok auth token.
*/
Expand Down
8 changes: 8 additions & 0 deletions tests/NgrokTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,18 @@ public function test_it_matches_correct_share_tunnel()
],
'public_url' => 'http://right-one.ngrok.io/',
],
(object) [
'proto' => 'https',
'config' => (object) [
'addr' => 'http://mysecuresite.test:80',
],
'public_url' => 'http://secure-right-one.ngrok.io/',
],
];

$ngrok = resolve(Ngrok::class);
$this->assertEquals('http://right-one.ngrok.io/', $ngrok->findHttpTunnelUrl($tunnels, 'mysite'));
$this->assertEquals('http://secure-right-one.ngrok.io/', $ngrok->findHttpTunnelUrl($tunnels, 'mysecuresite'));
}

public function test_it_checks_against_lowercased_domain()
Expand Down

0 comments on commit 9c922e5

Please sign in to comment.