|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace fkooman\VPN\Server\Info; |
|
4
|
|
|
|
|
5
|
|
|
use fkooman\Http\Request; |
|
6
|
|
|
use fkooman\Rest\Service; |
|
7
|
|
|
use fkooman\Rest\ServiceModuleInterface; |
|
8
|
|
|
use fkooman\Http\JsonResponse; |
|
9
|
|
|
use fkooman\Http\Exception\ForbiddenException; |
|
10
|
|
|
use fkooman\Rest\Plugin\Authentication\Bearer\TokenInfo; |
|
11
|
|
|
use fkooman\VPN\Server\Utils; |
|
12
|
|
|
|
|
13
|
|
|
class InfoModule implements ServiceModuleInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var array */ |
|
16
|
|
|
private $v4; |
|
17
|
|
|
|
|
18
|
|
|
/** @var array */ |
|
19
|
|
|
private $v6; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct(array $v4, array $v6) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->v4 = $v4; |
|
24
|
|
|
$this->v6 = $v6; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function init(Service $service) |
|
28
|
|
|
{ |
|
29
|
|
|
$service->get( |
|
30
|
|
|
'/info/net', |
|
31
|
|
|
function (Request $request, TokenInfo $tokenInfo) { |
|
32
|
|
|
self::requireScope($tokenInfo, 'info_get'); |
|
33
|
|
|
|
|
34
|
|
|
return $this->getInfo(); |
|
35
|
|
|
} |
|
36
|
|
|
); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
private function getInfo() |
|
40
|
|
|
{ |
|
41
|
|
|
$prefix6 = $this->v6['prefix']; |
|
42
|
|
|
$net4 = $this->v4['range']; |
|
43
|
|
|
|
|
44
|
|
|
$responseData = []; |
|
45
|
|
|
$responseData['range'] = $net4; |
|
46
|
|
|
$responseData['range6'] = $prefix6; |
|
47
|
|
|
$responseData['dns'] = array_merge($this->v4['dns'], $this->v6['dns']); |
|
48
|
|
|
|
|
49
|
|
|
$response = new JsonResponse(); |
|
50
|
|
|
$response->setBody($responseData); |
|
51
|
|
|
|
|
52
|
|
|
return $response; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
private static function requireScope(TokenInfo $tokenInfo, $requiredScope) |
|
56
|
|
|
{ |
|
57
|
|
|
if (!$tokenInfo->getScope()->hasScope($requiredScope)) { |
|
58
|
|
|
throw new ForbiddenException('insufficient_scope', sprintf('"%s" scope required', $requiredScope)); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|