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 ( 36987f...5a4612 )
by François
03:22
created

ClientConfig   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 7
c 2
b 0
f 1
lcom 1
cbo 6
dl 0
loc 90
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B get() 0 44 3
A parseConfig() 0 13 3
1
<?php
2
/**
3
 * Copyright 2015 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\Config;
19
20
use RuntimeException;
21
use fkooman\VPN\Server\Utils;
22
23
/**
24
 * This class generates a configuration file specific for the connecting
25
 * client CN.
26
 */
27
class ClientConfig
28
{
29
    /** @var string */
30
    private $leaseDir;
31
32
    /** @var ConfigStorageInterface */
33
    private $configStorage;
34
35
    /** @var IPv4 */
36
    private $v4;
37
38
    /** @var IPv6 */
39
    private $v6;
40
41
    public function __construct(array $input, ConfigStorageInterface $configStorage)
42
    {
43
        $this->parseConfig($input);
44
        $this->configStorage = $configStorage;
45
    }
46
47
    /**
48
     * Generate a configuration file for a CN.
49
     *
50
     * @param string $commonName the CN
51
     *
52
     * @return string the config file for the CN
53
     */
54
    public function get($commonName)
55
    {
56
        $configData = [];
57
58
        // read the CN configuration
59
        $cnConfig = $this->configStorage->getConfig($commonName);
60
61
        // is the CN disabled?
62
        if ($cnConfig->getDisable()) {
63
            return false;
64
        }
65
66
        // get the pool
67
        $cnPool = $cnConfig->getPool();
68
        $poolRange = $this->v4->getPool($cnPool)->getRange();
69
        $v4s = $poolRange->getFirstHost();
70
        $v4e = $poolRange->getLastHost();
71
        $v4n = $this->v4->getRange()->getNetmask();
72
        $v6p = $this->v6->getPrefix();
73
        $v6r = AddressPool::getIp6($v6p, $this->v4->getRange()->getFirstHost());
74
75
        $activeLeases = array_merge(
76
            [$this->v4->getRange()->getFirstHost()], // the IP address of the VPN server cannot be used 
77
            Utils::getActiveLeases($this->leaseDir)
78
        );
79
80
        if (false === $v4 = AddressPool::getIp4($v4s, $v4e, $activeLeases)) {
81
            throw new RuntimeException(sprintf('"%s" could not connect, ran out of IP space', $envData['common_name']));
82
        }
83
        $v6 = AddressPool::getIp6($v6p, $v4);
84
85
        $configData['v4'] = $v4;
86
        $configData['v4_netmask'] = $v4n;
87
        $configData['v4_gw'] = $this->v4->getRange()->getFirstHost();
88
        $configData['v6'] = $v6;
89
        $configData['v6_gw'] = $v6r;
90
        $configData['dns'] = array_merge($this->v4->getDns(), $this->v6->getDns());
91
        $configData['default_gw'] = $this->v4->getPool($cnPool)->useDefaultGateway();
92
        $configData['dst_net4'] = $this->v4->getPool($cnPool)->getDstNet4();
93
        // XXX we should not have v6 prefixes in v4 config section
94
        $configData['dst_net6'] = $this->v4->getPool($cnPool)->getDstNet6();
95
96
        return $configData;
97
    }
98
99
    /**
100
     * Parse and validate the configuration and set default values if they
101
     * are missing from the configuration file.
102
     */
103
    private function parseConfig(array $input)
104
    {
105
        foreach (['leaseDir', 'v4', 'v6'] as $k) {
106
            if (!array_key_exists($k, $input)) {
107
                throw new RuntimeException(sprintf('missing key "%s" in configuration', $k));
108
            }
109
        }
110
111
        $this->leaseDir = $input['leaseDir'];
112
113
        $this->v4 = new IPv4($input['v4']);
114
        $this->v6 = new IPv6($input['v6']);
115
    }
116
}
117