|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright 2016 François Kooman <[email protected]>. |
|
4
|
|
|
* |
|
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
6
|
|
|
* you may not use this file except in compliance with the License. |
|
7
|
|
|
* You may obtain a copy of the License at |
|
8
|
|
|
* |
|
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
10
|
|
|
* |
|
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14
|
|
|
* See the License for the specific language governing permissions and |
|
15
|
|
|
* limitations under the License. |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace fkooman\VPN\Server\Api; |
|
19
|
|
|
|
|
20
|
|
|
use fkooman\Http\Request; |
|
21
|
|
|
use fkooman\Rest\Service; |
|
22
|
|
|
use fkooman\Rest\ServiceModuleInterface; |
|
23
|
|
|
use fkooman\VPN\Server\InputValidation; |
|
24
|
|
|
use fkooman\Rest\Plugin\Authentication\Bearer\TokenInfo; |
|
25
|
|
|
use DateTime; |
|
26
|
|
|
use fkooman\VPN\Server\ApiResponse; |
|
27
|
|
|
use fkooman\Json\Json; |
|
28
|
|
|
|
|
29
|
|
|
class LogModule implements ServiceModuleInterface |
|
30
|
|
|
{ |
|
31
|
|
|
/** @var string */ |
|
32
|
|
|
private $logPath; |
|
33
|
|
|
|
|
34
|
|
|
/** @var \DateTime */ |
|
35
|
|
|
private $dateTime; |
|
36
|
|
|
|
|
37
|
|
|
public function __construct($logPath, DateTime $dateTime = null) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->logPath = $logPath; |
|
40
|
|
|
if (null === $dateTime) { |
|
41
|
|
|
$dateTime = new DateTime(); |
|
42
|
|
|
} |
|
43
|
|
|
$this->dateTime = $dateTime; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function init(Service $service) |
|
47
|
|
|
{ |
|
48
|
|
|
$service->get( |
|
49
|
|
|
'/log', |
|
50
|
|
|
function (Request $request, TokenInfo $tokenInfo) { |
|
51
|
|
|
$tokenInfo->getScope()->requireScope(['admin']); |
|
52
|
|
|
|
|
53
|
|
|
$dateTime = $request->getUrl()->getQueryParameter('date_time'); |
|
54
|
|
|
InputValidation::dateTime($dateTime); |
|
55
|
|
|
$dateTimeUnix = strtotime($dateTime); |
|
56
|
|
|
|
|
57
|
|
|
$ipAddress = $request->getUrl()->getQueryParameter('ip_address'); |
|
58
|
|
|
InputValidation::ipAddress($ipAddress); |
|
59
|
|
|
|
|
60
|
|
|
return new ApiResponse('log', $this->get($dateTimeUnix, $ipAddress)); |
|
61
|
|
|
} |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
|
|
$service->get( |
|
65
|
|
|
'/stats', |
|
66
|
|
|
function (Request $request, TokenInfo $tokenInfo) { |
|
67
|
|
|
$tokenInfo->getScope()->requireScope(['admin']); |
|
68
|
|
|
|
|
69
|
|
|
return new ApiResponse('stats', Json::decodeFile(sprintf('%s/stats.json', $this->logPath))); |
|
70
|
|
|
} |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function get($dateTimeUnix, $ipAddress) |
|
75
|
|
|
{ |
|
76
|
|
|
$returnData = []; |
|
77
|
|
|
|
|
78
|
|
|
$logData = Json::decodeFile(sprintf('%s/log.json', $this->logPath)); |
|
79
|
|
|
foreach ($logData['entries'] as $k => $v) { |
|
80
|
|
|
$connectTime = $v['connect_time']; |
|
81
|
|
|
$disconnectTime = array_key_exists('disconnect_time', $v) ? $v['disconnect_time'] : null; |
|
82
|
|
|
|
|
83
|
|
|
if ($connectTime <= $dateTimeUnix && (is_null($disconnectTime) || $disconnectTime >= $dateTimeUnix)) { |
|
84
|
|
|
// XXX edge cases? still connected? just disconnected? |
|
85
|
|
|
$v4 = $v['v4']; |
|
86
|
|
|
$v6 = $v['v6']; |
|
87
|
|
|
if ($v4 === $ipAddress || $v6 === $ipAddress) { |
|
88
|
|
|
$returnData[] = [ |
|
89
|
|
|
// XXX deal with still connected |
|
90
|
|
|
'user_id' => $v['user_id'], |
|
91
|
|
|
'v4' => $v4, |
|
92
|
|
|
'v6' => $v6, |
|
93
|
|
|
'config_name' => $v['config_name'], |
|
94
|
|
|
'connect_time' => $connectTime, |
|
95
|
|
|
'disconnect_time' => $disconnectTime, |
|
96
|
|
|
]; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
// XXX could there actually be multiple results? |
|
101
|
|
|
|
|
102
|
|
|
return $returnData; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|