mirror of
https://github.com/laravel/valet.git
synced 2026-02-05 00:20:08 +01:00
Add a new share tool, cloudflared
This commit is contained in:
52
cli/Valet/Cloudflared.php
Normal file
52
cli/Valet/Cloudflared.php
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Valet;
|
||||||
|
|
||||||
|
use GuzzleHttp\Client;
|
||||||
|
|
||||||
|
class Cloudflared
|
||||||
|
{
|
||||||
|
public $brew_formula = 'cloudflare/cloudflare/cloudflared';
|
||||||
|
|
||||||
|
public function __construct(public CommandLine $cli, public Brew $brew)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function currentTunnelUrl(?string $domain = null)
|
||||||
|
{
|
||||||
|
$urls = [];
|
||||||
|
$processes = array_filter(explode("\n", $this->cli->run('pgrep -fl cloudflared')));
|
||||||
|
|
||||||
|
foreach ($processes as $process) {
|
||||||
|
preg_match('/(?<pid>\d+)\s.+(?<=--http-host-header\s)(?<domain>[^\s]+).*/', $process, $matches);
|
||||||
|
if (count($matches) > 2) {
|
||||||
|
$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)) {
|
||||||
|
$body = (new Client())->get("http://{$matches['server']}/metrics")->getBody();
|
||||||
|
preg_match('/userHostname="(?<url>.+)"/', $body->getContents(), $matches);
|
||||||
|
$urls[$local_domain] = array_key_exists('url', $matches) ? $matches['url'] : false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return array_key_exists($domain, $urls) ? $urls[$domain] : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return whether cloudflared is installed.
|
||||||
|
*/
|
||||||
|
public function installed(): bool
|
||||||
|
{
|
||||||
|
return $this->brew->installed($this->brew_formula);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make sure cloudflared is installed.
|
||||||
|
*/
|
||||||
|
public function ensureInstalled(): void
|
||||||
|
{
|
||||||
|
$this->brew->ensureInstalled($this->brew_formula);
|
||||||
|
}
|
||||||
|
}
|
||||||
72
cli/app.php
72
cli/app.php
@@ -47,6 +47,12 @@ function (ConsoleCommandEvent $event) {
|
|||||||
|
|
||||||
Upgrader::onEveryRun();
|
Upgrader::onEveryRun();
|
||||||
|
|
||||||
|
$share_tools = [
|
||||||
|
'cloudflared',
|
||||||
|
'expose',
|
||||||
|
'ngrok'
|
||||||
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Install Valet and any required services.
|
* Install Valet and any required services.
|
||||||
*/
|
*/
|
||||||
@@ -378,83 +384,59 @@ function (ConsoleCommandEvent $event) {
|
|||||||
/**
|
/**
|
||||||
* Echo the currently tunneled URL.
|
* Echo the currently tunneled URL.
|
||||||
*/
|
*/
|
||||||
$app->command('fetch-share-url [domain]', function ($domain = null) {
|
$app->command('fetch-share-url [domain]', function ($domain = null) use ($share_tools) {
|
||||||
$tool = Configuration::read()['share-tool'] ?? null;
|
$tool = Configuration::read()['share-tool'] ?? null;
|
||||||
|
|
||||||
switch ($tool) {
|
if ($tool && in_array($tool, $share_tools) && class_exists($tool)) {
|
||||||
case 'expose':
|
|
||||||
if ($url = Expose::currentTunnelUrl($domain ?: Site::host(getcwd()))) {
|
|
||||||
output($url);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 'ngrok':
|
|
||||||
try {
|
try {
|
||||||
output(Ngrok::currentTunnelUrl(Site::domain($domain)));
|
output($tool::currentTunnelUrl(Site::domain($domain)));
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
warning($e->getMessage());
|
warning($e->getMessage());
|
||||||
}
|
}
|
||||||
break;
|
} else {
|
||||||
default:
|
info('Please set your share tool with `valet share-tool`.');
|
||||||
info('Please set your share tool with `valet share-tool expose` or `valet share-tool ngrok`.');
|
|
||||||
|
|
||||||
return Command::FAILURE;
|
return Command::FAILURE;
|
||||||
}
|
}
|
||||||
})->descriptions('Get the URL to the current share tunnel (for Expose or ngrok)');
|
})->descriptions('Get the URL to the current share tunnel');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Echo or set the name of the currently-selected share tool (either "ngrok" or "expose").
|
* Echo or set the name of the currently-selected share tool (either "ngrok" or "expose").
|
||||||
*/
|
*/
|
||||||
$app->command('share-tool [tool]', function (InputInterface $input, OutputInterface $output, $tool = null) {
|
$share_tools_list = preg_replace('/,\s([^,]+)$/', ' or $1',
|
||||||
|
join(', ', array_map(fn($t) => "`$t`", $share_tools)));
|
||||||
|
|
||||||
|
$app->command('share-tool [tool]', function (InputInterface $input, OutputInterface $output, $tool = null)
|
||||||
|
use ($share_tools, $share_tools_list) {
|
||||||
if ($tool === null) {
|
if ($tool === null) {
|
||||||
return output(Configuration::read()['share-tool'] ?? '(not set)');
|
return output(Configuration::read()['share-tool'] ?? '(not set)');
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($tool !== 'expose' && $tool !== 'ngrok') {
|
if (! in_array($tool, $share_tools) || ! class_exists(ucfirst($tool))) {
|
||||||
warning($tool.' is not a valid share tool. Please use `ngrok` or `expose`.');
|
warning($tool.' is not a valid share tool. Please use '.$share_tools_list .'.');
|
||||||
|
|
||||||
return Command::FAILURE;
|
return Command::FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
Configuration::updateKey('share-tool', $tool);
|
Configuration::updateKey('share-tool', $tool);
|
||||||
info('Share tool set to '.$tool.'.');
|
info('Share tool set to '.$tool.'.');
|
||||||
|
$share_tool = ucfirst($tool);
|
||||||
|
|
||||||
if ($tool === 'expose') {
|
if (! $share_tool::installed()) {
|
||||||
if (Expose::installed()) {
|
|
||||||
// @todo: Check it's the right version (has /api/tunnels/)
|
|
||||||
// E.g. if (Expose::installedVersion)
|
|
||||||
// if (version_compare(Expose::installedVersion(), $minimumExposeVersion) < 0) {
|
|
||||||
// prompt them to upgrade
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$helper = $this->getHelperSet()->get('question');
|
$helper = $this->getHelperSet()->get('question');
|
||||||
$question = new ConfirmationQuestion('Would you like to install Expose now? [y/N] ', false);
|
$question = new ConfirmationQuestion(
|
||||||
|
'Would you like to install '.ucfirst($tool).' now? [y/N] ',
|
||||||
|
false);
|
||||||
|
|
||||||
if ($helper->ask($input, $output, $question) === false) {
|
if ($helper->ask($input, $output, $question) === false) {
|
||||||
info('Proceeding without installing Expose.');
|
info('Proceeding without installing '.ucfirst($tool).'.');
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Expose::ensureInstalled();
|
$share_tool::ensureInstalled();
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
})->descriptions('Get the name of the current share tool ('.$share_tools_list.').');
|
||||||
if (! Ngrok::installed()) {
|
|
||||||
info("\nIn order to share with ngrok, you'll need a version\nof ngrok installed and managed by Homebrew.");
|
|
||||||
$helper = $this->getHelperSet()->get('question');
|
|
||||||
$question = new ConfirmationQuestion('Would you like to install ngrok via Homebrew now? [y/N] ', false);
|
|
||||||
|
|
||||||
if ($helper->ask($input, $output, $question) === false) {
|
|
||||||
info('Proceeding without installing ngrok.');
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ngrok::ensureInstalled();
|
|
||||||
}
|
|
||||||
})->descriptions('Get the name of the current share tool (Expose or ngrok).');
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the ngrok auth token.
|
* Set the ngrok auth token.
|
||||||
|
|||||||
@@ -68,3 +68,6 @@ class Upgrader extends Facade
|
|||||||
class Valet extends Facade
|
class Valet extends Facade
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
class Cloudflared extends Facade
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|||||||
82
valet
82
valet
@@ -47,23 +47,12 @@ if [[ "$1" = "share" ]]
|
|||||||
then
|
then
|
||||||
SHARETOOL="$($PHP "$DIR/cli/valet.php" share-tool)"
|
SHARETOOL="$($PHP "$DIR/cli/valet.php" share-tool)"
|
||||||
|
|
||||||
if [[ $SHARETOOL = "ngrok" ]]
|
# Check for parameters to pass through to share tool (these will start with '-' or '--')
|
||||||
then
|
|
||||||
# ngrok
|
|
||||||
# Check to make sure ngrok is configured correctly
|
|
||||||
BREW_PREFIX=$(brew --prefix)
|
|
||||||
$($BREW_PREFIX/bin/ngrok config check >/dev/null 2>&1)
|
|
||||||
|
|
||||||
if [[ $? -ne 0 ]]; then
|
|
||||||
echo "Please sign up for a free ngrok account and then run valet set-ngrok-token {yourTokenHere}."
|
|
||||||
exit
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Check for parameters to pass through to ngrok (these will start with '-' or '--')
|
|
||||||
PARAMS=(${@:2})
|
PARAMS=(${@:2})
|
||||||
|
|
||||||
for PARAM in ${PARAMS[@]}
|
for PARAM in ${PARAMS[@]}
|
||||||
do
|
do
|
||||||
if [[ ${PARAM:0:1} != '-' ]]; then
|
if [[ ${PARAM:0:1} == '-' ]]; then
|
||||||
PARAMS=("${PARAMS[@]/$PARAM}") # Quotes when working with strings
|
PARAMS=("${PARAMS[@]/$PARAM}") # Quotes when working with strings
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
@@ -81,19 +70,31 @@ then
|
|||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
|
# Lowercase the host to match the rest of our domains are looked up
|
||||||
|
HOST=$(echo "$HOST" | tr '[:upper:]' '[:lower:]')
|
||||||
TLD=$("$PHP" "$DIR/cli/valet.php" tld)
|
TLD=$("$PHP" "$DIR/cli/valet.php" tld)
|
||||||
|
SECURED=$(grep --quiet --no-messages 443 ~/.config/valet/Nginx/$HOST*)
|
||||||
|
|
||||||
|
if [[ $SHARETOOL = "ngrok" ]]
|
||||||
|
then
|
||||||
|
# ngrok
|
||||||
|
# Check to make sure ngrok is configured correctly
|
||||||
|
BREW_PREFIX=$(brew --prefix)
|
||||||
|
$($BREW_PREFIX/bin/ngrok config check >/dev/null 2>&1)
|
||||||
|
|
||||||
|
if [[ $? -ne 0 ]]; then
|
||||||
|
echo "Please sign up for a free ngrok account and then run valet set-ngrok-token {yourTokenHere}."
|
||||||
|
exit
|
||||||
|
fi
|
||||||
|
|
||||||
# Decide the correct PORT: uses 60 for secure, else 80
|
# Decide the correct PORT: uses 60 for secure, else 80
|
||||||
if grep --quiet --no-messages 443 ~/.config/valet/Nginx/$HOST*
|
if $SECURED
|
||||||
then
|
then
|
||||||
PORT=60
|
PORT=60
|
||||||
else
|
else
|
||||||
PORT=80
|
PORT=80
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Lowercase the host to match how the rest of our domains are looked up
|
|
||||||
HOST=$(echo "$HOST" | tr '[:upper:]' '[:lower:]')
|
|
||||||
|
|
||||||
sudo -u "$USER" "$BREW_PREFIX/bin/ngrok" http "$HOST.$TLD:$PORT" --host-header=rewrite $PARAMS
|
sudo -u "$USER" "$BREW_PREFIX/bin/ngrok" http "$HOST.$TLD:$PORT" --host-header=rewrite $PARAMS
|
||||||
|
|
||||||
exit
|
exit
|
||||||
@@ -102,48 +103,35 @@ then
|
|||||||
then
|
then
|
||||||
|
|
||||||
# expose
|
# expose
|
||||||
# Check for parameters to pass through to Expose (these will start with '-' or '--')
|
|
||||||
PARAMS=(${@:2})
|
|
||||||
for PARAM in ${PARAMS[@]}
|
|
||||||
do
|
|
||||||
if [[ ${PARAM:0:1} != '-' ]]; then
|
|
||||||
PARAMS=("${PARAMS[@]/$PARAM}") #Quotes when working with strings
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
PARAMS=${PARAMS[@]}
|
|
||||||
|
|
||||||
HOST="${PWD##*/}"
|
|
||||||
|
|
||||||
# Find the first linked site for the current dir, if one exists
|
|
||||||
for linkname in ~/.config/valet/Sites/*; do
|
|
||||||
if [[ "$(readlink $linkname)" = "$PWD" ]]
|
|
||||||
then
|
|
||||||
HOST="${linkname##*/}"
|
|
||||||
break
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
TLD=$("$PHP" "$DIR/cli/valet.php" tld)
|
|
||||||
|
|
||||||
# Decide the correct PORT: uses 443 for secure, else 80
|
# Decide the correct PORT: uses 443 for secure, else 80
|
||||||
if grep --quiet --no-messages 443 ~/.config/valet/Nginx/$HOST*
|
if $SECURED
|
||||||
then
|
then
|
||||||
PORT=443
|
PORT=443
|
||||||
else
|
else
|
||||||
PORT=80
|
PORT=80
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Lowercase the host to match how the rest of our domains are looked up
|
|
||||||
HOST=$(echo "$HOST" | tr '[:upper:]' '[:lower:]')
|
|
||||||
|
|
||||||
sudo -u "$USER" expose share "$HOST.$TLD:$PORT" $PARAMS
|
sudo -u "$USER" expose share "$HOST.$TLD:$PORT" $PARAMS
|
||||||
|
|
||||||
exit
|
exit
|
||||||
|
|
||||||
|
elif [[ $SHARETOOL = "cloudflared" ]]
|
||||||
|
then
|
||||||
|
# cloudflared
|
||||||
|
if $SECURED
|
||||||
|
then
|
||||||
|
SCHEME="https"
|
||||||
|
else
|
||||||
|
SCHEME="http"
|
||||||
|
fi
|
||||||
|
echo "Scheme: $SCHEME"
|
||||||
|
sudo -u "$USER" cloudflared tunnel --no-tls-verify --url "$SCHEME://localhost" --http-host-header "$HOST.$TLD" $PARAMS
|
||||||
|
|
||||||
|
exit
|
||||||
|
|
||||||
else
|
else
|
||||||
echo ''
|
echo ''
|
||||||
echo "Please use 'valet share-tool ngrok' or 'valet share-tool expose'"
|
echo "Please use 'valet share-tool cloudflared', 'valet share-tool expose' or 'valet share-tool ngrok'"
|
||||||
echo "to set your preferred share tool."
|
echo "to set your preferred share tool."
|
||||||
exit
|
exit
|
||||||
fi
|
fi
|
||||||
|
|||||||
Reference in New Issue
Block a user