1 /*
2 * Copyright (c) 2022 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 "parameter.h"
17
18 #include "base64_utils.h"
19 #include "net_http_proxy_tracker.h"
20 #include "net_mgr_log_wrapper.h"
21
22 namespace OHOS {
23 namespace NetManagerStandard {
24 namespace {
25 constexpr int32_t SYSPARA_MAX_SIZE = 96;
26 constexpr const char *EXCLUSIONS_SPLIT_SYMBOL = ",";
27 constexpr const char *DEFAULT_HTTP_PROXY_HOST = "NONE";
28 constexpr const char *DEFAULT_HTTP_PROXY_PORT = "0";
29 constexpr const char *DEFAULT_HTTP_PROXY_EXCLUSION_LIST = "NONE";
30 constexpr const char *HTTP_PROXY_HOST_KEY = "persist.netmanager_base.http_proxy.host";
31 constexpr const char *HTTP_PROXY_PORT_KEY = "persist.netmanager_base.http_proxy.port";
32 constexpr const char *HTTP_PROXY_EXCLUSIONS_KEY = "persist.netmanager_base.http_proxy.exclusion_list";
33 } // namespace
34
ReadFromSystemParameter(HttpProxy & httpProxy)35 bool NetHttpProxyTracker::ReadFromSystemParameter(HttpProxy &httpProxy)
36 {
37 char httpProxyHost[SYSPARA_MAX_SIZE] = {0};
38 char httpProxyPort[SYSPARA_MAX_SIZE] = {0};
39 char httpProxyExclusions[SYSPARA_MAX_SIZE] = {0};
40 GetParameter(HTTP_PROXY_HOST_KEY, DEFAULT_HTTP_PROXY_HOST, httpProxyHost, sizeof(httpProxyHost));
41 GetParameter(HTTP_PROXY_PORT_KEY, DEFAULT_HTTP_PROXY_PORT, httpProxyPort, sizeof(httpProxyPort));
42 GetParameter(HTTP_PROXY_EXCLUSIONS_KEY, DEFAULT_HTTP_PROXY_EXCLUSION_LIST, httpProxyExclusions,
43 sizeof(httpProxyExclusions));
44 std::string host = Base64::Decode(httpProxyHost);
45 host = (host == DEFAULT_HTTP_PROXY_HOST ? "" : host);
46 std::set<std::string> exclusionList = ParseExclusionList(httpProxyExclusions);
47 uint16_t port = static_cast<uint16_t>(std::atoi(httpProxyPort));
48 httpProxy = {host, port, exclusionList};
49 return true;
50 }
51
WriteToSystemParameter(const HttpProxy & httpProxy)52 bool NetHttpProxyTracker::WriteToSystemParameter(const HttpProxy &httpProxy)
53 {
54 std::string host = Base64::Encode(httpProxy.GetHost());
55 if (host.empty()) {
56 host = Base64::Encode(DEFAULT_HTTP_PROXY_HOST);
57 }
58 int32_t ret = SetParameter(HTTP_PROXY_HOST_KEY, host.c_str());
59 if (ret) {
60 NETMGR_LOG_E("Set host:%{public}s to system parameter:%{public}s failed, ret:%{public}d", host.c_str(),
61 HTTP_PROXY_HOST_KEY, ret);
62 return false;
63 }
64
65 std::string port = std::to_string(httpProxy.GetPort());
66 ret = SetParameter(HTTP_PROXY_PORT_KEY, port.c_str());
67 if (ret) {
68 NETMGR_LOG_E("Set port:%{public}s to system parameter:%{public}s failed, ret:%{public}d", port.c_str(),
69 HTTP_PROXY_PORT_KEY, ret);
70 return false;
71 }
72
73 std::string exclusions = GetExclusionsAsAstring(httpProxy.GetExclusionList());
74 if (exclusions.empty()) {
75 exclusions = DEFAULT_HTTP_PROXY_EXCLUSION_LIST;
76 }
77 ret = SetParameter(HTTP_PROXY_EXCLUSIONS_KEY, exclusions.c_str());
78 if (ret) {
79 NETMGR_LOG_E("Set exclusions:%{public}s to system parameter:%{public}s failed, ret:%{public}d",
80 exclusions.c_str(), HTTP_PROXY_EXCLUSIONS_KEY, ret);
81 return false;
82 }
83 return true;
84 }
85
ParseExclusionList(const std::string & exclusions) const86 std::set<std::string> NetHttpProxyTracker::ParseExclusionList(const std::string &exclusions) const
87 {
88 std::set<std::string> exclusionList;
89 if (exclusions.empty() || exclusions == DEFAULT_HTTP_PROXY_EXCLUSION_LIST) {
90 return exclusionList;
91 }
92 size_t startPos = 0;
93 size_t searchePos = exclusions.find(EXCLUSIONS_SPLIT_SYMBOL);
94 std::string exclusion;
95 while (searchePos != std::string::npos) {
96 exclusion = exclusions.substr(startPos, (searchePos - startPos));
97 exclusionList.insert(exclusion);
98 startPos = searchePos + 1;
99 searchePos = exclusions.find(EXCLUSIONS_SPLIT_SYMBOL, startPos);
100 }
101 exclusion = exclusions.substr(startPos, (exclusions.size() - startPos));
102 exclusionList.insert(exclusion);
103 return exclusionList;
104 }
105
GetExclusionsAsAstring(const std::set<std::string> & exculisonList) const106 std::string NetHttpProxyTracker::GetExclusionsAsAstring(const std::set<std::string> &exculisonList) const
107 {
108 std::string exclusions;
109 int32_t index = 0;
110 for (auto exclusion : exculisonList) {
111 if (exclusions.size() + exclusion.size() >= SYSPARA_MAX_SIZE) {
112 break;
113 }
114 if (index > 0) {
115 exclusions = exclusions + EXCLUSIONS_SPLIT_SYMBOL;
116 }
117 exclusions = exclusions + exclusion;
118 index++;
119 }
120 return exclusions;
121 }
122 } // namespace NetManagerStandard
123 } // namespace OHOS