|
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
|
|
|
/** |
|
21
|
|
|
* Parses the response from the OpenVPN `status 2` command. |
|
22
|
|
|
* |
|
23
|
|
|
* NOTE: The OpenVPN instance MUST NOT have --duplicate-cn in the configuration |
|
24
|
|
|
* as we do not deal with multiple connections with the same CN, due to bugs in |
|
25
|
|
|
* udp6 status report where the client port is not mentioned in the |
|
26
|
|
|
* 'Real Address' column |
|
27
|
|
|
*/ |
|
28
|
|
|
class StatusParser |
|
29
|
|
|
{ |
|
30
|
|
|
public static function parse(array $statusData) |
|
31
|
|
|
{ |
|
32
|
|
|
//TITLE,OpenVPN 2.3.9 x86_64-redhat-linux-gnu [SSL (OpenSSL)] [LZO] [EPOLL] [PKCS11] [MH] [IPv6] built on Dec 16 2015 |
|
|
|
|
|
|
33
|
|
|
//TIME,Wed Dec 23 12:52:08 2015,1450875128 |
|
|
|
|
|
|
34
|
|
|
//HEADER,CLIENT_LIST,Common Name,Real Address,Virtual Address,Bytes Received,Bytes Sent,Connected Since,Connected Since (time_t),Username |
|
35
|
|
|
//CLIENT_LIST,fkooman_ziptest,::ffff:91.64.87.183,10.42.42.2,127707,127903,Wed Dec 23 12:49:15 2015,1450874955,UNDEF |
|
|
|
|
|
|
36
|
|
|
//CLIENT_LIST,sebas_tuxed_SGS6,::ffff:83.83.194.107,10.42.42.3,127229,180419,Wed Dec 23 12:05:28 2015,1450872328,UNDEF |
|
|
|
|
|
|
37
|
|
|
//HEADER,ROUTING_TABLE,Virtual Address,Common Name,Real Address,Last Ref,Last Ref (time_t) |
|
38
|
|
|
//ROUTING_TABLE,10.42.42.2,fkooman_ziptest,::ffff:91.64.87.183,Wed Dec 23 12:52:07 2015,1450875127 |
|
|
|
|
|
|
39
|
|
|
//ROUTING_TABLE,fd00:4242:4242::1000,fkooman_ziptest,::ffff:91.64.87.183,Wed Dec 23 12:50:42 2015,1450875042 |
|
|
|
|
|
|
40
|
|
|
//ROUTING_TABLE,fd00:4242:4242::1001,sebas_tuxed_SGS6,::ffff:83.83.194.107,Wed Dec 23 12:28:53 2015,1450873733 |
|
|
|
|
|
|
41
|
|
|
//ROUTING_TABLE,10.42.42.3,sebas_tuxed_SGS6,::ffff:83.83.194.107,Wed Dec 23 12:50:46 2015,1450875046 |
|
|
|
|
|
|
42
|
|
|
//GLOBAL_STATS,Max bcast/mcast queue length,0 |
|
43
|
|
|
//END |
|
44
|
|
|
|
|
45
|
|
|
// for now, we log all statusData to get a good corpus for writing |
|
46
|
|
|
// tests |
|
47
|
|
|
|
|
48
|
|
|
//error_log(json_encode($statusData)); |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
$clientListStart = 0; |
|
51
|
|
|
$routingTableStart = 0; |
|
52
|
|
|
$globalStatsStart = 0; |
|
53
|
|
|
|
|
54
|
|
|
for ($i = 0; $i < sizeof($statusData); ++$i) { |
|
|
|
|
|
|
55
|
|
|
if (0 === strpos($statusData[$i], 'HEADER,CLIENT_LIST')) { |
|
56
|
|
|
$clientListStart = $i; |
|
57
|
|
|
} |
|
58
|
|
|
if (0 === strpos($statusData[$i], 'HEADER,ROUTING_TABLE')) { |
|
59
|
|
|
$routingTableStart = $i; |
|
60
|
|
|
} |
|
61
|
|
|
if (0 === strpos($statusData[$i], 'GLOBAL_STATS')) { |
|
62
|
|
|
$globalStatsStart = $i; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$parsedClientList = self::parseClientList(array_slice($statusData, $clientListStart, $routingTableStart - $clientListStart)); |
|
67
|
|
|
$parsedRoutingTable = self::parseRoutingTable(array_slice($statusData, $routingTableStart, $globalStatsStart - $routingTableStart)); |
|
68
|
|
|
|
|
69
|
|
|
// merge routing table in client list |
|
70
|
|
|
foreach ($parsedClientList as $key => $value) { |
|
71
|
|
|
if (!array_key_exists($key, $parsedRoutingTable)) { |
|
72
|
|
|
$parsedClientList[$key]['virtual_address'] = array(); |
|
73
|
|
|
} else { |
|
74
|
|
|
$parsedClientList[$key]['virtual_address'] = $parsedRoutingTable[$key]; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return array_values($parsedClientList); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
private static function parseClientList(array $clientList) |
|
82
|
|
|
{ |
|
83
|
|
|
//HEADER,CLIENT_LIST,Common Name,Real Address,Virtual Address,Bytes Received,Bytes Sent,Connected Since,Connected Since (time_t),Username |
|
84
|
|
|
//CLIENT_LIST,fkooman_ziptest,::ffff:91.64.87.183,10.42.42.2,127707,127903,Wed Dec 23 12:49:15 2015,1450874955,UNDEF |
|
|
|
|
|
|
85
|
|
|
//CLIENT_LIST,sebas_tuxed_SGS6,::ffff:83.83.194.107,10.42.42.3,127229,180419,Wed Dec 23 12:05:28 2015,1450872328,UNDEF |
|
|
|
|
|
|
86
|
|
|
$parsedClientList = array(); |
|
87
|
|
|
for ($i = 1; $i < sizeof($clientList); ++$i) { |
|
|
|
|
|
|
88
|
|
|
$parsedClient = str_getcsv($clientList[$i]); |
|
89
|
|
|
$commonName = $parsedClient[1]; |
|
90
|
|
|
if (array_key_exists($commonName, $parsedClientList)) { |
|
|
|
|
|
|
91
|
|
|
//syslog(LOG_ERR('duplicate common name, possibly --duplicate-cn enabled in server configuration')); |
|
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
$parsedClientList[$commonName] = array( |
|
94
|
|
|
'common_name' => $commonName, |
|
95
|
|
|
'user_id' => explode('_', $commonName, 2)[0], |
|
96
|
|
|
'name' => explode('_', $commonName, 2)[1], |
|
97
|
|
|
'real_address' => $parsedClient[2], |
|
98
|
|
|
//'virtual_address' => $parsedClient[3], |
|
|
|
|
|
|
99
|
|
|
'bytes_in' => intval($parsedClient[4]), |
|
100
|
|
|
'bytes_out' => intval($parsedClient[5]), |
|
101
|
|
|
'connected_since' => intval($parsedClient[7]), |
|
102
|
|
|
); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $parsedClientList; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
private static function parseRoutingTable(array $routingTable) |
|
109
|
|
|
{ |
|
110
|
|
|
//HEADER,ROUTING_TABLE,Virtual Address,Common Name,Real Address,Last Ref,Last Ref (time_t) |
|
111
|
|
|
//ROUTING_TABLE,10.42.42.2,fkooman_ziptest,::ffff:91.64.87.183,Wed Dec 23 12:52:07 2015,1450875127 |
|
|
|
|
|
|
112
|
|
|
//ROUTING_TABLE,fd00:4242:4242::1000,fkooman_ziptest,::ffff:91.64.87.183,Wed Dec 23 12:50:42 2015,1450875042 |
|
|
|
|
|
|
113
|
|
|
//ROUTING_TABLE,fd00:4242:4242::1001,sebas_tuxed_SGS6,::ffff:83.83.194.107,Wed Dec 23 12:28:53 2015,1450873733 |
|
|
|
|
|
|
114
|
|
|
//ROUTING_TABLE,10.42.42.3,sebas_tuxed_SGS6,::ffff:83.83.194.107,Wed Dec 23 12:50:46 2015,1450875046 |
|
|
|
|
|
|
115
|
|
|
$parsedRoutingTable = array(); |
|
116
|
|
|
for ($i = 1; $i < sizeof($routingTable); ++$i) { |
|
|
|
|
|
|
117
|
|
|
$parsedRoute = str_getcsv($routingTable[$i]); |
|
118
|
|
|
$commonName = $parsedRoute[2]; |
|
119
|
|
|
if (!array_key_exists($commonName, $parsedRoutingTable)) { |
|
120
|
|
|
$parsedRoutingTable[$commonName] = array(); |
|
121
|
|
|
} |
|
122
|
|
|
$parsedRoutingTable[$commonName][] = $parsedRoute[1]; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return $parsedRoutingTable; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.