mirror of
https://github.com/laravel/valet.git
synced 2026-02-05 00:20:08 +01:00
Add loopback command.
This commit is contained in:
@@ -36,7 +36,7 @@ function install()
|
||||
|
||||
/**
|
||||
* Forcefully delete the Valet home configuration directory and contents.
|
||||
*
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function uninstall()
|
||||
@@ -130,7 +130,7 @@ function createCertificatesDirectory()
|
||||
function writeBaseConfiguration()
|
||||
{
|
||||
if (! $this->files->exists($this->path())) {
|
||||
$this->write(['tld' => 'test', 'paths' => []]);
|
||||
$this->write(['tld' => 'test', 'loopback' => VALET_LOOPBACK, 'paths' => []]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -138,11 +138,13 @@ function writeBaseConfiguration()
|
||||
*/
|
||||
$config = $this->read();
|
||||
|
||||
if (isset($config['tld'])) {
|
||||
return;
|
||||
if (! isset($config['tld'])) {
|
||||
$this->updateKey('tld', !empty($config['domain']) ? $config['domain'] : 'test');
|
||||
}
|
||||
|
||||
$this->updateKey('tld', !empty($config['domain']) ? $config['domain'] : 'test');
|
||||
if (! isset($config['loopback'])) {
|
||||
$this->updateKey('loopback', VALET_LOOPBACK);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -46,7 +46,7 @@ function install($tld = 'test')
|
||||
|
||||
/**
|
||||
* Forcefully uninstall dnsmasq.
|
||||
*
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function uninstall()
|
||||
@@ -60,7 +60,7 @@ function uninstall()
|
||||
|
||||
/**
|
||||
* Tell Homebrew to restart dnsmasq
|
||||
*
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function restart()
|
||||
@@ -113,12 +113,13 @@ function ensureUsingDnsmasqDForConfigs()
|
||||
function createDnsmasqTldConfigFile($tld)
|
||||
{
|
||||
$tldConfigFile = $this->dnsmasqUserConfigDir() . 'tld-' . $tld . '.conf';
|
||||
$loopback = $this->configuration->read()['loopback'];
|
||||
|
||||
$this->files->putAsUser($tldConfigFile, 'address=/.'.$tld.'/127.0.0.1'.PHP_EOL.'listen-address=127.0.0.1'.PHP_EOL);
|
||||
$this->files->putAsUser($tldConfigFile, 'address=/.'.$tld.'/'.$loopback.PHP_EOL.'listen-address='.$loopback.PHP_EOL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the resolver file to point the configured TLD to 127.0.0.1.
|
||||
* Create the resolver file to point the configured TLD to configured loopback address.
|
||||
*
|
||||
* @param string $tld
|
||||
* @return void
|
||||
@@ -126,8 +127,9 @@ function createDnsmasqTldConfigFile($tld)
|
||||
function createTldResolver($tld)
|
||||
{
|
||||
$this->files->ensureDirExists($this->resolverPath);
|
||||
$loopback = $this->configuration->read()['loopback'];
|
||||
|
||||
$this->files->put($this->resolverPath.'/'.$tld, 'nameserver 127.0.0.1'.PHP_EOL);
|
||||
$this->files->put($this->resolverPath.'/'.$tld, 'nameserver '.$loopback.PHP_EOL);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -145,6 +147,18 @@ function updateTld($oldTld, $newTld)
|
||||
$this->install($newTld);
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh the DnsMasq configuration.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function refreshConfiguration()
|
||||
{
|
||||
$tld = $this->configuration->read()['tld'];
|
||||
|
||||
$this->updateTld($tld, $tld);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the custom configuration path.
|
||||
*
|
||||
|
||||
@@ -75,11 +75,13 @@ 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_HOME_PATH', 'VALET_SERVER_PATH', 'VALET_STATIC_PREFIX'],
|
||||
[VALET_HOME_PATH, VALET_SERVER_PATH, VALET_STATIC_PREFIX],
|
||||
['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')
|
||||
)
|
||||
);
|
||||
@@ -131,8 +133,10 @@ function ($exitCode, $outputMessage) {
|
||||
function rewriteSecureNginxFiles()
|
||||
{
|
||||
$tld = $this->configuration->read()['tld'];
|
||||
$loopback = $this->configuration->read()['loopback'];
|
||||
|
||||
$this->site->resecureForNewTld($tld, $tld);
|
||||
$this->site->resecureForNewLoopback($loopback, $loopback);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -384,6 +384,61 @@ 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.
|
||||
*
|
||||
* @param string $siteConf Nginx site config content
|
||||
* @param string $old Old loopback address
|
||||
* @param string $new New loopback address
|
||||
* @return string
|
||||
*/
|
||||
function replaceOldLoopbackWithNew($siteConf, $old, $new)
|
||||
{
|
||||
$lookups = [];
|
||||
$lookups[] = '~listen .*:80;~';
|
||||
$lookups[] = '~listen .*:443 ssl http2;~';
|
||||
$lookups[] = '~listen .*:60;~';
|
||||
|
||||
foreach ($lookups as $lookup) {
|
||||
preg_match($lookup, $siteConf, $matches);
|
||||
foreach ($matches as $match) {
|
||||
$replaced = str_replace($old, $new, $match);
|
||||
$siteConf = str_replace($match, $replaced, $siteConf);
|
||||
}
|
||||
}
|
||||
return $siteConf;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all of the URLs that are currently secured.
|
||||
*
|
||||
@@ -577,8 +632,9 @@ function buildSecureNginxServer($url, $siteConf = null)
|
||||
}
|
||||
|
||||
return str_replace(
|
||||
['VALET_HOME_PATH', 'VALET_SERVER_PATH', 'VALET_STATIC_PREFIX', 'VALET_SITE', 'VALET_CERT', 'VALET_KEY'],
|
||||
['VALET_LOOPBACK', '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,
|
||||
@@ -667,8 +723,8 @@ function proxyCreate($url, $host)
|
||||
$siteConf = $this->files->get(__DIR__.'/../stubs/proxy.valet.conf');
|
||||
|
||||
$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],
|
||||
['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],
|
||||
$siteConf
|
||||
);
|
||||
|
||||
@@ -701,6 +757,11 @@ function valetHomePath()
|
||||
return VALET_HOME_PATH;
|
||||
}
|
||||
|
||||
function valetLoopback()
|
||||
{
|
||||
return $this->config->read()['loopback'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the path to Nginx site configuration files.
|
||||
*/
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
/**
|
||||
* Define the ~/.config/valet path as a constant.
|
||||
*/
|
||||
define('VALET_LOOPBACK', '127.0.0.1');
|
||||
define('VALET_HOME_PATH', $_SERVER['HOME'].'/.config/valet');
|
||||
define('VALET_SERVER_PATH', realpath(__DIR__ . '/../../server.php'));
|
||||
define('VALET_STATIC_PREFIX', '41c270e4-5535-4daa-b23e-c269744c2f45');
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
# valet stub: proxy.valet.conf
|
||||
|
||||
server {
|
||||
listen 127.0.0.1:80;
|
||||
listen VALET_LOOPBACK:80;
|
||||
server_name VALET_SITE www.VALET_SITE *.VALET_SITE;
|
||||
return 301 https://$host$request_uri;
|
||||
}
|
||||
|
||||
server {
|
||||
listen 127.0.0.1:443 ssl http2;
|
||||
listen VALET_LOOPBACK:443 ssl http2;
|
||||
server_name VALET_SITE www.VALET_SITE *.VALET_SITE;
|
||||
root /;
|
||||
charset utf-8;
|
||||
@@ -55,7 +55,7 @@ server {
|
||||
}
|
||||
|
||||
server {
|
||||
listen 127.0.0.1:60;
|
||||
listen VALET_LOOPBACK:60;
|
||||
server_name VALET_SITE www.VALET_SITE *.VALET_SITE;
|
||||
root /;
|
||||
charset utf-8;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
server {
|
||||
listen 127.0.0.1:80;
|
||||
listen VALET_LOOPBACK:80;
|
||||
server_name VALET_SITE www.VALET_SITE *.VALET_SITE;
|
||||
return 301 https://$host$request_uri;
|
||||
}
|
||||
|
||||
server {
|
||||
listen 127.0.0.1:443 ssl http2;
|
||||
listen VALET_LOOPBACK:443 ssl http2;
|
||||
server_name VALET_SITE www.VALET_SITE *.VALET_SITE;
|
||||
root /;
|
||||
charset utf-8;
|
||||
@@ -48,7 +48,7 @@ server {
|
||||
}
|
||||
|
||||
server {
|
||||
listen 127.0.0.1:60;
|
||||
listen VALET_LOOPBACK:60;
|
||||
server_name VALET_SITE www.VALET_SITE *.VALET_SITE;
|
||||
root /;
|
||||
charset utf-8;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
server {
|
||||
listen 127.0.0.1:80 default_server;
|
||||
listen VALET_LOOPBACK:80 default_server;
|
||||
root /;
|
||||
charset utf-8;
|
||||
client_max_body_size 128M;
|
||||
|
||||
@@ -72,6 +72,13 @@
|
||||
Configuration::writeBaseConfiguration();
|
||||
}
|
||||
|
||||
/**
|
||||
* Upgrade helper: ensure the loopback config exists
|
||||
*/
|
||||
if (empty(Configuration::read()['loopback'])) {
|
||||
Configuration::writeBaseConfiguration();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get or set the TLD currently being used by Valet.
|
||||
*/
|
||||
@@ -93,6 +100,28 @@
|
||||
info('Your Valet TLD has been updated to ['.$tld.'].');
|
||||
}, ['domain'])->descriptions('Get or set the TLD used for Valet sites.');
|
||||
|
||||
/**
|
||||
* Get or set the loopback address currently being used by Valet.
|
||||
*/
|
||||
$app->command('loopback [loopback]', function ($loopback = null) {
|
||||
if ($loopback === null) {
|
||||
return output(Configuration::read()['loopback']);
|
||||
}
|
||||
|
||||
$oldLoopback = Configuration::read()['loopback'];
|
||||
|
||||
Configuration::updateKey('loopback', $loopback = trim($loopback, '.'));
|
||||
|
||||
DnsMasq::refreshConfiguration();
|
||||
Site::resecureForNewLoopback($oldLoopback, $loopback);
|
||||
Nginx::installServer();
|
||||
PhpFpm::restart();
|
||||
Nginx::restart();
|
||||
|
||||
info('Your valet loopback address has been updated to ['.$loopback.']');
|
||||
|
||||
}, ['address'])->descriptions('Get or set the loopback address used for Valet sites');
|
||||
|
||||
/**
|
||||
* Add the current working directory to the paths configuration.
|
||||
*/
|
||||
|
||||
@@ -46,8 +46,10 @@ public function test_install_installs_and_places_configuration_files_in_proper_l
|
||||
|
||||
$dnsMasq->install('test');
|
||||
|
||||
$this->assertSame('nameserver 127.0.0.1'.PHP_EOL, file_get_contents(__DIR__.'/output/resolver/test'));
|
||||
$this->assertSame('address=/.test/127.0.0.1'.PHP_EOL.'listen-address=127.0.0.1'.PHP_EOL, file_get_contents(__DIR__.'/output/tld-test.conf'));
|
||||
$loopback = \Configuration::read()['loopback'];
|
||||
|
||||
$this->assertSame('nameserver '.$loopback.PHP_EOL, file_get_contents(__DIR__.'/output/resolver/test'));
|
||||
$this->assertSame('address=/.test/'.$loopback.PHP_EOL.'listen-address='.$loopback.PHP_EOL, file_get_contents(__DIR__.'/output/tld-test.conf'));
|
||||
$this->assertSame('test-contents
|
||||
' . PHP_EOL . 'conf-dir='.BREW_PREFIX.'/etc/dnsmasq.d/,*.conf' . PHP_EOL,
|
||||
file_get_contents($dnsMasq->dnsmasqMasterConfigFile)
|
||||
|
||||
@@ -46,7 +46,7 @@ public function test_install_nginx_directories_creates_location_for_site_specifi
|
||||
$files->shouldReceive('putAsUser')->with(VALET_HOME_PATH.'/Nginx/.keep', "\n")->once();
|
||||
|
||||
swap(Filesystem::class, $files);
|
||||
swap(Configuration::class, $config = Mockery::spy(Configuration::class, ['read' => ['tld' => 'test']]));
|
||||
swap(Configuration::class, $config = Mockery::spy(Configuration::class, ['read' => ['tld' => 'test', 'loopback' => VALET_LOOPBACK]]));
|
||||
swap(Site::class, Mockery::spy(Site::class));
|
||||
|
||||
$nginx = resolve(Nginx::class);
|
||||
@@ -61,7 +61,7 @@ public function test_nginx_directory_is_never_created_if_it_already_exists()
|
||||
$files->shouldReceive('putAsUser')->with(VALET_HOME_PATH.'/Nginx/.keep', "\n")->once();
|
||||
|
||||
swap(Filesystem::class, $files);
|
||||
swap(Configuration::class, $config = Mockery::spy(Configuration::class, ['read' => ['tld' => 'test']]));
|
||||
swap(Configuration::class, $config = Mockery::spy(Configuration::class, ['read' => ['tld' => 'test', 'loopback' => VALET_LOOPBACK]]));
|
||||
swap(Site::class, Mockery::spy(Site::class));
|
||||
|
||||
$nginx = resolve(Nginx::class);
|
||||
@@ -76,7 +76,7 @@ public function test_install_nginx_directories_rewrites_secure_nginx_files()
|
||||
$files->shouldReceive('putAsUser')->with(VALET_HOME_PATH.'/Nginx/.keep', "\n")->once();
|
||||
|
||||
swap(Filesystem::class, $files);
|
||||
swap(Configuration::class, $config = Mockery::spy(Configuration::class, ['read' => ['tld' => 'test']]));
|
||||
swap(Configuration::class, $config = Mockery::spy(Configuration::class, ['read' => ['tld' => 'test', 'loopback' => VALET_LOOPBACK]]));
|
||||
swap(Site::class, $site = Mockery::spy(Site::class));
|
||||
|
||||
$nginx = resolve(Nginx::class);
|
||||
|
||||
@@ -339,7 +339,7 @@ public function test_add_proxy()
|
||||
{
|
||||
$config = Mockery::mock(Configuration::class);
|
||||
$config->shouldReceive('read')
|
||||
->andReturn(['tld' => 'test']);
|
||||
->andReturn(['tld' => 'test', 'loopback' => VALET_LOOPBACK]);
|
||||
|
||||
swap(Configuration::class, $config);
|
||||
|
||||
@@ -372,7 +372,7 @@ public function test_add_proxy_clears_previous_proxy_certificate()
|
||||
{
|
||||
$config = Mockery::mock(Configuration::class);
|
||||
$config->shouldReceive('read')
|
||||
->andReturn(['tld' => 'test']);
|
||||
->andReturn(['tld' => 'test', 'loopback' => VALET_LOOPBACK]);
|
||||
|
||||
swap(Configuration::class, $config);
|
||||
|
||||
@@ -416,7 +416,7 @@ public function test_add_proxy_clears_previous_non_proxy_certificate()
|
||||
{
|
||||
$config = Mockery::mock(Configuration::class);
|
||||
$config->shouldReceive('read')
|
||||
->andReturn(['tld' => 'test']);
|
||||
->andReturn(['tld' => 'test', 'loopback' => VALET_LOOPBACK]);
|
||||
|
||||
swap(Configuration::class, $config);
|
||||
|
||||
@@ -456,7 +456,7 @@ public function test_remove_proxy()
|
||||
{
|
||||
$config = Mockery::mock(Configuration::class);
|
||||
$config->shouldReceive('read')
|
||||
->andReturn(['tld' => 'test']);
|
||||
->andReturn(['tld' => 'test', 'loopback' => VALET_LOOPBACK]);
|
||||
|
||||
swap(Configuration::class, $config);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user