Skip to content

Commit

Permalink
Refactor heavily nested cloudflared currentTunnelUrl
Browse files Browse the repository at this point in the history
Co-Authored-By: Mateus Junges <[email protected]>
  • Loading branch information
mattstauffer and mateusjunges committed Aug 25, 2024
1 parent 3e2789c commit ea57d98
Showing 1 changed file with 29 additions and 25 deletions.
54 changes: 29 additions & 25 deletions cli/Valet/Cloudflared.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,44 @@ public function __construct(public CommandLine $cli, public Brew $brew)

public function currentTunnelUrl(string $domain): ?string
{
return $this->currentCloudflaredTunnels()[$domain] ?? false;
}

protected function currentCloudflaredTunnels(): array
{
$urls = [];

// Get all cloudflared processes
$processes = array_filter(explode("\n", $this->cli->run('pgrep -fl cloudflared')));

// Every cloudflared process will start a "metrics" web server where the
// Quick Tunnel URL will be mentioned under the /metrics endpoint
foreach ($processes as $process) {
// Quick Tunnel URL will be mentioned under the /metrics endpoint; there
// can potentially be more than one process that matches, but the lsof
// command will show which one is actually functionally running
foreach (array_filter(explode(PHP_EOL, $this->cli->run('pgrep -fl cloudflared'))) as $process) {
// Get the URL shared in this process
preg_match('/(?<pid>\d+)\s.+--http-host-header\s(?<domain>[^\s]+).*/', $process, $pgrepMatches);

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

if ($pgrepMatches['domain'] !== $domain) {
continue;
}

// Get the localhost URL for the metrics server
$lsof = $this->cli->run("lsof -iTCP -P -a -p {$pgrepMatches['pid']}");
preg_match('/TCP\s(?<server>[^\s]+:\d+)\s\(LISTEN\)/', $lsof, $lsofMatches);

if (array_key_exists('server', $lsofMatches)) {
try {
// Get the shared cloudflared URL from the metrics server output
$body = (new Client())->get("http://{$lsofMatches['server']}/metrics")->getBody();
preg_match('/userHostname="(?<url>.+)"/', $body->getContents(), $lsofMatches);
} catch (\Exception $e) {}
if (! array_key_exists('server', $lsofMatches)) {
continue;
}

try {
// Get the cloudflared share URL from the metrics server output
$body = (new Client())->get("http://{$lsofMatches['server']}/metrics")->getBody();
preg_match('/userHostname="(?<url>.+)"/', $body->getContents(), $userHostnameMatches);
} catch (\Exception $e) {
return false;
}

$urls[$pgrepMatches['domain']] = $lsofMatches['url'] ?? false;
}
if (array_key_exists('url', $userHostnameMatches)) {
return $userHostnameMatches['url'];
}
}

return $urls;
return false;
}

/**
Expand Down

0 comments on commit ea57d98

Please sign in to comment.