-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathClient.php
262 lines (242 loc) · 7.62 KB
/
Client.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
declare(strict_types=1);
namespace ArrayAccess\RdapClient;
use ArrayAccess\RdapClient\Exceptions\EmptyArgumentException;
use ArrayAccess\RdapClient\Exceptions\InvalidServiceDefinitionException;
use ArrayAccess\RdapClient\Exceptions\UnsupportedProtocolException;
use ArrayAccess\RdapClient\Interfaces\RdapClientInterface;
use ArrayAccess\RdapClient\Interfaces\RdapProtocolInterface;
use ArrayAccess\RdapClient\Protocols\AsnProtocol;
use ArrayAccess\RdapClient\Protocols\DomainProtocol;
use ArrayAccess\RdapClient\Protocols\IPv4Protocol;
use ArrayAccess\RdapClient\Protocols\IPv6Protocol;
use ArrayAccess\RdapClient\Protocols\NsProtocol;
use ArrayAccess\RdapClient\Services\AsnService;
use ArrayAccess\RdapClient\Util\CIDR;
use function explode;
use function idn_to_ascii;
use function is_a;
use function is_array;
use function is_int;
use function is_object;
use function preg_match;
use function sprintf;
use function str_contains;
use function strlen;
use function strtolower;
use function trim;
class Client implements RdapClientInterface
{
/**
* @var string Version
*/
public const VERSION = '1.0.1';
/**
* @var array<string, class-string<RdapProtocolInterface>>
*/
final public const PROTOCOLS = [
self::IPV4 => IPv4Protocol::class,
self::IPV6 => IPv6Protocol::class,
self::ASN => AsnProtocol::class,
self::DOMAIN => DomainProtocol::class,
self::NS => NsProtocol::class,
];
/**
* @var array<class-string<RdapProtocolInterface>|RdapProtocolInterface>
*/
protected array $protocols = self::PROTOCOLS;
/**
* Check if protocol is supported
*
* @param string $protocolType
* @return bool
*/
public function hasProtocol(string $protocolType) : bool
{
return isset($this->protocols[$protocolType]);
}
/**
* Set Protocol
*
* @param RdapProtocolInterface $protocol
* @return void
* @throws InvalidServiceDefinitionException if protocol is not supported
*/
public function setProtocol(RdapProtocolInterface $protocol): void
{
foreach (self::PROTOCOLS as $protocolVersion => $obj) {
if (is_a($protocol, $obj)) {
$this->protocols[$protocolVersion] = $obj;
break;
}
}
throw new InvalidServiceDefinitionException(
sprintf(
'Service protocol "%s" is not supported',
$protocol::class
)
);
}
/**
* Get Protocol
*
* @param string $protocolType
* @return RdapProtocolInterface
* @throws UnsupportedProtocolException if protocol is not supported
*/
public function getProtocol(string $protocolType) : RdapProtocolInterface
{
if (!isset($this->protocols[$protocolType])
&& isset($this->protocols[strtolower(trim($protocolType))])
) {
$protocolType = strtolower(trim($protocolType));
}
if (isset($this->protocols[$protocolType])) {
if (!is_object($this->protocols[$protocolType])) {
$this->protocols[$protocolType] = new $this->protocols[$protocolType]($this);
}
return $this->protocols[$protocolType];
}
throw new UnsupportedProtocolException($protocolType);
}
/**
* Guess type of target
*
* @param string $target
* @return ?array{0:string, 1:string}
*/
public function guessType(string $target): ?array
{
$target = trim($target);
if (!$target) {
return null;
}
if (str_contains($target, '/') && ($cidr = CIDR::cidrToRange($target))) {
if (str_contains($cidr[0], ':') || str_contains($cidr[1], ':')) {
if (CIDR::filterIp6($cidr[0]) && CIDR::filterIp6($cidr[1])) {
return [self::IPV6, $target];
}
}
if (str_contains($cidr[0], '.') || str_contains($cidr[1], '.')) {
if (CIDR::filterIp4($cidr[0]) && CIDR::filterIp4($cidr[1])) {
return [self::IPV4, $target];
}
}
}
if (preg_match('~^(?:ASN?)?([0-9]+)$~i', $target, $match)) {
$target = $match[1] > 0 && $match[1] <= AsnService::MAX_INTEGER
? $match[1]
: null;
return $target ? [self::ASN, $target] : null;
}
if (str_contains($target, ':') && ($ip6 = CIDR::filterIp6($target))) {
return [self::IPV6, $ip6];
}
if (!str_contains($target, '.')) {
return null;
}
if ($ip4 = CIDR::filterIp4($target)) {
return [self::IPV4, $ip4];
}
$target = idn_to_ascii($target)?:null;
if (!$target) {
return null;
}
if (strlen($target) > 255) {
return null;
}
foreach (explode('.', $target) as $part) {
if ($part === '' || strlen($part) > 63) {
return null;
}
}
// just to try to get nameserver if domains started with ns[0-9]* or name.ns[0-9]*
if (preg_match('~^((?:[^.]+\.)?ns[0-9]*)\.[^.]+\.~', $target)) {
return [self::NS, $target];
}
return [self::DOMAIN, $target];
}
/**
* Get RDAP Request
*
* @param string|int $target
* @param string|null $protocol
* @return Interfaces\RdapRequestInterface|null
*/
public function request(string|int $target, ?string $protocol = null): ?Interfaces\RdapRequestInterface
{
if (is_int($target)) {
$target = (string) $target;
return $this->getProtocol(self::ASN)->find($target);
}
$target = trim($target);
if ($target === '') {
throw new EmptyArgumentException(
'Argument target could not be empty'
);
}
if ($protocol === null) {
$definitions = $this->guessType($target);
if (is_array($definitions)) {
[$protocol, $target] = $definitions;
}
}
if (!$protocol) {
throw new EmptyArgumentException(
'Protocol is empty & can not guess.'
);
}
$object = $this->getProtocol($protocol);
return $object->find($target);
}
/**
* Get Domain Request
*
* @param string $target
* @return Interfaces\RdapRequestInterface|null
*/
public function domain(string $target): ?Interfaces\RdapRequestInterface
{
return $this->request($target, self::DOMAIN);
}
/**
* Get ASN Request
*
* @param string|int $target
* @return Interfaces\RdapRequestInterface|null
*/
public function asn(string|int $target): ?Interfaces\RdapRequestInterface
{
return $this->request($target, self::ASN);
}
/**
* Get ipv4 Request
*
* @param string $target
* @return Interfaces\RdapRequestInterface|null
*/
public function ipv4(string $target): ?Interfaces\RdapRequestInterface
{
return $this->request($target, self::IPV4);
}
/**
* Get IPv6 Request
*
* @param string $target
* @return Interfaces\RdapRequestInterface|null
*/
public function ipv6(string $target): ?Interfaces\RdapRequestInterface
{
return $this->request($target, self::IPV6);
}
/**
* Get nameserver Request
*
* @param string $target
* @return Interfaces\RdapRequestInterface|null
*/
public function nameserver(string $target): ?Interfaces\RdapRequestInterface
{
return $this->request($target, self::NS);
}
}