-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathSurvivalDevice.php
147 lines (116 loc) · 4 KB
/
SurvivalDevice.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
<?php
namespace Ivoz\Provider\Domain\Model\SurvivalDevice;
use Assert\Assertion;
/**
* SurvivalDevice
*/
class SurvivalDevice extends SurvivalDeviceAbstract implements SurvivalDeviceInterface
{
use SurvivalDeviceTrait;
/**
* @codeCoverageIgnore
* @return array<string, mixed>
*/
public function getChangeSet(): array
{
return parent::getChangeSet();
}
/**
* Get id
* @codeCoverageIgnore
*/
public function getId(): ?int
{
return $this->id;
}
protected function sanitizeValues(): void
{
$this->sanitizeOutboundProxy();
$this->sanitizeProxy();
}
protected function setUdpPort(int $udpPort): static
{
Assertion::between($udpPort, 1, 65535, "Invalid udp port number: $udpPort. Must be between 1 and 65535");
return parent::setUdpPort($udpPort);
}
protected function setTcpPort(int $tcpPort): static
{
Assertion::between($tcpPort, 1, 65535, "Invalid tcp port number: $tcpPort. Must be between 1 and 65535");
return parent::setTcpPort($tcpPort);
}
protected function setTlsPort(int $tlsPort): static
{
Assertion::between($tlsPort, 1, 65535, "Invalid tls port number: $tlsPort. Must be between 1 and 65535");
return parent::setTlsPort($tlsPort);
}
protected function setWssPort(int $wssPort): static
{
Assertion::between($wssPort, 1, 65535, "Invalid wss port number: $wssPort. Must be between 1 and 65535");
return parent::setWssPort($wssPort);
}
protected function sanitizeProxy(): void
{
$proxy = trim($this->getProxy());
if ($proxy === '') {
return;
}
if (str_contains($proxy, ' ')) {
throw new \InvalidArgumentException('Proxy must not contain spaces.');
}
if (preg_match('/^(\d{1,3}\.){3}\d{1,3}$/', $proxy)) {
foreach (explode('.', $proxy) as $octet) {
$intOctet = (int) $octet;
if ($intOctet < 0 || $intOctet > 255) {
throw new \DomainException("Each segment of the IP must be between 0 and 255.");
}
}
$this->setProxy($proxy);
return;
}
if (str_contains($proxy, ':')) {
throw new \DomainException("Domain must not contain colons.");
}
if (!str_contains($proxy, '.')) {
throw new \DomainException("Domain must contain at least one dot.");
}
if (str_ends_with($proxy, '.')) {
throw new \DomainException("Domain must not end with a dot.");
}
$this->setProxy($proxy);
}
protected function sanitizeOutboundProxy(): void
{
$proxy = trim($this->getOutboundProxy() ?? '');
if ($proxy === '') {
return;
}
if (str_contains($proxy, ' ')) {
throw new \DomainException('Outbound Proxy must not contain spaces.', 70001);
}
$parts = explode(':', $proxy);
if (count($parts) > 2) {
throw new \DomainException('Outbound Proxy must be in format IP or IP:port, not more than one colon.', 70002);
}
$ip = $parts[0];
if (!preg_match('/^(\d{1,3}\.){3}\d{1,3}$/', $ip)) {
throw new \DomainException('IP format is invalid in Outbound Proxy.', 70003);
}
foreach (explode('.', $ip) as $octet) {
$value = (int) $octet;
if ($value < 0 || $value > 255) {
throw new \DomainException('Each segment of the IP must be between 0 and 255.', 70006);
}
}
if (isset($parts[1])) {
$port = $parts[1];
if (!ctype_digit($port)) {
throw new \DomainException('Port must be a numeric value.', 70007);
}
$port = (int) $port;
if ($port < 1 || $port > 65535) {
throw new \DomainException('Port must be between 1 and 65535.', 70004);
}
}
return;
}
}