@@ -11,6 +11,7 @@ class DnsMasq
11
11
public $ sm ;
12
12
public $ cli ;
13
13
public $ files ;
14
+ public $ rclocal ;
14
15
public $ configPath ;
15
16
public $ nmConfigPath ;
16
17
public $ resolvedConfig ;
@@ -25,11 +26,15 @@ class DnsMasq
25
26
*/
26
27
public function __construct (PackageManager $ pm , ServiceManager $ sm , Filesystem $ files , CommandLine $ cli )
27
28
{
28
- $ this ->pm = $ pm ;
29
- $ this ->sm = $ sm ;
30
- $ this ->cli = $ cli ;
29
+ $ this ->pm = $ pm ;
30
+ $ this ->sm = $ sm ;
31
+ $ this ->cli = $ cli ;
31
32
$ this ->files = $ files ;
32
- $ this ->configPath = '/etc/NetworkManager/dnsmasq.d/valet ' ;
33
+ $ this ->rclocal = '/etc/rc.local ' ;
34
+ $ this ->resolvconf = '/etc/resolv.conf ' ;
35
+ $ this ->dnsmasqconf = '/etc/dnsmasq.conf ' ;
36
+ $ this ->configPath = '/etc/dnsmasq.d/valet ' ;
37
+ $ this ->dnsmasqOpts = '/etc/dnsmasq.d/options ' ;
33
38
$ this ->nmConfigPath = '/etc/NetworkManager/conf.d/valet.conf ' ;
34
39
$ this ->resolvedConfigPath = '/etc/systemd/resolved.conf ' ;
35
40
}
@@ -39,12 +44,94 @@ public function __construct(PackageManager $pm, ServiceManager $sm, Filesystem $
39
44
*
40
45
* @return void
41
46
*/
42
- public function install ()
47
+ private function lockResolvConf ($ lock = true )
48
+ {
49
+ $ arg = $ lock ? '+i ' : '-i ' ;
50
+
51
+ if (! $ this ->files ->isLink ($ this ->resolvconf )) {
52
+ $ this ->cli ->run ("chattr {$ arg } {$ this ->resolvconf }" , function ($ code , $ msg ) {
53
+ warning ($ msg );
54
+ });
55
+ }
56
+ }
57
+
58
+ /**
59
+ * Control dns watcher.
60
+ *
61
+ * @param string $action start|stop|restart
62
+ * @return boolean
63
+ */
64
+ private function dnsWatch ($ action = 'start ' )
65
+ {
66
+ if ($ action === 'start ' ) {
67
+ $ this ->cli ->quietly ('/opt/valet-linux/get-dns-servers ' );
68
+ return true ;
69
+ }
70
+
71
+ if ($ action === 'stop ' ) {
72
+ $ this ->cli ->passthru ('pkill -f "inotifywait -q -m -e modify --format" ' );
73
+ $ this ->cli ->passthru ('pkill -f "bash .*/get-dns-servers" ' );
74
+ return true ;
75
+ }
76
+
77
+ if ($ action === 'restart ' ) {
78
+ $ this ->dnsWatch ('stop ' );
79
+ $ this ->dnsWatch ('start ' );
80
+ return true ;
81
+ }
82
+
83
+ return false ;
84
+ }
85
+
86
+ /**
87
+ * Enable nameserver merging
88
+ *
89
+ * @return void
90
+ */
91
+ private function mergeDns ()
92
+ {
93
+ $ optDir = '/opt/valet-linux ' ;
94
+ $ script = $ optDir .'/get-dns-servers ' ;
95
+ $ rclocal = $ this ->files ->get ($ this ->rclocal );
96
+ $ output = [];
97
+
98
+ $ this ->pm ->ensureInstalled ('inotify-tools ' );
99
+ $ this ->files ->ensureDirExists ($ optDir );
100
+ $ this ->files ->put ($ script , $ this ->files ->get (__DIR__ .'/../stubs/get-dns-servers ' ));
101
+ $ this ->cli ->run ("chmod +x {$ script }" );
102
+
103
+ if (strpos ($ rclocal , $ script ) === false ) {
104
+ $ this ->files ->backup ($ this ->rclocal );
105
+
106
+ foreach ( explode ("\n" , $ rclocal ) as $ line ) {
107
+ if ($ line == 'exit 0 ' ) {
108
+ $ output [] = $ script ;
109
+ $ output [] = '' ;
110
+ }
111
+
112
+ $ output [] = $ line ;
113
+ }
114
+
115
+ $ this ->files ->put ($ this ->rclocal , implode ("\n" , $ output ));
116
+ $ this ->cli ->run ("chmod +x {$ this ->rclocal }" );
117
+ $ this ->dnsWatch ('restart ' );
118
+ }
119
+
120
+ return true ;
121
+ }
122
+
123
+ /**
124
+ * Install and configure DnsMasq.
125
+ *
126
+ * @return void
127
+ */
128
+ public function install ($ domain = 'dev ' )
43
129
{
44
130
$ this ->dnsmasqSetup ();
45
131
$ this ->fixResolved ();
46
- $ this ->createCustomConfigFile ('dev ' );
47
- $ this ->pm ->dnsmasqRestart ($ this ->sm );
132
+ $ this ->createCustomConfigFile ($ domain );
133
+ $ this ->pm ->nmRestart ($ this ->sm );
134
+ $ this ->sm ->restart ('dnsmasq ' );
48
135
}
49
136
50
137
/**
@@ -69,6 +156,9 @@ public function fixResolved()
69
156
70
157
$ this ->files ->backup ($ resolved );
71
158
$ this ->files ->putAsUser ($ resolved , $ this ->files ->get (__DIR__ .'/../stubs/resolved.conf ' ));
159
+
160
+ $ this ->sm ->disable ('systemd-resolved ' );
161
+ $ this ->sm ->stop ('systemd-resolved ' );
72
162
}
73
163
74
164
/**
@@ -77,9 +167,28 @@ public function fixResolved()
77
167
public function dnsmasqSetup ()
78
168
{
79
169
$ this ->pm ->ensureInstalled ('dnsmasq ' );
170
+ $ this ->sm ->enable ('dnsmasq ' );
171
+
80
172
$ this ->files ->ensureDirExists ('/etc/NetworkManager/conf.d ' );
173
+ $ this ->files ->ensureDirExists ('/etc/dnsmasq.d ' );
174
+
175
+ $ this ->files ->uncommentLine ('IGNORE_RESOLVCONF ' , '/etc/default/dnsmasq ' );
176
+
177
+ $ this ->mergeDns ();
178
+
179
+ $ this ->lockResolvConf (false );
81
180
181
+ $ this ->files ->unlink ('/etc/dnsmasq.d/network-manager ' );
182
+ $ this ->files ->backup ($ this ->resolvconf );
183
+ $ this ->files ->unlink ($ this ->resolvconf );
184
+ $ this ->files ->backup ($ this ->dnsmasqconf );
185
+
186
+ $ this ->files ->putAsUser ($ this ->resolvconf , 'nameserver 127.0.0.1 ' .PHP_EOL );
187
+ $ this ->files ->putAsUser ($ this ->dnsmasqconf , $ this ->files ->get (__DIR__ .'/../stubs/dnsmasq.conf ' ));
188
+ $ this ->files ->putAsUser ($ this ->dnsmasqOpts , $ this ->files ->get (__DIR__ .'/../stubs/dnsmasq_options ' ));
82
189
$ this ->files ->putAsUser ($ this ->nmConfigPath , $ this ->files ->get (__DIR__ .'/../stubs/networkmanager.conf ' ));
190
+
191
+ $ this ->lockResolvConf ();
83
192
}
84
193
85
194
/**
@@ -90,9 +199,8 @@ public function dnsmasqSetup()
90
199
*/
91
200
public function updateDomain ($ oldDomain , $ newDomain )
92
201
{
93
- $ this ->fixResolved ();
94
202
$ this ->createCustomConfigFile ($ newDomain );
95
- $ this ->pm -> dnsmasqRestart ( $ this -> sm );
203
+ $ this ->sm -> restart ( ' dnsmasq ' );
96
204
}
97
205
98
206
/**
@@ -102,10 +210,22 @@ public function updateDomain($oldDomain, $newDomain)
102
210
*/
103
211
public function uninstall ()
104
212
{
213
+ $ this ->dnsWatch ('stop ' );
214
+ $ this ->cli ->passthru ('rm -rf /opt/valet-linux ' );
105
215
$ this ->files ->unlink ($ this ->configPath );
216
+ $ this ->files ->unlink ($ this ->dnsmasqOpts );
106
217
$ this ->files ->unlink ($ this ->nmConfigPath );
107
218
$ this ->files ->restore ($ this ->resolvedConfigPath );
219
+ $ this ->lockResolvConf (false );
220
+ $ this ->files ->restore ($ this ->rclocal );
221
+ $ this ->files ->restore ($ this ->resolvconf );
222
+ $ this ->files ->restore ($ this ->dnsmasqconf );
223
+ $ this ->files ->commentLine ('IGNORE_RESOLVCONF ' , '/etc/default/dnsmasq ' );
224
+
225
+ $ this ->pm ->nmRestart ($ this ->sm );
226
+ $ this ->sm ->restart ('dnsmasq ' );
108
227
109
- $ this ->pm ->dnsmasqRestart ($ this ->sm );
228
+ info ('Valet DNS changes have been rolled back ' );
229
+ warning ('If your system depended on systemd-resolved (like Ubuntu 17.04), please enable it manually ' );
110
230
}
111
231
}
0 commit comments