-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDns.php
More file actions
140 lines (122 loc) · 3.59 KB
/
Dns.php
File metadata and controls
140 lines (122 loc) · 3.59 KB
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
140
<?php
namespace Cleantalk\Common\Dns;
class Dns
{
private static $min_server_timeout = 500;
private static $max_server_timeout = 1500;
private static $default_server_ttl;
/**
* Function DNS request
*
* @param string $host URL
* @param bool $return_first returns only first found IP, HOST, TTL
* @param null|int $type DNS type name
*
* @return array
* @psalm-suppress NullableReturnStatement
*/
public static function getRecord($host, $return_first = false, $type = null)
{
$servers = array(
"ip" => null,
"host" => $host,
"ttl" => static::$default_server_ttl
);
// Get DNS records about URL
if (function_exists('dns_get_record')) {
// Localhosts generates errors. block these by @
$records = $type
? @dns_get_record($host, $type)
: @dns_get_record($host);
if ($records !== false) {
$servers = array();
foreach ($records as $server) {
$servers[] = $server;
}
}
}
// Another try if first failed
if (function_exists('gethostbynamel') && empty($records)) {
$records = gethostbynamel($host);
if ($records !== false) {
$servers = array();
foreach ($records as $server) {
$servers[] = array(
"ip" => $server,
"host" => $host,
"ttl" => static::$default_server_ttl
);
}
}
}
return $return_first
? reset($servers)
: $servers;
}
/**
* @param $servers
*
* @return array|null
* @psalm-suppress PossiblyUnusedMethod
*/
public static function findFastestServer($servers)
{
$tmp = array();
$fast_server_found = false;
foreach ($servers as $server) {
if ($fast_server_found) {
$ping = static::$max_server_timeout;
} else {
$ping = static::getResponseTime($server['ip']);
$ping *= 1000;
}
$tmp[$ping] = $server;
$fast_server_found = $ping < static::$min_server_timeout;
}
if (count($tmp)) {
ksort($tmp);
$response = $tmp;
}
return $response ?: null;
}
/**
* Function to check response time
*
* @param string URL
*
* @return int|float Response time
*/
public static function getResponseTime($host)
{
// Skip localhost ping cause it raise error at fsockopen.
// And return minimum value
if ($host === 'localhost') {
return 0.001;
}
$starttime = microtime(true);
$file = @fsockopen($host, 80, $errno, $errstr, static::$max_server_timeout / 1000);
$stoptime = microtime(true);
if ( ! $file) {
$status = static::$max_server_timeout / 1000; // Site is down
} else {
fclose($file);
$status = ($stoptime - $starttime);
$status = round($status, 4);
}
return $status;
}
/**
* Get server TTL
* Wrapper for self::getRecord()
*
* @param $host
*
* @return int|false
* @psalm-suppress PossiblyUnusedMethod
*/
public static function getServerTTL($host)
{
$server = static::getRecord($host, true);
return $server['ttl'];
}
}