Skip to content

Commit ea57d98

Browse files
Refactor heavily nested cloudflared currentTunnelUrl
Co-Authored-By: Mateus Junges <[email protected]>
1 parent 3e2789c commit ea57d98

File tree

1 file changed

+29
-25
lines changed

1 file changed

+29
-25
lines changed

cli/Valet/Cloudflared.php

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,40 +12,44 @@ public function __construct(public CommandLine $cli, public Brew $brew)
1212

1313
public function currentTunnelUrl(string $domain): ?string
1414
{
15-
return $this->currentCloudflaredTunnels()[$domain] ?? false;
16-
}
17-
18-
protected function currentCloudflaredTunnels(): array
19-
{
20-
$urls = [];
21-
22-
// Get all cloudflared processes
23-
$processes = array_filter(explode("\n", $this->cli->run('pgrep -fl cloudflared')));
24-
2515
// Every cloudflared process will start a "metrics" web server where the
26-
// Quick Tunnel URL will be mentioned under the /metrics endpoint
27-
foreach ($processes as $process) {
16+
// Quick Tunnel URL will be mentioned under the /metrics endpoint; there
17+
// can potentially be more than one process that matches, but the lsof
18+
// command will show which one is actually functionally running
19+
foreach (array_filter(explode(PHP_EOL, $this->cli->run('pgrep -fl cloudflared'))) as $process) {
2820
// Get the URL shared in this process
2921
preg_match('/(?<pid>\d+)\s.+--http-host-header\s(?<domain>[^\s]+).*/', $process, $pgrepMatches);
3022

31-
if (array_key_exists('domain', $pgrepMatches) && array_key_exists('pid', $pgrepMatches)) {
32-
// Get the localhost URL (localhost:port) for the metrics server
33-
$lsof = $this->cli->run("lsof -iTCP -P -a -p {$pgrepMatches['pid']}");
34-
preg_match('/TCP\s(?<server>[^\s]+:\d+)\s\(LISTEN\)/', $lsof, $lsofMatches);
23+
if (! array_key_exists('domain', $pgrepMatches) || ! array_key_exists('pid', $pgrepMatches)) {
24+
continue;
25+
}
26+
27+
if ($pgrepMatches['domain'] !== $domain) {
28+
continue;
29+
}
30+
31+
// Get the localhost URL for the metrics server
32+
$lsof = $this->cli->run("lsof -iTCP -P -a -p {$pgrepMatches['pid']}");
33+
preg_match('/TCP\s(?<server>[^\s]+:\d+)\s\(LISTEN\)/', $lsof, $lsofMatches);
3534

36-
if (array_key_exists('server', $lsofMatches)) {
37-
try {
38-
// Get the shared cloudflared URL from the metrics server output
39-
$body = (new Client())->get("http://{$lsofMatches['server']}/metrics")->getBody();
40-
preg_match('/userHostname="(?<url>.+)"/', $body->getContents(), $lsofMatches);
41-
} catch (\Exception $e) {}
35+
if (! array_key_exists('server', $lsofMatches)) {
36+
continue;
37+
}
38+
39+
try {
40+
// Get the cloudflared share URL from the metrics server output
41+
$body = (new Client())->get("http://{$lsofMatches['server']}/metrics")->getBody();
42+
preg_match('/userHostname="(?<url>.+)"/', $body->getContents(), $userHostnameMatches);
43+
} catch (\Exception $e) {
44+
return false;
45+
}
4246

43-
$urls[$pgrepMatches['domain']] = $lsofMatches['url'] ?? false;
44-
}
47+
if (array_key_exists('url', $userHostnameMatches)) {
48+
return $userHostnameMatches['url'];
4549
}
4650
}
4751

48-
return $urls;
52+
return false;
4953
}
5054

5155
/**

0 commit comments

Comments
 (0)