|
14 | 14 | $executor = new TcpTransportExecutor('8.8.8.8:53');
|
15 | 15 |
|
16 | 16 | $name = $argv[1] ?? 'google.com';
|
| 17 | +assert(is_string($name)); |
17 | 18 |
|
18 | 19 | $any = new Query($name, Message::TYPE_ANY, Message::CLASS_IN);
|
19 | 20 |
|
20 |
| -$executor->query($any)->then(function (Message $message) { |
| 21 | +$executor->query($any)->then(function (Message $message): void { |
21 | 22 | foreach ($message->answers as $answer) {
|
22 |
| - /* @var $answer Record */ |
23 |
| - |
24 | 23 | $data = $answer->data;
|
25 | 24 |
|
26 | 25 | switch ($answer->type) {
|
27 | 26 | case Message::TYPE_A:
|
| 27 | + assert(\is_string($data)); |
28 | 28 | $type = 'A';
|
29 | 29 | break;
|
30 | 30 | case Message::TYPE_AAAA:
|
| 31 | + assert(\is_string($data)); |
31 | 32 | $type = 'AAAA';
|
32 | 33 | break;
|
33 | 34 | case Message::TYPE_NS:
|
| 35 | + assert(\is_string($data)); |
34 | 36 | $type = 'NS';
|
35 | 37 | break;
|
36 | 38 | case Message::TYPE_PTR:
|
| 39 | + assert(\is_string($data)); |
37 | 40 | $type = 'PTR';
|
38 | 41 | break;
|
39 | 42 | case Message::TYPE_CNAME:
|
| 43 | + assert(\is_string($data)); |
40 | 44 | $type = 'CNAME';
|
41 | 45 | break;
|
42 | 46 | case Message::TYPE_TXT:
|
| 47 | + assert(\is_array($data)); |
43 | 48 | // TXT records can contain a list of (binary) strings for each record.
|
44 | 49 | // here, we assume this is printable ASCII and simply concatenate output
|
45 | 50 | $type = 'TXT';
|
46 | 51 | $data = implode('', $data);
|
47 | 52 | break;
|
48 | 53 | case Message::TYPE_MX:
|
| 54 | + assert(\is_array($data)); |
49 | 55 | // MX records contain "priority" and "target", only dump its values here
|
50 | 56 | $type = 'MX';
|
51 | 57 | $data = implode(' ', $data);
|
52 | 58 | break;
|
53 | 59 | case Message::TYPE_SRV:
|
| 60 | + assert(\is_array($data)); |
54 | 61 | // SRV records contain priority, weight, port and target, dump structure here
|
55 | 62 | $type = 'SRV';
|
56 | 63 | $data = json_encode($data);
|
57 | 64 | break;
|
58 | 65 | case Message::TYPE_SSHFP:
|
| 66 | + assert(\is_array($data)); |
59 | 67 | // SSHFP records contain algorithm, fingerprint type and hex fingerprint value
|
60 | 68 | $type = 'SSHFP';
|
61 | 69 | $data = implode(' ', $data);
|
62 | 70 | break;
|
63 | 71 | case Message::TYPE_SOA:
|
| 72 | + assert(\is_array($data)); |
64 | 73 | // SOA records contain structured data, dump structure here
|
65 | 74 | $type = 'SOA';
|
66 | 75 | $data = json_encode($data);
|
67 | 76 | break;
|
68 | 77 | case Message::TYPE_CAA:
|
| 78 | + assert(\is_array($data)); |
69 | 79 | // CAA records contains flag, tag and value
|
70 | 80 | $type = 'CAA';
|
71 | 81 | $data = $data['flag'] . ' ' . $data['tag'] . ' "' . $data['value'] . '"';
|
72 | 82 | break;
|
73 | 83 | default:
|
| 84 | + assert(\is_string($data)); |
74 | 85 | // unknown type uses HEX format
|
75 | 86 | $type = 'TYPE' . $answer->type;
|
76 | 87 | $data = wordwrap(strtoupper(bin2hex($data)), 2, ' ', true);
|
77 | 88 | }
|
78 | 89 |
|
79 | 90 | echo $type . ': ' . $data . PHP_EOL;
|
80 | 91 | }
|
81 |
| -}, 'printf'); |
| 92 | +}, static function (Throwable $error) { |
| 93 | + echo $error; |
| 94 | +}); |
0 commit comments