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.

ServerManager::toPort()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 2
nc 1
nop 3
1
<?php
2
3
/**
4
 * eduVPN - End-user friendly VPN.
5
 *
6
 * Copyright: 2016-2017, The Commons Conservancy eduVPN Programme
7
 * SPDX-License-Identifier: AGPL-3.0+
8
 */
9
10
namespace SURFnet\VPN\Server\OpenVpn;
11
12
use Psr\Log\LoggerInterface;
13
use SURFnet\VPN\Common\Config;
14
use SURFnet\VPN\Common\ProfileConfig;
15
use SURFnet\VPN\Server\OpenVpn\Exception\ManagementSocketException;
16
17
/**
18
 * Manage all OpenVPN processes controlled by this service.
19
 */
20
class ServerManager
21
{
22
    /** @var \SURFnet\VPN\Common\Config */
23
    private $config;
24
25
    /** @var ManagementSocketInterface */
26
    private $managementSocket;
27
28
    /** @var \Psr\Log\LoggerInterface */
29
    private $logger;
30
31
    public function __construct(Config $config, ManagementSocketInterface $managementSocket, LoggerInterface $logger)
32
    {
33
        $this->config = $config;
34
        $this->managementSocket = $managementSocket;
35
        $this->logger = $logger;
36
    }
37
38
    /**
39
     * Get the connection information about connected clients.
40
     */
41
    public function connections()
42
    {
43
        $clientConnections = [];
44
        $instanceNumber = $this->config->getItem('instanceNumber');
45
46
        // loop over all profiles
47
        foreach (array_keys($this->config->getSection('vpnProfiles')->toArray()) as $profileId) {
48
            $profileConfig = new ProfileConfig($this->config->getSection('vpnProfiles')->getSection($profileId)->toArray());
49
            $managementIp = $profileConfig->getItem('managementIp');
50
            $profileNumber = $profileConfig->getItem('profileNumber');
51
52
            $profileConnections = [];
53
            // loop over all processes
54
            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...
55
                // add all connections from this instance to profileConnections
56
                try {
57
                    // open the socket connection
58
                    $this->managementSocket->open(
59
                        sprintf(
60
                            'tcp://%s:%d',
61
                            $managementIp,
62
                            11940 + $this->toPort($instanceNumber, $profileNumber, $i)
63
                        )
64
                    );
65
                    $profileConnections = array_merge(
66
                        $profileConnections,
67
                        StatusParser::parse($this->managementSocket->command('status 2'))
68
                    );
69
                    // close the socket connection
70
                    $this->managementSocket->close();
71
                } catch (ManagementSocketException $e) {
72
                    // we log the error, but continue with the next instance
73
                    $this->logger->error(
74
                        sprintf(
75
                            'error with socket "tcp://%s:%d", message: "%s"',
76
                            $managementIp,
77
                            11940 + $this->toPort($instanceNumber, $profileNumber, $i),
78
                            $e->getMessage()
79
                        )
80
                    );
81
                }
82
            }
83
            // we add the profileConnections to the clientConnections array
84
            $clientConnections[] = ['id' => $profileId, 'connections' => $profileConnections];
85
        }
86
87
        return $clientConnections;
88
    }
89
90
    /**
91
     * Disconnect all clients with this CN from all profiles and instances
92
     * managed by this service.
93
     *
94
     * @param string $commonName the CN to kill
95
     */
96
    public function kill($commonName)
97
    {
98
        $clientsKilled = 0;
99
        $instanceNumber = $this->config->getItem('instanceNumber');
100
101
        // loop over all profiles
102
        foreach (array_keys($this->config->getSection('vpnProfiles')->toArray()) as $profileId) {
103
            $profileConfig = new ProfileConfig($this->config->getSection('vpnProfiles')->getSection($profileId)->toArray());
104
            $managementIp = $profileConfig->getItem('managementIp');
105
            $profileNumber = $profileConfig->getItem('profileNumber');
106
107
            // loop over all processes
108
            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...
109
                // add all kills from this instance to profileKills
110
                try {
111
                    // open the socket connection
112
                    $this->managementSocket->open(
113
                        sprintf(
114
                            'tcp://%s:%d',
115
                            $managementIp,
116
                            11940 + $this->toPort($instanceNumber, $profileNumber, $i)
117
                        )
118
                    );
119
120
                    $response = $this->managementSocket->command(sprintf('kill %s', $commonName));
121
                    if (0 === mb_strpos($response[0], 'SUCCESS: ')) {
122
                        ++$clientsKilled;
123
                    }
124
                    // close the socket connection
125
                    $this->managementSocket->close();
126
                } catch (ManagementSocketException $e) {
127
                    // we log the error, but continue with the next instance
128
                    $this->logger->error(
129
                        sprintf(
130
                            'error with socket "tcp://%s:%d", message: "%s"',
131
                            $managementIp,
132
                            11940 + $this->toPort($instanceNumber, $profileNumber, $i),
133
                            $e->getMessage()
134
                        )
135
                    );
136
                }
137
            }
138
        }
139
140
        return 0 !== $clientsKilled;
141
    }
142
143
    private function toPort($instanceNumber, $profileNumber, $processNumber)
144
    {
145
        // convert an instanceNumber, $profileNumber and $processNumber to a management port
146
147
        // instanceId = 6 bits (max 64)
148
        // profileNumber = 4 bits (max 16)
149
        // processNumber = 4 bits  (max 16)
150
        return ($instanceNumber - 1 << 8) | ($profileNumber - 1 << 4) | ($processNumber);
151
    }
152
}
153