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 "executer_factory.h"
17
18 #include <algorithm>
19
20 #include "domain_executer.h"
21 #include "firewall_executer.h"
22 #include "rule_utils.h"
23
24 namespace OHOS {
25 namespace EDM {
26 namespace IPTABLES {
27
28 const std::string OUTPUT_CHAIN = "OUTPUT";
29 const std::string INPUT_CHAIN = "INPUT";
30
31 std::shared_ptr<ExecuterFactory> ExecuterFactory::instance_;
32 std::mutex ExecuterFactory::mutexLock_;
33
GetInstance()34 std::shared_ptr<ExecuterFactory> ExecuterFactory::GetInstance()
35 {
36 if (instance_ == nullptr) {
37 std::lock_guard<std::mutex> lock(mutexLock_);
38 if (instance_ == nullptr) {
39 std::shared_ptr<ExecuterFactory> temp = std::make_shared<ExecuterFactory>();
40 instance_ = temp;
41
42 instance_->chainNames_.emplace_back(EDM_DEFAULT_DENY_OUTPUT_CHAIN_NAME);
43 instance_->executerVector_.emplace_back(
44 std::make_shared<FirewallExecuter>(OUTPUT_CHAIN, EDM_DEFAULT_DENY_OUTPUT_CHAIN_NAME));
45 instance_->chainNames_.emplace_back(EDM_DEFAULT_DNS_DENY_OUTPUT_CHAIN_NAME);
46 instance_->executerVector_.emplace_back(std::make_shared<DomainExecuter>(
47 EDM_DEFAULT_DNS_DENY_OUTPUT_CHAIN_NAME));
48
49 instance_->chainNames_.emplace_back(EDM_DENY_OUTPUT_CHAIN_NAME);
50 instance_->executerVector_.emplace_back(std::make_shared<FirewallExecuter>(OUTPUT_CHAIN,
51 EDM_DENY_OUTPUT_CHAIN_NAME));
52 instance_->chainNames_.emplace_back(EDM_DENY_INPUT_CHAIN_NAME);
53 instance_->executerVector_.emplace_back(std::make_shared<FirewallExecuter>(INPUT_CHAIN,
54 EDM_DENY_INPUT_CHAIN_NAME));
55 instance_->chainNames_.emplace_back(EDM_ALLOW_OUTPUT_CHAIN_NAME);
56 instance_->executerVector_.emplace_back(std::make_shared<FirewallExecuter>(OUTPUT_CHAIN,
57 EDM_ALLOW_OUTPUT_CHAIN_NAME));
58 instance_->chainNames_.emplace_back(EDM_ALLOW_INPUT_CHAIN_NAME);
59 instance_->executerVector_.emplace_back(std::make_shared<FirewallExecuter>(INPUT_CHAIN,
60 EDM_ALLOW_INPUT_CHAIN_NAME));
61
62 instance_->chainNames_.emplace_back(EDM_DNS_DENY_OUTPUT_CHAIN_NAME);
63 instance_->executerVector_.emplace_back(std::make_shared<DomainExecuter>(EDM_DNS_DENY_OUTPUT_CHAIN_NAME));
64 instance_->chainNames_.emplace_back(EDM_DNS_ALLOW_OUTPUT_CHAIN_NAME);
65 instance_->executerVector_.emplace_back(std::make_shared<DomainExecuter>(EDM_DNS_ALLOW_OUTPUT_CHAIN_NAME));
66 }
67 }
68 return instance_;
69 }
70
GetExecuter(const std::string & chainName) const71 std::shared_ptr<IExecuter> ExecuterFactory::GetExecuter(const std::string& chainName) const
72 {
73 auto it = std::find(chainNames_.begin(), chainNames_.end(), chainName);
74 if (it != chainNames_.end()) {
75 int index = it - chainNames_.begin();
76 return executerVector_[index];
77 }
78 return nullptr;
79 }
80
GetAllExecuter() const81 std::vector<std::shared_ptr<IExecuter>> ExecuterFactory::GetAllExecuter() const
82 {
83 return executerVector_;
84 }
85 } // namespace IPTABLES
86 } // namespace EDM
87 } // namespace OHOS