1 /*
2 * Copyright (c) 2022-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 "net_http_proxy_tracker.h"
17
18 #include "base64_utils.h"
19 #include "net_datashare_utils.h"
20 #include "net_manager_constants.h"
21 #include "net_mgr_log_wrapper.h"
22 #include "netmanager_base_common_utils.h"
23
24 namespace OHOS {
25 namespace NetManagerStandard {
26 namespace {
27 constexpr const char *EXCLUSIONS_SPLIT_SYMBOL = ",";
28 constexpr const char *DEFAULT_HTTP_PROXY_HOST = "NONE";
29 constexpr const char *DEFAULT_HTTP_PROXY_PORT = "0";
30 constexpr const char *DEFAULT_HTTP_PROXY_EXCLUSION_LIST = "NONE";
31 } // namespace
32
ReadFromSettingsData(HttpProxy & httpProxy)33 void NetHttpProxyTracker::ReadFromSettingsData(HttpProxy &httpProxy)
34 {
35 auto dataShareHelperUtils = std::make_unique<NetDataShareHelperUtils>();
36 std::string proxyHost, proxyPort, proxyExclusions;
37 Uri hostUri(GLOBAL_PROXY_HOST_URI);
38 int32_t ret = dataShareHelperUtils->Query(hostUri, KEY_GLOBAL_PROXY_HOST, proxyHost);
39 if (ret != NETMANAGER_SUCCESS) {
40 NETMGR_LOG_E("Query global proxy host failed.");
41 }
42 std::string host = Base64::Decode(proxyHost);
43 host = (host == DEFAULT_HTTP_PROXY_HOST ? "" : host);
44
45 Uri portUri(GLOBAL_PROXY_PORT_URI);
46 ret = dataShareHelperUtils->Query(portUri, KEY_GLOBAL_PROXY_PORT, proxyPort);
47 if (ret != NETMANAGER_SUCCESS) {
48 NETMGR_LOG_E("Query global proxy port failed.");
49 }
50 uint16_t port = (proxyPort.empty() || host.empty()) ? 0 : static_cast<uint16_t>(CommonUtils::StrToUint(proxyPort));
51
52 Uri exclusionsUri(GLOBAL_PROXY_EXCLUSIONS_URI);
53 ret = dataShareHelperUtils->Query(exclusionsUri, KEY_GLOBAL_PROXY_EXCLUSIONS, proxyExclusions);
54 if (ret != NETMANAGER_SUCCESS) {
55 NETMGR_LOG_E("Query global proxy exclusions failed.");
56 }
57 std::list<std::string> exclusionList =
58 host.empty() ? std::list<std::string>() : ParseExclusionList(proxyExclusions);
59 httpProxy = {host, port, exclusionList};
60 }
61
WriteToSettingsData(HttpProxy & httpProxy)62 bool NetHttpProxyTracker::WriteToSettingsData(HttpProxy &httpProxy)
63 {
64 std::string host =
65 httpProxy.GetHost().empty() ? Base64::Encode(DEFAULT_HTTP_PROXY_HOST) : Base64::Encode(httpProxy.GetHost());
66 auto dataShareHelperUtils = std::make_unique<NetDataShareHelperUtils>();
67 Uri hostUri(GLOBAL_PROXY_HOST_URI);
68 int32_t ret = dataShareHelperUtils->Update(hostUri, KEY_GLOBAL_PROXY_HOST, host);
69 if (ret != NETMANAGER_SUCCESS) {
70 NETMGR_LOG_E("Set host:%{public}s to datashare failed", host.c_str());
71 return false;
72 }
73
74 Uri portUri(GLOBAL_PROXY_PORT_URI);
75 std::string port = httpProxy.GetHost().empty() ? DEFAULT_HTTP_PROXY_PORT : std::to_string(httpProxy.GetPort());
76 ret = dataShareHelperUtils->Update(portUri, KEY_GLOBAL_PROXY_PORT, port);
77 if (ret != NETMANAGER_SUCCESS) {
78 NETMGR_LOG_E("Set port:%{public}s to datashare failed", port.c_str());
79 return false;
80 }
81
82 std::string exclusions = GetExclusionsAsString(httpProxy.GetExclusionList());
83 exclusions = (httpProxy.GetHost().empty() || exclusions.empty()) ? DEFAULT_HTTP_PROXY_EXCLUSION_LIST : exclusions;
84 Uri exclusionsUri(GLOBAL_PROXY_EXCLUSIONS_URI);
85 ret = dataShareHelperUtils->Update(exclusionsUri, KEY_GLOBAL_PROXY_EXCLUSIONS, exclusions);
86 if (ret != NETMANAGER_SUCCESS) {
87 NETMGR_LOG_E("Set exclusions:%{public}s to datashare", exclusions.c_str());
88 return false;
89 }
90 httpProxy.SetExclusionList(ParseExclusionList(exclusions));
91 return true;
92 }
93
ParseExclusionList(const std::string & exclusions) const94 std::list<std::string> NetHttpProxyTracker::ParseExclusionList(const std::string &exclusions) const
95 {
96 std::list<std::string> exclusionList;
97 if (exclusions.empty() || exclusions == DEFAULT_HTTP_PROXY_EXCLUSION_LIST) {
98 return exclusionList;
99 }
100 size_t startPos = 0;
101 size_t searchPos = exclusions.find(EXCLUSIONS_SPLIT_SYMBOL);
102 std::string exclusion;
103 while (searchPos != std::string::npos) {
104 exclusion = exclusions.substr(startPos, (searchPos - startPos));
105 exclusionList.push_back(exclusion);
106 startPos = searchPos + 1;
107 searchPos = exclusions.find(EXCLUSIONS_SPLIT_SYMBOL, startPos);
108 }
109 exclusion = exclusions.substr(startPos, (exclusions.size() - startPos));
110 exclusionList.push_back(exclusion);
111 return exclusionList;
112 }
113
GetExclusionsAsString(const std::list<std::string> & exclusionList) const114 std::string NetHttpProxyTracker::GetExclusionsAsString(const std::list<std::string> &exclusionList) const
115 {
116 std::string exclusions;
117 int32_t index = 0;
118 for (const auto &exclusion : exclusionList) {
119 if (exclusion.empty()) {
120 continue;
121 }
122 if (index > 0) {
123 exclusions = exclusions + EXCLUSIONS_SPLIT_SYMBOL;
124 }
125 exclusions = exclusions + exclusion;
126 index++;
127 }
128 return exclusions;
129 }
130 } // namespace NetManagerStandard
131 } // namespace OHOS