Skip to content

Commit 0c0ab72

Browse files
🐛 Fixing Nginx configuration.
✨ Adding alias generation and launch daemon generation.
1 parent 545c075 commit 0c0ab72

File tree

7 files changed

+132
-15
lines changed

7 files changed

+132
-15
lines changed

cli/Valet/Nginx.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,12 @@ function installServer()
7575
{
7676
$this->files->ensureDirExists(BREW_PREFIX.'/etc/nginx/valet');
7777

78-
$loopback = $this->configuration->read()['loopback'];
79-
8078
$this->files->putAsUser(
8179
BREW_PREFIX.'/etc/nginx/valet/valet.conf',
8280
str_replace(
83-
['VALET_LOOPBACK', 'VALET_HOME_PATH', 'VALET_SERVER_PATH', 'VALET_STATIC_PREFIX'],
84-
[$loopback, VALET_HOME_PATH, VALET_SERVER_PATH, VALET_STATIC_PREFIX],
85-
$this->files->get(__DIR__.'/../stubs/valet.conf')
81+
['VALET_HOME_PATH', 'VALET_SERVER_PATH', 'VALET_STATIC_PREFIX'],
82+
[VALET_HOME_PATH, VALET_SERVER_PATH, VALET_STATIC_PREFIX],
83+
$this->replaceLoopback($this->files->get(__DIR__.'/../stubs/valet.conf'))
8684
)
8785
);
8886

@@ -92,6 +90,23 @@ function installServer()
9290
);
9391
}
9492

93+
function replaceLoopback($siteConf)
94+
{
95+
$loopback = $this->configuration->read()['loopback'];
96+
97+
if ($loopback === VALET_LOOPBACK) {
98+
return $siteConf;
99+
}
100+
101+
$str = '#listen VALET_LOOPBACK:80 default_server; # valet loopback';
102+
103+
return str_replace(
104+
$str,
105+
substr(str_replace('VALET_LOOPBACK', $loopback, $str), 1),
106+
$siteConf
107+
);
108+
}
109+
95110
/**
96111
* Install the Nginx configuration directory to the ~/.config/valet directory.
97112
*

cli/Valet/Site.php

Lines changed: 80 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -424,15 +424,26 @@ function resecureForNewLoopback($oldLoopback, $loopback)
424424
*/
425425
function replaceOldLoopbackWithNew($siteConf, $old, $new)
426426
{
427+
$shouldComment = $new === VALET_LOOPBACK;
428+
427429
$lookups = [];
428-
$lookups[] = '~listen .*:80;~';
429-
$lookups[] = '~listen .*:443 ssl http2;~';
430-
$lookups[] = '~listen .*:60;~';
430+
$lookups[] = '~#?listen .*:80; # valet loopback~';
431+
$lookups[] = '~#?listen .*:443 ssl http2; # valet loopback~';
432+
$lookups[] = '~#?listen .*:60; # valet loopback~';
431433

432434
foreach ($lookups as $lookup) {
433435
preg_match($lookup, $siteConf, $matches);
434436
foreach ($matches as $match) {
435437
$replaced = str_replace($old, $new, $match);
438+
439+
if ($shouldComment && strpos($replaced, '#') !== 0) {
440+
$replaced = '#'.$replaced;
441+
}
442+
443+
if (! $shouldComment) {
444+
$replaced = ltrim($replaced, '#');
445+
}
446+
436447
$siteConf = str_replace($match, $replaced, $siteConf);
437448
}
438449
}
@@ -752,6 +763,62 @@ function proxyDelete($url)
752763
info('Valet will no longer proxy [https://'.$url.'].');
753764
}
754765

766+
/**
767+
* Remove old loopback interface alias and new one if necessary.
768+
*
769+
* @param string $oldLoopback
770+
* @param string $loopback
771+
* @return void
772+
*/
773+
function aliasLoopback($oldLoopback, $loopback)
774+
{
775+
if ($oldLoopback !== VALET_LOOPBACK) {
776+
$this->cli->run(sprintf(
777+
'sudo ifconfig lo0 -alias %s', $oldLoopback
778+
));
779+
780+
info('Old loopback interface alias removed ['.$oldLoopback.']');
781+
}
782+
783+
if ($loopback !== VALET_LOOPBACK) {
784+
$this->cli->run(sprintf(
785+
'sudo ifconfig lo0 alias %s', $loopback
786+
));
787+
788+
info('New loopback interface alias added ['.$loopback.']');
789+
}
790+
791+
$this->updatePlist($loopback);
792+
}
793+
794+
/**
795+
* Remove old LaunchDaemon and create a new one if necessary.
796+
*
797+
* @param string $loopback
798+
* @return void
799+
*/
800+
function updatePlist($loopback)
801+
{
802+
if ($this->files->exists($this->plistPath())) {
803+
$this->files->unlink($this->plistPath());
804+
805+
info('Old plist file removed ['.$this->plistPath().']');
806+
}
807+
808+
if ($loopback !== VALET_LOOPBACK) {
809+
$this->files->put(
810+
$this->plistPath(),
811+
str_replace(
812+
'VALET_LOOPBACK',
813+
$loopback,
814+
$this->files->get(__DIR__.'/../stubs/ifconfig.plist')
815+
)
816+
);
817+
818+
info('New plist file added ['.$this->plistPath().']');
819+
}
820+
}
821+
755822
function valetHomePath()
756823
{
757824
return VALET_HOME_PATH;
@@ -762,6 +829,16 @@ function valetLoopback()
762829
return $this->config->read()['loopback'];
763830
}
764831

832+
/**
833+
* Get the path to loopback LaunchDaemon.
834+
*
835+
* @return string
836+
*/
837+
function plistPath()
838+
{
839+
return '/Library/LaunchDaemons/com.laravel.valet.ifconfig.plist';
840+
}
841+
765842
/**
766843
* Get the path to Nginx site configuration files.
767844
*/

cli/stubs/ifconfig.plist

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>Label</key>
6+
<string>com.laravel.valet.ifconfig</string>
7+
<key>RunAtLoad</key>
8+
<true/>
9+
<key>ProgramArguments</key>
10+
<array>
11+
<string>/sbin/ifconfig</string>
12+
<string>lo0</string>
13+
<string>alias</string>
14+
<string>VALET_LOOPBACK</string>
15+
</array>
16+
</dict>
17+
</plist>

cli/stubs/proxy.valet.conf

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
# valet stub: proxy.valet.conf
22

33
server {
4-
listen VALET_LOOPBACK:80;
4+
listen 127.0.0.1:80;
5+
#listen VALET_LOOPBACK:80; # valet loopback
56
server_name VALET_SITE www.VALET_SITE *.VALET_SITE;
67
return 301 https://$host$request_uri;
78
}
89

910
server {
10-
listen VALET_LOOPBACK:443 ssl http2;
11+
listen 127.0.0.1:443 ssl http2;
12+
#listen VALET_LOOPBACK:443 ssl http2; # valet loopback
1113
server_name VALET_SITE www.VALET_SITE *.VALET_SITE;
1214
root /;
1315
charset utf-8;
@@ -55,7 +57,8 @@ server {
5557
}
5658

5759
server {
58-
listen VALET_LOOPBACK:60;
60+
listen 127.0.0.1:60;
61+
#listen VALET_LOOPBACK:60; # valet loopback
5962
server_name VALET_SITE www.VALET_SITE *.VALET_SITE;
6063
root /;
6164
charset utf-8;

cli/stubs/secure.valet.conf

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
server {
2-
listen VALET_LOOPBACK:80;
2+
listen 127.0.0.1:80;
3+
#listen VALET_LOOPBACK:80; # valet loopback
34
server_name VALET_SITE www.VALET_SITE *.VALET_SITE;
45
return 301 https://$host$request_uri;
56
}
67

78
server {
8-
listen VALET_LOOPBACK:443 ssl http2;
9+
listen 127.0.0.1:443 ssl http2;
10+
#listen VALET_LOOPBACK:443 ssl http2; # valet loopback
911
server_name VALET_SITE www.VALET_SITE *.VALET_SITE;
1012
root /;
1113
charset utf-8;
@@ -48,7 +50,8 @@ server {
4850
}
4951

5052
server {
51-
listen VALET_LOOPBACK:60;
53+
listen 127.0.0.1:60;
54+
#listen VALET_LOOPBACK:60; # valet loopback
5255
server_name VALET_SITE www.VALET_SITE *.VALET_SITE;
5356
root /;
5457
charset utf-8;

cli/stubs/valet.conf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
server {
2-
listen VALET_LOOPBACK:80 default_server;
2+
listen 127.0.0.1:80 default_server;
3+
#listen VALET_LOOPBACK:80 default_server; # valet loopback
34
root /;
45
charset utf-8;
56
client_max_body_size 128M;

cli/valet.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
Configuration::updateKey('loopback', $loopback = trim($loopback, '.'));
114114

115115
DnsMasq::refreshConfiguration();
116+
Site::aliasLoopback($oldLoopback, $loopback);
116117
Site::resecureForNewLoopback($oldLoopback, $loopback);
117118
Nginx::installServer();
118119
PhpFpm::restart();

0 commit comments

Comments
 (0)