|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* eduVPN - End-user friendly VPN. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright: 2016-2017, The Commons Conservancy eduVPN Programme |
|
7
|
|
|
* SPDX-License-Identifier: AGPL-3.0+ |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace SURFnet\VPN\Server\Api; |
|
11
|
|
|
|
|
12
|
|
|
use SURFnet\VPN\Common\Http\ApiResponse; |
|
13
|
|
|
use SURFnet\VPN\Common\Http\AuthUtils; |
|
14
|
|
|
use SURFnet\VPN\Common\Http\InputValidation; |
|
15
|
|
|
use SURFnet\VPN\Common\Http\Request; |
|
16
|
|
|
use SURFnet\VPN\Common\Http\Service; |
|
17
|
|
|
use SURFnet\VPN\Common\Http\ServiceModuleInterface; |
|
18
|
|
|
use SURFnet\VPN\Server\OpenVpn\ServerManager; |
|
19
|
|
|
use SURFnet\VPN\Server\Storage; |
|
20
|
|
|
|
|
21
|
|
|
class OpenVpnModule implements ServiceModuleInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var \SURFnet\VPN\Server\OpenVpn\ServerManager */ |
|
24
|
|
|
private $serverManager; |
|
25
|
|
|
|
|
26
|
|
|
/** @var \SURFnet\VPN\Server\Storage */ |
|
27
|
|
|
private $storage; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct(ServerManager $serverManager, Storage $storage) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->serverManager = $serverManager; |
|
32
|
|
|
$this->storage = $storage; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function init(Service $service) |
|
36
|
|
|
{ |
|
37
|
|
|
$service->get( |
|
38
|
|
|
'/client_connections', |
|
39
|
|
|
function (Request $request, array $hookData) { |
|
40
|
|
|
AuthUtils::requireUser($hookData, ['vpn-admin-portal']); |
|
41
|
|
|
|
|
42
|
|
|
$clientConnections = $this->serverManager->connections(); |
|
43
|
|
|
// add user information to connection information |
|
44
|
|
|
foreach ($clientConnections as $k => $v) { |
|
45
|
|
|
foreach ($v['connections'] as $k1 => $v2) { |
|
46
|
|
|
if (false === $certInfo = $this->storage->getUserCertificateInfo($v2['common_name'])) { |
|
47
|
|
|
error_log(sprintf('"common_name "%s" not found', $v2['common_name'])); |
|
48
|
|
|
unset($clientConnections[$k]['connections'][$k1]); |
|
49
|
|
|
continue; |
|
50
|
|
|
} |
|
51
|
|
|
$clientConnections[$k]['connections'][$k1] = array_merge($v2, $certInfo); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return new ApiResponse('client_connections', $clientConnections); |
|
56
|
|
|
} |
|
57
|
|
|
); |
|
58
|
|
|
|
|
59
|
|
|
$service->post( |
|
60
|
|
|
'/kill_client', |
|
61
|
|
View Code Duplication |
function (Request $request, array $hookData) { |
|
|
|
|
|
|
62
|
|
|
AuthUtils::requireUser($hookData, ['vpn-admin-portal', 'vpn-user-portal']); |
|
63
|
|
|
|
|
64
|
|
|
$commonName = InputValidation::commonName($request->getPostParameter('common_name')); |
|
65
|
|
|
|
|
66
|
|
|
return new ApiResponse('kill_client', $this->serverManager->kill($commonName)); |
|
67
|
|
|
} |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
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.