From 0c0ab726c024c33073792c3ae803f6bb9524af86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Popowicz?= Date: Mon, 22 Feb 2021 18:43:39 +0100 Subject: [PATCH] :bug: Fixing Nginx configuration. :sparkles: Adding alias generation and launch daemon generation. --- cli/Valet/Nginx.php | 25 ++++++++--- cli/Valet/Site.php | 83 +++++++++++++++++++++++++++++++++++-- cli/stubs/ifconfig.plist | 17 ++++++++ cli/stubs/proxy.valet.conf | 9 ++-- cli/stubs/secure.valet.conf | 9 ++-- cli/stubs/valet.conf | 3 +- cli/valet.php | 1 + 7 files changed, 132 insertions(+), 15 deletions(-) create mode 100644 cli/stubs/ifconfig.plist diff --git a/cli/Valet/Nginx.php b/cli/Valet/Nginx.php index e7a704b..552d100 100644 --- a/cli/Valet/Nginx.php +++ b/cli/Valet/Nginx.php @@ -75,14 +75,12 @@ function installServer() { $this->files->ensureDirExists(BREW_PREFIX.'/etc/nginx/valet'); - $loopback = $this->configuration->read()['loopback']; - $this->files->putAsUser( BREW_PREFIX.'/etc/nginx/valet/valet.conf', str_replace( - ['VALET_LOOPBACK', 'VALET_HOME_PATH', 'VALET_SERVER_PATH', 'VALET_STATIC_PREFIX'], - [$loopback, VALET_HOME_PATH, VALET_SERVER_PATH, VALET_STATIC_PREFIX], - $this->files->get(__DIR__.'/../stubs/valet.conf') + ['VALET_HOME_PATH', 'VALET_SERVER_PATH', 'VALET_STATIC_PREFIX'], + [VALET_HOME_PATH, VALET_SERVER_PATH, VALET_STATIC_PREFIX], + $this->replaceLoopback($this->files->get(__DIR__.'/../stubs/valet.conf')) ) ); @@ -92,6 +90,23 @@ function installServer() ); } + function replaceLoopback($siteConf) + { + $loopback = $this->configuration->read()['loopback']; + + if ($loopback === VALET_LOOPBACK) { + return $siteConf; + } + + $str = '#listen VALET_LOOPBACK:80 default_server; # valet loopback'; + + return str_replace( + $str, + substr(str_replace('VALET_LOOPBACK', $loopback, $str), 1), + $siteConf + ); + } + /** * Install the Nginx configuration directory to the ~/.config/valet directory. * diff --git a/cli/Valet/Site.php b/cli/Valet/Site.php index a9faba2..ac9ff4d 100644 --- a/cli/Valet/Site.php +++ b/cli/Valet/Site.php @@ -424,15 +424,26 @@ function resecureForNewLoopback($oldLoopback, $loopback) */ function replaceOldLoopbackWithNew($siteConf, $old, $new) { + $shouldComment = $new === VALET_LOOPBACK; + $lookups = []; - $lookups[] = '~listen .*:80;~'; - $lookups[] = '~listen .*:443 ssl http2;~'; - $lookups[] = '~listen .*:60;~'; + $lookups[] = '~#?listen .*:80; # valet loopback~'; + $lookups[] = '~#?listen .*:443 ssl http2; # valet loopback~'; + $lookups[] = '~#?listen .*:60; # valet loopback~'; foreach ($lookups as $lookup) { preg_match($lookup, $siteConf, $matches); foreach ($matches as $match) { $replaced = str_replace($old, $new, $match); + + if ($shouldComment && strpos($replaced, '#') !== 0) { + $replaced = '#'.$replaced; + } + + if (! $shouldComment) { + $replaced = ltrim($replaced, '#'); + } + $siteConf = str_replace($match, $replaced, $siteConf); } } @@ -752,6 +763,62 @@ function proxyDelete($url) info('Valet will no longer proxy [https://'.$url.'].'); } + /** + * Remove old loopback interface alias and new one if necessary. + * + * @param string $oldLoopback + * @param string $loopback + * @return void + */ + function aliasLoopback($oldLoopback, $loopback) + { + if ($oldLoopback !== VALET_LOOPBACK) { + $this->cli->run(sprintf( + 'sudo ifconfig lo0 -alias %s', $oldLoopback + )); + + info('Old loopback interface alias removed ['.$oldLoopback.']'); + } + + if ($loopback !== VALET_LOOPBACK) { + $this->cli->run(sprintf( + 'sudo ifconfig lo0 alias %s', $loopback + )); + + info('New loopback interface alias added ['.$loopback.']'); + } + + $this->updatePlist($loopback); + } + + /** + * Remove old LaunchDaemon and create a new one if necessary. + * + * @param string $loopback + * @return void + */ + function updatePlist($loopback) + { + if ($this->files->exists($this->plistPath())) { + $this->files->unlink($this->plistPath()); + + info('Old plist file removed ['.$this->plistPath().']'); + } + + if ($loopback !== VALET_LOOPBACK) { + $this->files->put( + $this->plistPath(), + str_replace( + 'VALET_LOOPBACK', + $loopback, + $this->files->get(__DIR__.'/../stubs/ifconfig.plist') + ) + ); + + info('New plist file added ['.$this->plistPath().']'); + } + } + function valetHomePath() { return VALET_HOME_PATH; @@ -762,6 +829,16 @@ function valetLoopback() return $this->config->read()['loopback']; } + /** + * Get the path to loopback LaunchDaemon. + * + * @return string + */ + function plistPath() + { + return '/Library/LaunchDaemons/com.laravel.valet.ifconfig.plist'; + } + /** * Get the path to Nginx site configuration files. */ diff --git a/cli/stubs/ifconfig.plist b/cli/stubs/ifconfig.plist new file mode 100644 index 0000000..501ffb7 --- /dev/null +++ b/cli/stubs/ifconfig.plist @@ -0,0 +1,17 @@ + + + + + Label + com.laravel.valet.ifconfig + RunAtLoad + + ProgramArguments + + /sbin/ifconfig + lo0 + alias + VALET_LOOPBACK + + + diff --git a/cli/stubs/proxy.valet.conf b/cli/stubs/proxy.valet.conf index 1e868fa..ce741eb 100644 --- a/cli/stubs/proxy.valet.conf +++ b/cli/stubs/proxy.valet.conf @@ -1,13 +1,15 @@ # valet stub: proxy.valet.conf server { - listen VALET_LOOPBACK:80; + listen 127.0.0.1:80; + #listen VALET_LOOPBACK:80; # valet loopback server_name VALET_SITE www.VALET_SITE *.VALET_SITE; return 301 https://$host$request_uri; } server { - listen VALET_LOOPBACK:443 ssl http2; + listen 127.0.0.1:443 ssl http2; + #listen VALET_LOOPBACK:443 ssl http2; # valet loopback server_name VALET_SITE www.VALET_SITE *.VALET_SITE; root /; charset utf-8; @@ -55,7 +57,8 @@ server { } server { - listen VALET_LOOPBACK:60; + listen 127.0.0.1:60; + #listen VALET_LOOPBACK:60; # valet loopback server_name VALET_SITE www.VALET_SITE *.VALET_SITE; root /; charset utf-8; diff --git a/cli/stubs/secure.valet.conf b/cli/stubs/secure.valet.conf index ee98705..a7cd231 100644 --- a/cli/stubs/secure.valet.conf +++ b/cli/stubs/secure.valet.conf @@ -1,11 +1,13 @@ server { - listen VALET_LOOPBACK:80; + listen 127.0.0.1:80; + #listen VALET_LOOPBACK:80; # valet loopback server_name VALET_SITE www.VALET_SITE *.VALET_SITE; return 301 https://$host$request_uri; } server { - listen VALET_LOOPBACK:443 ssl http2; + listen 127.0.0.1:443 ssl http2; + #listen VALET_LOOPBACK:443 ssl http2; # valet loopback server_name VALET_SITE www.VALET_SITE *.VALET_SITE; root /; charset utf-8; @@ -48,7 +50,8 @@ server { } server { - listen VALET_LOOPBACK:60; + listen 127.0.0.1:60; + #listen VALET_LOOPBACK:60; # valet loopback server_name VALET_SITE www.VALET_SITE *.VALET_SITE; root /; charset utf-8; diff --git a/cli/stubs/valet.conf b/cli/stubs/valet.conf index 583e6c8..3d41312 100644 --- a/cli/stubs/valet.conf +++ b/cli/stubs/valet.conf @@ -1,5 +1,6 @@ server { - listen VALET_LOOPBACK:80 default_server; + listen 127.0.0.1:80 default_server; + #listen VALET_LOOPBACK:80 default_server; # valet loopback root /; charset utf-8; client_max_body_size 128M; diff --git a/cli/valet.php b/cli/valet.php index 2f694e7..d5a42ac 100755 --- a/cli/valet.php +++ b/cli/valet.php @@ -113,6 +113,7 @@ Configuration::updateKey('loopback', $loopback = trim($loopback, '.')); DnsMasq::refreshConfiguration(); + Site::aliasLoopback($oldLoopback, $loopback); Site::resecureForNewLoopback($oldLoopback, $loopback); Nginx::installServer(); PhpFpm::restart();