1
0
mirror of https://github.com/laravel/valet.git synced 2026-02-04 16:10:08 +01:00

Part 2: Fix auto-detection amongst multiple ngrok processes

Thanks to the generosity of Alan at Ngrok, I've been able to do some additional testing with temporary added features.
Turns out #864 was incomplete. This PR now also loops through the default endpoints in order to find a process matching the passed domain.

Ref: #864
Ref: #145
This commit is contained in:
Chris Brown
2019-12-06 23:33:20 -05:00
parent 3b302b7593
commit c8435c5187

View File

@@ -7,7 +7,10 @@
class Ngrok
{
var $tunnelsEndpoint = 'http://127.0.0.1:4040/api/tunnels';
var $tunnelsEndpoints = [
'http://127.0.0.1:4040/api/tunnels',
'http://127.0.0.1:4041/api/tunnels',
];
/**
* Get the current tunnel URL from the Ngrok API.
@@ -16,18 +19,24 @@ class Ngrok
*/
function currentTunnelUrl($domain = null)
{
return retry(20, function () use ($domain) {
$body = Request::get($this->tunnelsEndpoint)->send()->body;
// wait a second for ngrok to start before attempting to find available tunnels
sleep(1);
// 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 testing purposes we just desire the plain HTTP URL endpoint.
if (isset($body->tunnels) && count($body->tunnels) > 0) {
return $this->findHttpTunnelUrl($body->tunnels, $domain);
} else {
throw new DomainException("Tunnel not established.");
foreach ($this->tunnelsEndpoints as $endpoint) {
$response = retry(20, function () use ($endpoint, $domain) {
$body = Request::get($endpoint)->send()->body;
if (isset($body->tunnels) && count($body->tunnels) > 0) {
return $this->findHttpTunnelUrl($body->tunnels, $domain);
}
}, 250);
if (!empty($response)) {
return $response;
}
}, 250);
}
throw new DomainException("Tunnel not established.");
}
/**
@@ -38,6 +47,9 @@ function currentTunnelUrl($domain = null)
*/
function findHttpTunnelUrl($tunnels, $domain)
{
// 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.
foreach ($tunnels as $tunnel) {
if ($tunnel->proto === 'http' && strpos($tunnel->config->addr, $domain) ) {
return $tunnel->public_url;