diff --git a/cli/Valet/Nginx.php b/cli/Valet/Nginx.php index 552d100..28daa67 100644 --- a/cli/Valet/Nginx.php +++ b/cli/Valet/Nginx.php @@ -98,7 +98,7 @@ function replaceLoopback($siteConf) return $siteConf; } - $str = '#listen VALET_LOOPBACK:80 default_server; # valet loopback'; + $str = '#listen VALET_LOOPBACK:80; # valet loopback'; return str_replace( $str, @@ -150,8 +150,9 @@ function rewriteSecureNginxFiles() $tld = $this->configuration->read()['tld']; $loopback = $this->configuration->read()['loopback']; - $this->site->resecureForNewTld($tld, $tld); - $this->site->resecureForNewLoopback($loopback, $loopback); + $config = compact('tld', 'loopback'); + + $this->site->resecureForNewConfiguration($config, $config); } /** diff --git a/cli/Valet/Site.php b/cli/Valet/Site.php index ac9ff4d..d30866b 100644 --- a/cli/Valet/Site.php +++ b/cli/Valet/Site.php @@ -328,13 +328,13 @@ function pruneLinks() } /** - * Resecure all currently secured sites with a fresh tld. + * Resecure all currently secured sites with a fresh configuration. * - * @param string $oldTld - * @param string $tld + * @param array $old + * @param array $new * @return void */ - function resecureForNewTld($oldTld, $tld) + function resecureForNewConfiguration(array $old, array $new) { if (! $this->files->exists($this->certificatesPath())) { return; @@ -342,6 +342,14 @@ function resecureForNewTld($oldTld, $tld) $secured = $this->secured(); + $defaultTld = $this->config->read()['tld']; + $oldTld = isset($old['tld']) && ! empty($old['tld']) ? $old['tld'] : $defaultTld; + $tld = isset($new['tld']) && ! empty($new['tld']) ? $new['tld'] : $defaultTld; + + $defaultLoopback = $this->config->read()['loopback']; + $oldLoopback = isset($old['loopback']) && ! empty($old['loopback']) ? $old['loopback'] : $defaultLoopback; + $loopback = isset($new['loopback']) && ! empty($new['loopback']) ? $new['loopback'] : $defaultLoopback; + foreach ($secured as $url) { $newUrl = str_replace('.'.$oldTld, '.'.$tld, $url); $siteConf = $this->getSiteConfigFileContents($url, '.'.$oldTld); @@ -349,7 +357,14 @@ function resecureForNewTld($oldTld, $tld) if (!empty($siteConf) && strpos($siteConf, '# valet stub: proxy.valet.conf') === 0) { // proxy config $this->unsecure($url); - $this->secure($newUrl, $this->replaceOldDomainWithNew($siteConf, $url, $newUrl)); + $this->secure( + $newUrl, + $this->replaceOldLoopbackWithNew( + $this->replaceOldDomainWithNew($siteConf, $url, $newUrl), + $oldLoopback, + $loopback + ) + ); } else { // normal config $this->unsecure($url); @@ -384,36 +399,6 @@ function replaceOldDomainWithNew($siteConf, $old, $new) return $siteConf; } - /** - * Resecure all currently secured sites with a fresh loopback address. - * - * @param string $oldLoopback - * @param string $loopback - * @return void - */ - function resecureForNewLoopback($oldLoopback, $loopback) - { - if (! $this->files->exists($this->certificatesPath())) { - return; - } - - $secured = $this->secured(); - - foreach ($secured as $url) { - $siteConf = $this->getSiteConfigFileContents($url); - - if (!empty($siteConf) && strpos($siteConf, '# valet stub: proxy.valet.conf') === 0) { - // proxy config - $this->unsecure($url); - $this->secure($url, $this->replaceOldLoopbackWithNew($siteConf, $oldLoopback, $loopback)); - } else { - // normal config - $this->unsecure($url); - $this->secure($url); - } - } - } - /** * Parse Nginx site config file contents to swap old loopback address to new. * @@ -639,13 +624,16 @@ function buildCertificateConf($path, $url) function buildSecureNginxServer($url, $siteConf = null) { if ($siteConf === null) { - $siteConf = $this->files->get(__DIR__.'/../stubs/secure.valet.conf'); + $siteConf = $this->replaceOldLoopbackWithNew( + $this->files->get(__DIR__.'/../stubs/secure.valet.conf'), + 'VALET_LOOPBACK', + $this->valetLoopback() + ); } return str_replace( - ['VALET_LOOPBACK', 'VALET_HOME_PATH', 'VALET_SERVER_PATH', 'VALET_STATIC_PREFIX', 'VALET_SITE', 'VALET_CERT', 'VALET_KEY'], + ['VALET_HOME_PATH', 'VALET_SERVER_PATH', 'VALET_STATIC_PREFIX', 'VALET_SITE', 'VALET_CERT', 'VALET_KEY'], [ - $this->valetLoopback(), $this->valetHomePath(), VALET_SERVER_PATH, VALET_STATIC_PREFIX, @@ -731,11 +719,15 @@ function proxyCreate($url, $host) $url .= '.'.$tld; } - $siteConf = $this->files->get(__DIR__.'/../stubs/proxy.valet.conf'); + $siteConf = $this->replaceOldLoopbackWithNew( + $this->files->get(__DIR__.'/../stubs/proxy.valet.conf'), + 'VALET_LOOPBACK', + $this->valetLoopback() + ); $siteConf = str_replace( - ['VALET_LOOPBACK', 'VALET_HOME_PATH', 'VALET_SERVER_PATH', 'VALET_STATIC_PREFIX', 'VALET_SITE', 'VALET_PROXY_HOST'], - [$this->valetLoopback(), $this->valetHomePath(), VALET_SERVER_PATH, VALET_STATIC_PREFIX, $url, $host], + ['VALET_HOME_PATH', 'VALET_SERVER_PATH', 'VALET_STATIC_PREFIX', 'VALET_SITE', 'VALET_PROXY_HOST'], + [$this->valetHomePath(), VALET_SERVER_PATH, VALET_STATIC_PREFIX, $url, $host], $siteConf ); @@ -811,7 +803,7 @@ function updatePlist($loopback) str_replace( 'VALET_LOOPBACK', $loopback, - $this->files->get(__DIR__.'/../stubs/ifconfig.plist') + $this->files->get(__DIR__.'/../stubs/loopback.plist') ) ); @@ -836,7 +828,7 @@ function valetLoopback() */ function plistPath() { - return '/Library/LaunchDaemons/com.laravel.valet.ifconfig.plist'; + return '/Library/LaunchDaemons/com.laravel.valet.loopback.plist'; } /** diff --git a/cli/stubs/ifconfig.plist b/cli/stubs/loopback.plist similarity index 90% rename from cli/stubs/ifconfig.plist rename to cli/stubs/loopback.plist index 501ffb7..4c75147 100644 --- a/cli/stubs/ifconfig.plist +++ b/cli/stubs/loopback.plist @@ -3,7 +3,7 @@ Label - com.laravel.valet.ifconfig + com.laravel.valet.loopback RunAtLoad ProgramArguments diff --git a/cli/valet.php b/cli/valet.php index d5a42ac..b63338d 100755 --- a/cli/valet.php +++ b/cli/valet.php @@ -66,16 +66,9 @@ */ if (is_dir(VALET_HOME_PATH)) { /** - * Upgrade helper: ensure the tld config exists + * Upgrade helper: ensure the tld config exists or the loopback config exists */ - if (empty(Configuration::read()['tld'])) { - Configuration::writeBaseConfiguration(); - } - - /** - * Upgrade helper: ensure the loopback config exists - */ - if (empty(Configuration::read()['loopback'])) { + if (empty(Configuration::read()['tld']) || empty(Configuration::read()['loopback'])) { Configuration::writeBaseConfiguration(); } @@ -93,7 +86,7 @@ Configuration::updateKey('tld', $tld); - Site::resecureForNewTld($oldTld, $tld); + Site::resecureForNewConfiguration(['tld' => $oldTld], ['tld' => $tld]); PhpFpm::restart(); Nginx::restart(); @@ -114,9 +107,9 @@ DnsMasq::refreshConfiguration(); Site::aliasLoopback($oldLoopback, $loopback); - Site::resecureForNewLoopback($oldLoopback, $loopback); - Nginx::installServer(); + Site::resecureForNewConfiguration(['loopback' => $oldLoopback], ['loopback' => $loopback]); PhpFpm::restart(); + Nginx::installServer(); Nginx::restart(); info('Your valet loopback address has been updated to ['.$loopback.']'); diff --git a/tests/NginxTest.php b/tests/NginxTest.php index ed1a222..0af1cde 100644 --- a/tests/NginxTest.php +++ b/tests/NginxTest.php @@ -82,6 +82,8 @@ public function test_install_nginx_directories_rewrites_secure_nginx_files() $nginx = resolve(Nginx::class); $nginx->installNginxDirectory(); - $site->shouldHaveReceived('resecureForNewTld', ['test', 'test']); + $data = ['tld' => 'test', 'loopback' => '127.0.0.1']; + + $site->shouldHaveReceived('resecureForNewConfiguration', [$data, $data]); } }