|
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
|
|
|
|
|
19
|
|
|
namespace SURFnet\VPN\Server\Api; |
|
20
|
|
|
|
|
21
|
|
|
use SURFnet\VPN\Common\Http\AuthUtils; |
|
22
|
|
|
use SURFnet\VPN\Common\Http\ServiceModuleInterface; |
|
23
|
|
|
use SURFnet\VPN\Common\Http\Service; |
|
24
|
|
|
use SURFnet\VPN\Common\Http\Request; |
|
25
|
|
|
use SURFnet\VPN\Common\Http\ApiResponse; |
|
26
|
|
|
use SURFnet\VPN\Common\FileIO; |
|
27
|
|
|
use RuntimeException; |
|
28
|
|
|
|
|
29
|
|
|
class StatsModule implements ServiceModuleInterface |
|
30
|
|
|
{ |
|
31
|
|
|
/** @var string */ |
|
32
|
|
|
private $dataDir; |
|
33
|
|
|
|
|
34
|
|
|
public function __construct($dataDir) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->dataDir = $dataDir; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function init(Service $service) |
|
40
|
|
|
{ |
|
41
|
|
|
$service->get( |
|
42
|
|
|
'/stats', |
|
43
|
|
|
function (Request $request, array $hookData) { |
|
44
|
|
|
AuthUtils::requireUser($hookData, ['vpn-admin-portal']); |
|
45
|
|
|
$statsFile = sprintf('%s/stats.json', $this->dataDir); |
|
46
|
|
|
|
|
47
|
|
|
try { |
|
48
|
|
|
return new ApiResponse('stats', FileIO::readJsonFile($statsFile)); |
|
49
|
|
|
} catch (RuntimeException $e) { |
|
50
|
|
|
// no stats file available yet |
|
51
|
|
|
return new ApiResponse('stats', false); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|