-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Client.php
197 lines (177 loc) · 6.18 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
<?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
{
const VERSION = '1.0.0';
final 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;
public function hasProtocol(string $protocolType) : bool
{
return isset($this->protocols[$protocolType]);
}
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
)
);
}
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);
}
/**
* @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];
}
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);
}
public function domain(string $target): ?Interfaces\RdapRequestInterface
{
return $this->request($target, self::DOMAIN);
}
public function asn(string|int $target): ?Interfaces\RdapRequestInterface
{
return $this->request($target, self::ASN);
}
public function ipv4(string $target): ?Interfaces\RdapRequestInterface
{
return $this->request($target, self::IPV4);
}
public function ipv6(string $target): ?Interfaces\RdapRequestInterface
{
return $this->request($target, self::IPV6);
}
public function nameserver(string $target): ?Interfaces\RdapRequestInterface
{
return $this->request($target, self::NS);
}
}