Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 40 additions & 51 deletions cli/Valet/DnsMasq.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ class DnsMasq
{
var $brew, $cli, $files;

var $dnsmasqMasterConfigFile = '/usr/local/etc/dnsmasq.conf';
var $dnsmasqSystemConfDir = '/usr/local/etc/dnsmasq.d';
var $resolverPath = '/etc/resolver';
var $configPath = '/usr/local/etc/dnsmasq.conf';
var $exampleConfigPath = '/usr/local/opt/dnsmasq/dnsmasq.conf.example';

/**
* Create a new DnsMasq instance.
Expand All @@ -37,10 +37,12 @@ function install($tld = 'test')
{
$this->brew->ensureInstalled('dnsmasq');

// For DnsMasq, we create our own custom configuration file which will be imported
// in the main DnsMasq file. This allows Valet to make changes to our own files
// without needing to modify the "primary" DnsMasq configuration files again.
$this->createCustomConfigFile($tld);
// For DnsMasq, we enable its feature of loading *.conf from /usr/local/etc/dnsmasq.d/
// and then we put a valet config file in there to point to the user's home .config/valet/dnsmasq.d
// This allows Valet to make changes to our own files without needing to modify the core dnsmasq configs
$this->ensureUsingDnsmasqDForConfigs();

$this->createDnsmasqTldConfigFile($tld);

$this->createTldResolver($tld);

Expand All @@ -50,65 +52,52 @@ function install($tld = 'test')
}

/**
* Append the custom DnsMasq configuration file to the main configuration file.
* Ensure the DnsMasq configuration primary config is set to read custom configs
*
* @param string $tld
* @return void
*/
function createCustomConfigFile($tld)
function ensureUsingDnsmasqDForConfigs()
{
$customConfigPath = $this->customConfigPath();
info('Updating Dnsmasq configuration...');

$this->copyExampleConfig();
// set primary config to look for configs in /usr/local/etc/dnsmasq.d/*.conf
$contents = $this->files->get($this->dnsmasqMasterConfigFile);
// ensure the line we need to use is present, and uncomment it if needed
if (false === strpos($contents, 'conf-dir=/usr/local/etc/dnsmasq.d/,*.conf')) {
$contents .= PHP_EOL . 'conf-dir=/usr/local/etc/dnsmasq.d/,*.conf' . PHP_EOL;
}
$contents = str_replace('#conf-dir=/usr/local/etc/dnsmasq.d/,*.conf', 'conf-dir=/usr/local/etc/dnsmasq.d/,*.conf', $contents);

$this->appendCustomConfigImport($customConfigPath);
// remove entries used by older Valet versions:
$contents = preg_replace('/^conf-file.*valet.*$/m', '', $contents);

$this->files->putAsUser($customConfigPath, 'address=/.'.$tld.'/127.0.0.1'.PHP_EOL.'listen-address=127.0.0.1'.PHP_EOL);
}
// save the updated config file
$this->files->put($this->dnsmasqMasterConfigFile, $contents);

/**
* Copy the Homebrew installed example DnsMasq configuration file.
*
* @return void
*/
function copyExampleConfig()
{
if (! $this->files->exists($this->configPath)) {
$this->files->copyAsUser(
$this->exampleConfigPath,
$this->configPath
);
// remove old ~/.config/valet/dnsmasq.conf file because things are moved to the ~/.config/valet/dnsmasq.d/ folder now
if (file_exists($file = dirname($this->dnsmasqUserConfigDir()) . '/dnsmasq.conf')) {
unlink($file);
}

// add a valet-specific config file to point to user's home directory valet config
$contents = $this->files->get(__DIR__.'/../stubs/etc-dnsmasq-valet.conf');
$contents = str_replace('VALET_HOME_PATH', VALET_HOME_PATH, $contents);
$this->files->ensureDirExists($this->dnsmasqSystemConfDir, user());
$this->files->putAsUser($this->dnsmasqSystemConfDir . '/dnsmasq-valet.conf', $contents);

$this->files->ensureDirExists(VALET_HOME_PATH . '/dnsmasq.d', user());
}

/**
* Append import command for our custom configuration to DnsMasq file.
*
* @param string $customConfigPath
* Create the TLD-specific dnsmasq config file
* @param string $tld
* @return void
*/
function appendCustomConfigImport($customConfigPath)
function createDnsmasqTldConfigFile($tld)
{
$contents = preg_replace('/^conf-file=.*\/\.valet\/.*$/m', '', $this->files->get($this->configPath));
$this->files->putAsUser($this->configPath, $contents);

if (! $this->customConfigIsBeingImported($customConfigPath)) {
$this->files->appendAsUser(
$this->configPath,
PHP_EOL.'conf-file='.$customConfigPath.PHP_EOL
);
}
}
$tldConfigFile = $this->dnsmasqUserConfigDir() . 'tld-' . $tld . '.conf';

/**
* Determine if Valet's custom DnsMasq configuration is being imported.
*
* @param string $customConfigPath
* @return bool
*/
function customConfigIsBeingImported($customConfigPath)
{
return strpos($this->files->get($this->configPath), $customConfigPath) !== false;
$this->files->putAsUser($tldConfigFile, 'address=/.'.$tld.'/127.0.0.1'.PHP_EOL.'listen-address=127.0.0.1'.PHP_EOL);
}

/**
Expand Down Expand Up @@ -143,8 +132,8 @@ function updateTld($oldTld, $newTld)
*
* @return string
*/
function customConfigPath()
function dnsmasqUserConfigDir()
{
return $_SERVER['HOME'].'/.config/valet/dnsmasq.conf';
return $_SERVER['HOME'].'/.config/valet/dnsmasq.d/';
}
}
3 changes: 3 additions & 0 deletions cli/stubs/etc-dnsmasq-valet.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Valet
# Include all *.conf files in user's valet directory
conf-dir=VALET_HOME_PATH/dnsmasq.d/,*.conf
19 changes: 11 additions & 8 deletions tests/DnsMasqTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,21 @@ public function test_install_installs_and_places_configuration_files_in_proper_l

$dnsMasq = resolve(StubForCreatingCustomDnsMasqConfigFiles::class);

$dnsMasq->exampleConfigPath = __DIR__.'/files/dnsmasq.conf';
$dnsMasq->configPath = __DIR__.'/output/dnsmasq.conf';

$dnsMasq->dnsmasqMasterConfigFile = __DIR__.'/output/dnsmasq.conf';
$dnsMasq->dnsmasqSystemConfDir = __DIR__.'/output/dnsmasq.d';
$dnsMasq->resolverPath = __DIR__.'/output/resolver';

file_put_contents($dnsMasq->dnsmasqMasterConfigFile, file_get_contents(__DIR__.'/files/dnsmasq.conf'));

$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/custom-dnsmasq.conf'));
$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'));
$this->assertSame('test-contents

conf-file='.__DIR__.'/output/custom-dnsmasq.conf
', file_get_contents(__DIR__.'/output/dnsmasq.conf'));
' . PHP_EOL . 'conf-dir=/usr/local/etc/dnsmasq.d/,*.conf' . PHP_EOL,
file_get_contents($dnsMasq->dnsmasqMasterConfigFile)
);
}


Expand All @@ -66,8 +69,8 @@ public function test_update_tld_removes_old_resolver_and_reinstalls()

class StubForCreatingCustomDnsMasqConfigFiles extends DnsMasq
{
public function customConfigPath()
public function dnsmasqUserConfigDir()
{
return __DIR__.'/output/custom-dnsmasq.conf';
return __DIR__.'/output/';
}
}