mirror of
https://github.com/laravel/valet.git
synced 2026-02-06 00:40:06 +01:00
Add support for proxying multiple domains at once
This commit is contained in:
@@ -768,31 +768,34 @@ public function proxyCreate(string $url, string $host, bool $secure = false): vo
|
|||||||
}
|
}
|
||||||
|
|
||||||
$tld = $this->config->read()['tld'];
|
$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(
|
||||||
|
$this->files->getStub($secure ? 'secure.proxy.valet.conf' : 'proxy.valet.conf'),
|
||||||
|
'VALET_LOOPBACK',
|
||||||
|
$this->valetLoopback()
|
||||||
|
);
|
||||||
|
|
||||||
|
$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, $proxyUrl, $host],
|
||||||
|
$siteConf
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($secure) {
|
||||||
|
$this->secure($proxyUrl, $siteConf);
|
||||||
|
} else {
|
||||||
|
$this->put($proxyUrl, $siteConf);
|
||||||
|
}
|
||||||
|
|
||||||
|
$protocol = $secure ? 'https' : 'http';
|
||||||
|
|
||||||
|
info('Valet will now proxy ['.$protocol.'://'.$proxyUrl.'] traffic to ['.$host.'].');
|
||||||
}
|
}
|
||||||
|
|
||||||
$siteConf = $this->replaceOldLoopbackWithNew(
|
|
||||||
$this->files->getStub($secure ? 'secure.proxy.valet.conf' : 'proxy.valet.conf'),
|
|
||||||
'VALET_LOOPBACK',
|
|
||||||
$this->valetLoopback()
|
|
||||||
);
|
|
||||||
|
|
||||||
$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],
|
|
||||||
$siteConf
|
|
||||||
);
|
|
||||||
|
|
||||||
if ($secure) {
|
|
||||||
$this->secure($url, $siteConf);
|
|
||||||
} else {
|
|
||||||
$this->put($url, $siteConf);
|
|
||||||
}
|
|
||||||
|
|
||||||
$protocol = $secure ? 'https' : 'http';
|
|
||||||
|
|
||||||
info('Valet will now proxy ['.$protocol.'://'.$url.'] traffic to ['.$host.'].');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -801,14 +804,17 @@ public function proxyCreate(string $url, string $host, bool $secure = false): vo
|
|||||||
public function proxyDelete(string $url): void
|
public function proxyDelete(string $url): void
|
||||||
{
|
{
|
||||||
$tld = $this->config->read()['tld'];
|
$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($proxyUrl);
|
||||||
|
$this->files->unlink($this->nginxPath($proxyUrl));
|
||||||
|
|
||||||
|
info('Valet will no longer proxy [https://'.$proxyUrl.'].');
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->unsecure($url);
|
|
||||||
$this->files->unlink($this->nginxPath($url));
|
|
||||||
|
|
||||||
info('Valet will no longer proxy [https://'.$url.'].');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -396,6 +396,23 @@ public function test_proxy_command()
|
|||||||
$tester->assertCommandIsSuccessful();
|
$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()
|
public function test_unproxy_command()
|
||||||
{
|
{
|
||||||
[$app, $tester] = $this->appAndTester();
|
[$app, $tester] = $this->appAndTester();
|
||||||
|
|||||||
@@ -405,6 +405,49 @@ public function test_add_proxy()
|
|||||||
], $site->proxies()->all());
|
], $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()
|
public function test_add_non_secure_proxy()
|
||||||
{
|
{
|
||||||
$config = Mockery::mock(Configuration::class);
|
$config = Mockery::mock(Configuration::class);
|
||||||
@@ -438,6 +481,49 @@ public function test_add_non_secure_proxy()
|
|||||||
], $site->proxies()->all());
|
], $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()
|
public function test_add_proxy_clears_previous_proxy_certificate()
|
||||||
{
|
{
|
||||||
$config = Mockery::mock(Configuration::class);
|
$config = Mockery::mock(Configuration::class);
|
||||||
@@ -564,6 +650,121 @@ public function test_remove_proxy()
|
|||||||
$this->assertEquals([], $site->proxies()->all());
|
$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()
|
public function test_gets_site_url_from_directory()
|
||||||
{
|
{
|
||||||
$config = Mockery::mock(Configuration::class);
|
$config = Mockery::mock(Configuration::class);
|
||||||
|
|||||||
Reference in New Issue
Block a user