• 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_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 #include "hi_app_event_report.h"
29 #ifdef SUPPORT_SYSVPN
30 #include "uuid.h"
31 #endif // SUPPORT_SYSVPN
32 
33 namespace OHOS {
34 namespace NetManagerStandard {
35 namespace VpnExecExt {
GetVpnConnectionInstance(ContextT * context)36 template <typename ContextT> static inline NetworkVpnClient *GetVpnConnectionInstance(ContextT *context)
37 {
38     if (context == nullptr) {
39         return nullptr;
40     }
41     auto manager = context->GetManager();
42     return (manager == nullptr) ? nullptr : reinterpret_cast<NetworkVpnClient *>(manager->GetData());
43 }
44 
ExecPrepare(PrepareContext * context)45 bool ExecPrepare(PrepareContext *context)
46 {
47     auto vpnClient = GetVpnConnectionInstance(context);
48     if (vpnClient == nullptr) {
49         NETMANAGER_EXT_LOGE("vpnClient is nullptr");
50         return false;
51     }
52     int32_t result = vpnClient->Prepare(context->isExistVpn_, context->isRun_, context->package_);
53     if (result != NETMANAGER_EXT_SUCCESS) {
54         context->SetErrorCode(result);
55         return false;
56     }
57     return true;
58 }
59 
ExecSetUp(SetUpContext * context)60 bool ExecSetUp(SetUpContext *context)
61 {
62     if (context == nullptr) {
63         NETMANAGER_EXT_LOGE("context is nullptr");
64         return false;
65     }
66 
67     HiAppEventReport hiAppEventReport("NetworkKit", "VpnSetUp");
68     auto vpnClient = GetVpnConnectionInstance(context);
69     if (vpnClient == nullptr) {
70         NETMANAGER_EXT_LOGE("vpnClient is nullptr");
71         return false;
72     }
73 
74     int32_t result = NETMANAGER_EXT_SUCCESS;
75 #ifdef SUPPORT_SYSVPN
76     if (context->sysVpnConfig_ != nullptr) {
77         result = vpnClient->SetUpVpn(context->sysVpnConfig_, true);
78     } else {
79         result = vpnClient->SetUpVpn(context->vpnConfig_, context->fd_, true);
80     }
81 #else
82     result = vpnClient->SetUpVpn(context->vpnConfig_, context->fd_, true);
83 #endif // SUPPORT_SYSVPN
84     if (result != NETMANAGER_EXT_SUCCESS) {
85         context->SetErrorCode(result);
86         hiAppEventReport.ReportSdkEvent(RESULT_SUCCESS, result);
87         return false;
88     }
89     hiAppEventReport.ReportSdkEvent(RESULT_SUCCESS, result);
90     return true;
91 }
92 
ExecProtect(ProtectContext * context)93 bool ExecProtect(ProtectContext *context)
94 {
95     HiAppEventReport hiAppEventReport("NetworkKit", "VpnProtect");
96     auto vpnClient = GetVpnConnectionInstance(context);
97     if (vpnClient == nullptr) {
98         NETMANAGER_EXT_LOGE("vpnClient is nullptr");
99         return false;
100     }
101     int32_t result = vpnClient->Protect(context->socketFd_, true);
102     if (result != NETMANAGER_EXT_SUCCESS) {
103         context->SetErrorCode(result);
104         hiAppEventReport.ReportSdkEvent(RESULT_SUCCESS, result);
105         return false;
106     }
107     hiAppEventReport.ReportSdkEvent(RESULT_SUCCESS, result);
108     return true;
109 }
110 
ExecDestroy(DestroyContext * context)111 bool ExecDestroy(DestroyContext *context)
112 {
113     HiAppEventReport hiAppEventReport("NetworkKit", "VpnDestroy");
114     auto vpnClient = GetVpnConnectionInstance(context);
115     if (vpnClient == nullptr) {
116         NETMANAGER_EXT_LOGE("vpnClient is nullptr");
117         return false;
118     }
119     int32_t result = 0;
120 #ifdef SUPPORT_SYSVPN
121     if (!context->vpnId_.empty()) {
122         result = vpnClient->DestroyVpn(context->vpnId_);
123     } else {
124         result = vpnClient->DestroyVpn(true);
125     }
126 #else
127     result = vpnClient->DestroyVpn(true);
128 #endif // SUPPORT_SYSVPN
129     if (result != NETMANAGER_EXT_SUCCESS) {
130         context->SetErrorCode(result);
131         hiAppEventReport.ReportSdkEvent(RESULT_SUCCESS, result);
132         return false;
133     }
134     hiAppEventReport.ReportSdkEvent(RESULT_SUCCESS, ERR_NONE);
135     return true;
136 }
137 
138 #ifdef SUPPORT_SYSVPN
ExecGenerateVpnId(GenerateVpnIdContext * context)139 bool ExecGenerateVpnId(GenerateVpnIdContext *context)
140 {
141     if (context == nullptr) {
142         NETMANAGER_EXT_LOGE("context is nullptr");
143         return false;
144     }
145     context->vpnId_ = UUID::RandomUUID().ToString();
146     return true;
147 }
148 #endif // SUPPORT_SYSVPN
149 
PrepareCallback(PrepareContext * context)150 napi_value PrepareCallback(PrepareContext *context)
151 {
152     napi_value obj = NapiUtils::CreateObject(context->GetEnv());
153     NapiUtils::SetBooleanProperty(context->GetEnv(), obj, "isExistVpn", context->isExistVpn_);
154     NapiUtils::SetBooleanProperty(context->GetEnv(), obj, "isRun", context->isRun_);
155     NapiUtils::SetStringPropertyUtf8(context->GetEnv(), obj, "package", context->package_);
156     return obj;
157 }
158 
SetUpCallback(SetUpContext * context)159 napi_value SetUpCallback(SetUpContext *context)
160 {
161     return NapiUtils::CreateInt32(context->GetEnv(), context->fd_);
162 }
163 
ProtectCallback(ProtectContext * context)164 napi_value ProtectCallback(ProtectContext *context)
165 {
166     return NapiUtils::GetUndefined(context->GetEnv());
167 }
168 
DestroyCallback(DestroyContext * context)169 napi_value DestroyCallback(DestroyContext *context)
170 {
171     return NapiUtils::GetUndefined(context->GetEnv());
172 }
173 #ifdef SUPPORT_SYSVPN
GenerateVpnIdCallback(GenerateVpnIdContext * context)174 napi_value GenerateVpnIdCallback(GenerateVpnIdContext *context)
175 {
176     return NapiUtils::CreateStringUtf8(context->GetEnv(), context->vpnId_);
177 }
178 #endif // SUPPORT_SYSVPN
179 } // namespace VpnExecExt
180 } // namespace NetManagerStandard
181 } // namespace OHOS