blob: 77c8a4f4579bb4d663121920ce4535faba747904 [file] [log] [blame]
Remi NGUYEN VAN0f8f6302021-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 VANb33335c2021-02-03 10:18:20 +090019import android.annotation.Nullable;
Remi NGUYEN VAN0f8f6302021-01-15 18:08:24 +090020import android.annotation.SdkConstant;
21import android.annotation.SdkConstant.SdkConstantType;
Remi NGUYEN VANb33335c2021-02-03 10:18:20 +090022import android.annotation.SystemApi;
Remi NGUYEN VAN0f8f6302021-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;
Remi NGUYEN VAN0f8f6302021-01-15 18:08:24 +090035
36/**
37 * A convenience class for accessing the user and default proxy
38 * settings.
39 */
40public final class Proxy {
41
42 private static final String TAG = "Proxy";
43
44 private static final ProxySelector sDefaultProxySelector;
45
46 /**
47 * Used to notify an app that's caching the proxy that either the default
48 * connection has changed or any connection's proxy has changed. The new
49 * proxy should be queried using {@link ConnectivityManager#getDefaultProxy()}.
50 *
51 * <p class="note">This is a protected intent that can only be sent by the system
52 */
53 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
54 public static final String PROXY_CHANGE_ACTION = "android.intent.action.PROXY_CHANGE";
55 /**
56 * Intent extra included with {@link #PROXY_CHANGE_ACTION} intents.
57 * It describes the new proxy being used (as a {@link ProxyInfo} object).
58 * @deprecated Because {@code PROXY_CHANGE_ACTION} is sent whenever the proxy
59 * for any network on the system changes, applications should always use
60 * {@link ConnectivityManager#getDefaultProxy()} or
61 * {@link ConnectivityManager#getLinkProperties(Network)}.{@link LinkProperties#getHttpProxy()}
62 * to get the proxy for the Network(s) they are using.
63 */
64 @Deprecated
65 public static final String EXTRA_PROXY_INFO = "android.intent.extra.PROXY_INFO";
66
Remi NGUYEN VAN0f8f6302021-01-15 18:08:24 +090067 private static ConnectivityManager sConnectivityManager = null;
68
Remi NGUYEN VAN0f8f6302021-01-15 18:08:24 +090069 static {
Remi NGUYEN VAN0f8f6302021-01-15 18:08:24 +090070 sDefaultProxySelector = ProxySelector.getDefault();
71 }
72
73 /**
74 * Return the proxy object to be used for the URL given as parameter.
75 * @param ctx A Context used to get the settings for the proxy host.
76 * @param url A URL to be accessed. Used to evaluate exclusion list.
77 * @return Proxy (java.net) object containing the host name. If the
78 * user did not set a hostname it returns the default host.
79 * A null value means that no host is to be used.
80 * {@hide}
81 */
82 @UnsupportedAppUsage
83 public static final java.net.Proxy getProxy(Context ctx, String url) {
84 String host = "";
85 if ((url != null) && !isLocalHost(host)) {
86 URI uri = URI.create(url);
87 ProxySelector proxySelector = ProxySelector.getDefault();
88
89 List<java.net.Proxy> proxyList = proxySelector.select(uri);
90
91 if (proxyList.size() > 0) {
92 return proxyList.get(0);
93 }
94 }
95 return java.net.Proxy.NO_PROXY;
96 }
97
98
99 /**
100 * Return the proxy host set by the user.
101 * @param ctx A Context used to get the settings for the proxy host.
102 * @return String containing the host name. If the user did not set a host
103 * name it returns the default host. A null value means that no
104 * host is to be used.
105 * @deprecated Use standard java vm proxy values to find the host, port
106 * and exclusion list. This call ignores the exclusion list.
107 */
108 @Deprecated
109 public static final String getHost(Context ctx) {
110 java.net.Proxy proxy = getProxy(ctx, null);
111 if (proxy == java.net.Proxy.NO_PROXY) return null;
112 try {
113 return ((InetSocketAddress)(proxy.address())).getHostName();
114 } catch (Exception e) {
115 return null;
116 }
117 }
118
119 /**
120 * Return the proxy port set by the user.
121 * @param ctx A Context used to get the settings for the proxy port.
122 * @return The port number to use or -1 if no proxy is to be used.
123 * @deprecated Use standard java vm proxy values to find the host, port
124 * and exclusion list. This call ignores the exclusion list.
125 */
126 @Deprecated
127 public static final int getPort(Context ctx) {
128 java.net.Proxy proxy = getProxy(ctx, null);
129 if (proxy == java.net.Proxy.NO_PROXY) return -1;
130 try {
131 return ((InetSocketAddress)(proxy.address())).getPort();
132 } catch (Exception e) {
133 return -1;
134 }
135 }
136
137 /**
138 * Return the default proxy host specified by the carrier.
139 * @return String containing the host name or null if there is no proxy for
140 * this carrier.
141 * @deprecated Use standard java vm proxy values to find the host, port and
142 * exclusion list. This call ignores the exclusion list and no
143 * longer reports only mobile-data apn-based proxy values.
144 */
145 @Deprecated
146 public static final String getDefaultHost() {
147 String host = System.getProperty("http.proxyHost");
148 if (TextUtils.isEmpty(host)) return null;
149 return host;
150 }
151
152 /**
153 * Return the default proxy port specified by the carrier.
154 * @return The port number to be used with the proxy host or -1 if there is
155 * no proxy for this carrier.
156 * @deprecated Use standard java vm proxy values to find the host, port and
157 * exclusion list. This call ignores the exclusion list and no
158 * longer reports only mobile-data apn-based proxy values.
159 */
160 @Deprecated
161 public static final int getDefaultPort() {
162 if (getDefaultHost() == null) return -1;
163 try {
164 return Integer.parseInt(System.getProperty("http.proxyPort"));
165 } catch (NumberFormatException e) {
166 return -1;
167 }
168 }
169
170 private static final boolean isLocalHost(String host) {
171 if (host == null) {
172 return false;
173 }
174 try {
175 if (host != null) {
176 if (host.equalsIgnoreCase("localhost")) {
177 return true;
178 }
179 if (InetAddresses.parseNumericAddress(host).isLoopbackAddress()) {
180 return true;
181 }
182 }
183 } catch (IllegalArgumentException iex) {
184 }
185 return false;
186 }
187
Remi NGUYEN VAN0f8f6302021-01-15 18:08:24 +0900188 /** @hide */
189 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Remi NGUYEN VANb33335c2021-02-03 10:18:20 +0900190 @Deprecated
191 public static void setHttpProxySystemProperty(ProxyInfo p) {
192 setHttpProxyConfiguration(p);
193 }
194
195 /**
196 * Set HTTP proxy configuration for the process to match the provided ProxyInfo.
197 *
198 * If the provided ProxyInfo is null, the proxy configuration will be cleared.
199 * @hide
200 */
201 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
202 public static void setHttpProxyConfiguration(@Nullable ProxyInfo p) {
Remi NGUYEN VAN0f8f6302021-01-15 18:08:24 +0900203 String host = null;
204 String port = null;
205 String exclList = null;
206 Uri pacFileUrl = Uri.EMPTY;
207 if (p != null) {
208 host = p.getHost();
209 port = Integer.toString(p.getPort());
210 exclList = ProxyUtils.exclusionListAsString(p.getExclusionList());
211 pacFileUrl = p.getPacFileUrl();
212 }
Remi NGUYEN VANb33335c2021-02-03 10:18:20 +0900213 setHttpProxyConfiguration(host, port, exclList, pacFileUrl);
Remi NGUYEN VAN0f8f6302021-01-15 18:08:24 +0900214 }
215
216 /** @hide */
Remi NGUYEN VANb33335c2021-02-03 10:18:20 +0900217 public static void setHttpProxyConfiguration(String host, String port, String exclList,
Remi NGUYEN VAN0f8f6302021-01-15 18:08:24 +0900218 Uri pacFileUrl) {
219 if (exclList != null) exclList = exclList.replace(",", "|");
220 if (false) Log.d(TAG, "setHttpProxySystemProperty :"+host+":"+port+" - "+exclList);
221 if (host != null) {
222 System.setProperty("http.proxyHost", host);
223 System.setProperty("https.proxyHost", host);
224 } else {
225 System.clearProperty("http.proxyHost");
226 System.clearProperty("https.proxyHost");
227 }
228 if (port != null) {
229 System.setProperty("http.proxyPort", port);
230 System.setProperty("https.proxyPort", port);
231 } else {
232 System.clearProperty("http.proxyPort");
233 System.clearProperty("https.proxyPort");
234 }
235 if (exclList != null) {
236 System.setProperty("http.nonProxyHosts", exclList);
237 System.setProperty("https.nonProxyHosts", exclList);
238 } else {
239 System.clearProperty("http.nonProxyHosts");
240 System.clearProperty("https.nonProxyHosts");
241 }
242 if (!Uri.EMPTY.equals(pacFileUrl)) {
243 ProxySelector.setDefault(new PacProxySelector());
244 } else {
245 ProxySelector.setDefault(sDefaultProxySelector);
246 }
247 }
248}