-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathIpv4Service.php
More file actions
152 lines (143 loc) · 4.14 KB
/
Copy pathIpv4Service.php
File metadata and controls
152 lines (143 loc) · 4.14 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
141
142
143
144
145
146
147
148
149
150
151
152
<?php
declare(strict_types=1);
namespace ArrayAccess\RdapClient\Services;
use ArrayAccess\RdapClient\Util\CIDR;
use DateTimeInterface;
use function array_merge;
use function explode;
use function is_array;
use function is_numeric;
use function preg_match;
use function reset;
use function str_contains;
use function str_ends_with;
use function str_starts_with;
use function strlen;
class Ipv4Service extends AbstractRdapService
{
/**
* @var array<string, array{0:string, 1:string}|false> $cidrRanges The RDAP services
*/
private array $cidrRanges = [];
/**
* @var bool $addedRecovered The RDAP services
*/
private bool $addedRecovered = false;
/**
* @inheritDoc
*/
public function __construct(
string $version,
string $description,
DateTimeInterface|string $publication,
array $services,
bool $addRecoveredIpv4 = false
) {
parent::__construct($version, $description, $publication, $services);
if (true === $addRecoveredIpv4) {
$this->addRecoveredIpv4();
}
}
/**
* @inheritDoc
*/
protected function normalizeSource(string $target): string
{
// 255.255.255.255 -> 15 chars
// 1.1.1.1 -> 7 chars
$explode = explode('/', $target);
if (count($explode) === 2
&& is_numeric($explode[1])
&& !str_contains($explode[1], '.')
&& ((int) $explode[1]) >= 0
&& ((int) $explode[1]) <= 32
&& str_contains($explode[0], '.')
&& strlen($explode[0]) <= 15
&& strlen($explode[0]) >= 7
&& ($_target = $this->normalize($explode[0]))
) {
return "$_target/$explode[1]";
}
$this->throwInvalidTarget($target);
}
/**
* Add the recovered IPv4 addresses
* @return $this
*/
public function addRecoveredIpv4(): static
{
if ($this->addedRecovered) {
return $this;
}
$this->addedRecovered = true;
foreach (RecoveredIPv4::RECOVERED_IPS as $target => $ips) {
$offset = $this->getOffset($target);
if ($offset === null) {
continue;
}
$this->services[$offset][0] = array_merge($ips, $this->services[$offset][0]);
}
return $this;
}
/**
* @inheritDoc
*/
public function getRdapURL(string $target) : ?string
{
$target = $this->normalize($target);
if (!$target) {
return null;
}
[$start, $second] = explode('.', $target);
$start .= '.';
$second = "$start$second.";
foreach ($this->services as $service) {
$urls = $service[1]??[];
$url = reset($urls);
if (!$url) {
continue;
}
foreach ($service[0] as $cidr) {
if (!str_starts_with($cidr, $start)) {
continue;
}
if (str_ends_with($cidr, '.0.0.0/8')) {
return $url;
}
if (!str_starts_with($cidr, $second)) {
continue;
}
if (!isset($this->cidrRanges[$cidr])) {
$this->cidrRanges[$cidr] = CIDR::cidrToRange($cidr)?:false;
}
if (!is_array($this->cidrRanges[$cidr])) {
continue;
}
if (CIDR::inRange($target, $this->cidrRanges[$cidr][0], $this->cidrRanges[$cidr][1])) {
return $url;
}
}
}
return null;
}
/**
* @inheritDoc
*/
public function normalize(string $target): ?string
{
if (str_contains($target, '/')) {
return $this->normalizeSource($target);
}
if (!preg_match(
'~^(?:[01]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])(?:\.(?:[01]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])){3}$~x',
$target
)) {
return null;
}
$target = CIDR::filter($target);
if (!$target) {
return null;
}
return $target;
}
}