1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "vpn_exec.h"
17
18 #include <cstdint>
19 #include <securec.h>
20
21 #include "napi_utils.h"
22 #include "net_manager_constants.h"
23 #include "netmanager_ext_log.h"
24 #include "networkvpn_client.h"
25 #ifdef SUPPORT_SYSVPN
26 #include "vpn_config_utils.h"
27 #endif // SUPPORT_SYSVPN
28
29 namespace OHOS {
30 namespace NetManagerStandard {
31 namespace VpnExec {
GetVpnConnectionInstance(ContextT * context)32 template <typename ContextT> static inline NetworkVpnClient *GetVpnConnectionInstance(ContextT *context)
33 {
34 if (context == nullptr) {
35 return nullptr;
36 }
37 auto manager = context->GetManager();
38 return (manager == nullptr) ? nullptr : reinterpret_cast<NetworkVpnClient *>(manager->GetData());
39 }
40
ExecPrepare(PrepareContext * context)41 bool ExecPrepare(PrepareContext *context)
42 {
43 auto vpnClient = GetVpnConnectionInstance(context);
44 if (vpnClient == nullptr) {
45 NETMANAGER_EXT_LOGE("vpnClient is nullptr");
46 return false;
47 }
48 int32_t result = vpnClient->Prepare(context->isExistVpn_, context->isRun_, context->package_);
49 if (result != NETMANAGER_EXT_SUCCESS) {
50 context->SetErrorCode(result);
51 return false;
52 }
53 return true;
54 }
55
ExecSetUp(SetUpContext * context)56 bool ExecSetUp(SetUpContext *context)
57 {
58 auto vpnClient = GetVpnConnectionInstance(context);
59 if (vpnClient == nullptr) {
60 NETMANAGER_EXT_LOGE("vpnClient is nullptr");
61 return false;
62 }
63 int32_t result = NETMANAGER_EXT_SUCCESS;
64 #ifdef SUPPORT_SYSVPN
65 if (context == nullptr) {
66 NETMANAGER_EXT_LOGE("context is nullptr");
67 return false;
68 }
69 if (context->sysVpnConfig_ != nullptr) {
70 // is system vpn
71 result = vpnClient->SetUpVpn(context->sysVpnConfig_);
72 } else {
73 result = vpnClient->SetUpVpn(context->vpnConfig_, context->fd_);
74 }
75 #else
76 result = vpnClient->SetUpVpn(context->vpnConfig_, context->fd_);
77 #endif // SUPPORT_SYSVPN
78 if (result != NETMANAGER_EXT_SUCCESS) {
79 context->SetErrorCode(result);
80 return false;
81 }
82 return true;
83 }
84
ExecProtect(ProtectContext * context)85 bool ExecProtect(ProtectContext *context)
86 {
87 auto vpnClient = GetVpnConnectionInstance(context);
88 if (vpnClient == nullptr) {
89 NETMANAGER_EXT_LOGE("vpnClient is nullptr");
90 return false;
91 }
92 int32_t result = vpnClient->Protect(context->socketFd_);
93 if (result != NETMANAGER_EXT_SUCCESS) {
94 context->SetErrorCode(result);
95 return false;
96 }
97 return true;
98 }
99
ExecDestroy(DestroyContext * context)100 bool ExecDestroy(DestroyContext *context)
101 {
102 auto vpnClient = GetVpnConnectionInstance(context);
103 if (vpnClient == nullptr) {
104 NETMANAGER_EXT_LOGE("vpnClient is nullptr");
105 return false;
106 }
107 int32_t result = vpnClient->DestroyVpn();
108 if (result != NETMANAGER_EXT_SUCCESS) {
109 context->SetErrorCode(result);
110 return false;
111 }
112 return true;
113 }
114
115 #ifdef SUPPORT_SYSVPN
ExecAddSysVpnConfig(AddContext * context)116 bool ExecAddSysVpnConfig(AddContext *context)
117 {
118 if (context == nullptr) {
119 NETMANAGER_EXT_LOGE("context is nullptr");
120 return false;
121 }
122 int32_t result = NetworkVpnClient::GetInstance().AddSysVpnConfig(context->vpnConfig_);
123 if (result != NETMANAGER_EXT_SUCCESS) {
124 context->SetErrorCode(result);
125 return false;
126 }
127 return true;
128 }
129
ExecDeleteSysVpnConfig(DeleteContext * context)130 bool ExecDeleteSysVpnConfig(DeleteContext *context)
131 {
132 if (context == nullptr) {
133 NETMANAGER_EXT_LOGE("context is nullptr");
134 return false;
135 }
136 int32_t result = NetworkVpnClient::GetInstance().DeleteSysVpnConfig(context->vpnId_);
137 if (result != NETMANAGER_EXT_SUCCESS) {
138 context->SetErrorCode(result);
139 return false;
140 }
141 return true;
142 }
143
ExecGetSysVpnConfigList(GetListContext * context)144 bool ExecGetSysVpnConfigList(GetListContext *context)
145 {
146 if (context == nullptr) {
147 NETMANAGER_EXT_LOGE("context is nullptr");
148 return false;
149 }
150 int32_t result = NetworkVpnClient::GetInstance().GetSysVpnConfigList(context->vpnList_);
151 if (result != NETMANAGER_EXT_SUCCESS) {
152 context->SetErrorCode(result);
153 return false;
154 }
155 return true;
156 }
157
ExecGetSysVpnConfig(GetContext * context)158 bool ExecGetSysVpnConfig(GetContext *context)
159 {
160 if (context == nullptr) {
161 NETMANAGER_EXT_LOGE("context is nullptr");
162 return false;
163 }
164 int32_t result = NetworkVpnClient::GetInstance().GetSysVpnConfig(context->vpnConfig_, context->vpnId_);
165 if (result != NETMANAGER_EXT_SUCCESS) {
166 context->SetErrorCode(result);
167 return false;
168 }
169 return true;
170 }
171
ExecGetConnectedSysVpnConfig(GetConnectedContext * context)172 bool ExecGetConnectedSysVpnConfig(GetConnectedContext *context)
173 {
174 if (context == nullptr) {
175 NETMANAGER_EXT_LOGE("context is nullptr");
176 return false;
177 }
178 int32_t result = NetworkVpnClient::GetInstance().GetConnectedSysVpnConfig(context->vpnConfig_);
179 if (result != NETMANAGER_EXT_SUCCESS) {
180 context->SetErrorCode(result);
181 return false;
182 }
183 return true;
184 }
185 #endif // SUPPORT_SYSVPN
186
PrepareCallback(PrepareContext * context)187 napi_value PrepareCallback(PrepareContext *context)
188 {
189 napi_value obj = NapiUtils::CreateObject(context->GetEnv());
190 NapiUtils::SetBooleanProperty(context->GetEnv(), obj, "isExistVpn", context->isExistVpn_);
191 NapiUtils::SetBooleanProperty(context->GetEnv(), obj, "isRun", context->isRun_);
192 NapiUtils::SetStringPropertyUtf8(context->GetEnv(), obj, "package", context->package_);
193 return obj;
194 }
195
SetUpCallback(SetUpContext * context)196 napi_value SetUpCallback(SetUpContext *context)
197 {
198 return NapiUtils::CreateInt32(context->GetEnv(), context->fd_);
199 }
200
ProtectCallback(ProtectContext * context)201 napi_value ProtectCallback(ProtectContext *context)
202 {
203 return NapiUtils::GetUndefined(context->GetEnv());
204 }
205
DestroyCallback(DestroyContext * context)206 napi_value DestroyCallback(DestroyContext *context)
207 {
208 return NapiUtils::GetUndefined(context->GetEnv());
209 }
210
211 #ifdef SUPPORT_SYSVPN
AddSysVpnConfigCallback(AddContext * context)212 napi_value AddSysVpnConfigCallback(AddContext *context)
213 {
214 if (context == nullptr) {
215 NETMANAGER_EXT_LOGE("context is nullptr");
216 return nullptr;
217 }
218 return NapiUtils::GetUndefined(context->GetEnv());
219 }
220
DeleteSysVpnConfigCallback(DeleteContext * context)221 napi_value DeleteSysVpnConfigCallback(DeleteContext *context)
222 {
223 if (context == nullptr) {
224 NETMANAGER_EXT_LOGE("context is nullptr");
225 return nullptr;
226 }
227 return NapiUtils::GetUndefined(context->GetEnv());
228 }
229
GetSysVpnConfigCallback(GetContext * context)230 napi_value GetSysVpnConfigCallback(GetContext *context)
231 {
232 if (context == nullptr) {
233 NETMANAGER_EXT_LOGE("context is nullptr");
234 return nullptr;
235 }
236 return VpnConfigUtils::CreateNapiVpnConfig(context->GetEnv(), context->vpnConfig_);
237 }
238
GetSysVpnConfigListCallback(GetListContext * context)239 napi_value GetSysVpnConfigListCallback(GetListContext *context)
240 {
241 if (context == nullptr) {
242 NETMANAGER_EXT_LOGE("context is nullptr");
243 return nullptr;
244 }
245 int32_t index = 0;
246 auto len = context->vpnList_.size();
247 napi_value array = NapiUtils::CreateArray(context->GetEnv(), len);
248 for (const auto &info : context->vpnList_) {
249 napi_value config = NapiUtils::CreateObject(context->GetEnv());
250 NapiUtils::SetStringPropertyUtf8(context->GetEnv(), config, VpnConfigUtils::CONFIG_VPN_ID, info.vpnId_);
251 NapiUtils::SetStringPropertyUtf8(context->GetEnv(), config, VpnConfigUtils::CONFIG_VPN_NAME, info.vpnName_);
252 NapiUtils::SetArrayElement(context->GetEnv(), array, index, config);
253 ++index;
254 }
255 return array;
256 }
257
GetConnectedSysVpnConfigCallback(GetConnectedContext * context)258 napi_value GetConnectedSysVpnConfigCallback(GetConnectedContext *context)
259 {
260 if (context == nullptr) {
261 NETMANAGER_EXT_LOGE("context is nullptr");
262 return nullptr;
263 }
264 return VpnConfigUtils::CreateNapiVpnConfig(context->GetEnv(), context->vpnConfig_);
265 }
266 #endif // SUPPORT_SYSVPN
267 } // namespace VpnExec
268 } // namespace NetManagerStandard
269 } // namespace OHOS