| 3 |
liveuser |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace React\Dns\Config;
|
|
|
4 |
|
|
|
5 |
use RuntimeException;
|
|
|
6 |
|
|
|
7 |
final class Config
|
|
|
8 |
{
|
|
|
9 |
/**
|
|
|
10 |
* Loads the system DNS configuration
|
|
|
11 |
*
|
|
|
12 |
* Note that this method may block while loading its internal files and/or
|
|
|
13 |
* commands and should thus be used with care! While this should be
|
|
|
14 |
* relatively fast for most systems, it remains unknown if this may block
|
|
|
15 |
* under certain circumstances. In particular, this method should only be
|
|
|
16 |
* executed before the loop starts, not while it is running.
|
|
|
17 |
*
|
|
|
18 |
* Note that this method will try to access its files and/or commands and
|
|
|
19 |
* try to parse its output. Currently, this will only parse valid nameserver
|
|
|
20 |
* entries from its output and will ignore all other output without
|
|
|
21 |
* complaining.
|
|
|
22 |
*
|
|
|
23 |
* Note that the previous section implies that this may return an empty
|
|
|
24 |
* `Config` object if no valid nameserver entries can be found.
|
|
|
25 |
*
|
|
|
26 |
* @return self
|
|
|
27 |
* @codeCoverageIgnore
|
|
|
28 |
*/
|
|
|
29 |
public static function loadSystemConfigBlocking()
|
|
|
30 |
{
|
|
|
31 |
// Use WMIC output on Windows
|
|
|
32 |
if (DIRECTORY_SEPARATOR === '\\') {
|
|
|
33 |
return self::loadWmicBlocking();
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
// otherwise (try to) load from resolv.conf
|
|
|
37 |
try {
|
|
|
38 |
return self::loadResolvConfBlocking();
|
|
|
39 |
} catch (RuntimeException $ignored) {
|
|
|
40 |
// return empty config if parsing fails (file not found)
|
|
|
41 |
return new self();
|
|
|
42 |
}
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* Loads a resolv.conf file (from the given path or default location)
|
|
|
47 |
*
|
|
|
48 |
* Note that this method blocks while loading the given path and should
|
|
|
49 |
* thus be used with care! While this should be relatively fast for normal
|
|
|
50 |
* resolv.conf files, this may be an issue if this file is located on a slow
|
|
|
51 |
* device or contains an excessive number of entries. In particular, this
|
|
|
52 |
* method should only be executed before the loop starts, not while it is
|
|
|
53 |
* running.
|
|
|
54 |
*
|
|
|
55 |
* Note that this method will throw if the given file can not be loaded,
|
|
|
56 |
* such as if it is not readable or does not exist. In particular, this file
|
|
|
57 |
* is not available on Windows.
|
|
|
58 |
*
|
|
|
59 |
* Currently, this will only parse valid "nameserver X" lines from the
|
|
|
60 |
* given file contents. Lines can be commented out with "#" and ";" and
|
|
|
61 |
* invalid lines will be ignored without complaining. See also
|
|
|
62 |
* `man resolv.conf` for more details.
|
|
|
63 |
*
|
|
|
64 |
* Note that the previous section implies that this may return an empty
|
|
|
65 |
* `Config` object if no valid "nameserver X" lines can be found. See also
|
|
|
66 |
* `man resolv.conf` which suggests that the DNS server on the localhost
|
|
|
67 |
* should be used in this case. This is left up to higher level consumers
|
|
|
68 |
* of this API.
|
|
|
69 |
*
|
|
|
70 |
* @param ?string $path (optional) path to resolv.conf file or null=load default location
|
|
|
71 |
* @return self
|
|
|
72 |
* @throws RuntimeException if the path can not be loaded (does not exist)
|
|
|
73 |
*/
|
|
|
74 |
public static function loadResolvConfBlocking($path = null)
|
|
|
75 |
{
|
|
|
76 |
if ($path === null) {
|
|
|
77 |
$path = '/etc/resolv.conf';
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
$contents = @file_get_contents($path);
|
|
|
81 |
if ($contents === false) {
|
|
|
82 |
throw new RuntimeException('Unable to load resolv.conf file "' . $path . '"');
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
preg_match_all('/^nameserver\s+(\S+)\s*$/m', $contents, $matches);
|
|
|
86 |
|
|
|
87 |
$config = new self();
|
|
|
88 |
$config->nameservers = $matches[1];
|
|
|
89 |
|
|
|
90 |
return $config;
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
/**
|
|
|
94 |
* Loads the DNS configurations from Windows's WMIC (from the given command or default command)
|
|
|
95 |
*
|
|
|
96 |
* Note that this method blocks while loading the given command and should
|
|
|
97 |
* thus be used with care! While this should be relatively fast for normal
|
|
|
98 |
* WMIC commands, it remains unknown if this may block under certain
|
|
|
99 |
* circumstances. In particular, this method should only be executed before
|
|
|
100 |
* the loop starts, not while it is running.
|
|
|
101 |
*
|
|
|
102 |
* Note that this method will only try to execute the given command try to
|
|
|
103 |
* parse its output, irrespective of whether this command exists. In
|
|
|
104 |
* particular, this command is only available on Windows. Currently, this
|
|
|
105 |
* will only parse valid nameserver entries from the command output and will
|
|
|
106 |
* ignore all other output without complaining.
|
|
|
107 |
*
|
|
|
108 |
* Note that the previous section implies that this may return an empty
|
|
|
109 |
* `Config` object if no valid nameserver entries can be found.
|
|
|
110 |
*
|
|
|
111 |
* @param ?string $command (advanced) should not be given (NULL) unless you know what you're doing
|
|
|
112 |
* @return self
|
|
|
113 |
* @link https://ss64.com/nt/wmic.html
|
|
|
114 |
*/
|
|
|
115 |
public static function loadWmicBlocking($command = null)
|
|
|
116 |
{
|
|
|
117 |
$contents = shell_exec($command === null ? 'wmic NICCONFIG get "DNSServerSearchOrder" /format:CSV' : $command);
|
|
|
118 |
preg_match_all('/(?<=[{;,"])([\da-f.:]{4,})(?=[};,"])/i', $contents, $matches);
|
|
|
119 |
|
|
|
120 |
$config = new self();
|
|
|
121 |
$config->nameservers = $matches[1];
|
|
|
122 |
|
|
|
123 |
return $config;
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
public $nameservers = array();
|
|
|
127 |
}
|