1 /*
2 * Copyright (c) 2024 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 <cstdint>
17 #include <securec.h>
18
19 #include "napi_utils.h"
20 #include "net_firewall_exec.h"
21 #include "net_firewall_rule_parse.h"
22 #include "net_manager_constants.h"
23 #include "netmanager_ext_log.h"
24 #include "netfirewall_client.h"
25 #include "singleton.h"
26 #include "hi_app_event_report.h"
27
28 namespace OHOS {
29 namespace NetManagerStandard {
30 namespace NetFirewallExec {
GetNetFirewallInstance(ContextT * context)31 template <typename ContextT> static inline NetFirewallClient *GetNetFirewallInstance(ContextT *context)
32 {
33 if (context == nullptr) {
34 return nullptr;
35 }
36 auto manager = context->GetManager();
37 return (manager == nullptr) ? nullptr : reinterpret_cast<NetFirewallClient *>(manager->GetData());
38 }
39
ExecSetNetFirewallPolicy(SetNetFirewallPolicyContext * context)40 bool ExecSetNetFirewallPolicy(SetNetFirewallPolicyContext *context)
41 {
42 HiAppEventReport hiAppEventReport("NetworkKit", "NetFirewallSetNetFirewallPolicy");
43 if (context == nullptr || context->status_ == nullptr) {
44 return false;
45 }
46 if (!context->IsParseOK()) {
47 return false;
48 }
49 int32_t result =
50 DelayedSingleton<NetFirewallClient>::GetInstance()->SetNetFirewallPolicy(context->userId_, context->status_);
51 if (result != FIREWALL_SUCCESS) {
52 NETMANAGER_EXT_LOGE("ExecSetIfaceConfig error, errorCode: %{public}d", result);
53 context->SetErrorCode(result);
54 hiAppEventReport.ReportSdkEvent(RESULT_SUCCESS, result);
55 return false;
56 }
57 hiAppEventReport.ReportSdkEvent(RESULT_SUCCESS, ERR_NONE);
58 return true;
59 }
60
SetNetFirewallPolicyCallback(SetNetFirewallPolicyContext * context)61 napi_value SetNetFirewallPolicyCallback(SetNetFirewallPolicyContext *context)
62 {
63 if (context == nullptr) {
64 return nullptr;
65 }
66 return NapiUtils::GetUndefined(context->GetEnv());
67 }
68
ExecGetNetFirewallPolicy(GetNetFirewallPolicyContext * context)69 bool ExecGetNetFirewallPolicy(GetNetFirewallPolicyContext *context)
70 {
71 if (context == nullptr) {
72 return false;
73 }
74 if (!context->IsParseOK()) {
75 return false;
76 }
77 int32_t result =
78 DelayedSingleton<NetFirewallClient>::GetInstance()->GetNetFirewallPolicy(context->userId_, context->status_);
79 if (result != FIREWALL_SUCCESS || context->status_ == nullptr) {
80 NETMANAGER_EXT_LOGE("ExecGetIfaceConfig error, errorCode: %{public}d", result);
81 context->SetErrorCode(result);
82 return false;
83 }
84 return true;
85 }
86
GetNetFirewallPolicyCallback(GetNetFirewallPolicyContext * context)87 napi_value GetNetFirewallPolicyCallback(GetNetFirewallPolicyContext *context)
88 {
89 if (context == nullptr || context->status_ == nullptr) {
90 return nullptr;
91 }
92 napi_value firewallStatus = NapiUtils::CreateObject(context->GetEnv());
93
94 NapiUtils::SetBooleanProperty(context->GetEnv(), firewallStatus, NET_FIREWALL_IS_OPEN, context->status_->isOpen);
95 NapiUtils::SetInt32Property(context->GetEnv(), firewallStatus, NET_FIREWALL_IN_ACTION,
96 static_cast<int32_t>(context->status_->inAction));
97 NapiUtils::SetInt32Property(context->GetEnv(), firewallStatus, NET_FIREWALL_OUT_ACTION,
98 static_cast<int32_t>(context->status_->outAction));
99 return firewallStatus;
100 }
101
ExecAddNetFirewallRule(AddNetFirewallRuleContext * context)102 bool ExecAddNetFirewallRule(AddNetFirewallRuleContext *context)
103 {
104 HiAppEventReport hiAppEventReport("NetworkKit", "NetFirewallAddNetFirewallRule");
105 if (context == nullptr || context->rule_ == nullptr) {
106 return false;
107 }
108 if (!context->IsParseOK()) {
109 return false;
110 }
111 int32_t result =
112 DelayedSingleton<NetFirewallClient>::GetInstance()->AddNetFirewallRule(context->rule_, context->reslut_);
113 if (result != FIREWALL_SUCCESS) {
114 NETMANAGER_EXT_LOGE("ExecAddNetFirewallRule error, errorCode: %{public}d", result);
115 context->SetErrorCode(result);
116 hiAppEventReport.ReportSdkEvent(RESULT_SUCCESS, result);
117 return false;
118 }
119 hiAppEventReport.ReportSdkEvent(RESULT_SUCCESS, ERR_NONE);
120 return true;
121 }
122
AddNetFirewallRuleCallback(AddNetFirewallRuleContext * context)123 napi_value AddNetFirewallRuleCallback(AddNetFirewallRuleContext *context)
124 {
125 if (context == nullptr) {
126 return nullptr;
127 }
128 // return basic data type
129 return NapiUtils::CreateInt32(context->GetEnv(), context->reslut_);
130 }
131
ExecUpdateNetFirewallRule(UpdateNetFirewallRuleContext * context)132 bool ExecUpdateNetFirewallRule(UpdateNetFirewallRuleContext *context)
133 {
134 HiAppEventReport hiAppEventReport("NetworkKit", "FirewallUpdateNetFirewallRule");
135 if (context == nullptr || context->rule_ == nullptr) {
136 return false;
137 }
138 if (!context->IsParseOK()) {
139 return false;
140 }
141 int32_t result = DelayedSingleton<NetFirewallClient>::GetInstance()->UpdateNetFirewallRule(context->rule_);
142 if (result != FIREWALL_SUCCESS) {
143 NETMANAGER_EXT_LOGE("ExecUpdateNetFirewallRule error, errorCode: %{public}d", result);
144 context->SetErrorCode(result);
145 hiAppEventReport.ReportSdkEvent(RESULT_SUCCESS, result);
146 return false;
147 }
148 hiAppEventReport.ReportSdkEvent(RESULT_SUCCESS, ERR_NONE);
149 return true;
150 }
151
UpdateNetFirewallRuleCallback(UpdateNetFirewallRuleContext * context)152 napi_value UpdateNetFirewallRuleCallback(UpdateNetFirewallRuleContext *context)
153 {
154 if (context == nullptr) {
155 return nullptr;
156 }
157 // return basic data type
158 return NapiUtils::GetUndefined(context->GetEnv());
159 }
160
ExecDeleteNetFirewallRule(DeleteNetFirewallRuleContext * context)161 bool ExecDeleteNetFirewallRule(DeleteNetFirewallRuleContext *context)
162 {
163 HiAppEventReport hiAppEventReport("NetworkKit", "NetFirewallremoveNetFirewallRule");
164 if (context == nullptr) {
165 return false;
166 }
167 if (!context->IsParseOK()) {
168 return false;
169 }
170 int32_t result =
171 DelayedSingleton<NetFirewallClient>::GetInstance()->DeleteNetFirewallRule(context->userId_, context->ruleId_);
172 if (result != FIREWALL_SUCCESS) {
173 NETMANAGER_EXT_LOGE("ExecDeleteNetFirewallRule error, errorCode: %{public}d", result);
174 context->SetErrorCode(result);
175 hiAppEventReport.ReportSdkEvent(RESULT_SUCCESS, result);
176 return false;
177 }
178 hiAppEventReport.ReportSdkEvent(RESULT_SUCCESS, ERR_NONE);
179 return true;
180 }
181
DeleteNetFirewallRuleCallback(DeleteNetFirewallRuleContext * context)182 napi_value DeleteNetFirewallRuleCallback(DeleteNetFirewallRuleContext *context)
183 {
184 if (context == nullptr) {
185 return nullptr;
186 }
187 // return basic data type
188 return NapiUtils::GetUndefined(context->GetEnv());
189 }
190
ExecGetNetFirewallRules(GetNetFirewallRulesContext * context)191 bool ExecGetNetFirewallRules(GetNetFirewallRulesContext *context)
192 {
193 if (context == nullptr || context->requestParam_ == nullptr) {
194 return false;
195 }
196 if (!context->IsParseOK()) {
197 return false;
198 }
199 int32_t result = DelayedSingleton<NetFirewallClient>::GetInstance()->GetNetFirewallRules(context->userId_,
200 context->requestParam_, context->pageInfo_);
201 if (result != FIREWALL_SUCCESS || context->pageInfo_ == nullptr) {
202 NETMANAGER_EXT_LOGE("ExecGetNetFirewallRules error, errorCode: %{public}d", result);
203 context->SetErrorCode(result);
204 return false;
205 }
206 return true;
207 }
208
GetNetFirewallRulesCallback(GetNetFirewallRulesContext * context)209 napi_value GetNetFirewallRulesCallback(GetNetFirewallRulesContext *context)
210 {
211 if (context == nullptr || context->pageInfo_ == nullptr) {
212 return nullptr;
213 }
214 napi_value pageInfo = NapiUtils::CreateObject(context->GetEnv());
215 if (pageInfo == nullptr) {
216 return nullptr;
217 }
218 NapiUtils::SetInt32Property(context->GetEnv(), pageInfo, NET_FIREWALL_PAGE, context->pageInfo_->page);
219 NapiUtils::SetInt32Property(context->GetEnv(), pageInfo, NET_FIREWALL_PAGE_SIZE, context->pageInfo_->pageSize);
220 NapiUtils::SetInt32Property(context->GetEnv(), pageInfo, NET_FIREWALL_TOTAL_PAGE, context->pageInfo_->totalPage);
221 napi_value list = NapiUtils::CreateArray(context->GetEnv(), context->pageInfo_->data.size());
222 uint32_t index = 0;
223 for (const auto &iface : context->pageInfo_->data) {
224 napi_value rule = NapiUtils::CreateObject(context->GetEnv());
225 NetFirewallRuleParse::SetRuleParams(context->GetEnv(), rule, iface);
226 NapiUtils::SetArrayElement(context->GetEnv(), list, index++, rule);
227 }
228 NapiUtils::SetNamedProperty(context->GetEnv(), pageInfo, NET_FIREWALL_PAGE_DATA, list);
229 return pageInfo;
230 }
231
ExecGetNetFirewallRule(GetNetFirewallRuleContext * context)232 bool ExecGetNetFirewallRule(GetNetFirewallRuleContext *context)
233 {
234 if (context == nullptr) {
235 return false;
236 }
237 if (!context->IsParseOK()) {
238 return false;
239 }
240 int32_t result = DelayedSingleton<NetFirewallClient>::GetInstance()->GetNetFirewallRule(context->userId_,
241 context->ruleId_, context->rule_);
242 if (result != FIREWALL_SUCCESS || context->rule_ == nullptr) {
243 NETMANAGER_EXT_LOGE("ExecGetNetFirewallRule error, errorCode: %{public}d", result);
244 context->SetErrorCode(result);
245 return false;
246 }
247 return true;
248 }
249
GetNetFirewallRuleCallback(GetNetFirewallRuleContext * context)250 napi_value GetNetFirewallRuleCallback(GetNetFirewallRuleContext *context)
251 {
252 if (context == nullptr || context->rule_ == nullptr) {
253 return nullptr;
254 }
255 napi_value rule = NapiUtils::CreateObject(context->GetEnv());
256 NetFirewallRuleParse::SetRuleParams(context->GetEnv(), rule, *(context->rule_));
257 return rule;
258 }
259
ExecGetInterceptRecords(GetInterceptRecordsContext * context)260 bool ExecGetInterceptRecords(GetInterceptRecordsContext *context)
261 {
262 if (context == nullptr || context->requestParam_ == nullptr) {
263 return false;
264 }
265 if (!context->IsParseOK()) {
266 return false;
267 }
268 int32_t result = DelayedSingleton<NetFirewallClient>::GetInstance()->GetInterceptRecords(context->userId_,
269 context->requestParam_, context->pageInfo_);
270 if (result != FIREWALL_SUCCESS || context->pageInfo_ == nullptr) {
271 NETMANAGER_EXT_LOGE("ExecGetNetFirewallRules error, errorCode: %{public}d", result);
272 context->SetErrorCode(result);
273 return false;
274 }
275 return true;
276 }
277
GetInterceptRecordCallbacks(GetInterceptRecordsContext * context)278 napi_value GetInterceptRecordCallbacks(GetInterceptRecordsContext *context)
279 {
280 if (context == nullptr) {
281 return nullptr;
282 }
283 napi_value pageInfo = NapiUtils::CreateObject(context->GetEnv());
284 if (context->pageInfo_ == nullptr) {
285 return nullptr;
286 }
287 NapiUtils::SetInt32Property(context->GetEnv(), pageInfo, NET_FIREWALL_PAGE, context->pageInfo_->page);
288 NapiUtils::SetInt32Property(context->GetEnv(), pageInfo, NET_FIREWALL_PAGE_SIZE, context->pageInfo_->pageSize);
289 NapiUtils::SetInt32Property(context->GetEnv(), pageInfo, NET_FIREWALL_TOTAL_PAGE, context->pageInfo_->totalPage);
290 napi_value list = NapiUtils::CreateArray(context->GetEnv(), context->pageInfo_->data.size());
291
292 uint32_t index = 0;
293 for (const auto &iface : context->pageInfo_->data) {
294 napi_value rule = NapiUtils::CreateObject(context->GetEnv());
295 NapiUtils::SetInt32Property(context->GetEnv(), rule, NET_FIREWALL_RECORD_TIME, iface.time);
296 NapiUtils::SetStringPropertyUtf8(context->GetEnv(), rule, NET_FIREWALL_RECORD_LOCAL_IP, iface.localIp);
297 NapiUtils::SetStringPropertyUtf8(context->GetEnv(), rule, NET_FIREWALL_RECORD_REMOTE_IP, iface.remoteIp);
298 NapiUtils::SetInt32Property(context->GetEnv(), rule, NET_FIREWALL_RECORD_LOCAL_PORT, iface.localPort);
299 NapiUtils::SetInt32Property(context->GetEnv(), rule, NET_FIREWALL_RECORD_REMOTE_PORT, iface.remotePort);
300 NapiUtils::SetInt32Property(context->GetEnv(), rule, NET_FIREWALL_RECORD_PROTOCOL, iface.protocol);
301 NapiUtils::SetInt32Property(context->GetEnv(), rule, NET_FIREWALL_RECORD_UID, iface.appUid);
302 NapiUtils::SetStringPropertyUtf8(context->GetEnv(), rule, NET_FIREWALL_DOMAIN, iface.domain);
303
304 NapiUtils::SetArrayElement(context->GetEnv(), list, index++, rule);
305 }
306 NapiUtils::SetNamedProperty(context->GetEnv(), pageInfo, NET_FIREWALL_PAGE_DATA, list);
307 return pageInfo;
308 }
309 } // namespace NetFirewallExec
310 } // namespace NetManagerStandard
311 } // namespace OHOS