• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "get_proxy.h"
16 
17 #include <want.h>
18 
19 #include <mutex>
20 
21 #include "common_event_data.h"
22 #include "common_event_manager.h"
23 #include "common_event_publish_info.h"
24 #include "log.h"
25 #include "net_conn_client.h"
26 
27 std::mutex g_proxyMutex;
28 using namespace OHOS::EventFwk;
29 static constexpr const char *DEFAULT_HTTP_PROXY_HOST = "NONE";
30 static constexpr const char *DEFAULT_HTTP_PROXY_EXCLUSION_LIST = "NONE";
31 
GetInstance()32 SysNetProxyManager &SysNetProxyManager::GetInstance()
33 {
34     static SysNetProxyManager proxyManager;
35     return proxyManager;
36 }
37 
GetHost()38 CStringWrapper SysNetProxyManager::GetHost()
39 {
40     return WrapperCString(host_);
41 }
GetPort()42 CStringWrapper SysNetProxyManager::GetPort()
43 {
44     REQUEST_HILOGD("SysNetProxyManager::GetPort() is %{public}s", port_.c_str());
45     return WrapperCString(port_);
46 }
GetExclusionList()47 CStringWrapper SysNetProxyManager::GetExclusionList()
48 {
49     REQUEST_HILOGD("SysNetProxyManager::GetExclusionList() is %{public}s", exclusionList_.c_str());
50     return WrapperCString(exclusionList_);
51 }
52 
SubscriberEvent()53 void SysNetProxyManager::SubscriberEvent()
54 {
55     REQUEST_HILOGD("SubscriberEvent start.");
56     if (subscriber_) {
57         REQUEST_HILOGE("Common Event is already subscribered.");
58         return;
59     }
60     {
61         std::lock_guard<std::mutex> lock(proxyMutex);
62         InitProxy(host_, port_, exclusionList_);
63     }
64     OHOS::EventFwk::MatchingSkills matchingSkills;
65     matchingSkills.AddEvent(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_HTTP_PROXY_CHANGE);
66     OHOS::EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
67     subscriber_ = std::make_shared<SysNetProxySubscriber>(subscribeInfo);
68 
69     bool subscribeResult = OHOS::EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber_);
70     if (subscribeResult == false) {
71         REQUEST_HILOGE("Start sysproxy listen, subscribe common event failed");
72         return;
73     }
74 }
75 
InitProxy(std::string & host,std::string & port,std::string & exclusion)76 void SysNetProxyManager::InitProxy(std::string &host, std::string &port, std::string &exclusion)
77 {
78     OHOS::NetManagerStandard::HttpProxy httpProxy;
79     int32_t ret = OHOS::NetManagerStandard::NetConnClient::GetInstance().GetDefaultHttpProxy(httpProxy);
80     if (ret != OHOS::NetManagerStandard::NET_CONN_SUCCESS) {
81         REQUEST_HILOGE("Netproxy config change, get default http proxy from OH network failed");
82         return;
83     }
84     std::string host_res = httpProxy.GetHost();
85     host = host_res;
86     if (host == DEFAULT_HTTP_PROXY_HOST) {
87         host = std::string();
88     }
89     std::string httpProxyExclusions;
90     for (const auto &s : httpProxy.GetExclusionList()) {
91         httpProxyExclusions.append(s + ",");
92     }
93     if (!httpProxyExclusions.empty()) {
94         httpProxyExclusions.pop_back();
95     }
96 
97     exclusion = httpProxyExclusions;
98     if (exclusion == DEFAULT_HTTP_PROXY_EXCLUSION_LIST) {
99         exclusion = std::string();
100     }
101     std::string port_res = std::to_string(httpProxy.GetPort());
102     port = port_res;
103 }
104 
OnReceiveEvent(const OHOS::EventFwk::CommonEventData & data)105 void SysNetProxySubscriber::OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data)
106 {
107     const std::string action = data.GetWant().GetAction();
108     REQUEST_HILOGD("Receive system proxy change action: %{public}s", action.c_str());
109     if (action != OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_HTTP_PROXY_CHANGE) {
110         REQUEST_HILOGE("Receive system proxy change, action error, action is %{public}s", action.c_str());
111         return;
112     }
113     std::string host;
114     std::string port;
115     std::string exclusionList;
116     const std::string proxyContent = data.GetWant().GetStringParam("HttpProxy");
117     SysNetProxyManager::GetInstance().GetHttpProxy(proxyContent, host, port, exclusionList);
118     g_proxyMutex.lock();
119     SysNetProxyManager::GetInstance().SetHttpProxy(host, port, exclusionList);
120     g_proxyMutex.unlock();
121 }
122 
GetHttpProxy(const std::string proxyContent,std::string & host,std::string & port,std::string & exclusionList)123 void SysNetProxyManager::GetHttpProxy(
124     const std::string proxyContent, std::string &host, std::string &port, std::string &exclusionList)
125 {
126     typedef std::string::const_iterator iter_t;
127     iter_t proxyContentEnd = proxyContent.end();
128     iter_t hostStart = proxyContent.cbegin();
129     iter_t hostEnd = std::find(hostStart, proxyContentEnd, '\t');
130     std::string hostContent = std::string(hostStart, hostEnd);
131     hostEnd += 1;
132     iter_t portStart = hostEnd;
133     iter_t portEnd = std::find(portStart, proxyContentEnd, '\t');
134     std::string portContent = std::string(portStart, portEnd);
135     host = hostContent;
136     port = portContent;
137     if (portEnd != proxyContentEnd) {
138         portEnd += 1;
139         iter_t exclusionListStart = portEnd;
140         std::string exclusionListContent = std::string(exclusionListStart, proxyContentEnd);
141         exclusionList = exclusionListContent;
142     }
143 }
144 
RegisterProxySubscriber()145 void RegisterProxySubscriber()
146 {
147     SysNetProxyManager::GetInstance().SubscriberEvent();
148 }
149 
GetHost()150 CStringWrapper GetHost()
151 {
152     return SysNetProxyManager::GetInstance().GetHost();
153 }
154 
GetPort()155 CStringWrapper GetPort()
156 {
157     return SysNetProxyManager::GetInstance().GetPort();
158 }
159 
GetExclusionList()160 CStringWrapper GetExclusionList()
161 {
162     return SysNetProxyManager::GetInstance().GetExclusionList();
163 }
164