|
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 DateTime; |
|
21
|
|
|
|
|
22
|
|
|
class LogModule implements ServiceModuleInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var string */ |
|
25
|
|
|
private $logPath; |
|
26
|
|
|
|
|
27
|
|
|
/** @var \DateTime */ |
|
28
|
|
|
private $dateTime; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct($logPath, DateTime $dateTime = null) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->logPath = $logPath; |
|
33
|
|
|
if (null === $dateTime) { |
|
34
|
|
|
$dateTime = new DateTime(); |
|
35
|
|
|
} |
|
36
|
|
|
$this->dateTime = $dateTime; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function init(Service $service) |
|
40
|
|
|
{ |
|
41
|
|
|
$service->get( |
|
42
|
|
|
'/log', |
|
43
|
|
View Code Duplication |
function (array $serverData, array $getData, array $postData, array $hookData) { |
|
|
|
|
|
|
44
|
|
|
Utils::requireUser($hookData, ['admin']); |
|
45
|
|
|
|
|
46
|
|
|
$dateTime = Utils::requireParameter($getData, 'date_time'); |
|
47
|
|
|
InputValidation::dateTime($dateTime); |
|
48
|
|
|
$dateTimeUnix = strtotime($dateTime); |
|
49
|
|
|
|
|
50
|
|
|
$ipAddress = Utils::requireParameter($getData, 'ip_address'); |
|
51
|
|
|
InputValidation::ipAddress($ipAddress); |
|
52
|
|
|
|
|
53
|
|
|
return new ApiResponse('log', $this->get($dateTimeUnix, $ipAddress)); |
|
54
|
|
|
} |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
|
|
$service->get( |
|
58
|
|
|
'/stats', |
|
59
|
|
|
function (array $serverData, array $getData, array $postData, array $hookData) { |
|
60
|
|
|
Utils::requireUser($hookData, ['admin']); |
|
61
|
|
|
|
|
62
|
|
|
return new ApiResponse('stats', Json::decodeFile(sprintf('%s/stats.json', $this->logPath))); |
|
63
|
|
|
} |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function get($dateTimeUnix, $ipAddress) |
|
68
|
|
|
{ |
|
69
|
|
|
$returnData = []; |
|
70
|
|
|
|
|
71
|
|
|
$logData = Json::decodeFile(sprintf('%s/log.json', $this->logPath)); |
|
72
|
|
|
foreach ($logData['entries'] as $k => $v) { |
|
73
|
|
|
$connectTime = $v['connect_time']; |
|
74
|
|
|
$disconnectTime = array_key_exists('disconnect_time', $v) ? $v['disconnect_time'] : null; |
|
75
|
|
|
|
|
76
|
|
|
if ($connectTime <= $dateTimeUnix && (is_null($disconnectTime) || $disconnectTime >= $dateTimeUnix)) { |
|
77
|
|
|
// XXX edge cases? still connected? just disconnected? |
|
78
|
|
|
$v4 = $v['v4']; |
|
79
|
|
|
$v6 = $v['v6']; |
|
80
|
|
|
if ($v4 === $ipAddress || $v6 === $ipAddress) { |
|
81
|
|
|
$returnData[] = [ |
|
82
|
|
|
// XXX deal with still connected |
|
83
|
|
|
'user_id' => $v['user_id'], |
|
84
|
|
|
'v4' => $v4, |
|
85
|
|
|
'v6' => $v6, |
|
86
|
|
|
'config_name' => $v['config_name'], |
|
87
|
|
|
'connect_time' => $connectTime, |
|
88
|
|
|
'disconnect_time' => $disconnectTime, |
|
89
|
|
|
]; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
// XXX could there actually be multiple results? |
|
94
|
|
|
|
|
95
|
|
|
return $returnData; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.