• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
37     std::string proxyPort;
38     std::string proxyExclusions;
39     Uri hostUri(GLOBAL_PROXY_HOST_URI);
40     int32_t ret = dataShareHelperUtils->Query(hostUri, KEY_GLOBAL_PROXY_HOST, proxyHost);
41     if (ret != NETMANAGER_SUCCESS) {
42         NETMGR_LOG_E("Query global proxy host failed.");
43     }
44     std::string host = Base64::Decode(proxyHost);
45     host = (host == DEFAULT_HTTP_PROXY_HOST ? "" : host);
46 
47     Uri portUri(GLOBAL_PROXY_PORT_URI);
48     ret = dataShareHelperUtils->Query(portUri, KEY_GLOBAL_PROXY_PORT, proxyPort);
49     if (ret != NETMANAGER_SUCCESS) {
50         NETMGR_LOG_E("Query global proxy port failed.");
51     }
52     uint16_t port = (proxyPort.empty() || host.empty()) ? 0 : static_cast<uint16_t>(CommonUtils::StrToUint(proxyPort));
53 
54     Uri exclusionsUri(GLOBAL_PROXY_EXCLUSIONS_URI);
55     ret = dataShareHelperUtils->Query(exclusionsUri, KEY_GLOBAL_PROXY_EXCLUSIONS, proxyExclusions);
56     if (ret != NETMANAGER_SUCCESS) {
57         NETMGR_LOG_E("Query global proxy exclusions failed.");
58     }
59     std::list<std::string> exclusionList =
60         host.empty() ? std::list<std::string>() : ParseExclusionList(proxyExclusions);
61     httpProxy = {host, port, exclusionList};
62 }
63 
WriteToSettingsData(HttpProxy & httpProxy)64 bool NetHttpProxyTracker::WriteToSettingsData(HttpProxy &httpProxy)
65 {
66     std::string host =
67         httpProxy.GetHost().empty() ? Base64::Encode(DEFAULT_HTTP_PROXY_HOST) : Base64::Encode(httpProxy.GetHost());
68     auto dataShareHelperUtils = std::make_unique<NetDataShareHelperUtils>();
69     Uri hostUri(GLOBAL_PROXY_HOST_URI);
70     int32_t ret = dataShareHelperUtils->Update(hostUri, KEY_GLOBAL_PROXY_HOST, host);
71     if (ret != NETMANAGER_SUCCESS) {
72         NETMGR_LOG_E("Set host:%{public}s to datashare failed", host.c_str());
73         return false;
74     }
75 
76     Uri portUri(GLOBAL_PROXY_PORT_URI);
77     std::string port = httpProxy.GetHost().empty() ? DEFAULT_HTTP_PROXY_PORT : std::to_string(httpProxy.GetPort());
78     ret = dataShareHelperUtils->Update(portUri, KEY_GLOBAL_PROXY_PORT, port);
79     if (ret != NETMANAGER_SUCCESS) {
80         NETMGR_LOG_E("Set port:%{public}s to datashare failed", port.c_str());
81         return false;
82     }
83 
84     std::string exclusions = GetExclusionsAsString(httpProxy.GetExclusionList());
85     exclusions = (httpProxy.GetHost().empty() || exclusions.empty()) ? DEFAULT_HTTP_PROXY_EXCLUSION_LIST : exclusions;
86     Uri exclusionsUri(GLOBAL_PROXY_EXCLUSIONS_URI);
87     ret = dataShareHelperUtils->Update(exclusionsUri, KEY_GLOBAL_PROXY_EXCLUSIONS, exclusions);
88     if (ret != NETMANAGER_SUCCESS) {
89         NETMGR_LOG_E("Set exclusions:%{public}s to datashare", exclusions.c_str());
90         return false;
91     }
92     httpProxy.SetExclusionList(ParseExclusionList(exclusions));
93     return true;
94 }
95 
ParseExclusionList(const std::string & exclusions) const96 std::list<std::string> NetHttpProxyTracker::ParseExclusionList(const std::string &exclusions) const
97 {
98     std::list<std::string> exclusionList;
99     if (exclusions.empty() || exclusions == DEFAULT_HTTP_PROXY_EXCLUSION_LIST) {
100         return exclusionList;
101     }
102     size_t startPos = 0;
103     size_t searchPos = exclusions.find(EXCLUSIONS_SPLIT_SYMBOL);
104     std::string exclusion;
105     while (searchPos != std::string::npos) {
106         exclusion = exclusions.substr(startPos, (searchPos - startPos));
107         exclusionList.push_back(exclusion);
108         startPos = searchPos + 1;
109         searchPos = exclusions.find(EXCLUSIONS_SPLIT_SYMBOL, startPos);
110     }
111     exclusion = exclusions.substr(startPos, (exclusions.size() - startPos));
112     exclusionList.push_back(exclusion);
113     return exclusionList;
114 }
115 
GetExclusionsAsString(const std::list<std::string> & exclusionList) const116 std::string NetHttpProxyTracker::GetExclusionsAsString(const std::list<std::string> &exclusionList) const
117 {
118     std::string exclusions;
119     int32_t index = 0;
120     for (const auto &exclusion : exclusionList) {
121         if (exclusion.empty()) {
122             continue;
123         }
124         if (index > 0) {
125             exclusions = exclusions + EXCLUSIONS_SPLIT_SYMBOL;
126         }
127         exclusions = exclusions + exclusion;
128         index++;
129     }
130     return exclusions;
131 }
132 } // namespace NetManagerStandard
133 } // namespace OHOS