• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
ExecGetConnectedVpnAppInfo(GetAppInfoContext * context)186 bool ExecGetConnectedVpnAppInfo(GetAppInfoContext *context)
187 {
188     if (context == nullptr) {
189         NETMANAGER_EXT_LOGE("context is nullptr");
190         return false;
191     }
192     int32_t result = NetworkVpnClient::GetInstance().GetConnectedVpnAppInfo(context->bundleNameList_);
193     if (result != NETMANAGER_EXT_SUCCESS) {
194         context->SetErrorCode(result);
195         return false;
196     }
197     return true;
198 }
199 #endif // SUPPORT_SYSVPN
200 
PrepareCallback(PrepareContext * context)201 napi_value PrepareCallback(PrepareContext *context)
202 {
203     napi_value obj = NapiUtils::CreateObject(context->GetEnv());
204     NapiUtils::SetBooleanProperty(context->GetEnv(), obj, "isExistVpn", context->isExistVpn_);
205     NapiUtils::SetBooleanProperty(context->GetEnv(), obj, "isRun", context->isRun_);
206     NapiUtils::SetStringPropertyUtf8(context->GetEnv(), obj, "package", context->package_);
207     return obj;
208 }
209 
SetUpCallback(SetUpContext * context)210 napi_value SetUpCallback(SetUpContext *context)
211 {
212     return NapiUtils::CreateInt32(context->GetEnv(), context->fd_);
213 }
214 
ProtectCallback(ProtectContext * context)215 napi_value ProtectCallback(ProtectContext *context)
216 {
217     return NapiUtils::GetUndefined(context->GetEnv());
218 }
219 
DestroyCallback(DestroyContext * context)220 napi_value DestroyCallback(DestroyContext *context)
221 {
222     return NapiUtils::GetUndefined(context->GetEnv());
223 }
224 
225 #ifdef SUPPORT_SYSVPN
AddSysVpnConfigCallback(AddContext * context)226 napi_value AddSysVpnConfigCallback(AddContext *context)
227 {
228     if (context == nullptr) {
229         NETMANAGER_EXT_LOGE("context is nullptr");
230         return nullptr;
231     }
232     return NapiUtils::GetUndefined(context->GetEnv());
233 }
234 
DeleteSysVpnConfigCallback(DeleteContext * context)235 napi_value DeleteSysVpnConfigCallback(DeleteContext *context)
236 {
237     if (context == nullptr) {
238         NETMANAGER_EXT_LOGE("context is nullptr");
239         return nullptr;
240     }
241     return NapiUtils::GetUndefined(context->GetEnv());
242 }
243 
GetSysVpnConfigCallback(GetContext * context)244 napi_value GetSysVpnConfigCallback(GetContext *context)
245 {
246     if (context == nullptr) {
247         NETMANAGER_EXT_LOGE("context is nullptr");
248         return nullptr;
249     }
250     return VpnConfigUtils::CreateNapiVpnConfig(context->GetEnv(), context->vpnConfig_);
251 }
252 
GetSysVpnConfigListCallback(GetListContext * context)253 napi_value GetSysVpnConfigListCallback(GetListContext *context)
254 {
255     if (context == nullptr) {
256         NETMANAGER_EXT_LOGE("context is nullptr");
257         return nullptr;
258     }
259     int32_t index = 0;
260     auto len = context->vpnList_.size();
261     napi_value array = NapiUtils::CreateArray(context->GetEnv(), len);
262     for (const auto &info : context->vpnList_) {
263         napi_value config = NapiUtils::CreateObject(context->GetEnv());
264         NapiUtils::SetStringPropertyUtf8(context->GetEnv(), config, VpnConfigUtils::CONFIG_VPN_ID, info->vpnId_);
265         NapiUtils::SetStringPropertyUtf8(context->GetEnv(), config, VpnConfigUtils::CONFIG_VPN_NAME, info->vpnName_);
266         NapiUtils::SetArrayElement(context->GetEnv(), array, index, config);
267         ++index;
268     }
269     return array;
270 }
271 
GetConnectedSysVpnConfigCallback(GetConnectedContext * context)272 napi_value GetConnectedSysVpnConfigCallback(GetConnectedContext *context)
273 {
274     if (context == nullptr) {
275         NETMANAGER_EXT_LOGE("context is nullptr");
276         return nullptr;
277     }
278     return VpnConfigUtils::CreateNapiVpnConfig(context->GetEnv(), context->vpnConfig_);
279 }
280 
GetConnectedVpnAppInfoCallback(GetAppInfoContext * context)281 napi_value GetConnectedVpnAppInfoCallback(GetAppInfoContext *context)
282 {
283     if (context == nullptr) {
284         NETMANAGER_EXT_LOGE("context is nullptr");
285         return nullptr;
286     }
287     int32_t index = 0;
288     auto len = context->bundleNameList_.size();
289     napi_value array = NapiUtils::CreateArray(context->GetEnv(), len);
290     for (const auto &info : context->bundleNameList_) {
291         napi_value bundleName = NapiUtils::CreateStringUtf8(context->GetEnv(), info);
292         NapiUtils::SetArrayElement(context->GetEnv(), array, index, bundleName);
293         ++index;
294     }
295     return array;
296 }
297 #endif // SUPPORT_SYSVPN
298 } // namespace VpnExec
299 } // namespace NetManagerStandard
300 } // namespace OHOS