@@ -19,9 +19,11 @@ class DnsMasq
19
19
/**
20
20
* Create a new DnsMasq instance.
21
21
*
22
- * @param PackageManager $pm
23
- * @param ServiceManager $sm
24
- * @param Filesystem $files
22
+ * @param PackageManager $pm PackageManager object
23
+ * @param ServiceManager $sm ServiceManager object
24
+ * @param Filesystem $files Filesystem object
25
+ * @param CommandLine $cli CommandLine object
26
+ *
25
27
* @return void
26
28
*/
27
29
public function __construct(PackageManager $pm, ServiceManager $sm, Filesystem $files, CommandLine $cli)
@@ -42,91 +44,57 @@ public function __construct(PackageManager $pm, ServiceManager $sm, Filesystem $
42
44
/**
43
45
* Install and configure DnsMasq.
44
46
*
47
+ * @param bool $lock Lock or Unlock the file
48
+ *
45
49
* @return void
46
50
*/
47
- private function lockResolvConf ($lock = true)
51
+ private function _lockResolvConf ($lock = true)
48
52
{
49
53
$arg = $lock ? '+i' : '-i';
50
54
51
55
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;
56
+ $this->cli->run(
57
+ "chattr {$arg} {$this->resolvconf}",
58
+ function ($code, $msg) {
59
+ warning($msg);
60
+ }
61
+ );
81
62
}
82
-
83
- return false;
84
63
}
85
64
86
65
/**
87
66
* Enable nameserver merging
88
67
*
89
68
* @return void
90
69
*/
91
- private function mergeDns ()
70
+ private function _mergeDns ()
92
71
{
93
72
$optDir = '/opt/valet-linux';
94
- $script = $optDir.'/get-dns-servers';
95
- $output = [];
73
+ $script = $optDir.'/valet-dns';
96
74
97
75
$this->pm->ensureInstalled('inotify-tools');
76
+ $this->files->remove($optDir);
98
77
$this->files->ensureDirExists($optDir);
99
- $this->files->put($script, $this->files->get(__DIR__.'/../stubs/get-dns-servers'));
100
- $this->cli->run("chmod +x {$script}");
78
+ $this->files->put($script, $this->files->get(__DIR__.'/../stubs/valet-dns'));
79
+ $this->cli->run("chmod +x $script");
80
+ $this->sm->installValetDns($this->files);
101
81
102
- if (! $this->files->exists($this->rclocal)) {
103
- $this->files->put($this->rclocal, implode("\n", ['exit 0', '']));
104
- }
105
-
106
- $rclocal = $this->files->get($this->rclocal);
107
-
108
- if (strpos($rclocal, $script) === false) {
109
- $this->files->backup($this->rclocal);
110
-
111
- foreach( explode("\n", $rclocal) as $line) {
112
- if ($line == 'exit 0') {
113
- $output[] = $script;
114
- $output[] = '';
115
- }
116
-
117
- $output[] = $line;
118
- }
119
-
120
- $this->files->put($this->rclocal, implode("\n", $output));
121
- $this->cli->run("chmod +x {$this->rclocal}");
82
+ if ($this->files->exists($this->rclocal)) {
83
+ $this->files->restore($this->rclocal);
122
84
}
85
+
86
+ $this->files->backup($this->resolvconf);
87
+ $this->files->unlink($this->resolvconf);
88
+ $this->files->symlink($script, $this->resolvconf);
123
89
124
90
return true;
125
91
}
126
92
127
93
/**
128
94
* Install and configure DnsMasq.
129
95
*
96
+ * @param string $domain Domain TLD to use
97
+ *
130
98
* @return void
131
99
*/
132
100
public function install($domain = 'test')
@@ -136,12 +104,14 @@ public function install($domain = 'test')
136
104
$this->createCustomConfigFile($domain);
137
105
$this->pm->nmRestart($this->sm);
138
106
$this->sm->restart('dnsmasq');
107
+ $this->sm->start('valet-dns');
139
108
}
140
109
141
110
/**
142
111
* Append the custom DnsMasq configuration file to the main configuration file.
143
112
*
144
- * @param string $domain
113
+ * @param string $domain Domain TLD to use
114
+ *
145
115
* @return void
146
116
*/
147
117
public function createCustomConfigFile($domain)
@@ -156,17 +126,19 @@ public function createCustomConfigFile($domain)
156
126
*/
157
127
public function fixResolved()
158
128
{
159
- $resolved = $this->resolvedConfigPath;
129
+ // $resolved = $this->resolvedConfigPath;
160
130
161
- $this->files->backup($resolved);
162
- $this->files->putAsUser($resolved, $this->files->get(__DIR__.'/../stubs/resolved.conf'));
131
+ // $this->files->backup($resolved);
132
+ // $this->files->putAsUser($resolved, $this->files->get(__DIR__.'/../stubs/resolved.conf'));
163
133
164
134
$this->sm->disable('systemd-resolved');
165
135
$this->sm->stop('systemd-resolved');
166
136
}
167
137
168
138
/**
169
139
* Setup dnsmasq with Network Manager.
140
+ *
141
+ * @return void
170
142
*/
171
143
public function dnsmasqSetup()
172
144
{
@@ -178,28 +150,23 @@ public function dnsmasqSetup()
178
150
179
151
$this->files->uncommentLine('IGNORE_RESOLVCONF', '/etc/default/dnsmasq');
180
152
181
- $this->mergeDns();
182
-
183
- $this->lockResolvConf(false);
153
+ $this->_lockResolvConf(false);
154
+ $this->_mergeDns();
184
155
185
156
$this->files->unlink('/etc/dnsmasq.d/network-manager');
186
- $this->files->backup($this->resolvconf);
187
- $this->files->unlink($this->resolvconf);
188
157
$this->files->backup($this->dnsmasqconf);
189
158
190
- $this->files->putAsUser($this->resolvconf, 'nameserver 127.0.0.1'.PHP_EOL);
191
159
$this->files->putAsUser($this->dnsmasqconf, $this->files->get(__DIR__.'/../stubs/dnsmasq.conf'));
192
160
$this->files->putAsUser($this->dnsmasqOpts, $this->files->get(__DIR__.'/../stubs/dnsmasq_options'));
193
161
$this->files->putAsUser($this->nmConfigPath, $this->files->get(__DIR__.'/../stubs/networkmanager.conf'));
194
-
195
- $this->lockResolvConf();
196
- $this->dnsWatch('restart');
197
162
}
198
163
199
164
/**
200
165
* Update the domain used by DnsMasq.
201
166
*
202
- * @param string $newDomain
167
+ * @param string $oldDomain Old TLD
168
+ * @param string $newDomain New TLD
169
+ *
203
170
* @return void
204
171
*/
205
172
public function updateDomain($oldDomain, $newDomain)
@@ -215,13 +182,16 @@ public function updateDomain($oldDomain, $newDomain)
215
182
*/
216
183
public function uninstall()
217
184
{
218
- $this->dnsWatch('stop');
185
+ $this->sm->stop('valet-dns');
186
+ $this->sm->disable('valet-dns');
187
+
219
188
$this->cli->passthru('rm -rf /opt/valet-linux');
220
189
$this->files->unlink($this->configPath);
221
190
$this->files->unlink($this->dnsmasqOpts);
222
191
$this->files->unlink($this->nmConfigPath);
223
192
$this->files->restore($this->resolvedConfigPath);
224
- $this->lockResolvConf(false);
193
+
194
+ $this->_lockResolvConf(false);
225
195
$this->files->restore($this->rclocal);
226
196
$this->files->restore($this->resolvconf);
227
197
$this->files->restore($this->dnsmasqconf);
0 commit comments