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 ( 4062df...6cdcbb )
by François
02:22
created

ServerManager::getManagementIp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 4
c 1
b 1
f 0
nc 2
nop 1
dl 0
loc 8
rs 9.4285
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
namespace SURFnet\VPN\Server\OpenVpn;
19
20
use Psr\Log\LoggerInterface;
21
use SURFnet\VPN\Server\OpenVpn\Exception\ManagementSocketException;
22
use SURFnet\VPN\Common\Config;
23
use SURFnet\VPN\Server\PoolConfig;
24
25
/**
26
 * Manage all OpenVPN processes controlled by this service.
27
 */
28
class ServerManager
29
{
30
    /** @var \SURFnet\VPN\Common\Config */
31
    private $config;
32
33
    /** @var ManagementSocketInterface */
34
    private $managementSocket;
35
36
    /** @var \Psr\Log\LoggerInterface */
37
    private $logger;
38
39
    public function __construct(Config $config, ManagementSocketInterface $managementSocket, LoggerInterface $logger)
40
    {
41
        $this->config = $config;
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
53
        // loop over all pools
54
        foreach (array_keys($this->config->v('vpnPools')) as $poolId) {
55
            $poolConfig = new PoolConfig($this->config->v('vpnPools', $poolId));
56
            $managementIp = $this->getManagementIp($poolConfig);
57
58
            $poolConnections = [];
59
            // loop over all processes
60
            for ($i = 0; $i < $poolConfig->v('processCount'); ++$i) {
61
                // add all connections from this instance to poolConnections
62
                try {
63
                    // open the socket connection
64
                    $this->managementSocket->open(
65
                        sprintf(
66
                            'tcp://%s:%d',
67
                            $managementIp,
68
                            11940 + $i
69
                        )
70
                    );
71
                    $poolConnections = array_merge(
72
                        $poolConnections,
73
                        StatusParser::parse($this->managementSocket->command('status 2'))
74
                    );
75
                    // close the socket connection
76
                    $this->managementSocket->close();
77
                } catch (ManagementSocketException $e) {
78
                    // we log the error, but continue with the next instance
79
                    $this->logger->error(
80
                        sprintf(
81
                            'error with socket "%s:%s", message: "%s"',
82
                            $managementIp,
83
                            11940 + $i,
84
                            $e->getMessage()
85
                        )
86
                    );
87
                }
88
            }
89
            // we add the poolConnections to the clientConnections array
90
            $clientConnections[] = ['id' => $poolId, 'connections' => $poolConnections];
91
        }
92
93
        return $clientConnections;
94
    }
95
96
    /**
97
     * Disconnect all clients with this CN from all pools and instances
98
     * managed by this service.
99
     *
100
     * @param string $commonName the CN to kill
101
     */
102
    public function kill($commonName)
103
    {
104
        $clientsKilled = 0;
105
        // loop over all pools
106
        foreach (array_keys($this->config->v('vpnPools')) as $poolId) {
107
            $poolConfig = new PoolConfig($this->config->v('vpnPools', $poolId));
108
            $managementIp = $this->getManagementIp($poolConfig);
109
110
            // loop over all processes
111
            for ($i = 0; $i < $poolConfig->v('processCount'); ++$i) {
112
                // add all kills from this instance to poolKills
113
                try {
114
                    // open the socket connection
115
                    $this->managementSocket->open(
116
                        sprintf(
117
                            'tcp://%s:%d',
118
                            $managementIp,
119
                            11940 + $i
120
                        )
121
                    );
122
123
                    $response = $this->managementSocket->command(sprintf('kill %s', $commonName));
124
                    if (0 === mb_strpos($response[0], 'SUCCESS: ')) {
125
                        ++$clientsKilled;
126
                    }
127
                    // close the socket connection
128
                    $this->managementSocket->close();
129
                } catch (ManagementSocketException $e) {
130
                    // we log the error, but continue with the next instance
131
                    $this->logger->error(
132
                        sprintf(
133
                            'error with socket "%s:%s", message: "%s"',
134
                            $managementIp,
135
                            11940 + $i,
136
                            $e->getMessage()
137
                        )
138
                    );
139
                }
140
            }
141
        }
142
143
        return 0 !== $clientsKilled;
144
    }
145
146
    private function getManagementIp(PoolConfig $poolConfig)
147
    {
148
        if ($poolConfig->e('managementIp')) {
149
            return $poolConfig->v('managementIp');
150
        }
151
152
        return sprintf('127.42.%d.%d', 100 + $this->config->v('instanceNumber'), 100 + $poolConfig->v('poolNumber'));
153
    }
154
}
155