@@ -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 */
0 commit comments