blob: 9cd7ab2c3e40c0ec0c3d39aaece0574116ae3188 [file] [log] [blame]
Remi NGUYEN VANfbbccbc2021-01-15 18:08:24 +09001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.net;
18
19import android.annotation.SdkConstant;
20import android.annotation.SdkConstant.SdkConstantType;
21import android.compat.annotation.UnsupportedAppUsage;
22import android.content.Context;
23import android.os.Build;
24import android.text.TextUtils;
25import android.util.Log;
26
27import com.android.net.module.util.ProxyUtils;
28
29import java.net.InetSocketAddress;
30import java.net.ProxySelector;
31import java.net.URI;
32import java.util.List;
Remi NGUYEN VANfbbccbc2021-01-15 18:08:24 +090033
34/**
35 * A convenience class for accessing the user and default proxy
36 * settings.
37 */
38public final class Proxy {
39
40 private static final String TAG = "Proxy";
41
42 private static final ProxySelector sDefaultProxySelector;
43
44 /**
45 * Used to notify an app that's caching the proxy that either the default
46 * connection has changed or any connection's proxy has changed. The new
47 * proxy should be queried using {@link ConnectivityManager#getDefaultProxy()}.
48 *
49 * <p class="note">This is a protected intent that can only be sent by the system
50 */
51 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
52 public static final String PROXY_CHANGE_ACTION = "android.intent.action.PROXY_CHANGE";
53 /**
54 * Intent extra included with {@link #PROXY_CHANGE_ACTION} intents.
55 * It describes the new proxy being used (as a {@link ProxyInfo} object).
56 * @deprecated Because {@code PROXY_CHANGE_ACTION} is sent whenever the proxy
57 * for any network on the system changes, applications should always use
58 * {@link ConnectivityManager#getDefaultProxy()} or
59 * {@link ConnectivityManager#getLinkProperties(Network)}.{@link LinkProperties#getHttpProxy()}
60 * to get the proxy for the Network(s) they are using.
61 */
62 @Deprecated
63 public static final String EXTRA_PROXY_INFO = "android.intent.extra.PROXY_INFO";
64
Remi NGUYEN VANfbbccbc2021-01-15 18:08:24 +090065 private static ConnectivityManager sConnectivityManager = null;
66
Remi NGUYEN VANfbbccbc2021-01-15 18:08:24 +090067 static {
Remi NGUYEN VANfbbccbc2021-01-15 18:08:24 +090068 sDefaultProxySelector = ProxySelector.getDefault();
69 }
70
71 /**
72 * Return the proxy object to be used for the URL given as parameter.
73 * @param ctx A Context used to get the settings for the proxy host.
74 * @param url A URL to be accessed. Used to evaluate exclusion list.
75 * @return Proxy (java.net) object containing the host name. If the
76 * user did not set a hostname it returns the default host.
77 * A null value means that no host is to be used.
78 * {@hide}
79 */
80 @UnsupportedAppUsage
81 public static final java.net.Proxy getProxy(Context ctx, String url) {
82 String host = "";
83 if ((url != null) && !isLocalHost(host)) {
84 URI uri = URI.create(url);
85 ProxySelector proxySelector = ProxySelector.getDefault();
86
87 List<java.net.Proxy> proxyList = proxySelector.select(uri);
88
89 if (proxyList.size() > 0) {
90 return proxyList.get(0);
91 }
92 }
93 return java.net.Proxy.NO_PROXY;
94 }
95
96
97 /**
98 * Return the proxy host set by the user.
99 * @param ctx A Context used to get the settings for the proxy host.
100 * @return String containing the host name. If the user did not set a host
101 * name it returns the default host. A null value means that no
102 * host is to be used.
103 * @deprecated Use standard java vm proxy values to find the host, port
104 * and exclusion list. This call ignores the exclusion list.
105 */
106 @Deprecated
107 public static final String getHost(Context ctx) {
108 java.net.Proxy proxy = getProxy(ctx, null);
109 if (proxy == java.net.Proxy.NO_PROXY) return null;
110 try {
111 return ((InetSocketAddress)(proxy.address())).getHostName();
112 } catch (Exception e) {
113 return null;
114 }
115 }
116
117 /**
118 * Return the proxy port set by the user.
119 * @param ctx A Context used to get the settings for the proxy port.
120 * @return The port number to use or -1 if no proxy is to be used.
121 * @deprecated Use standard java vm proxy values to find the host, port
122 * and exclusion list. This call ignores the exclusion list.
123 */
124 @Deprecated
125 public static final int getPort(Context ctx) {
126 java.net.Proxy proxy = getProxy(ctx, null);
127 if (proxy == java.net.Proxy.NO_PROXY) return -1;
128 try {
129 return ((InetSocketAddress)(proxy.address())).getPort();
130 } catch (Exception e) {
131 return -1;
132 }
133 }
134
135 /**
136 * Return the default proxy host specified by the carrier.
137 * @return String containing the host name or null if there is no proxy for
138 * this carrier.
139 * @deprecated Use standard java vm proxy values to find the host, port and
140 * exclusion list. This call ignores the exclusion list and no
141 * longer reports only mobile-data apn-based proxy values.
142 */
143 @Deprecated
144 public static final String getDefaultHost() {
145 String host = System.getProperty("http.proxyHost");
146 if (TextUtils.isEmpty(host)) return null;
147 return host;
148 }
149
150 /**
151 * Return the default proxy port specified by the carrier.
152 * @return The port number to be used with the proxy host or -1 if there is
153 * no proxy for this carrier.
154 * @deprecated Use standard java vm proxy values to find the host, port and
155 * exclusion list. This call ignores the exclusion list and no
156 * longer reports only mobile-data apn-based proxy values.
157 */
158 @Deprecated
159 public static final int getDefaultPort() {
160 if (getDefaultHost() == null) return -1;
161 try {
162 return Integer.parseInt(System.getProperty("http.proxyPort"));
163 } catch (NumberFormatException e) {
164 return -1;
165 }
166 }
167
168 private static final boolean isLocalHost(String host) {
169 if (host == null) {
170 return false;
171 }
172 try {
173 if (host != null) {
174 if (host.equalsIgnoreCase("localhost")) {
175 return true;
176 }
177 if (InetAddresses.parseNumericAddress(host).isLoopbackAddress()) {
178 return true;
179 }
180 }
181 } catch (IllegalArgumentException iex) {
182 }
183 return false;
184 }
185
Remi NGUYEN VANfbbccbc2021-01-15 18:08:24 +0900186 /** @hide */
187 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
188 public static final void setHttpProxySystemProperty(ProxyInfo p) {
189 String host = null;
190 String port = null;
191 String exclList = null;
192 Uri pacFileUrl = Uri.EMPTY;
193 if (p != null) {
194 host = p.getHost();
195 port = Integer.toString(p.getPort());
196 exclList = ProxyUtils.exclusionListAsString(p.getExclusionList());
197 pacFileUrl = p.getPacFileUrl();
198 }
199 setHttpProxySystemProperty(host, port, exclList, pacFileUrl);
200 }
201
202 /** @hide */
203 public static final void setHttpProxySystemProperty(String host, String port, String exclList,
204 Uri pacFileUrl) {
205 if (exclList != null) exclList = exclList.replace(",", "|");
206 if (false) Log.d(TAG, "setHttpProxySystemProperty :"+host+":"+port+" - "+exclList);
207 if (host != null) {
208 System.setProperty("http.proxyHost", host);
209 System.setProperty("https.proxyHost", host);
210 } else {
211 System.clearProperty("http.proxyHost");
212 System.clearProperty("https.proxyHost");
213 }
214 if (port != null) {
215 System.setProperty("http.proxyPort", port);
216 System.setProperty("https.proxyPort", port);
217 } else {
218 System.clearProperty("http.proxyPort");
219 System.clearProperty("https.proxyPort");
220 }
221 if (exclList != null) {
222 System.setProperty("http.nonProxyHosts", exclList);
223 System.setProperty("https.nonProxyHosts", exclList);
224 } else {
225 System.clearProperty("http.nonProxyHosts");
226 System.clearProperty("https.nonProxyHosts");
227 }
228 if (!Uri.EMPTY.equals(pacFileUrl)) {
229 ProxySelector.setDefault(new PacProxySelector());
230 } else {
231 ProxySelector.setDefault(sDefaultProxySelector);
232 }
233 }
234}