|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright 2015 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
|
|
|
namespace fkooman\VPN\Server\Config; |
|
18
|
|
|
|
|
19
|
|
|
use fkooman\Http\Request; |
|
20
|
|
|
use fkooman\Rest\Service; |
|
21
|
|
|
use fkooman\Rest\ServiceModuleInterface; |
|
22
|
|
|
use fkooman\VPN\Server\Utils; |
|
23
|
|
|
use fkooman\Rest\Plugin\Authentication\UserInfoInterface; |
|
24
|
|
|
use fkooman\Http\JsonResponse; |
|
25
|
|
|
use fkooman\Http\Exception\BadRequestException; |
|
26
|
|
|
|
|
27
|
|
|
class ConfigModule implements ServiceModuleInterface |
|
28
|
|
|
{ |
|
29
|
|
|
/** @var StaticConfig */ |
|
30
|
|
|
private $staticConfig; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct(StaticConfig $staticConfig) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->staticConfig = $staticConfig; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function init(Service $service) |
|
38
|
|
|
{ |
|
39
|
|
|
$service->get( |
|
40
|
|
|
'/static/ip', |
|
41
|
|
|
function (Request $request, UserInfoInterface $userInfo) { |
|
|
|
|
|
|
42
|
|
|
// we typically deal with CNs, not user IDs, but the part of |
|
43
|
|
|
// the CN before the first '_' is considered the user ID |
|
44
|
|
|
$userId = $request->getUrl()->getQueryParameter('user_id'); |
|
45
|
|
|
$commonName = $request->getUrl()->getQueryParameter('common_name'); |
|
46
|
|
|
if (!is_null($userId)) { |
|
47
|
|
|
// per user |
|
48
|
|
|
Utils::validateUserId($userId); |
|
49
|
|
|
$ipConfig = $this->staticConfig->getStaticAddresses($userId); |
|
50
|
|
|
} elseif (!is_null($commonName)) { |
|
51
|
|
|
// per CN |
|
52
|
|
|
Utils::validateCommonName($commonName); |
|
53
|
|
|
$ipConfig = $this->staticConfig->getStaticAddress($commonName); |
|
54
|
|
|
} else { |
|
55
|
|
|
// all |
|
56
|
|
|
$ipConfig = $this->staticConfig->getStaticAddresses(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$response = new JsonResponse(); |
|
60
|
|
|
$response->setBody( |
|
61
|
|
|
array( |
|
62
|
|
|
'ok' => true, |
|
63
|
|
|
'ip' => $ipConfig, |
|
64
|
|
|
'ipRange' => $this->staticConfig->getIpRange()->getRange(), |
|
65
|
|
|
'poolRange' => $this->staticConfig->getPoolRange()->getRange(), |
|
66
|
|
|
) |
|
67
|
|
|
); |
|
68
|
|
|
|
|
69
|
|
|
return $response; |
|
70
|
|
|
} |
|
71
|
|
|
); |
|
72
|
|
|
|
|
73
|
|
|
$service->post( |
|
74
|
|
|
'/static/ip', |
|
75
|
|
|
function (Request $request, UserInfoInterface $userInfo) { |
|
|
|
|
|
|
76
|
|
|
$commonName = $request->getPostParameter('common_name'); |
|
77
|
|
|
if (is_null($commonName)) { |
|
78
|
|
|
throw new BadRequestException('missing common_name'); |
|
79
|
|
|
} |
|
80
|
|
|
Utils::validateCommonName($commonName); |
|
81
|
|
|
|
|
82
|
|
|
$v4 = $request->getPostParameter('v4'); |
|
83
|
|
|
if (empty($v4)) { |
|
84
|
|
|
$v4 = null; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$response = new JsonResponse(); |
|
88
|
|
|
$response->setBody( |
|
89
|
|
|
array( |
|
90
|
|
|
'ok' => $this->staticConfig->setStaticAddresses($commonName, $v4), |
|
91
|
|
|
) |
|
92
|
|
|
); |
|
93
|
|
|
|
|
94
|
|
|
// $this->logInfo('setting static IP', array('api_user' => $userInfo->getUserId(), 'cn' => $commonName, 'v4' => $v4)); |
|
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
return $response; |
|
97
|
|
|
} |
|
98
|
|
|
); |
|
99
|
|
|
|
|
100
|
|
|
$service->post( |
|
101
|
|
|
'/ccd/disable', |
|
102
|
|
View Code Duplication |
function (Request $request, UserInfoInterface $userInfo) { |
|
|
|
|
|
|
103
|
|
|
$commonName = $request->getPostParameter('common_name'); |
|
104
|
|
|
if (is_null($commonName)) { |
|
105
|
|
|
throw new BadRequestException('missing common_name'); |
|
106
|
|
|
} |
|
107
|
|
|
Utils::validateCommonName($commonName); |
|
108
|
|
|
|
|
109
|
|
|
// $this->logInfo('disabling cn', array('api_user' => $userInfo->getUserId(), 'cn' => $commonName)); |
|
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
$response = new JsonResponse(); |
|
112
|
|
|
$response->setBody( |
|
113
|
|
|
array( |
|
114
|
|
|
'ok' => $this->staticConfig->disableCommonName($commonName), |
|
115
|
|
|
) |
|
116
|
|
|
); |
|
117
|
|
|
|
|
118
|
|
|
return $response; |
|
119
|
|
|
} |
|
120
|
|
|
); |
|
121
|
|
|
|
|
122
|
|
|
$service->delete( |
|
123
|
|
|
'/ccd/disable', |
|
124
|
|
View Code Duplication |
function (Request $request, UserInfoInterface $userInfo) { |
|
|
|
|
|
|
125
|
|
|
$commonName = $request->getUrl()->getQueryParameter('common_name'); |
|
126
|
|
|
Utils::validateCommonName($commonName); |
|
127
|
|
|
|
|
128
|
|
|
// $this->logInfo('enabling cn', array('api_user' => $userInfo->getUserId(), 'cn' => $commonName)); |
|
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
$response = new JsonResponse(); |
|
131
|
|
|
$response->setBody( |
|
132
|
|
|
array( |
|
133
|
|
|
'ok' => $this->staticConfig->enableCommonName($commonName), |
|
134
|
|
|
) |
|
135
|
|
|
); |
|
136
|
|
|
|
|
137
|
|
|
return $response; |
|
138
|
|
|
} |
|
139
|
|
|
); |
|
140
|
|
|
|
|
141
|
|
|
$service->get( |
|
142
|
|
|
'/ccd/disable', |
|
143
|
|
View Code Duplication |
function (Request $request, UserInfoInterface $userInfo) { |
|
|
|
|
|
|
144
|
|
|
// we typically deal with CNs, not user IDs, but the part of |
|
145
|
|
|
// the CN before the first '_' is considered the user ID |
|
146
|
|
|
$userId = $request->getUrl()->getQueryParameter('user_id'); |
|
147
|
|
|
if (!is_null($userId)) { |
|
148
|
|
|
Utils::validateUserId($userId); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
$response = new JsonResponse(); |
|
152
|
|
|
$response->setBody( |
|
153
|
|
|
array( |
|
154
|
|
|
'ok' => true, |
|
155
|
|
|
'disabled' => $this->staticConfig->getDisabledCommonNames($userId), |
|
156
|
|
|
) |
|
157
|
|
|
); |
|
158
|
|
|
|
|
159
|
|
|
return $response; |
|
160
|
|
|
} |
|
161
|
|
|
); |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.