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_ext.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 #include "want.h"
26 #include "ability_manager_client.h"
27 #include "extension_ability_info.h"
28
29 namespace OHOS {
30 namespace NetManagerStandard {
31 namespace VpnExecExt {
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 = vpnClient->SetUpVpn(context->vpnConfig_, context->fd_, true);
61 if (result != NETMANAGER_EXT_SUCCESS) {
62 context->SetErrorCode(result);
63 return false;
64 }
65 return true;
66 }
67
ExecProtect(ProtectContext * context)68 bool ExecProtect(ProtectContext *context)
69 {
70 auto vpnClient = GetVpnConnectionInstance(context);
71 if (vpnClient == nullptr) {
72 NETMANAGER_EXT_LOGE("vpnClient is nullptr");
73 return false;
74 }
75 int32_t result = vpnClient->Protect(context->socketFd_, true);
76 if (result != NETMANAGER_EXT_SUCCESS) {
77 context->SetErrorCode(result);
78 return false;
79 }
80 return true;
81 }
82
ExecDestroy(DestroyContext * context)83 bool ExecDestroy(DestroyContext *context)
84 {
85 auto vpnClient = GetVpnConnectionInstance(context);
86 if (vpnClient == nullptr) {
87 NETMANAGER_EXT_LOGE("vpnClient is nullptr");
88 return false;
89 }
90 int32_t result = vpnClient->DestroyVpn(true);
91 if (result != NETMANAGER_EXT_SUCCESS) {
92 context->SetErrorCode(result);
93 return false;
94 }
95 return true;
96 }
97
PrepareCallback(PrepareContext * context)98 napi_value PrepareCallback(PrepareContext *context)
99 {
100 napi_value obj = NapiUtils::CreateObject(context->GetEnv());
101 NapiUtils::SetBooleanProperty(context->GetEnv(), obj, "isExistVpn", context->isExistVpn_);
102 NapiUtils::SetBooleanProperty(context->GetEnv(), obj, "isRun", context->isRun_);
103 NapiUtils::SetStringPropertyUtf8(context->GetEnv(), obj, "package", context->package_);
104 return obj;
105 }
106
SetUpCallback(SetUpContext * context)107 napi_value SetUpCallback(SetUpContext *context)
108 {
109 return NapiUtils::CreateInt32(context->GetEnv(), context->fd_);
110 }
111
ProtectCallback(ProtectContext * context)112 napi_value ProtectCallback(ProtectContext *context)
113 {
114 return NapiUtils::GetUndefined(context->GetEnv());
115 }
116
DestroyCallback(DestroyContext * context)117 napi_value DestroyCallback(DestroyContext *context)
118 {
119 return NapiUtils::GetUndefined(context->GetEnv());
120 }
121 } // namespace VpnExecExt
122 } // namespace NetManagerStandard
123 } // namespace OHOS