|
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\Api; |
|
19
|
|
|
|
|
20
|
|
|
use SURFnet\VPN\Common\Http\ServiceModuleInterface; |
|
21
|
|
|
use SURFnet\VPN\Common\Http\Service; |
|
22
|
|
|
use SURFnet\VPN\Common\Http\ApiResponse; |
|
23
|
|
|
use SURFnet\VPN\Common\Http\Request; |
|
24
|
|
|
use SURFnet\VPN\Common\FileIO; |
|
25
|
|
|
|
|
26
|
|
|
class LogModule implements ServiceModuleInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** @var ConnectionLog */ |
|
29
|
|
|
private $connectionLog; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct(ConnectionLog $connectionLog) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->connectionLog = $connectionLog; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function init(Service $service) |
|
37
|
|
|
{ |
|
38
|
|
|
$service->get( |
|
39
|
|
|
'/log', |
|
40
|
|
|
function (Request $request, array $hookData) { |
|
41
|
|
|
Utils::requireUser($hookData, ['vpn-admin-portal']); |
|
42
|
|
|
|
|
43
|
|
|
$dateTime = $request->getQueryParameter('date_time'); |
|
44
|
|
|
InputValidation::dateTime($dateTime); |
|
45
|
|
|
|
|
46
|
|
|
// do not convert if we have a number, it is probably already unix time |
|
47
|
|
|
$dateTimeUnix = is_numeric($dateTime) ? intval($dateTime) : strtotime($dateTime); |
|
48
|
|
|
|
|
49
|
|
|
$ipAddress = $request->getQueryParameter('ip_address'); |
|
50
|
|
|
InputValidation::ipAddress($ipAddress); |
|
51
|
|
|
|
|
52
|
|
|
$logData = $this->connectionLog->get($dateTimeUnix, $ipAddress); |
|
53
|
|
|
if (false !== $logData) { |
|
54
|
|
|
foreach ($logData as $k => $value) { |
|
55
|
|
|
$logData[$k]['user_id'] = substr($value['common_name'], 0, strpos($value['common_name'], '_')); |
|
56
|
|
|
$logData[$k]['config_name'] = substr($value['common_name'], strpos($value['common_name'], '_') + 1); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return new ApiResponse('log', $logData); |
|
61
|
|
|
} |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
|
|
// $service->get( |
|
|
|
|
|
|
65
|
|
|
// '/stats', |
|
66
|
|
|
// function (Request $request, array $hookData) { |
|
67
|
|
|
// Utils::requireUser($hookData, ['vpn-admin-portal']); |
|
68
|
|
|
// $statsFile = sprintf('%s/stats.json', $this->dataDir); |
|
69
|
|
|
|
|
70
|
|
|
// return new ApiResponse('stats', FileIO::readJsonFile($statsFile)); |
|
|
|
|
|
|
71
|
|
|
// } |
|
72
|
|
|
// ); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
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.