Skip to content

Commit 628997d

Browse files
committed
Extract Cloudflared tunnels method
1 parent 81efb8e commit 628997d

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

cli/Valet/Cloudflared.php

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,41 @@ public function __construct(public CommandLine $cli, public Brew $brew)
1111
}
1212

1313
public function currentTunnelUrl(?string $domain = null)
14+
{
15+
return $this->currentCloudflaredTunnels()[$domain] ?? false;
16+
}
17+
18+
protected function currentCloudflaredTunnels(): array
1419
{
1520
$urls = [];
21+
22+
// Get all cloudflared processes
1623
$processes = array_filter(explode("\n", $this->cli->run('pgrep -fl cloudflared')));
1724

18-
// Every cloudflare process will start a metrics web server
19-
// where Quick Tunnel URL is mentioned under /metrics endpoint
25+
// Every cloudflared process will start a "metrics" web server where the
26+
// Quick Tunnel URL will be mentioned under the /metrics endpoint
2027
foreach ($processes as $process) {
21-
preg_match('/(?<pid>\d+)\s.+--http-host-header\s(?<domain>[^\s]+).*/', $process, $matches);
22-
if (array_key_exists('domain', $matches) && array_key_exists('pid', $matches)) {
23-
$local_domain = $matches['domain'];
24-
$lsof = $this->cli->run("lsof -iTCP -P -a -p {$matches['pid']}");
25-
preg_match('/TCP\s(?<server>[^\s]+:\d+)\s\(LISTEN\)/', $lsof, $matches);
26-
if (array_key_exists('server', $matches)) {
28+
// Get the URL shared in this process
29+
preg_match('/(?<pid>\d+)\s.+--http-host-header\s(?<domain>[^\s]+).*/', $process, $pgrepMatches);
30+
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);
35+
36+
if (array_key_exists('server', $lsofMatches)) {
2737
try {
28-
$body = (new Client())->get("http://{$matches['server']}/metrics")->getBody();
29-
preg_match('/userHostname="(?<url>.+)"/', $body->getContents(), $matches);
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);
3041
} catch (\Exception $e) {}
3142

32-
$urls[$local_domain] = array_key_exists('url', $matches) ? $matches['url'] : false;
43+
$urls[$pgrepMatches['domain']] = $lsofMatches['url'] ?? false;
3344
}
3445
}
3546
}
3647

37-
return array_key_exists($domain, $urls) ? $urls[$domain] : false;
48+
return $urls;
3849
}
3950

4051
/**

valet

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ else
4141
fi
4242

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

0 commit comments

Comments
 (0)