-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolver.php
139 lines (117 loc) · 4.43 KB
/
resolver.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/*
* Copyright 2017, 2023 Shaun Cummiskey, <shaun@shaunc.com> <https://shaunc.com>
* <https://github.com/parseword/php-multithreaded-resolver/>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
error_reporting(E_ALL);
define('DEBUG', false);
define('MAX_THREADS', 16);
//We need pthreads in order to proceed
if (!extension_loaded('pthreads')) {
die('This script requires PHP compiled with the pthreads extension.' . PHP_EOL);
}
//A function to write debug messages to the console
function debug($message) {
echo DEBUG ? sprintf("%17.6f", microtime(true)) . ':: ' . $message . PHP_EOL : '';
}
//A class for the worker threads
class ResolverThread extends Thread
{
private $id = null;
protected $host = null;
public function __construct($id, $host) {
$this->id = $id;
$this->host = $host;
}
public function run(): void {
if (preg_match('|[a-z]|i', $this->host)) {
//We got a hostname, resolve to an IP
if ($result = @dns_get_record($this->host, DNS_A)) {
echo $this->host . ':' . $result[0]['ip'] . PHP_EOL;
$this->debug("threadId {$this->id}:: {$this->host}:" . $result[0]['ip']);
}
else {
echo $this->host . ':NO_A_RECORD' . PHP_EOL;
$this->debug("threadId {$this->id}:: {$this->host}:NO_A_RECORD");
}
}
else {
//Construct in-addr.arpa address and resolve to PTR
$in_addr = join('.', array_reverse(explode('.', $this->host))) . '.in-addr.arpa';
if ($result = @dns_get_record($in_addr, DNS_PTR)) {
echo $this->host . ':' . $result[0]['target'] . PHP_EOL;
$this->debug("threadId {$this->id}:: {$this->host}:" . $in_addr . ':' . $result[0]['target']);
}
else {
echo $this->host . ':NO_PTR_RECORD' . PHP_EOL;
$this->debug("threadId {$this->id}:: {$this->host}:" . $in_addr . ":NO_PTR_RECORD");
}
}
usleep(100);
}
public function getId() {
return $this->id;
}
private function debug($message) {
echo DEBUG ? sprintf("%17.6f", microtime(true)) . ':: ' . $message . PHP_EOL : '';
}
}
//Open the list of hosts to resolve
$fp = fopen('hosts.txt', 'r');
$workers = [];
$timeStart = microtime(true);
//Load the workers queue with MAX_THREADS threads
while (@$i++ < MAX_THREADS && !feof($fp)) {
if (!empty($line = trim(fgets($fp)))) {
$threadId = substr(md5(microtime(true) . rand(0, 999)), 0, 16);
$workers[$threadId] = new ResolverThread($threadId, $line);
$workers[$threadId]->start();
}
}
//Manage the thread queue until there's no work left to do
while (1) {
usleep(100000);
$deadThreads = 0;
foreach ($workers as $thread) {
//If the thread is still working, skip it
if ($thread->isRunning()) {
continue;
}
//If the thread is completed, join and destroy it
debug('Calling join() on ' . $thread->getId());
if ($thread->join()) {
unset($workers[$thread->getId()]);
$deadThreads++;
}
}
//If no threads were finished, go wait again
if ($deadThreads == 0)
continue;
//Refill the thread queue
debug("Starting $deadThreads threads");
for ($i = 0; $i < $deadThreads && $host = fgets($fp); $i++) {
$threadId = substr(md5(microtime(true) . rand(0, 999)), 0, 16);
debug('memory_get_usage ' . memory_get_usage());
debug('workers[] ' . count($workers) . ' deadThreads ' . $deadThreads
. " Creating new thread $threadId");
$workers[$threadId] = new ResolverThread($threadId, trim($host));
$workers[$threadId]->start();
}
//Bail when we're out of work to do
if (feof($fp) && count($workers) == 0) {
break;
}
}
debug('#Finished in ' . sprintf('%.02f', microtime(true) - $timeStart) . ' seconds');