1
0
mirror of https://github.com/laravel/valet.git synced 2026-02-05 16:40:05 +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 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. * Get the current tunnel URL from the Ngrok API.
@@ -16,18 +19,24 @@ class Ngrok
*/ */
function currentTunnelUrl($domain = null) function currentTunnelUrl($domain = null)
{ {
return retry(20, function () use ($domain) { // wait a second for ngrok to start before attempting to find available tunnels
$body = Request::get($this->tunnelsEndpoint)->send()->body; sleep(1);
// If there are active tunnels on the Ngrok instance we will spin through them and foreach ($this->tunnelsEndpoints as $endpoint) {
// find the one responding on HTTP. Each tunnel has an HTTP and a HTTPS address $response = retry(20, function () use ($endpoint, $domain) {
// but for local testing purposes we just desire the plain HTTP URL endpoint. $body = Request::get($endpoint)->send()->body;
if (isset($body->tunnels) && count($body->tunnels) > 0) {
return $this->findHttpTunnelUrl($body->tunnels, $domain); if (isset($body->tunnels) && count($body->tunnels) > 0) {
} else { return $this->findHttpTunnelUrl($body->tunnels, $domain);
throw new DomainException("Tunnel not established."); }
}, 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) 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) { foreach ($tunnels as $tunnel) {
if ($tunnel->proto === 'http' && strpos($tunnel->config->addr, $domain) ) { if ($tunnel->proto === 'http' && strpos($tunnel->config->addr, $domain) ) {
return $tunnel->public_url; return $tunnel->public_url;