Debian Codegraph @

vendor/react/dns/src/Config/Config.php

raw
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 $matches = array();
86 preg_match_all('/^nameserver\s+(\S+)\s*$/m', $contents, $matches);
87
88 $config = new self();
89 foreach ($matches[1] as $ip) {
90 // remove IPv6 zone ID (`fe80::1%lo0` => `fe80:1`)
91 if (strpos($ip, ':') !== false && ($pos = strpos($ip, '%')) !== false) {
92 $ip = substr($ip, 0, $pos);
93 }
94
95 if (@inet_pton($ip) !== false) {
96 $config->nameservers[] = $ip;
97 }
98 }
99
100 return $config;
101 }
102
103 /**
104 * Loads the DNS configurations from Windows's WMIC (from the given command or default command)
105 *
106 * Note that this method blocks while loading the given command and should
107 * thus be used with care! While this should be relatively fast for normal
108 * WMIC commands, it remains unknown if this may block under certain
109 * circumstances. In particular, this method should only be executed before
110 * the loop starts, not while it is running.
111 *
112 * Note that this method will only try to execute the given command try to
113 * parse its output, irrespective of whether this command exists. In
114 * particular, this command is only available on Windows. Currently, this
115 * will only parse valid nameserver entries from the command output and will
116 * ignore all other output without complaining.
117 *
118 * Note that the previous section implies that this may return an empty
119 * `Config` object if no valid nameserver entries can be found.
120 *
121 * @param ?string $command (advanced) should not be given (NULL) unless you know what you're doing
122 * @return self
123 * @link https://ss64.com/nt/wmic.html
124 */
125 public static function loadWmicBlocking($command = null)
126 {
127 $contents = shell_exec($command === null ? 'wmic NICCONFIG get "DNSServerSearchOrder" /format:CSV' : $command);
128 preg_match_all('/(?<=[{;,"])([\da-f.:]{4,})(?=[};,"])/i', $contents, $matches);
129
130 $config = new self();
131 $config->nameservers = $matches[1];
132
133 return $config;
134 }
135
136 public $nameservers = array();
137 }