Skip to content

Commit 4c5fd95

Browse files
committed
A bit of refactoring
1 parent ca288d4 commit 4c5fd95

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

cli/Valet/Cloudflared.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,20 @@ public function currentTunnelUrl(?string $domain = null)
1717
$urls = [];
1818
$processes = array_filter(explode("\n", $this->cli->run('pgrep -fl cloudflared')));
1919

20+
// Every cloudflare process will start a metrics web server
21+
// where Quick Tunnel URL is mentioned under /metrics endpoint
2022
foreach ($processes as $process) {
21-
preg_match('/(?<pid>\d+)\s.+(?<=--http-host-header\s)(?<domain>[^\s]+).*/', $process, $matches);
22-
if (count($matches) > 2) {
23+
preg_match('/(?<pid>\d+)\s.+--http-host-header\s(?<domain>[^\s]+).*/', $process, $matches);
24+
if (array_key_exists('domain', $matches) && array_key_exists('pid', $matches)) {
2325
$local_domain = $matches['domain'];
24-
$lsof = $this->cli->run('lsof -iTCP -P -a -p '.$matches['pid']);
26+
$lsof = $this->cli->run("lsof -iTCP -P -a -p {$matches['pid']}");
2527
preg_match('/TCP\s(?<server>[^\s]+:\d+)\s\(LISTEN\)/', $lsof, $matches);
2628
if (array_key_exists('server', $matches)) {
27-
$body = (new Client())->get("http://{$matches['server']}/metrics")->getBody();
28-
preg_match('/userHostname="(?<url>.+)"/', $body->getContents(), $matches);
29+
try {
30+
$body = (new Client())->get("http://{$matches['server']}/metrics")->getBody();
31+
preg_match('/userHostname="(?<url>.+)"/', $body->getContents(), $matches);
32+
} catch (\Exception $e) {}
33+
2934
$urls[$local_domain] = array_key_exists('url', $matches) ? $matches['url'] : false;
3035
}
3136
}

cli/app.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -403,26 +403,25 @@ function (ConsoleCommandEvent $event) {
403403
/**
404404
* Echo or set the name of the currently-selected share tool (either "ngrok" or "expose").
405405
*/
406-
$share_tools_list = preg_replace('/,\s([^,]+)$/', ' or $1',
407-
join(', ', array_map(fn($t) => "`$t`", $share_tools)));
408-
409406
$app->command('share-tool [tool]', function (InputInterface $input, OutputInterface $output, $tool = null)
410-
use ($share_tools, $share_tools_list) {
407+
use ($share_tools) {
411408
if ($tool === null) {
412409
return output(Configuration::read()['share-tool'] ?? '(not set)');
413410
}
414411

415-
if (! in_array($tool, $share_tools) || ! class_exists(ucfirst($tool))) {
416-
warning($tool.' is not a valid share tool. Please use '.$share_tools_list .'.');
412+
$share_tools_list = preg_replace('/,\s([^,]+)$/', ' or $1',
413+
join(', ', array_map(fn($t) => "`$t`", $share_tools)));
414+
415+
if (! in_array($tool, $share_tools) || ! class_exists($tool)) {
416+
warning("$tool is not a valid share tool. Please use $share_tools_list.");
417417

418418
return Command::FAILURE;
419419
}
420420

421421
Configuration::updateKey('share-tool', $tool);
422-
info('Share tool set to '.$tool.'.');
423-
$share_tool = ucfirst($tool);
422+
info("Share tool set to $tool.");
424423

425-
if (! $share_tool::installed()) {
424+
if (! $tool::installed()) {
426425
$helper = $this->getHelperSet()->get('question');
427426
$question = new ConfirmationQuestion(
428427
'Would you like to install '.ucfirst($tool).' now? [y/N] ',
@@ -434,9 +433,11 @@ function (ConsoleCommandEvent $event) {
434433
return;
435434
}
436435

437-
$share_tool::ensureInstalled();
436+
$tool::ensureInstalled();
438437
}
439-
})->descriptions('Get the name of the current share tool ('.$share_tools_list.').');
438+
439+
return Command::SUCCESS;
440+
})->descriptions('Get the name of the current share tool.');
440441

441442
/**
442443
* Set the ngrok auth token.

valet

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,9 @@ then
124124
else
125125
SCHEME="http"
126126
fi
127-
echo "Scheme: $SCHEME"
128-
sudo -u "$USER" cloudflared tunnel --no-tls-verify --url "$SCHEME://localhost" --http-host-header "$HOST.$TLD" $PARAMS
127+
128+
sudo -u "$USER" cloudflared tunnel --no-tls-verify --url "$SCHEME://localhost" \
129+
--http-host-header "$HOST.$TLD" $PARAMS
129130

130131
exit
131132

0 commit comments

Comments
 (0)