forked from reactphp/dns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
92-query-any.php
84 lines (73 loc) · 2.84 KB
/
92-query-any.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
// $ php examples/92-query-any.php mailbox.org
// $ php examples/92-query-any.php _carddav._tcp.mailbox.org
use React\Dns\Model\Message;
use React\Dns\Model\Record;
use React\Dns\Query\Query;
use React\Dns\Query\TcpTransportExecutor;
use React\EventLoop\Factory;
require __DIR__ . '/../vendor/autoload.php';
$loop = Factory::create();
$executor = new TcpTransportExecutor('8.8.8.8:53', $loop);
$name = isset($argv[1]) ? $argv[1] : 'google.com';
$any = new Query($name, Message::TYPE_ANY, Message::CLASS_IN);
$executor->query($any)->then(function (Message $message) {
foreach ($message->answers as $answer) {
/* @var $answer Record */
$data = $answer->data;
switch ($answer->type) {
case Message::TYPE_A:
$type = 'A';
break;
case Message::TYPE_AAAA:
$type = 'AAAA';
break;
case Message::TYPE_NS:
$type = 'NS';
break;
case Message::TYPE_PTR:
$type = 'PTR';
break;
case Message::TYPE_CNAME:
$type = 'CNAME';
break;
case Message::TYPE_TXT:
// TXT records can contain a list of (binary) strings for each record.
// here, we assume this is printable ASCII and simply concatenate output
$type = 'TXT';
$data = implode('', $data);
break;
case Message::TYPE_MX:
// MX records contain "priority" and "target", only dump its values here
$type = 'MX';
$data = implode(' ', $data);
break;
case Message::TYPE_SRV:
// SRV records contain priority, weight, port and target, dump structure here
$type = 'SRV';
$data = json_encode($data);
break;
case Message::TYPE_SSHFP:
// SSHFP records contain algorithm, fingerprint type and hex fingerprint value
$type = 'SSHFP';
$data = implode(' ', $data);
break;
case Message::TYPE_SOA:
// SOA records contain structured data, dump structure here
$type = 'SOA';
$data = json_encode($data);
break;
case Message::TYPE_CAA:
// CAA records contains flag, tag and value
$type = 'CAA';
$data = $data['flag'] . ' ' . $data['tag'] . ' "' . $data['value'] . '"';
break;
default:
// unknown type uses HEX format
$type = 'Type ' . $answer->type;
$data = wordwrap(strtoupper(bin2hex($data)), 2, ' ', true);
}
echo $type . ': ' . $data . PHP_EOL;
}
}, 'printf');
$loop->run();