|
1
|
|
|
#!/usr/bin/php |
|
|
|
|
|
|
2
|
|
|
<?php |
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright (C) 2016 SURFnet. |
|
5
|
|
|
* |
|
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
7
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
8
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
9
|
|
|
* License, or (at your option) any later version. |
|
10
|
|
|
* |
|
11
|
|
|
* This program is distributed in the hope that it will be useful, |
|
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14
|
|
|
* GNU Affero General Public License for more details. |
|
15
|
|
|
* |
|
16
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
17
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
/* |
|
21
|
|
|
* Parse the systemd journal. |
|
22
|
|
|
* |
|
23
|
|
|
* Usage: |
|
24
|
|
|
* |
|
25
|
|
|
* $ sudo journalctl \ |
|
26
|
|
|
* -o json \ |
|
27
|
|
|
* -t vpn-server-api-client-connect \ |
|
28
|
|
|
* -t vpn-server-api-client-disconnect \ |
|
29
|
|
|
* | vpn-server-api-parse-journal |
|
30
|
|
|
*/ |
|
31
|
|
|
require_once sprintf('%s/vendor/autoload.php', dirname(__DIR__)); |
|
32
|
|
|
|
|
33
|
|
|
use SURFnet\VPN\Common\FileIO; |
|
34
|
|
|
|
|
35
|
|
|
/* |
|
36
|
|
|
* Due to not wanting to store all user data forever, we have a log window |
|
37
|
|
|
* after which log entries disappear from the system, this can result in some |
|
38
|
|
|
* issues. The log window is typically 1 month. |
|
39
|
|
|
* |
|
40
|
|
|
* LIMITATIONS: |
|
41
|
|
|
* |
|
42
|
|
|
* - if a client connected before this logging window started, it is not |
|
43
|
|
|
* visible in the logs; |
|
44
|
|
|
* - if a client stays connected for the whole logging window, it is not |
|
45
|
|
|
* available in the log at all; |
|
46
|
|
|
* - if a client was connected before the logging window started and |
|
47
|
|
|
* disconnected during, it is not added to the log; |
|
48
|
|
|
*/ |
|
49
|
|
|
|
|
50
|
|
|
$clientConnectSyslogIdentifier = 'vpn-server-api-client-connect'; |
|
51
|
|
|
$clientDisconnectSyslogIdentifier = 'vpn-server-api-client-disconnect'; |
|
52
|
|
|
|
|
53
|
|
|
function verifyMessage(array $messageData, $type) |
|
54
|
|
|
{ |
|
55
|
|
|
$requiredKeys = [ |
|
56
|
|
|
'INSTANCE_ID', |
|
57
|
|
|
'POOL_ID', |
|
58
|
|
|
'common_name', |
|
59
|
|
|
'time_unix', |
|
60
|
|
|
'ifconfig_pool_remote_ip', |
|
61
|
|
|
'ifconfig_pool_remote_ip6', |
|
62
|
|
|
]; |
|
63
|
|
|
|
|
64
|
|
|
if ('disconnect' === $type) { |
|
65
|
|
|
$requiredKeys[] = 'bytes_received'; |
|
66
|
|
|
$requiredKeys[] = 'bytes_sent'; |
|
67
|
|
|
$requiredKeys[] = 'time_duration'; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
foreach ($requiredKeys as $k) { |
|
71
|
|
|
if (!array_key_exists($k, $messageData)) { |
|
72
|
|
|
return false; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return true; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
try { |
|
80
|
|
|
$logData = []; |
|
81
|
|
|
|
|
82
|
|
|
// every line is a JSON object |
|
83
|
|
|
while ($jsonLine = fgets(STDIN)) { |
|
84
|
|
|
$jsonData = json_decode($jsonLine, true); |
|
85
|
|
|
|
|
86
|
|
|
if ($clientConnectSyslogIdentifier === $jsonData['SYSLOG_IDENTIFIER']) { |
|
87
|
|
|
// handle connect data |
|
88
|
|
|
$message = $jsonData['MESSAGE']; |
|
89
|
|
|
$messageData = json_decode($message, true); |
|
90
|
|
|
if (JSON_ERROR_NONE !== json_last_error()) { |
|
91
|
|
|
// XXX if an error occurred decoding the message, it was |
|
92
|
|
|
// probably a log error message, ignore them for now, but later we |
|
93
|
|
|
// will need them as well! |
|
94
|
|
|
continue; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
if (!verifyMessage($messageData, 'connect')) { |
|
98
|
|
|
continue; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$instanceId = $messageData['INSTANCE_ID']; |
|
102
|
|
|
$poolId = $messageData['POOL_ID']; |
|
103
|
|
|
$commonName = $messageData['common_name']; |
|
104
|
|
|
$userId = explode('_', $commonName, 2)[0]; |
|
105
|
|
|
$configName = explode('_', $commonName, 2)[1]; |
|
106
|
|
|
|
|
107
|
|
|
$logKey = sprintf('%s:%s:%s', $poolId, $messageData['common_name'], $messageData['time_unix']); |
|
108
|
|
|
$logData[$instanceId][$logKey] = [ |
|
109
|
|
|
'pool_id' => $poolId, |
|
110
|
|
|
'user_id' => $userId, |
|
111
|
|
|
'config_name' => $configName, |
|
112
|
|
|
'v4' => $messageData['ifconfig_pool_remote_ip'], |
|
113
|
|
|
'v6' => $messageData['ifconfig_pool_remote_ip6'], |
|
114
|
|
|
'connect_time' => intval($messageData['time_unix']), |
|
115
|
|
|
]; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
if ($clientDisconnectSyslogIdentifier === $jsonData['SYSLOG_IDENTIFIER']) { |
|
119
|
|
|
// handle disconnect data |
|
120
|
|
|
$message = $jsonData['MESSAGE']; |
|
121
|
|
|
$messageData = json_decode($message, true); |
|
122
|
|
|
if (JSON_ERROR_NONE !== json_last_error()) { |
|
123
|
|
|
// XXX if an error occurred decoding the message, it was |
|
124
|
|
|
// probably a log error message, ignore them for now, but later we |
|
125
|
|
|
// will need them as well! |
|
126
|
|
|
continue; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
if (!verifyMessage($messageData, 'disconnect')) { |
|
130
|
|
|
continue; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
$instanceId = $messageData['INSTANCE_ID']; |
|
134
|
|
|
$poolId = $messageData['POOL_ID']; |
|
135
|
|
|
$logKey = sprintf('%s:%s:%s', $poolId, $messageData['common_name'], $messageData['time_unix']); |
|
136
|
|
|
// XXX what if instanceId key does not exist? |
|
137
|
|
|
if (!array_key_exists($logKey, $logData[$instanceId])) { |
|
138
|
|
|
// XXX we did not find a matching connect entry... |
|
139
|
|
|
// just ignore it |
|
140
|
|
|
continue; |
|
141
|
|
|
} |
|
142
|
|
|
$dataTransferred = $messageData['bytes_sent'] + $messageData['bytes_received']; |
|
143
|
|
|
$logData[$instanceId][$logKey] = array_merge( |
|
144
|
|
|
$logData[$instanceId][$logKey], |
|
145
|
|
|
[ |
|
146
|
|
|
'disconnect_time' => $messageData['time_unix'] + intval($messageData['time_duration']), |
|
147
|
|
|
'traffic' => $dataTransferred, |
|
148
|
|
|
] |
|
149
|
|
|
); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
foreach ($logData as $instanceId => $logEntries) { |
|
154
|
|
|
$logFile = sprintf('%s/data/%s/log.json', dirname(__DIR__), $instanceId); |
|
155
|
|
|
$logDir = dirname($logFile); |
|
156
|
|
|
|
|
157
|
|
|
if (!file_exists($logDir)) { |
|
158
|
|
|
if (false === @mkdir($logDir, 0711, true)) { |
|
159
|
|
|
throw new RuntimeException(sprintf('unable to create directory "%s"', $logDir)); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
FileIO::writeJsonFile( |
|
164
|
|
|
$logFile, |
|
165
|
|
|
[ |
|
166
|
|
|
'entries' => array_values($logEntries), |
|
167
|
|
|
] |
|
168
|
|
|
); |
|
169
|
|
|
} |
|
170
|
|
|
} catch (Exception $e) { |
|
171
|
|
|
echo sprintf('ERROR: %s', $e->getMessage()).PHP_EOL; |
|
172
|
|
|
exit(1); |
|
173
|
|
|
} |
|
174
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.