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 ( 0f5b77...f26127 )
by François
05:01
created

ServerManager::kill()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 43
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 43
rs 8.439
cc 5
eloc 24
nc 9
nop 1
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\OpenVpn;
19
20
use fkooman\VPN\Server\Pools;
21
use Psr\Log\LoggerInterface;
22
use fkooman\VPN\Server\OpenVpn\Exception\ManagementSocketException;
23
24
/**
25
 * Manage all OpenVPN servers controlled by this service using each instance's
26
 * ServerApi.
27
 */
28
class ServerManager
29
{
30
    /** @var array */
31
    private $pools;
32
33
    /** @var ManagementSocketInterface */
34
    private $managementSocket;
35
36
    /** @var \Psr\Log\LoggerInterface */
37
    private $logger;
38
39
    public function __construct(Pools $pools, ManagementSocketInterface $managementSocket, LoggerInterface $logger)
40
    {
41
        $this->pools = $pools;
0 ignored issues
show
Documentation Bug introduced by
It seems like $pools of type object<fkooman\VPN\Server\Pools> is incompatible with the declared type array of property $pools.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
42
        $this->managementSocket = $managementSocket;
43
        $this->logger = $logger;
44
    }
45
46
    /**
47
     * Get the connection information about connected clients.
48
     */
49
    public function connections()
50
    {
51
        $clientConnections = [];
52
        // loop over all pools
53
        foreach ($this->pools as $pool) {
54
            $poolConnections = [];
55
            // loop over all instances
56
            foreach ($pool->getInstances() as $instance) {
57
                // add all connections from this instance to poolConnections
58
                try {
59
                    // open the socket connection
60
                    $this->managementSocket->open(
61
                        sprintf(
62
                            'tcp://%s:%d',
63
                            $pool->getManagementIp()->getAddress(),
64
                            $instance->getManagementPort()
65
                        )
66
                    );
67
                    $poolConnections = array_merge(
68
                        $poolConnections,
69
                        StatusParser::parse($this->managementSocket->command('status 2'))
70
                    );
71
                    // close the socket connection
72
                    $this->managementSocket->close();
73
                } catch (ManagementSocketException $e) {
74
                    // we log the error, but continue with the next instance
75
                    $this->logger->error(
76
                        sprintf(
77
                            'error with socket "%s:%s", message: "%s"',
78
                            $pool->getManagementIp()->getAddress(),
79
                            $instance->getManagementPort(),
80
                            $e->getMessage()
81
                        )
82
                    );
83
                }
84
            }
85
            // we add the poolConnections to the clientConnections array
86
            $clientConnections[] = ['id' => $pool->getId(), 'connections' => $poolConnections];
87
        }
88
89
        return ['data' => $clientConnections];
90
    }
91
92
    /**
93
     * Disconnect all clients with this CN from all pools and instances 
94
     * managed by this service.
95
     *
96
     * @param string $commonName the CN to kill
97
     */
98
    public function kill($commonName)
99
    {
100
        $clientsKills = [];
101
        // loop over all pools
102
        foreach ($this->pools as $pool) {
103
            $poolKill = 0;
104
            // loop over all instances
105
            foreach ($pool->getInstances() as $instance) {
106
                // add all kills from this instance to poolKills
107
                try {
108
                    // open the socket connection
109
                    $this->managementSocket->open(
110
                        sprintf(
111
                            'tcp://%s:%d',
112
                            $pool->getManagementIp()->getAddress(),
113
                            $instance->getManagementPort()
114
                        )
115
                    );
116
117
                    $response = $this->managementSocket->command(sprintf('kill %s', $commonName));
118
                    if (0 === strpos($response[0], 'SUCCESS: ')) {
119
                        ++$poolKill;
120
                    }
121
                    // close the socket connection
122
                    $this->managementSocket->close();
123
                } catch (ManagementSocketException $e) {
124
                    // we log the error, but continue with the next instance
125
                    $this->logger->error(
126
                        sprintf(
127
                            'error with socket "%s:%s", message: "%s"',
128
                            $pool->getManagementIp()->getAddress(),
129
                            $instance->getManagementPort(),
130
                            $e->getMessage()
131
                        )
132
                    );
133
                }
134
            }
135
            // we add the poolKill to the clientsKill array
136
            $clientsKills[] = ['id' => $pool->getId(), 'killCount' => $poolKill];
137
        }
138
139
        return ['data' => $clientsKills];
140
    }
141
}
142