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 ( 1ef55e...38a055 )
by François
03:08
created

ServerManager::getManagementIp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
/**
3
 *  Copyright (C) 2016 SURFnet.
4
 *
5
 *  This program is free software: you can redistribute it and/or modify
6
 *  it under the terms of the GNU Affero General Public License as
7
 *  published by the Free Software Foundation, either version 3 of the
8
 *  License, or (at your option) any later version.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU Affero General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU Affero General Public License
16
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace SURFnet\VPN\Server\OpenVpn;
20
21
use Psr\Log\LoggerInterface;
22
use SURFnet\VPN\Common\Config;
23
use SURFnet\VPN\Common\ProfileConfig;
24
use SURFnet\VPN\Server\OpenVpn\Exception\ManagementSocketException;
25
26
/**
27
 * Manage all OpenVPN processes controlled by this service.
28
 */
29
class ServerManager
30
{
31
    /** @var \SURFnet\VPN\Common\Config */
32
    private $config;
33
34
    /** @var ManagementSocketInterface */
35
    private $managementSocket;
36
37
    /** @var \Psr\Log\LoggerInterface */
38
    private $logger;
39
40
    public function __construct(Config $config, ManagementSocketInterface $managementSocket, LoggerInterface $logger)
41
    {
42
        $this->config = $config;
43
        $this->managementSocket = $managementSocket;
44
        $this->logger = $logger;
45
    }
46
47
    /**
48
     * Get the connection information about connected clients.
49
     */
50
    public function connections()
51
    {
52
        $clientConnections = [];
53
        $instanceNumber = $this->config->getItem('instanceNumber');
54
55
        // loop over all profiles
56
        foreach (array_keys($this->config->getSection('vpnProfiles')->toArray()) as $profileId) {
57
            $profileConfig = new ProfileConfig($this->config->getSection('vpnProfiles')->getSection($profileId)->toArray());
58
            $managementIp = $profileConfig->getItem('managementIp');
59
            $profileNumber = $profileConfig->getItem('profileNumber');
60
61
            $profileConnections = [];
62
            // loop over all processes
63
            for ($i = 0; $i < count($profileConfig->getItem('vpnProtoPorts')); ++$i) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
64
                // add all connections from this instance to profileConnections
65
                try {
66
                    // open the socket connection
67
                    $this->managementSocket->open(
68
                        sprintf(
69
                            'tcp://%s:%d',
70
                            $managementIp,
71
                            11940 + $this->toPort($instanceNumber, $profileNumber, $i)
72
                        )
73
                    );
74
                    $profileConnections = array_merge(
75
                        $profileConnections,
76
                        StatusParser::parse($this->managementSocket->command('status 2'))
77
                    );
78
                    // close the socket connection
79
                    $this->managementSocket->close();
80
                } catch (ManagementSocketException $e) {
81
                    // we log the error, but continue with the next instance
82
                    $this->logger->error(
83
                        sprintf(
84
                            'error with socket "tcp://%s:%d", message: "%s"',
85
                            $managementIp,
86
                            11940 + $this->toPort($instanceNumber, $profileNumber, $i),
87
                            $e->getMessage()
88
                        )
89
                    );
90
                }
91
            }
92
            // we add the profileConnections to the clientConnections array
93
            $clientConnections[] = ['id' => $profileId, 'connections' => $profileConnections];
94
        }
95
96
        return $clientConnections;
97
    }
98
99
    /**
100
     * Disconnect all clients with this CN from all profiles and instances
101
     * managed by this service.
102
     *
103
     * @param string $commonName the CN to kill
104
     */
105
    public function kill($commonName)
106
    {
107
        $clientsKilled = 0;
108
        $instanceNumber = $this->config->getItem('instanceNumber');
109
110
        // loop over all profiles
111
        foreach (array_keys($this->config->getSection('vpnProfiles')->toArray()) as $profileId) {
112
            $profileConfig = new ProfileConfig($this->config->getSection('vpnProfiles')->getSection($profileId)->toArray());
113
            $managementIp = $profileConfig->getItem('managementIp');
114
            $profileNumber = $profileConfig->getItem('profileNumber');
115
116
            // loop over all processes
117
            for ($i = 0; $i < count($profileConfig->getItem('vpnProtoPorts')); ++$i) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
118
                // add all kills from this instance to profileKills
119
                try {
120
                    // open the socket connection
121
                    $this->managementSocket->open(
122
                        sprintf(
123
                            'tcp://%s:%d',
124
                            $managementIp,
125
                            11940 + $this->toPort($instanceNumber, $profileNumber, $i)
126
                        )
127
                    );
128
129
                    $response = $this->managementSocket->command(sprintf('kill %s', $commonName));
130
                    if (0 === mb_strpos($response[0], 'SUCCESS: ')) {
131
                        ++$clientsKilled;
132
                    }
133
                    // close the socket connection
134
                    $this->managementSocket->close();
135
                } catch (ManagementSocketException $e) {
136
                    // we log the error, but continue with the next instance
137
                    $this->logger->error(
138
                        sprintf(
139
                            'error with socket "tcp://%s:%d", message: "%s"',
140
                            $managementIp,
141
                            11940 + $this->toPort($instanceNumber, $profileNumber, $i),
142
                            $e->getMessage()
143
                        )
144
                    );
145
                }
146
            }
147
        }
148
149
        return 0 !== $clientsKilled;
150
    }
151
152
    private function toPort($instanceNumber, $profileNumber, $processNumber)
153
    {
154
        // convert an instanceNumber, $profileNumber and $processNumber to a management port
155
156
        // instanceId = 6 bits (max 64)
157
        // profileNumber = 4 bits (max 16)
158
        // processNumber = 4 bits  (max 16)
159
        return ($instanceNumber - 1 << 8) | ($profileNumber - 1 << 4) | ($processNumber);
160
    }
161
}
162