GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 2430af...a94402 )
by François
02:36
created

Pool::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 0 Features 1
Metric Value
c 7
b 0
f 1
dl 0
loc 20
rs 9.4285
cc 1
eloc 17
nc 1
nop 2
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 RuntimeException;
21
22
class Pool
23
{
24
    /** @var int */
25
    private $id;
26
27
    /** @var string */
28
    private $poolName;
29
30
    /** @var string */
31
    private $hostName;
32
33
    /** @var bool */
34
    private $defaultGateway;
35
36
    /** @var IP */
37
    private $range;
38
39
    /** @var IP */
40
    private $range6;
41
42
    /** @var array */
43
    private $routes;
44
45
    /** @var array */
46
    private $dns;
47
48
    /** @var bool */
49
    private $useNat;
50
51
    /** @var string */
52
    private $extIf;
53
54
    /** @var bool */
55
    private $twoFactor;
56
57
    /** @var bool */
58
    private $clientToClient;
59
60
    /** @var IP */
61
    private $managementIp;
62
63
    /** @var IP */
64
    private $listen;
65
66
    /** @var array */
67
    private $instances;
68
69
    /** @var bool */
70
    private $enableLog;
71
72
    public function __construct($poolNumber, array $poolData)
73
    {
74
        $this->setId(self::validate($poolData, 'id'));
75
        $this->setName(self::validate($poolData, 'name', false, $this->getId()));
0 ignored issues
show
Documentation introduced by
$this->getId() is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
76
        $this->setHostName(self::validate($poolData, 'hostName'));
77
        $this->setDefaultGateway(self::validate($poolData, 'defaultGateway', false, false));
78
        $this->setRange(new IP(self::validate($poolData, 'range')));
79
        $this->setRange6(new IP(self::validate($poolData, 'range6')));
80
        $this->setRoutes(self::validate($poolData, 'routes', false, []));
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
81
        $this->setDns(self::validate($poolData, 'dns', false, []));
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
82
        $this->setUseNat(self::validate($poolData, 'useNat', false, false));
83
        $this->setExtIf(self::validate($poolData, 'extIf'));
84
        $this->setTwoFactor(self::validate($poolData, 'twoFactor', false, false));
85
        $this->setClientToClient(self::validate($poolData, 'clientToClient', false, false));
86
        $this->setManagementIp(new IP(sprintf('127.42.%d.1', $poolNumber)));
87
        $this->setListen(new IP(self::validate($poolData, 'listen', false, '::')));
0 ignored issues
show
Documentation introduced by
'::' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
88
        $this->setEnableLog(self::validate($poolData, 'enableLog', false, false));
89
90
        $this->populateInstances();
91
    }
92
93
    public function setId($id)
94
    {
95
        // XXX validate
96
        $this->id = $id;
97
    }
98
99
    public function getId()
100
    {
101
        return $this->id;
102
    }
103
104
    public function setName($poolName)
105
    {
106
        // XXX validate
107
        $this->poolName = $poolName;
108
    }
109
110
    public function getName()
111
    {
112
        return $this->poolName;
113
    }
114
115
    public function setHostName($hostName)
116
    {
117
        // XXX validate
118
        $this->hostName = $hostName;
119
    }
120
121
    public function getHostName()
122
    {
123
        return $this->hostName;
124
    }
125
126
    public function setDefaultGateway($defaultGateway)
127
    {
128
        $this->defaultGateway = (bool) $defaultGateway;
129
    }
130
131
    public function getDefaultGateway()
132
    {
133
        return $this->defaultGateway;
134
    }
135
136
    public function setRange(IP $range)
137
    {
138
        $this->range = $range;
139
    }
140
141
    public function getRange()
142
    {
143
        return $this->range;
144
    }
145
146
    public function setRange6(IP $range6)
147
    {
148
        $this->range6 = $range6;
149
    }
150
151
    public function getRange6()
152
    {
153
        return $this->range6;
154
    }
155
156
    public function setRoutes(array $routes)
157
    {
158
        $this->routes = [];
159
160
        foreach ($routes as $route) {
161
            $this->routes[] = new IP($route);
162
        }
163
    }
164
165
    public function getRoutes()
166
    {
167
        return $this->routes;
168
    }
169
170
    public function setDns(array $dns)
171
    {
172
        $this->dns = [];
173
174
        foreach ($dns as $server) {
175
            $this->dns[] = new IP($server);
176
        }
177
    }
178
179
    public function getDns()
180
    {
181
        return $this->dns;
182
    }
183
184
    public function setUseNat($useNat)
185
    {
186
        $this->useNat = (bool) $useNat;
187
    }
188
189
    public function getUseNat()
190
    {
191
        return $this->useNat;
192
    }
193
194
    public function setExtIf($extIf)
195
    {
196
        // XXX validate
197
        $this->extIf = $extIf;
198
    }
199
200
    public function getExtIf()
201
    {
202
        return $this->extIf;
203
    }
204
205
    public function setTwoFactor($twoFactor)
206
    {
207
        $this->twoFactor = (bool) $twoFactor;
208
    }
209
210
    public function getTwoFactor()
211
    {
212
        return $this->twoFactor;
213
    }
214
215
    public function setClientToClient($clientToClient)
216
    {
217
        $this->clientToClient = (bool) $clientToClient;
218
    }
219
220
    public function getClientToClient()
221
    {
222
        return $this->clientToClient;
223
    }
224
225
    public function setManagementIp(IP $managementIp)
226
    {
227
        $this->managementIp = $managementIp;
228
    }
229
230
    public function getManagementIp()
231
    {
232
        return $this->managementIp;
233
    }
234
235
    public function setListen(IP $listen)
236
    {
237
        $this->listen = $listen;
238
    }
239
240
    public function getListen()
241
    {
242
        return $this->listen;
243
    }
244
245
    public function getInstances()
246
    {
247
        return $this->instances;
248
    }
249
250
    public function setEnableLog($enableLog)
251
    {
252
        $this->enableLog = (bool) $enableLog;
253
    }
254
255
    public function getEnableLog()
256
    {
257
        return $this->enableLog;
258
    }
259
260
    private function populateInstances()
261
    {
262
        $instanceCount = self::getNetCount($this->getRange()->getPrefix());
263
        $splitRange = $this->getRange()->split($instanceCount);
264
        $splitRange6 = $this->getRange6()->split($instanceCount);
265
266
        for ($i = 0; $i < $instanceCount; ++$i) {
267
            // protocol is udp unless it is the last instance when there is
268
            // not just one instance
269
            if (1 !== $instanceCount && $i !== $instanceCount - 1) {
270
                $proto = 'udp';
271
                $port = 1194 + $i;
272
            } else {
273
                $proto = 'tcp';
274
                $port = 1194;
275
            }
276
277
            $this->instances[] = new Instance(
278
                [
279
                    'range' => $splitRange[$i],
280
                    'range6' => $splitRange6[$i],
281
                    'dev' => sprintf('tun-%s-%d', $this->getId(), $i),
282
                    'proto' => $proto,
283
                    'port' => $port,
284
                    'managementPort' => 11940 + $i,
285
                ]
286
            );
287
        }
288
    }
289
290
    /**
291
     * Depending on the prefix we will divide it in a number of nets to 
292
     * balance the load over the instances, it is recommended to use a least
293
     * a /24.
294
     * 
295
     * A /24 or 'bigger' will be split in 4 networks, everything 'smaller'  
296
     * will be either be split in 2 networks or remain 1 network.
297
     */
298
    private static function getNetCount($prefix)
299
    {
300
        switch ($prefix) {
301
            case 32:    // 1 IP   
302
            case 31:    // 2 IPs
303
                throw new RuntimeException('not enough available IPs in range');
304
            case 30:    // 4 IPs
305
                return 1;
306
            case 29:    // 8 IPs
307
            case 28:    // 16 IPs
308
            case 27:    // 32 IPs
309
            case 26:    // 64 IPs
310
            case 25:    // 128 IPs
311
                return 2;
312
            case 24:
313
                return 4;
314
        }
315
316
        return 8;
317
    }
318
319
    public function toArray()
320
    {
321
        $routesList = [];
322
        foreach ($this->getRoutes() as $route) {
323
            $routesList[] = $route->getAddressPrefix();
324
        }
325
326
        $dnsList = [];
327
        foreach ($this->getDns() as $dns) {
328
            $dnsList[] = $dns->getAddress();
329
        }
330
331
        $instancesList = [];
332
        foreach ($this->getInstances() as $instance) {
333
            $instancesList[] = $instance->toArray();
334
        }
335
336
        return [
337
            'clientToClient' => $this->getClientToClient(),
338
            'defaultGateway' => $this->getDefaultGateway(),
339
            'dns' => $dnsList,
340
            'enableLog' => $this->getEnableLog(),
341
            'extIf' => $this->getExtIf(),
342
            'hostName' => $this->getHostName(),
343
            'id' => $this->getId(),
344
            'instances' => $instancesList,
345
            'listen' => $this->getListen()->getAddress(),
346
            'managementIp' => $this->getManagementIp()->getAddress(),
347
            'name' => $this->getName(),
348
            'range' => $this->getRange()->getAddressPrefix(),
349
            'range6' => $this->getRange6()->getAddressPrefix(),
350
            'routes' => $routesList,
351
            'twoFactor' => $this->getTwoFactor(),
352
            'useNat' => $this->getUseNat(),
353
        ];
354
    }
355
356
    private static function validate(array $configData, $configName, $requiredField = true, $defaultValue = false)
357
    {
358
        if (!array_key_exists($configName, $configData)) {
359
            if ($requiredField) {
360
                throw new RuntimeException(sprintf('missing configuration field "%s"', $configName));
361
            }
362
363
            return $defaultValue;
364
        }
365
366
        return $configData[$configName];
367
    }
368
}
369