From bd5c998019b2895a05d7894c1aded02d1b3c3084 Mon Sep 17 00:00:00 2001 From: Chris Brown Date: Fri, 9 Dec 2016 10:31:23 -0500 Subject: [PATCH 1/6] Change `valet domain` command to `valet tld` Fixes #144 ".dev" is really a TLD, not a "domain" in the conventional sense. Changing the command to `valet tld` more accurately reflects the purpose of the command (to set or get the configured TLD served by Valet) --- cli/Valet/DnsMasq.php | 2 +- cli/Valet/Nginx.php | 4 ++-- cli/Valet/Site.php | 10 +++++----- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/cli/Valet/DnsMasq.php b/cli/Valet/DnsMasq.php index a494257..38e19c4 100644 --- a/cli/Valet/DnsMasq.php +++ b/cli/Valet/DnsMasq.php @@ -120,7 +120,7 @@ function createDomainResolver($domain) } /** - * Update the domain used by DnsMasq. + * Update the domain (TLD) used by DnsMasq. * * @param string $oldDomain * @param string $newDomain diff --git a/cli/Valet/Nginx.php b/cli/Valet/Nginx.php index 8680ce9..f6002f6 100644 --- a/cli/Valet/Nginx.php +++ b/cli/Valet/Nginx.php @@ -130,9 +130,9 @@ function ($exitCode, $outputMessage) { */ function rewriteSecureNginxFiles() { - $domain = $this->configuration->read()['domain']; + $tld = $this->configuration->read()['tld']; - $this->site->resecureForNewDomain($domain, $domain); + $this->site->resecureForNewTld($tld, $tld); } /** diff --git a/cli/Valet/Site.php b/cli/Valet/Site.php index 833ef7d..e742b5e 100644 --- a/cli/Valet/Site.php +++ b/cli/Valet/Site.php @@ -136,13 +136,13 @@ function pruneLinks() } /** - * Resecure all currently secured sites with a fresh domain. + * Resecure all currently secured sites with a fresh tld. * - * @param string $oldDomain - * @param string $domain + * @param string $oldTld + * @param string $tld * @return void */ - function resecureForNewDomain($oldDomain, $domain) + function resecureForNewTld($oldTld, $tld) { if (! $this->files->exists($this->certificatesPath())) { return; @@ -155,7 +155,7 @@ function resecureForNewDomain($oldDomain, $domain) } foreach ($secured as $url) { - $this->secure(str_replace('.'.$oldDomain, '.'.$domain, $url)); + $this->secure(str_replace('.'.$oldTld, '.'.$tld, $url)); } } From 9394811a09fe641f1cb5214a7ce913a5ac249bf0 Mon Sep 17 00:00:00 2001 From: Chris Brown Date: Fri, 9 Dec 2016 11:20:43 -0500 Subject: [PATCH 2/6] Make `valet install` handle the addition of `tld` to `config.json` --- cli/Valet/Configuration.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/cli/Valet/Configuration.php b/cli/Valet/Configuration.php index be4a97c..c9eee92 100644 --- a/cli/Valet/Configuration.php +++ b/cli/Valet/Configuration.php @@ -111,7 +111,15 @@ function createCertificatesDirectory() function writeBaseConfiguration() { if (! $this->files->exists($this->path())) { - $this->write(['domain' => 'test', 'paths' => []]); + $this->write(['tld' => 'test', 'paths' => []]); + } + + /** + * For upgrades, change config key 'domain' to 'tld' + */ + $config = $this->read(); + if (isset($config['domain']) && !isset($config['tld'])) { + $this->updateKey('tld', $config['domain']); } } From 58e4645fe279f4a79e5c6e598c3f2e27d3923606 Mon Sep 17 00:00:00 2001 From: Chris Brown Date: Tue, 17 Oct 2017 15:47:39 -0400 Subject: [PATCH 3/6] Change `valet domain` command to `valet tld` Fixes #144 ".test" or ".dev" is really a TLD, not a "domain" in the conventional sense. Changing the command to `valet tld` more accurately reflects the purpose of the command (to set or get the configured TLD served by Valet) The use of `valet domain` is currently preserved as an alias for `valet tld`, but will be removed at a later date. --- cli/Valet/DnsMasq.php | 34 ++++++++++++++++++---------------- cli/Valet/Site.php | 2 +- cli/valet.php | 28 ++++++++++++++-------------- server.php | 8 ++++---- tests/DnsMasqTest.php | 4 ++-- tests/NginxTest.php | 4 ++-- 6 files changed, 41 insertions(+), 39 deletions(-) diff --git a/cli/Valet/DnsMasq.php b/cli/Valet/DnsMasq.php index 38e19c4..5f823d9 100644 --- a/cli/Valet/DnsMasq.php +++ b/cli/Valet/DnsMasq.php @@ -33,27 +33,29 @@ function __construct(Brew $brew, CommandLine $cli, Filesystem $files) * * @return void */ - function install($domain = 'test') + function install($tld = 'test') { $this->brew->ensureInstalled('dnsmasq'); // For DnsMasq, we create our own custom configuration file which will be imported // in the main DnsMasq file. This allows Valet to make changes to our own files // without needing to modify the "primary" DnsMasq configuration files again. - $this->createCustomConfigFile($domain); + $this->createCustomConfigFile($tld); - $this->createDomainResolver($domain); + $this->createTldResolver($tld); $this->brew->restartService('dnsmasq'); + + info('Valet is configured to serve for TLD [.'.$tld.']'); } /** * Append the custom DnsMasq configuration file to the main configuration file. * - * @param string $domain + * @param string $tld * @return void */ - function createCustomConfigFile($domain) + function createCustomConfigFile($tld) { $customConfigPath = $this->customConfigPath(); @@ -61,7 +63,7 @@ function createCustomConfigFile($domain) $this->appendCustomConfigImport($customConfigPath); - $this->files->putAsUser($customConfigPath, 'address=/.'.$domain.'/127.0.0.1'.PHP_EOL.'listen-address=127.0.0.1'.PHP_EOL); + $this->files->putAsUser($customConfigPath, 'address=/.'.$tld.'/127.0.0.1'.PHP_EOL.'listen-address=127.0.0.1'.PHP_EOL); } /** @@ -107,30 +109,30 @@ function customConfigIsBeingImported($customConfigPath) } /** - * Create the resolver file to point the configured domain to 127.0.0.1. + * Create the resolver file to point the configured TLD to 127.0.0.1. * - * @param string $domain + * @param string $tld * @return void */ - function createDomainResolver($domain) + function createTldResolver($tld) { $this->files->ensureDirExists($this->resolverPath); - $this->files->put($this->resolverPath.'/'.$domain, 'nameserver 127.0.0.1'.PHP_EOL); + $this->files->put($this->resolverPath.'/'.$tld, 'nameserver 127.0.0.1'.PHP_EOL); } /** - * Update the domain (TLD) used by DnsMasq. + * Update the TLD/domain resolved by DnsMasq. * - * @param string $oldDomain - * @param string $newDomain + * @param string $oldTld + * @param string $newTld * @return void */ - function updateDomain($oldDomain, $newDomain) + function updateTld($oldTld, $newTld) { - $this->files->unlink($this->resolverPath.'/'.$oldDomain); + $this->files->unlink($this->resolverPath.'/'.$oldTld); - $this->install($newDomain); + $this->install($newTld); } /** diff --git a/cli/Valet/Site.php b/cli/Valet/Site.php index e742b5e..2a76190 100644 --- a/cli/Valet/Site.php +++ b/cli/Valet/Site.php @@ -104,7 +104,7 @@ function getLinks($path, $certs) return [$site => $this->files->readLink($path.'/'.$site)]; })->map(function ($path, $site) use ($certs, $config) { $secured = $certs->has($site); - $url = ($secured ? 'https': 'http').'://'.$site.'.'.$config['domain']; + $url = ($secured ? 'https': 'http').'://'.$site.'.'.$config['tld']; return [$site, $secured ? ' X': '', $url, $path]; }); diff --git a/cli/valet.php b/cli/valet.php index a15f37c..e504760 100755 --- a/cli/valet.php +++ b/cli/valet.php @@ -40,7 +40,7 @@ Configuration::install(); Nginx::install(); PhpFpm::install(); - DnsMasq::install(Configuration::read()['domain']); + DnsMasq::install(Configuration::read()['tld']); Nginx::restart(); Valet::symlinkToUsersBin(); @@ -52,25 +52,25 @@ */ if (is_dir(VALET_HOME_PATH)) { /** - * Get or set the domain currently being used by Valet. + * Get or set the TLD currently being used by Valet. */ - $app->command('domain [domain]', function ($domain = null) { - if ($domain === null) { - return info(Configuration::read()['domain']); + $app->command('tld [tld]', function ($tld = null) { + if ($tld === null) { + return info('Valet is configured to serve for TLD: .'.Configuration::read()['tld']); } - DnsMasq::updateDomain( - $oldDomain = Configuration::read()['domain'], $domain = trim($domain, '.') + DnsMasq::updateTld( + $oldTld = Configuration::read()['tld'], $tld = trim($tld, '.') ); - Configuration::updateKey('domain', $domain); + Configuration::updateKey('tld', $tld); - Site::resecureForNewDomain($oldDomain, $domain); + Site::resecureForNewTld($oldTld, $tld); PhpFpm::restart(); Nginx::restart(); - info('Your Valet domain has been updated to ['.$domain.'].'); - })->descriptions('Get or set the domain used for Valet sites'); + info('Your Valet TLD has been updated to ['.$tld.'].'); + }, ['domain'])->descriptions('Get or set the TLD used for Valet sites. (Currently: .'.Configuration::read()['tld'].')'); /** * Add the current working directory to the paths configuration. @@ -125,7 +125,7 @@ * Secure the given domain with a trusted TLS certificate. */ $app->command('secure [domain]', function ($domain = null) { - $url = ($domain ?: Site::host(getcwd())).'.'.Configuration::read()['domain']; + $url = ($domain ?: Site::host(getcwd())).'.'.Configuration::read()['tld']; Site::secure($url); @@ -140,7 +140,7 @@ * Stop serving the given domain over HTTPS and remove the trusted TLS certificate. */ $app->command('unsecure [domain]', function ($domain = null) { - $url = ($domain ?: Site::host(getcwd())).'.'.Configuration::read()['domain']; + $url = ($domain ?: Site::host(getcwd())).'.'.Configuration::read()['tld']; Site::unsecure($url); @@ -183,7 +183,7 @@ * Open the current or given directory in the browser. */ $app->command('open [domain]', function ($domain = null) { - $url = "http://".($domain ?: Site::host(getcwd())).'.'.Configuration::read()['domain']; + $url = "http://".($domain ?: Site::host(getcwd())).'.'.Configuration::read()['tld']; CommandLine::runAsUser("open ".escapeshellarg($url)); })->descriptions('Open the site for the current (or specified) directory in your browser'); diff --git a/server.php b/server.php index 90978f8..88ee1c5 100644 --- a/server.php +++ b/server.php @@ -18,9 +18,9 @@ function show_valet_404() } /** - * @param $domain string Domain to filter - * - * @return string Filtered domain (without wildcard dns feature (xip.io/nip.io)) + * You may use wildcard DNS providers xip.io or nip.io as a tool for testing your site via an IP address. + * To use it, first `valet link 192.168.3.3` (whatever your local IP is) inside your current project. + * Then you can reach your project's dev site by visiting http://your-local-ip.xip.io (or nip.io). */ function valet_support_wildcard_dns($domain) { @@ -52,7 +52,7 @@ function valet_support_wildcard_dns($domain) $siteName = basename( // Filter host to support wildcard dns feature valet_support_wildcard_dns($_SERVER['HTTP_HOST']), - '.'.$valetConfig['domain'] + '.'.$valetConfig['tld'] ); if (strpos($siteName, 'www.') === 0) { diff --git a/tests/DnsMasqTest.php b/tests/DnsMasqTest.php index 91868fc..fc3cc02 100644 --- a/tests/DnsMasqTest.php +++ b/tests/DnsMasqTest.php @@ -50,13 +50,13 @@ public function test_install_installs_and_places_configuration_files_in_proper_l } - public function test_update_domain_removes_old_resolver_and_reinstalls() + public function test_update_tld_removes_old_resolver_and_reinstalls() { $cli = Mockery::mock(CommandLine::class); $cli->shouldReceive('quietly')->with('rm /etc/resolver/old'); $dnsMasq = Mockery::mock(DnsMasq::class.'[install]', [resolve(Brew::class), $cli, new Filesystem]); $dnsMasq->shouldReceive('install')->with('new'); - $dnsMasq->updateDomain('old', 'new'); + $dnsMasq->updateTld('old', 'new'); } } diff --git a/tests/NginxTest.php b/tests/NginxTest.php index 41877c1..ae297c9 100644 --- a/tests/NginxTest.php +++ b/tests/NginxTest.php @@ -78,13 +78,13 @@ public function test_install_nginx_directories_rewrites_secure_nginx_files() $files->shouldReceive('putAsUser')->with(VALET_HOME_PATH.'/Nginx/.keep', "\n")->once(); swap(Filesystem::class, $files); - swap(Configuration::class, $config = Mockery::spy(Configuration::class, ['read' => ['domain' => 'test']])); + swap(Configuration::class, $config = Mockery::spy(Configuration::class, ['read' => ['tld' => 'test']])); swap(Site::class, $site = Mockery::spy(Site::class)); $nginx = resolve(Nginx::class); $nginx->installNginxDirectory(); - $site->shouldHaveReceived('resecureForNewDomain', ['test', 'test']); + $site->shouldHaveReceived('resecureForNewTld', ['test', 'test']); } } From 3f1c5fae60dd3b0ed7536a15a9415c2cf4426c91 Mon Sep 17 00:00:00 2001 From: Chris Brown Date: Tue, 17 Oct 2017 20:33:39 -0400 Subject: [PATCH 4/6] Fix bug with certificate search for TLDs of other than 3 chars The `substr` of `-8` was based on `.dev.crt`, but since the TLD has always been configurable, this should have always been dynamic. Running `valet links` will not show `https` or the `TLS` columns correctly if the substring matching is of the wrong length. --- cli/Valet/Site.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cli/Valet/Site.php b/cli/Valet/Site.php index 2a76190..e06d9e2 100644 --- a/cli/Valet/Site.php +++ b/cli/Valet/Site.php @@ -85,7 +85,8 @@ function getCertificates($path) return collect($this->files->scanDir($path))->filter(function ($value, $key) { return ends_with($value, '.crt'); })->map(function ($cert) { - return substr($cert, 0, -8); + $tld = $this->config->read()['tld']; + return substr($cert, 0, -(strlen($tld)+5)); })->flip(); } From 2c56a080e1dad61ded524bfd41233df27c240fdf Mon Sep 17 00:00:00 2001 From: Chris Brown Date: Tue, 17 Oct 2017 21:55:12 -0400 Subject: [PATCH 5/6] Fix xip.io / nip.io instructions (don't need to create a link to use it) --- server.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server.php b/server.php index 88ee1c5..de281ac 100644 --- a/server.php +++ b/server.php @@ -19,8 +19,8 @@ function show_valet_404() /** * You may use wildcard DNS providers xip.io or nip.io as a tool for testing your site via an IP address. - * To use it, first `valet link 192.168.3.3` (whatever your local IP is) inside your current project. - * Then you can reach your project's dev site by visiting http://your-local-ip.xip.io (or nip.io). + * It's simple to use: First determine the IP address of your local computer (like 192.168.0.10). + * Then simply use http://project.your-ip.xip.io - ie: http://laravel.192.168.0.10.xip.io */ function valet_support_wildcard_dns($domain) { From a7d8f32073c2b2076ad04510f02666463b0bf377 Mon Sep 17 00:00:00 2001 From: Chris Brown Date: Wed, 22 Aug 2018 13:40:32 -0400 Subject: [PATCH 6/6] Fix merge error The domain-name determination using strrpos wasn't very efficient. Stripping the `.crt` from the end of the filename is easier to parse. Now all it returns is the first part of the string up until the final `.`, treating the remainder as whatever tld was used for the domain, regardless of what tld valet is configured for. --- cli/Valet/Site.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cli/Valet/Site.php b/cli/Valet/Site.php index 0ed16f6..f7f2037 100644 --- a/cli/Valet/Site.php +++ b/cli/Valet/Site.php @@ -86,8 +86,8 @@ function getCertificates($path) return collect($this->files->scandir($path))->filter(function ($value, $key) { return ends_with($value, '.crt'); })->map(function ($cert) { - $tld = $this->config->read()['tld']; - return substr($cert, 0, strripos($tld, '.', -5)); + $certWithoutSuffix = substr($cert, 0, -4); + return substr($certWithoutSuffix, 0, strrpos($certWithoutSuffix, '.')); })->flip(); }