|
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
|
|
|
/** |
|
22
|
|
|
* Parses the response from the OpenVPN `status 2` command. |
|
23
|
|
|
* |
|
24
|
|
|
* NOTE: The OpenVPN instance MUST NOT have --duplicate-cn in the configuration |
|
25
|
|
|
* as we do not deal with multiple connections with the same CN, due to bugs in |
|
26
|
|
|
* udp6 status report where the client port is not mentioned in the |
|
27
|
|
|
* 'Real Address' column |
|
28
|
|
|
*/ |
|
29
|
|
|
class StatusParser |
|
30
|
|
|
{ |
|
31
|
|
|
public static function parse(array $statusData) |
|
32
|
|
|
{ |
|
33
|
|
|
$clientListStart = 0; |
|
34
|
|
|
$routingTableStart = 0; |
|
35
|
|
|
$globalStatsStart = 0; |
|
36
|
|
|
|
|
37
|
|
|
for ($i = 0; $i < sizeof($statusData); ++$i) { |
|
|
|
|
|
|
38
|
|
|
if (0 === mb_strpos($statusData[$i], 'HEADER,CLIENT_LIST')) { |
|
39
|
|
|
$clientListStart = $i; |
|
40
|
|
|
} |
|
41
|
|
|
if (0 === mb_strpos($statusData[$i], 'HEADER,ROUTING_TABLE')) { |
|
42
|
|
|
$routingTableStart = $i; |
|
43
|
|
|
} |
|
44
|
|
|
if (0 === mb_strpos($statusData[$i], 'GLOBAL_STATS')) { |
|
45
|
|
|
$globalStatsStart = $i; |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$parsedClientList = self::parseClientList(array_slice($statusData, $clientListStart, $routingTableStart - $clientListStart)); |
|
50
|
|
|
$parsedRoutingTable = self::parseRoutingTable(array_slice($statusData, $routingTableStart, $globalStatsStart - $routingTableStart)); |
|
51
|
|
|
|
|
52
|
|
|
// merge routing table in client list |
|
53
|
|
|
foreach (array_keys($parsedClientList) as $key) { |
|
54
|
|
|
if (!array_key_exists($key, $parsedRoutingTable)) { |
|
55
|
|
|
$parsedClientList[$key]['virtual_address'] = array(); |
|
56
|
|
|
} else { |
|
57
|
|
|
$parsedClientList[$key]['virtual_address'] = $parsedRoutingTable[$key]; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return array_values($parsedClientList); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
private static function parseClientList(array $clientList) |
|
65
|
|
|
{ |
|
66
|
|
|
$parsedClientList = array(); |
|
67
|
|
|
for ($i = 1; $i < sizeof($clientList); ++$i) { |
|
|
|
|
|
|
68
|
|
|
$parsedClient = str_getcsv($clientList[$i]); |
|
69
|
|
|
$commonName = $parsedClient[1]; |
|
70
|
|
|
if (array_key_exists($commonName, $parsedClientList)) { |
|
|
|
|
|
|
71
|
|
|
//syslog(LOG_ERR('duplicate common name, possibly --duplicate-cn enabled in server configuration')); |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
$parsedClientList[$commonName] = array( |
|
74
|
|
|
'common_name' => $commonName, |
|
75
|
|
|
'proto' => 3 === substr_count($parsedClient[2], '.') ? 4 : 6, |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $parsedClientList; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
private static function parseRoutingTable(array $routingTable) |
|
83
|
|
|
{ |
|
84
|
|
|
$parsedRoutingTable = array(); |
|
85
|
|
|
for ($i = 1; $i < sizeof($routingTable); ++$i) { |
|
|
|
|
|
|
86
|
|
|
$parsedRoute = str_getcsv($routingTable[$i]); |
|
87
|
|
|
$commonName = $parsedRoute[2]; |
|
88
|
|
|
if (!array_key_exists($commonName, $parsedRoutingTable)) { |
|
89
|
|
|
$parsedRoutingTable[$commonName] = array(); |
|
90
|
|
|
} |
|
91
|
|
|
$parsedRoutingTable[$commonName][] = $parsedRoute[1]; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $parsedRoutingTable; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
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: