1
0
mirror of https://github.com/laravel/valet.git synced 2026-02-05 16:40:05 +01:00

Add support for proxying multiple domains at once

This commit is contained in:
Robert Boes
2023-08-25 10:56:06 +02:00
parent 2af8aba068
commit bb36a3b437
3 changed files with 255 additions and 31 deletions

View File

@@ -768,8 +768,10 @@ public function proxyCreate(string $url, string $host, bool $secure = false): vo
}
$tld = $this->config->read()['tld'];
if (! ends_with($url, '.'.$tld)) {
$url .= '.'.$tld;
foreach (explode(',', $url) as $proxyUrl) {
if (! ends_with($proxyUrl, '.'.$tld)) {
$proxyUrl .= '.'.$tld;
}
$siteConf = $this->replaceOldLoopbackWithNew(
@@ -780,19 +782,20 @@ public function proxyCreate(string $url, string $host, bool $secure = false): vo
$siteConf = str_replace(
['VALET_HOME_PATH', 'VALET_SERVER_PATH', 'VALET_STATIC_PREFIX', 'VALET_SITE', 'VALET_PROXY_HOST'],
[$this->valetHomePath(), VALET_SERVER_PATH, VALET_STATIC_PREFIX, $url, $host],
[$this->valetHomePath(), VALET_SERVER_PATH, VALET_STATIC_PREFIX, $proxyUrl, $host],
$siteConf
);
if ($secure) {
$this->secure($url, $siteConf);
$this->secure($proxyUrl, $siteConf);
} else {
$this->put($url, $siteConf);
$this->put($proxyUrl, $siteConf);
}
$protocol = $secure ? 'https' : 'http';
info('Valet will now proxy ['.$protocol.'://'.$url.'] traffic to ['.$host.'].');
info('Valet will now proxy ['.$protocol.'://'.$proxyUrl.'] traffic to ['.$host.'].');
}
}
/**
@@ -801,14 +804,17 @@ public function proxyCreate(string $url, string $host, bool $secure = false): vo
public function proxyDelete(string $url): void
{
$tld = $this->config->read()['tld'];
if (! ends_with($url, '.'.$tld)) {
$url .= '.'.$tld;
foreach (explode(',', $url) as $proxyUrl) {
if (! ends_with($proxyUrl, '.'.$tld)) {
$proxyUrl .= '.'.$tld;
}
$this->unsecure($url);
$this->files->unlink($this->nginxPath($url));
$this->unsecure($proxyUrl);
$this->files->unlink($this->nginxPath($proxyUrl));
info('Valet will no longer proxy [https://'.$url.'].');
info('Valet will no longer proxy [https://'.$proxyUrl.'].');
}
}
/**

View File

@@ -396,6 +396,23 @@ public function test_proxy_command()
$tester->assertCommandIsSuccessful();
}
public function test_proxy_command_with_multiple_domains()
{
[$app, $tester] = $this->appAndTester();
$site = Mockery::mock(RealSite::class);
$site->shouldReceive('proxyCreate')->with('my-app,subdomain.my-app', 'http://127.0.0.1:8000', false)->once();
$nginx = Mockery::mock(Nginx::class);
$nginx->shouldReceive('restart')->once();
swap(Nginx::class, $nginx);
swap(RealSite::class, $site);
$tester->run(['command' => 'proxy', 'domain' => 'my-app,subdomain.my-app', 'host' => 'http://127.0.0.1:8000']);
$tester->assertCommandIsSuccessful();
}
public function test_unproxy_command()
{
[$app, $tester] = $this->appAndTester();

View File

@@ -405,6 +405,49 @@ public function test_add_proxy()
], $site->proxies()->all());
}
public function test_add_multiple_proxies()
{
$config = Mockery::mock(Configuration::class);
$config->shouldReceive('read')
->andReturn(['tld' => 'test', 'loopback' => VALET_LOOPBACK]);
swap(Configuration::class, $config);
swap(CommandLine::class, resolve(CommandLineFake::class));
/** @var FixturesSiteFake $site */
$site = resolve(FixturesSiteFake::class);
$site->useOutput();
$site->assertCertificateNotExists('my-new-proxy.com.test');
$site->assertCertificateNotExists('my-other-new-proxy.com.test');
$site->assertNginxNotExists('my-new-proxy.com.test');
$site->assertNginxNotExists('my-other-new-proxy.com.test');
$site->proxyCreate('my-new-proxy.com,my-other-new-proxy.com', 'https://127.0.0.1:9443', true);
$site->assertCertificateExistsWithCounterValue('my-new-proxy.com.test', 0);
$site->assertCertificateExistsWithCounterValue('my-other-new-proxy.com.test', 1);
$site->assertNginxExists('my-new-proxy.com.test');
$site->assertNginxExists('my-other-new-proxy.com.test');
$this->assertEquals([
'my-new-proxy.com' => [
'site' => 'my-new-proxy.com',
'secured' => ' X',
'url' => 'https://my-new-proxy.com.test',
'path' => 'https://127.0.0.1:9443',
],
'my-other-new-proxy.com' => [
'site' => 'my-other-new-proxy.com',
'secured' => ' X',
'url' => 'https://my-other-new-proxy.com.test',
'path' => 'https://127.0.0.1:9443',
],
], $site->proxies()->all());
}
public function test_add_non_secure_proxy()
{
$config = Mockery::mock(Configuration::class);
@@ -438,6 +481,49 @@ public function test_add_non_secure_proxy()
], $site->proxies()->all());
}
public function test_add_multiple_non_secure_proxies()
{
$config = Mockery::mock(Configuration::class);
$config->shouldReceive('read')
->andReturn(['tld' => 'test', 'loopback' => VALET_LOOPBACK]);
swap(Configuration::class, $config);
swap(CommandLine::class, resolve(CommandLineFake::class));
/** @var FixturesSiteFake $site */
$site = resolve(FixturesSiteFake::class);
$site->useOutput();
$site->assertCertificateNotExists('my-new-proxy.com.test');
$site->assertCertificateNotExists('my-other-new-proxy.com.test');
$site->assertNginxNotExists('my-new-proxy.com.test');
$site->assertNginxNotExists('my-other-new-proxy.com.test');
$site->proxyCreate('my-new-proxy.com,my-other-new-proxy.com', 'http://127.0.0.1:9443', false);
$site->assertCertificateNotExists('my-new-proxy.com.test');
$site->assertCertificateNotExists('my-other-new-proxy.com.test');
$site->assertNginxExists('my-new-proxy.com.test');
$site->assertNginxExists('my-other-new-proxy.com.test');
$this->assertEquals([
'my-new-proxy.com' => [
'site' => 'my-new-proxy.com',
'secured' => '',
'url' => 'http://my-new-proxy.com.test',
'path' => 'http://127.0.0.1:9443',
],
'my-other-new-proxy.com' => [
'site' => 'my-other-new-proxy.com',
'secured' => '',
'url' => 'http://my-other-new-proxy.com.test',
'path' => 'http://127.0.0.1:9443',
],
], $site->proxies()->all());
}
public function test_add_proxy_clears_previous_proxy_certificate()
{
$config = Mockery::mock(Configuration::class);
@@ -564,6 +650,121 @@ public function test_remove_proxy()
$this->assertEquals([], $site->proxies()->all());
}
public function test_remove_multiple_proxies()
{
$config = Mockery::mock(Configuration::class);
$config->shouldReceive('read')
->andReturn(['tld' => 'test', 'loopback' => VALET_LOOPBACK]);
swap(Configuration::class, $config);
swap(CommandLine::class, resolve(CommandLineFake::class));
/** @var FixturesSiteFake $site */
$site = resolve(FixturesSiteFake::class);
$site->useOutput();
$site->assertCertificateNotExists('my-new-proxy.com.test');
$site->assertCertificateNotExists('my-other-new-proxy.com.test');
$site->assertNginxNotExists('my-new-proxy.com.test');
$site->assertNginxNotExists('my-other-new-proxy.com.test');
$this->assertEquals([], $site->proxies()->all());
$site->proxyCreate('my-new-proxy.com,my-other-new-proxy.com', 'https://127.0.0.1:9443', true);
$this->assertEquals([
'my-new-proxy.com' => [
'site' => 'my-new-proxy.com',
'secured' => ' X',
'url' => 'https://my-new-proxy.com.test',
'path' => 'https://127.0.0.1:9443',
],
'my-other-new-proxy.com' => [
'site' => 'my-other-new-proxy.com',
'secured' => ' X',
'url' => 'https://my-other-new-proxy.com.test',
'path' => 'https://127.0.0.1:9443',
],
], $site->proxies()->all());
$site->assertCertificateExists('my-new-proxy.com.test');
$site->assertCertificateExists('my-other-new-proxy.com.test');
$site->assertNginxExists('my-new-proxy.com.test');
$site->assertNginxExists('my-other-new-proxy.com.test');
$site->proxyDelete('my-new-proxy.com,my-other-new-proxy.com');
$site->assertCertificateNotExists('my-new-proxy.com.test');
$site->assertCertificateNotExists('my-other-new-proxy.com.test');
$site->assertNginxNotExists('my-new-proxy.com.test');
$site->assertNginxNotExists('my-other-new-proxy.com.test');
$this->assertEquals([], $site->proxies()->all());
}
public function test_remove_single_proxy_from_multiple_proxies()
{
$config = Mockery::mock(Configuration::class);
$config->shouldReceive('read')
->andReturn(['tld' => 'test', 'loopback' => VALET_LOOPBACK]);
swap(Configuration::class, $config);
swap(CommandLine::class, resolve(CommandLineFake::class));
/** @var FixturesSiteFake $site */
$site = resolve(FixturesSiteFake::class);
$site->useOutput();
$site->assertCertificateNotExists('my-new-proxy.com.test');
$site->assertCertificateNotExists('my-other-new-proxy.com.test');
$site->assertNginxNotExists('my-new-proxy.com.test');
$site->assertNginxNotExists('my-other-new-proxy.com.test');
$this->assertEquals([], $site->proxies()->all());
$site->proxyCreate('my-new-proxy.com,my-other-new-proxy.com', 'https://127.0.0.1:9443', true);
$this->assertEquals([
'my-new-proxy.com' => [
'site' => 'my-new-proxy.com',
'secured' => ' X',
'url' => 'https://my-new-proxy.com.test',
'path' => 'https://127.0.0.1:9443',
],
'my-other-new-proxy.com' => [
'site' => 'my-other-new-proxy.com',
'secured' => ' X',
'url' => 'https://my-other-new-proxy.com.test',
'path' => 'https://127.0.0.1:9443',
],
], $site->proxies()->all());
$site->assertCertificateExists('my-new-proxy.com.test');
$site->assertCertificateExists('my-other-new-proxy.com.test');
$site->assertNginxExists('my-new-proxy.com.test');
$site->assertNginxExists('my-other-new-proxy.com.test');
$site->proxyDelete('my-new-proxy.com');
$site->assertCertificateNotExists('my-new-proxy.com.test');
$site->assertCertificateExists('my-other-new-proxy.com.test');
$site->assertNginxNotExists('my-new-proxy.com.test');
$site->assertNginxExists('my-other-new-proxy.com.test');
$this->assertEquals([
'my-other-new-proxy.com' => [
'site' => 'my-other-new-proxy.com',
'secured' => ' X',
'url' => 'https://my-other-new-proxy.com.test',
'path' => 'https://127.0.0.1:9443',
],
], $site->proxies()->all());
}
public function test_gets_site_url_from_directory()
{
$config = Mockery::mock(Configuration::class);