|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright 2016 François Kooman <[email protected]>. |
|
4
|
|
|
* |
|
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
6
|
|
|
* you may not use this file except in compliance with the License. |
|
7
|
|
|
* You may obtain a copy of the License at |
|
8
|
|
|
* |
|
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
10
|
|
|
* |
|
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14
|
|
|
* See the License for the specific language governing permissions and |
|
15
|
|
|
* limitations under the License. |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace fkooman\VPN\Server; |
|
19
|
|
|
|
|
20
|
|
|
use InvalidArgumentException; |
|
21
|
|
|
|
|
22
|
|
|
class Instance |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var IP */ |
|
25
|
|
|
private $range; |
|
26
|
|
|
|
|
27
|
|
|
/** @var IP */ |
|
28
|
|
|
private $range6; |
|
29
|
|
|
|
|
30
|
|
|
/** @var string */ |
|
31
|
|
|
private $proto; |
|
32
|
|
|
|
|
33
|
|
|
/** @var string */ |
|
34
|
|
|
private $dev; |
|
35
|
|
|
|
|
36
|
|
|
/** @var int */ |
|
37
|
|
|
private $managementPort; |
|
38
|
|
|
|
|
39
|
|
|
/** @var int */ |
|
40
|
|
|
private $port; |
|
41
|
|
|
|
|
42
|
|
|
public function __construct(array $instanceData) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->setRange($instanceData['range']); |
|
45
|
|
|
$this->setRange6($instanceData['range6']); |
|
46
|
|
|
$this->setProto($instanceData['proto']); |
|
47
|
|
|
$this->setDev($instanceData['dev']); |
|
48
|
|
|
$this->setManagementPort($instanceData['managementPort']); |
|
49
|
|
|
$this->setPort($instanceData['port']); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function setRange(IP $range) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->range = $range; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function getRange() |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->range; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function setRange6(IP $range6) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->range6 = $range6; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function getRange6() |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->range6; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function setProto($proto) |
|
73
|
|
|
{ |
|
74
|
|
|
if (!is_string($proto)) { |
|
75
|
|
|
throw new InvalidArgumentException('parameter must be string'); |
|
76
|
|
|
} |
|
77
|
|
|
$validProtocols = ['udp', 'tcp']; |
|
78
|
|
|
if (!in_array($proto, $validProtocols)) { |
|
79
|
|
|
throw new InstanceException('invalid proto'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$this->proto = $proto; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function getProto() |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->proto; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function setDev($dev) |
|
91
|
|
|
{ |
|
92
|
|
|
if (!is_string($dev)) { |
|
93
|
|
|
throw new InvalidArgumentException('parameter must be string'); |
|
94
|
|
|
} |
|
95
|
|
|
if (0 !== strpos($dev, 'tun-')) { |
|
96
|
|
|
throw new InstanceException('dev must start with "tun-"'); |
|
97
|
|
|
} |
|
98
|
|
|
$this->dev = $dev; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function getDev() |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->dev; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
View Code Duplication |
public function setManagementPort($managementPort) |
|
|
|
|
|
|
107
|
|
|
{ |
|
108
|
|
|
if (!is_int($managementPort)) { |
|
109
|
|
|
throw new InvalidArgumentException('parameter must be int'); |
|
110
|
|
|
} |
|
111
|
|
|
if (1024 >= $managementPort || 65536 <= $managementPort) { |
|
112
|
|
|
throw new InstanceException('invalid port, must be positive integer between 1025 and 65535'); |
|
113
|
|
|
} |
|
114
|
|
|
$this->managementPort = $managementPort; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function getManagementPort() |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->managementPort; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
View Code Duplication |
public function setPort($port) |
|
|
|
|
|
|
123
|
|
|
{ |
|
124
|
|
|
if (!is_int($port)) { |
|
125
|
|
|
throw new InvalidArgumentException('parameter must be int'); |
|
126
|
|
|
} |
|
127
|
|
|
if (1024 >= $port || 65536 <= $port) { |
|
128
|
|
|
throw new InstanceException('invalid port, must be positive integer between 1025 and 65535'); |
|
129
|
|
|
} |
|
130
|
|
|
$this->port = $port; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function getPort() |
|
134
|
|
|
{ |
|
135
|
|
|
return $this->port; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public function toArray() |
|
139
|
|
|
{ |
|
140
|
|
|
return [ |
|
141
|
|
|
'dev' => $this->getDev(), |
|
142
|
|
|
'managementPort' => $this->getManagementPort(), |
|
143
|
|
|
'port' => $this->getPort(), |
|
144
|
|
|
'proto' => $this->getProto(), |
|
145
|
|
|
'range' => $this->getRange()->getAddressPrefix(), |
|
146
|
|
|
'range6' => $this->getRange6()->getAddressPrefix(), |
|
147
|
|
|
]; |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.