blob: 03cfbbb4a22d80bbe675d1ce579509f63ae05a0f [file] [log] [blame]
Remi NGUYEN VAN8f37b3f2021-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
Remi NGUYEN VANa1860ff2021-02-03 10:18:20 +090019import android.annotation.Nullable;
Remi NGUYEN VAN8f37b3f2021-01-15 18:08:24 +090020import android.annotation.SdkConstant;
21import android.annotation.SdkConstant.SdkConstantType;
Remi NGUYEN VANa1860ff2021-02-03 10:18:20 +090022import android.annotation.SystemApi;
Remi NGUYEN VAN8f37b3f2021-01-15 18:08:24 +090023import android.compat.annotation.UnsupportedAppUsage;
24import android.content.Context;
25import android.os.Build;
26import android.text.TextUtils;
27import android.util.Log;
28
29import com.android.net.module.util.ProxyUtils;
30
31import java.net.InetSocketAddress;
32import java.net.ProxySelector;
33import java.net.URI;
34import java.util.List;
35import java.util.regex.Matcher;
36import java.util.regex.Pattern;
37
38/**
39 * A convenience class for accessing the user and default proxy
40 * settings.
41 */
42public final class Proxy {
43
44 private static final String TAG = "Proxy";
45
46 private static final ProxySelector sDefaultProxySelector;
47
48 /**
49 * Used to notify an app that's caching the proxy that either the default
50 * connection has changed or any connection's proxy has changed. The new
51 * proxy should be queried using {@link ConnectivityManager#getDefaultProxy()}.
52 *
53 * <p class="note">This is a protected intent that can only be sent by the system
54 */
55 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
56 public static final String PROXY_CHANGE_ACTION = "android.intent.action.PROXY_CHANGE";
57 /**
58 * Intent extra included with {@link #PROXY_CHANGE_ACTION} intents.
59 * It describes the new proxy being used (as a {@link ProxyInfo} object).
60 * @deprecated Because {@code PROXY_CHANGE_ACTION} is sent whenever the proxy
61 * for any network on the system changes, applications should always use
62 * {@link ConnectivityManager#getDefaultProxy()} or
63 * {@link ConnectivityManager#getLinkProperties(Network)}.{@link LinkProperties#getHttpProxy()}
64 * to get the proxy for the Network(s) they are using.
65 */
66 @Deprecated
67 public static final String EXTRA_PROXY_INFO = "android.intent.extra.PROXY_INFO";
68
69 /** @hide */
70 public static final int PROXY_VALID = 0;
71 /** @hide */
72 public static final int PROXY_HOSTNAME_EMPTY = 1;
73 /** @hide */
74 public static final int PROXY_HOSTNAME_INVALID = 2;
75 /** @hide */
76 public static final int PROXY_PORT_EMPTY = 3;
77 /** @hide */
78 public static final int PROXY_PORT_INVALID = 4;
79 /** @hide */
80 public static final int PROXY_EXCLLIST_INVALID = 5;
81
82 private static ConnectivityManager sConnectivityManager = null;
83
84 // Hostname / IP REGEX validation
85 // Matches blank input, ips, and domain names
86 private static final String NAME_IP_REGEX =
87 "[a-zA-Z0-9]+(\\-[a-zA-Z0-9]+)*(\\.[a-zA-Z0-9]+(\\-[a-zA-Z0-9]+)*)*";
88
89 private static final String HOSTNAME_REGEXP = "^$|^" + NAME_IP_REGEX + "$";
90
91 private static final Pattern HOSTNAME_PATTERN;
92
93 private static final String EXCL_REGEX =
94 "[a-zA-Z0-9*]+(\\-[a-zA-Z0-9*]+)*(\\.[a-zA-Z0-9*]+(\\-[a-zA-Z0-9*]+)*)*";
95
96 private static final String EXCLLIST_REGEXP = "^$|^" + EXCL_REGEX + "(," + EXCL_REGEX + ")*$";
97
98 private static final Pattern EXCLLIST_PATTERN;
99
100 static {
101 HOSTNAME_PATTERN = Pattern.compile(HOSTNAME_REGEXP);
102 EXCLLIST_PATTERN = Pattern.compile(EXCLLIST_REGEXP);
103 sDefaultProxySelector = ProxySelector.getDefault();
104 }
105
106 /**
107 * Return the proxy object to be used for the URL given as parameter.
108 * @param ctx A Context used to get the settings for the proxy host.
109 * @param url A URL to be accessed. Used to evaluate exclusion list.
110 * @return Proxy (java.net) object containing the host name. If the
111 * user did not set a hostname it returns the default host.
112 * A null value means that no host is to be used.
113 * {@hide}
114 */
115 @UnsupportedAppUsage
116 public static final java.net.Proxy getProxy(Context ctx, String url) {
117 String host = "";
118 if ((url != null) && !isLocalHost(host)) {
119 URI uri = URI.create(url);
120 ProxySelector proxySelector = ProxySelector.getDefault();
121
122 List<java.net.Proxy> proxyList = proxySelector.select(uri);
123
124 if (proxyList.size() > 0) {
125 return proxyList.get(0);
126 }
127 }
128 return java.net.Proxy.NO_PROXY;
129 }
130
131
132 /**
133 * Return the proxy host set by the user.
134 * @param ctx A Context used to get the settings for the proxy host.
135 * @return String containing the host name. If the user did not set a host
136 * name it returns the default host. A null value means that no
137 * host is to be used.
138 * @deprecated Use standard java vm proxy values to find the host, port
139 * and exclusion list. This call ignores the exclusion list.
140 */
141 @Deprecated
142 public static final String getHost(Context ctx) {
143 java.net.Proxy proxy = getProxy(ctx, null);
144 if (proxy == java.net.Proxy.NO_PROXY) return null;
145 try {
146 return ((InetSocketAddress)(proxy.address())).getHostName();
147 } catch (Exception e) {
148 return null;
149 }
150 }
151
152 /**
153 * Return the proxy port set by the user.
154 * @param ctx A Context used to get the settings for the proxy port.
155 * @return The port number to use or -1 if no proxy is to be used.
156 * @deprecated Use standard java vm proxy values to find the host, port
157 * and exclusion list. This call ignores the exclusion list.
158 */
159 @Deprecated
160 public static final int getPort(Context ctx) {
161 java.net.Proxy proxy = getProxy(ctx, null);
162 if (proxy == java.net.Proxy.NO_PROXY) return -1;
163 try {
164 return ((InetSocketAddress)(proxy.address())).getPort();
165 } catch (Exception e) {
166 return -1;
167 }
168 }
169
170 /**
171 * Return the default proxy host specified by the carrier.
172 * @return String containing the host name or null if there is no proxy for
173 * this carrier.
174 * @deprecated Use standard java vm proxy values to find the host, port and
175 * exclusion list. This call ignores the exclusion list and no
176 * longer reports only mobile-data apn-based proxy values.
177 */
178 @Deprecated
179 public static final String getDefaultHost() {
180 String host = System.getProperty("http.proxyHost");
181 if (TextUtils.isEmpty(host)) return null;
182 return host;
183 }
184
185 /**
186 * Return the default proxy port specified by the carrier.
187 * @return The port number to be used with the proxy host or -1 if there is
188 * no proxy for this carrier.
189 * @deprecated Use standard java vm proxy values to find the host, port and
190 * exclusion list. This call ignores the exclusion list and no
191 * longer reports only mobile-data apn-based proxy values.
192 */
193 @Deprecated
194 public static final int getDefaultPort() {
195 if (getDefaultHost() == null) return -1;
196 try {
197 return Integer.parseInt(System.getProperty("http.proxyPort"));
198 } catch (NumberFormatException e) {
199 return -1;
200 }
201 }
202
203 private static final boolean isLocalHost(String host) {
204 if (host == null) {
205 return false;
206 }
207 try {
208 if (host != null) {
209 if (host.equalsIgnoreCase("localhost")) {
210 return true;
211 }
212 if (InetAddresses.parseNumericAddress(host).isLoopbackAddress()) {
213 return true;
214 }
215 }
216 } catch (IllegalArgumentException iex) {
217 }
218 return false;
219 }
220
221 /**
222 * Validate syntax of hostname, port and exclusion list entries
223 * {@hide}
224 */
225 public static int validate(String hostname, String port, String exclList) {
226 Matcher match = HOSTNAME_PATTERN.matcher(hostname);
227 Matcher listMatch = EXCLLIST_PATTERN.matcher(exclList);
228
229 if (!match.matches()) return PROXY_HOSTNAME_INVALID;
230
231 if (!listMatch.matches()) return PROXY_EXCLLIST_INVALID;
232
233 if (hostname.length() > 0 && port.length() == 0) return PROXY_PORT_EMPTY;
234
235 if (port.length() > 0) {
236 if (hostname.length() == 0) return PROXY_HOSTNAME_EMPTY;
237 int portVal = -1;
238 try {
239 portVal = Integer.parseInt(port);
240 } catch (NumberFormatException ex) {
241 return PROXY_PORT_INVALID;
242 }
243 if (portVal <= 0 || portVal > 0xFFFF) return PROXY_PORT_INVALID;
244 }
245 return PROXY_VALID;
246 }
247
248 /** @hide */
249 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Remi NGUYEN VANa1860ff2021-02-03 10:18:20 +0900250 @Deprecated
251 public static void setHttpProxySystemProperty(ProxyInfo p) {
252 setHttpProxyConfiguration(p);
253 }
254
255 /**
256 * Set HTTP proxy configuration for the process to match the provided ProxyInfo.
257 *
258 * If the provided ProxyInfo is null, the proxy configuration will be cleared.
259 * @hide
260 */
261 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
262 public static void setHttpProxyConfiguration(@Nullable ProxyInfo p) {
Remi NGUYEN VAN8f37b3f2021-01-15 18:08:24 +0900263 String host = null;
264 String port = null;
265 String exclList = null;
266 Uri pacFileUrl = Uri.EMPTY;
267 if (p != null) {
268 host = p.getHost();
269 port = Integer.toString(p.getPort());
270 exclList = ProxyUtils.exclusionListAsString(p.getExclusionList());
271 pacFileUrl = p.getPacFileUrl();
272 }
Remi NGUYEN VANa1860ff2021-02-03 10:18:20 +0900273 setHttpProxyConfiguration(host, port, exclList, pacFileUrl);
Remi NGUYEN VAN8f37b3f2021-01-15 18:08:24 +0900274 }
275
276 /** @hide */
Remi NGUYEN VANa1860ff2021-02-03 10:18:20 +0900277 public static void setHttpProxyConfiguration(String host, String port, String exclList,
Remi NGUYEN VAN8f37b3f2021-01-15 18:08:24 +0900278 Uri pacFileUrl) {
279 if (exclList != null) exclList = exclList.replace(",", "|");
280 if (false) Log.d(TAG, "setHttpProxySystemProperty :"+host+":"+port+" - "+exclList);
281 if (host != null) {
282 System.setProperty("http.proxyHost", host);
283 System.setProperty("https.proxyHost", host);
284 } else {
285 System.clearProperty("http.proxyHost");
286 System.clearProperty("https.proxyHost");
287 }
288 if (port != null) {
289 System.setProperty("http.proxyPort", port);
290 System.setProperty("https.proxyPort", port);
291 } else {
292 System.clearProperty("http.proxyPort");
293 System.clearProperty("https.proxyPort");
294 }
295 if (exclList != null) {
296 System.setProperty("http.nonProxyHosts", exclList);
297 System.setProperty("https.nonProxyHosts", exclList);
298 } else {
299 System.clearProperty("http.nonProxyHosts");
300 System.clearProperty("https.nonProxyHosts");
301 }
302 if (!Uri.EMPTY.equals(pacFileUrl)) {
303 ProxySelector.setDefault(new PacProxySelector());
304 } else {
305 ProxySelector.setDefault(sDefaultProxySelector);
306 }
307 }
308}