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 auto manager = context->GetManager();
35 return (manager == nullptr) ? nullptr : reinterpret_cast<NetworkVpnClient *>(manager->GetData());
36 }
37
ExecPrepare(PrepareContext *context)38 bool ExecPrepare(PrepareContext *context)
39 {
40 auto vpnClient = GetVpnConnectionInstance(context);
41 if (vpnClient == nullptr) {
42 NETMANAGER_EXT_LOGE("vpnClient is nullptr");
43 return false;
44 }
45 int32_t result = vpnClient->Prepare(context->isExistVpn_, context->isRun_, context->package_);
46 if (result != NETMANAGER_EXT_SUCCESS) {
47 context->SetErrorCode(result);
48 return false;
49 }
50 return true;
51 }
52
ExecSetUp(SetUpContext *context)53 bool ExecSetUp(SetUpContext *context)
54 {
55 auto vpnClient = GetVpnConnectionInstance(context);
56 if (vpnClient == nullptr) {
57 NETMANAGER_EXT_LOGE("vpnClient is nullptr");
58 return false;
59 }
60 int32_t result = NETMANAGER_EXT_SUCCESS;
61 #ifdef SUPPORT_SYSVPN
62 if (context == nullptr) {
63 NETMANAGER_EXT_LOGE("context is nullptr");
64 return false;
65 }
66 if (context->sysVpnConfig_ != nullptr) {
67 // is system vpn
68 result = vpnClient->SetUpVpn(context->sysVpnConfig_);
69 } else {
70 result = vpnClient->SetUpVpn(context->vpnConfig_, context->fd_);
71 }
72 #else
73 result = vpnClient->SetUpVpn(context->vpnConfig_, context->fd_);
74 #endif // SUPPORT_SYSVPN
75 if (result != NETMANAGER_EXT_SUCCESS) {
76 context->SetErrorCode(result);
77 return false;
78 }
79 return true;
80 }
81
ExecProtect(ProtectContext *context)82 bool ExecProtect(ProtectContext *context)
83 {
84 auto vpnClient = GetVpnConnectionInstance(context);
85 if (vpnClient == nullptr) {
86 NETMANAGER_EXT_LOGE("vpnClient is nullptr");
87 return false;
88 }
89 int32_t result = vpnClient->Protect(context->socketFd_);
90 if (result != NETMANAGER_EXT_SUCCESS) {
91 context->SetErrorCode(result);
92 return false;
93 }
94 return true;
95 }
96
ExecDestroy(DestroyContext *context)97 bool ExecDestroy(DestroyContext *context)
98 {
99 auto vpnClient = GetVpnConnectionInstance(context);
100 if (vpnClient == nullptr) {
101 NETMANAGER_EXT_LOGE("vpnClient is nullptr");
102 return false;
103 }
104 int32_t result = vpnClient->DestroyVpn();
105 if (result != NETMANAGER_EXT_SUCCESS) {
106 context->SetErrorCode(result);
107 return false;
108 }
109 return true;
110 }
111
112 #ifdef SUPPORT_SYSVPN
ExecAddSysVpnConfig(AddContext *context)113 bool ExecAddSysVpnConfig(AddContext *context)
114 {
115 if (context == nullptr) {
116 NETMANAGER_EXT_LOGE("context is nullptr");
117 return false;
118 }
119 int32_t result = NetworkVpnClient::GetInstance().AddSysVpnConfig(context->vpnConfig_);
120 if (result != NETMANAGER_EXT_SUCCESS) {
121 context->SetErrorCode(result);
122 return false;
123 }
124 return true;
125 }
126
ExecDeleteSysVpnConfig(DeleteContext *context)127 bool ExecDeleteSysVpnConfig(DeleteContext *context)
128 {
129 if (context == nullptr) {
130 NETMANAGER_EXT_LOGE("context is nullptr");
131 return false;
132 }
133 int32_t result = NetworkVpnClient::GetInstance().DeleteSysVpnConfig(context->vpnId_);
134 if (result != NETMANAGER_EXT_SUCCESS) {
135 context->SetErrorCode(result);
136 return false;
137 }
138 return true;
139 }
140
ExecGetSysVpnConfigList(GetListContext *context)141 bool ExecGetSysVpnConfigList(GetListContext *context)
142 {
143 if (context == nullptr) {
144 NETMANAGER_EXT_LOGE("context is nullptr");
145 return false;
146 }
147 int32_t result = NetworkVpnClient::GetInstance().GetSysVpnConfigList(context->vpnList_);
148 if (result != NETMANAGER_EXT_SUCCESS) {
149 context->SetErrorCode(result);
150 return false;
151 }
152 return true;
153 }
154
ExecGetSysVpnConfig(GetContext *context)155 bool ExecGetSysVpnConfig(GetContext *context)
156 {
157 if (context == nullptr) {
158 NETMANAGER_EXT_LOGE("context is nullptr");
159 return false;
160 }
161 int32_t result = NetworkVpnClient::GetInstance().GetSysVpnConfig(context->vpnConfig_, context->vpnId_);
162 if (result != NETMANAGER_EXT_SUCCESS) {
163 context->SetErrorCode(result);
164 return false;
165 }
166 return true;
167 }
168
ExecGetConnectedSysVpnConfig(GetConnectedContext *context)169 bool ExecGetConnectedSysVpnConfig(GetConnectedContext *context)
170 {
171 if (context == nullptr) {
172 NETMANAGER_EXT_LOGE("context is nullptr");
173 return false;
174 }
175 int32_t result = NetworkVpnClient::GetInstance().GetConnectedSysVpnConfig(context->vpnConfig_);
176 if (result != NETMANAGER_EXT_SUCCESS) {
177 context->SetErrorCode(result);
178 return false;
179 }
180 return true;
181 }
182 #endif // SUPPORT_SYSVPN
183
PrepareCallback(PrepareContext *context)184 napi_value PrepareCallback(PrepareContext *context)
185 {
186 napi_value obj = NapiUtils::CreateObject(context->GetEnv());
187 NapiUtils::SetBooleanProperty(context->GetEnv(), obj, "isExistVpn", context->isExistVpn_);
188 NapiUtils::SetBooleanProperty(context->GetEnv(), obj, "isRun", context->isRun_);
189 NapiUtils::SetStringPropertyUtf8(context->GetEnv(), obj, "package", context->package_);
190 return obj;
191 }
192
SetUpCallback(SetUpContext *context)193 napi_value SetUpCallback(SetUpContext *context)
194 {
195 return NapiUtils::CreateInt32(context->GetEnv(), context->fd_);
196 }
197
ProtectCallback(ProtectContext *context)198 napi_value ProtectCallback(ProtectContext *context)
199 {
200 return NapiUtils::GetUndefined(context->GetEnv());
201 }
202
DestroyCallback(DestroyContext *context)203 napi_value DestroyCallback(DestroyContext *context)
204 {
205 return NapiUtils::GetUndefined(context->GetEnv());
206 }
207
208 #ifdef SUPPORT_SYSVPN
AddSysVpnConfigCallback(AddContext *context)209 napi_value AddSysVpnConfigCallback(AddContext *context)
210 {
211 if (context == nullptr) {
212 NETMANAGER_EXT_LOGE("context is nullptr");
213 return nullptr;
214 }
215 return NapiUtils::GetUndefined(context->GetEnv());
216 }
217
DeleteSysVpnConfigCallback(DeleteContext *context)218 napi_value DeleteSysVpnConfigCallback(DeleteContext *context)
219 {
220 if (context == nullptr) {
221 NETMANAGER_EXT_LOGE("context is nullptr");
222 return nullptr;
223 }
224 return NapiUtils::GetUndefined(context->GetEnv());
225 }
226
GetSysVpnConfigCallback(GetContext *context)227 napi_value GetSysVpnConfigCallback(GetContext *context)
228 {
229 if (context == nullptr) {
230 NETMANAGER_EXT_LOGE("context is nullptr");
231 return nullptr;
232 }
233 return VpnConfigUtils::CreateNapiVpnConfig(context->GetEnv(), context->vpnConfig_);
234 }
235
GetSysVpnConfigListCallback(GetListContext *context)236 napi_value GetSysVpnConfigListCallback(GetListContext *context)
237 {
238 if (context == nullptr) {
239 NETMANAGER_EXT_LOGE("context is nullptr");
240 return nullptr;
241 }
242 int32_t index = 0;
243 auto len = context->vpnList_.size();
244 napi_value array = NapiUtils::CreateArray(context->GetEnv(), len);
245 for (const auto &info : context->vpnList_) {
246 napi_value config = NapiUtils::CreateObject(context->GetEnv());
247 NapiUtils::SetStringPropertyUtf8(context->GetEnv(), config, VpnConfigUtils::CONFIG_VPN_ID, info.vpnId_);
248 NapiUtils::SetStringPropertyUtf8(context->GetEnv(), config, VpnConfigUtils::CONFIG_VPN_NAME, info.vpnName_);
249 NapiUtils::SetArrayElement(context->GetEnv(), array, index, config);
250 ++index;
251 }
252 return array;
253 }
254
GetConnectedSysVpnConfigCallback(GetConnectedContext *context)255 napi_value GetConnectedSysVpnConfigCallback(GetConnectedContext *context)
256 {
257 if (context == nullptr) {
258 NETMANAGER_EXT_LOGE("context is nullptr");
259 return nullptr;
260 }
261 return VpnConfigUtils::CreateNapiVpnConfig(context->GetEnv(), context->vpnConfig_);
262 }
263 #endif // SUPPORT_SYSVPN
264 } // namespace VpnExec
265 } // namespace NetManagerStandard
266 } // namespace OHOS