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

Extract Cloudflared tunnels method

This commit is contained in:
Matt Stauffer
2024-08-24 19:33:17 -05:00
parent 81efb8eee1
commit 628997d835
2 changed files with 25 additions and 14 deletions

View File

@@ -11,30 +11,41 @@ public function __construct(public CommandLine $cli, public Brew $brew)
}
public function currentTunnelUrl(?string $domain = null)
{
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 cloudflare process will start a metrics web server
// where Quick Tunnel URL is mentioned under /metrics endpoint
// 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) {
preg_match('/(?<pid>\d+)\s.+--http-host-header\s(?<domain>[^\s]+).*/', $process, $matches);
if (array_key_exists('domain', $matches) && array_key_exists('pid', $matches)) {
$local_domain = $matches['domain'];
$lsof = $this->cli->run("lsof -iTCP -P -a -p {$matches['pid']}");
preg_match('/TCP\s(?<server>[^\s]+:\d+)\s\(LISTEN\)/', $lsof, $matches);
if (array_key_exists('server', $matches)) {
// 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('server', $lsofMatches)) {
try {
$body = (new Client())->get("http://{$matches['server']}/metrics")->getBody();
preg_match('/userHostname="(?<url>.+)"/', $body->getContents(), $matches);
// 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) {}
$urls[$local_domain] = array_key_exists('url', $matches) ? $matches['url'] : false;
$urls[$pgrepMatches['domain']] = $lsofMatches['url'] ?? false;
}
}
}
return array_key_exists($domain, $urls) ? $urls[$domain] : false;
return $urls;
}
/**

4
valet
View File

@@ -41,8 +41,8 @@ else
fi
# If the command is the "share" command we will need to resolve out any
# symbolic links for the site. Before starting Ngrok, we will fire a
# process to retrieve the live Ngrok tunnel URL in the background.
# symbolic links for the site. Before starting the share tool, we will fire a
# process to retrieve the live the share tool tunnel URL in the background.
if [[ "$1" = "share" ]]
then
SHARETOOL="$("$PHP" "$DIR/cli/valet.php" share-tool)"