From 01f4b582e632938d705b0efa847bae1e6a371f07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Popowicz?= Date: Fri, 30 Apr 2021 14:31:55 +0200 Subject: [PATCH] :recycle: Refactoring proxy command. Proxy is not secured by default. --- cli/Valet/Site.php | 16 ++++++++-------- ...nsecure.proxy.valet.conf => proxy.valet.conf} | 2 +- cli/valet.php | 6 +++--- tests/SiteTest.php | 12 ++++++------ 4 files changed, 18 insertions(+), 18 deletions(-) rename cli/stubs/{insecure.proxy.valet.conf => proxy.valet.conf} (98%) diff --git a/cli/Valet/Site.php b/cli/Valet/Site.php index fde1b62..d019b8c 100644 --- a/cli/Valet/Site.php +++ b/cli/Valet/Site.php @@ -710,10 +710,10 @@ function unsecureAll() * * @param string $url The domain name to serve * @param string $host The URL to proxy to, eg: http://127.0.0.1:8080 - * @param bool $insecure + * @param bool $secure * @return string */ - function proxyCreate($url, $host, $insecure = false) + function proxyCreate($url, $host, $secure = false) { if (!preg_match('~^https?://.*$~', $host)) { throw new \InvalidArgumentException(sprintf('"%s" is not a valid URL', $host)); @@ -726,7 +726,7 @@ function proxyCreate($url, $host, $insecure = false) $siteConf = $this->replaceOldLoopbackWithNew( $this->files->get( - $insecure ? __DIR__.'/../stubs/insecure.proxy.valet.conf' : __DIR__.'/../stubs/secure.proxy.valet.conf' + $secure ? __DIR__.'/../stubs/secure.proxy.valet.conf' : __DIR__.'/../stubs/proxy.valet.conf' ), 'VALET_LOOPBACK', $this->valetLoopback() @@ -738,13 +738,13 @@ function proxyCreate($url, $host, $insecure = false) $siteConf ); - if ($insecure) { - $this->putInsecurely($url, $siteConf); - } else { + if ($secure) { $this->secure($url, $siteConf); + } else { + $this->put($url, $siteConf); } - $protocol = $insecure ? 'http' : 'https'; + $protocol = $secure ? 'https' : 'http'; info('Valet will now proxy ['.$protocol.'://'.$url.'] traffic to ['.$host.'].'); } @@ -775,7 +775,7 @@ function proxyDelete($url) * @param string $siteConf pregenerated Nginx config file contents * @return void */ - function putInsecurely($url, $siteConf) + function put($url, $siteConf) { $this->unsecure($url); diff --git a/cli/stubs/insecure.proxy.valet.conf b/cli/stubs/proxy.valet.conf similarity index 98% rename from cli/stubs/insecure.proxy.valet.conf rename to cli/stubs/proxy.valet.conf index 5accb93..3516193 100644 --- a/cli/stubs/insecure.proxy.valet.conf +++ b/cli/stubs/proxy.valet.conf @@ -1,4 +1,4 @@ -# valet stub: insecure.proxy.valet.conf +# valet stub: proxy.valet.conf server { listen 127.0.0.1:80; diff --git a/cli/valet.php b/cli/valet.php index 470886f..4db3575 100755 --- a/cli/valet.php +++ b/cli/valet.php @@ -210,13 +210,13 @@ /** * Create an Nginx proxy config for the specified domain */ - $app->command('proxy domain host [--insecure]', function ($domain, $host, $insecure) { + $app->command('proxy domain host [--secure]', function ($domain, $host, $secure) { - Site::proxyCreate($domain, $host, $insecure); + Site::proxyCreate($domain, $host, $secure); Nginx::restart(); })->descriptions('Create an Nginx proxy site for the specified host. Useful for docker, mailhog etc.', [ - '--insecure' => 'Create a proxy without SSL' + '--secure' => 'Create a proxy with a trusted TLS certificate' ]); /** diff --git a/tests/SiteTest.php b/tests/SiteTest.php index f00b319..5181a95 100644 --- a/tests/SiteTest.php +++ b/tests/SiteTest.php @@ -353,7 +353,7 @@ public function test_add_proxy() $site->assertCertificateNotExists('my-new-proxy.com.test'); $site->assertNginxNotExists('my-new-proxy.com.test'); - $site->proxyCreate('my-new-proxy.com', 'https://127.0.0.1:9443'); + $site->proxyCreate('my-new-proxy.com', 'https://127.0.0.1:9443', true); $site->assertCertificateExistsWithCounterValue('my-new-proxy.com.test', 0); $site->assertNginxExists('my-new-proxy.com.test'); @@ -387,7 +387,7 @@ public function test_add_non_secure_proxy() $site->assertCertificateNotExists('my-new-proxy.com.test'); $site->assertNginxNotExists('my-new-proxy.com.test'); - $site->proxyCreate('my-new-proxy.com', 'http://127.0.0.1:9443', true); + $site->proxyCreate('my-new-proxy.com', 'http://127.0.0.1:9443', false); $site->assertCertificateNotExists('my-new-proxy.com.test'); $site->assertNginxExists('my-new-proxy.com.test'); @@ -418,7 +418,7 @@ public function test_add_proxy_clears_previous_proxy_certificate() $site->useOutput(); - $site->proxyCreate('my-new-proxy.com', 'https://127.0.0.1:7443'); + $site->proxyCreate('my-new-proxy.com', 'https://127.0.0.1:7443', true); $site->assertCertificateExistsWithCounterValue('my-new-proxy.com.test', 0); @@ -432,7 +432,7 @@ public function test_add_proxy_clears_previous_proxy_certificate() ], $site->proxies()->all()); // Note: different proxy port - $site->proxyCreate('my-new-proxy.com', 'https://127.0.0.1:9443'); + $site->proxyCreate('my-new-proxy.com', 'https://127.0.0.1:9443', true); // This shows we created a new certificate. $site->assertCertificateExistsWithCounterValue('my-new-proxy.com.test', 1); @@ -470,7 +470,7 @@ public function test_add_proxy_clears_previous_non_proxy_certificate() $site->assertCertificateExistsWithCounterValue('my-new-proxy.com.test', 0); $site->assertNginxNotExists('my-new-proxy.com.test'); - $site->proxyCreate('my-new-proxy.com', 'https://127.0.0.1:9443'); + $site->proxyCreate('my-new-proxy.com', 'https://127.0.0.1:9443', true); // This shows we created a new certificate. $site->assertCertificateExistsWithCounterValue('my-new-proxy.com.test', 1); @@ -507,7 +507,7 @@ public function test_remove_proxy() $this->assertEquals([], $site->proxies()->all()); - $site->proxyCreate('my-new-proxy.com', 'https://127.0.0.1:9443'); + $site->proxyCreate('my-new-proxy.com', 'https://127.0.0.1:9443', true); $this->assertEquals([ 'my-new-proxy.com' => [