Skip to content

Commit 2ea79e6

Browse files
committed
Fixed fetch-share-url command and replaced dependancy...
- Replaced the outdated `nategood/httpful` with `guzzlehttp/guzzle` package for REST API requests. - Added a `--debug` option to the `share` command to prevent opening of a new CMD window, and allow error messages to be displayed from Ngrok. This is because Ngrok may fail silentyly by opening a new CMD window and quickly closes it if it encounters an error, so no errors are outputted. - Fixed `fetch-share-url` command: - Option `domain` to `site` to properly reference the project, and not get confused with the ngrok domain. - Output messages. - Use Guzzle to perform a API get request to get the current ngrok tunnel public URL, and copy it to the clipboard. - Changed findHttpTunnelUrl function to use the array bracket notation on the array, as it's not an object anymore.
1 parent e30fd4f commit 2ea79e6

3 files changed

Lines changed: 36 additions & 25 deletions

File tree

cli/Valet/Ngrok.php

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Valet;
44

55
use DomainException;
6-
use Httpful\Request;
6+
use GuzzleHttp\Client;
77
use Illuminate\Support\Collection;
88

99
class Ngrok
@@ -49,7 +49,7 @@ public function run(string $command)
4949
* @param array $options
5050
* @return void
5151
*/
52-
public function start(string $site, int $port, array $options = [])
52+
public function start(string $site, int $port, $debug = false, array $options = [])
5353
{
5454
if ($port === 443 && !$this->hasAuthToken()) {
5555
output('Forwarding to local port 443 or a local https:// URL is only available after you sign up.
@@ -67,7 +67,9 @@ public function start(string $site, int $port, array $options = [])
6767
$newCMDtitle = "\"Sharing $site\"";
6868
$ngrokCommand = "\"$ngrok\" http $site:$port " . $this->getNgrokConfig() . " $options";
6969

70-
$this->cli->passthru("start $newCMDtitle $ngrokCommand");
70+
$newCMD = ($debug) ? "" : "start $newCMDtitle";
71+
72+
$this->cli->passthru("$newCMD $ngrokCommand");
7173
}
7274

7375
/**
@@ -83,26 +85,30 @@ public function getNgrokConfig()
8385

8486
/**
8587
* Get the current tunnel URL from the Ngrok API.
88+
* @param string $site The site
8689
*
87-
* @return string
90+
* @return string $url The current tunnel URL
8891
*/
89-
public function currentTunnelUrl(string $domain = null)
92+
public function currentTunnelUrl(string $site)
9093
{
91-
// wait a second for ngrok to start before attempting to find available tunnels
92-
// sleep(1);
94+
// Set a new GuzzleHttp client.
95+
$client = new Client();
9396

94-
foreach ($this->tunnelsEndpoints as $endpoint) {
95-
$response = retry(20, function () use ($endpoint, $domain) {
96-
$body = Request::get($endpoint)->send()->body;
97+
// Create a GuzzleHttp get request to the ngrok tunnels API.
98+
$response = $client->get('http://127.0.0.1:4040/api/tunnels');
9799

98-
if (isset($body->tunnels) && count($body->tunnels) > 0) {
99-
return $this->findHttpTunnelUrl($body->tunnels, $domain);
100-
}
101-
}, 250);
100+
// Get the contents of the API response.
101+
$response = $response->getBody()->getContents();
102+
// Decode the json response into a properly formed PHP array.
103+
$tunnels = json_decode($response, true);
102104

103-
if (!empty($response)) {
104-
return $response;
105-
}
105+
// Find and get the public URL of the site.
106+
$url = $this->findHttpTunnelUrl($tunnels["tunnels"], $site);
107+
108+
if (!empty($url)) {
109+
// Use | clip to copy the URL to the clipboard.
110+
$this->cli->passthru("echo $url | clip");
111+
return $url;
106112
}
107113

108114
throw new DomainException('Tunnel not established.');
@@ -121,8 +127,8 @@ public function findHttpTunnelUrl(array $tunnels, string $domain = null)
121127
// find the one responding on HTTP. Each tunnel has an HTTP and a HTTPS address
122128
// but for local dev purposes we just desire the plain HTTP URL endpoint.
123129
foreach ($tunnels as $tunnel) {
124-
if ($tunnel->proto === 'http' && strpos($tunnel->config->addr, $domain)) {
125-
return $tunnel->public_url;
130+
if (($tunnel["proto"] === 'http' || $tunnel["proto"] === 'https') && strpos($tunnel["config"]["addr"], $domain)) {
131+
return $tunnel["public_url"];
126132
}
127133
}
128134
}

cli/valet.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -478,12 +478,12 @@
478478
*/
479479
// TODO: custom domain and ngrok params
480480
$app->command(
481-
'share [site] [--domain=] [--host-header=] [--region=]',
482-
function ($site = null, $domain = null, $hostheader = null, $region = null) {
481+
'share [site] [--domain=] [--host-header=] [--region=] [--debug]',
482+
function ($site = null, $domain = null, $hostheader = null, $region = null, $debug) {
483483

484484
$url = ($site ?: strtolower(Site::host(getcwd()))) . '.' . Configuration::read()['tld'];
485485

486-
Ngrok::start($url, Site::port($url), array_filter([
486+
Ngrok::start($url, Site::port($url), $debug, array_filter([
487487
'domain' => $domain,
488488
'host-header' => $hostheader,
489489
'region' => $region,
@@ -496,8 +496,13 @@ function ($site = null, $domain = null, $hostheader = null, $region = null) {
496496
/**
497497
* Echo the currently tunneled URL.
498498
*/
499-
$app->command('fetch-share-url [domain]', function ($domain = null) {
500-
output(Ngrok::currentTunnelUrl($domain ?: Site::host(getcwd()) . '.' . Configuration::read()['tld']));
499+
$app->command('fetch-share-url [site]', function ($site = null) {
500+
$site = $site ?: Site::host(getcwd()) . '.' . Configuration::read()['tld'];
501+
502+
$url = Ngrok::currentTunnelUrl($site);
503+
info("The public URL for $site is: <fg=blue>$url</>");
504+
info("It has been copied to your clipboard.");
505+
501506
})->descriptions('Get the URL to the current Ngrok tunnel');
502507

503508
/**

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"illuminate/container": "^8.0|^9.0",
4646
"mnapoli/silly": "^1.7",
4747
"symfony/process": "^4.0|^5.0|^6.0",
48-
"nategood/httpful": "^0.3",
48+
"guzzlehttp/guzzle": "^7.7",
4949
"phpseclib/phpseclib": "^3.0",
5050
"illuminate/collections": "^8.0|^9.0"
5151
},

0 commit comments

Comments
 (0)