1 /* 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 17 package android.net; 18 19 import android.annotation.Nullable; 20 import android.annotation.SdkConstant; 21 import android.annotation.SdkConstant.SdkConstantType; 22 import android.annotation.SystemApi; 23 import android.compat.annotation.UnsupportedAppUsage; 24 import android.content.Context; 25 import android.os.Build; 26 import android.text.TextUtils; 27 import android.util.Log; 28 29 import com.android.net.module.util.ProxyUtils; 30 31 import java.net.InetSocketAddress; 32 import java.net.ProxySelector; 33 import java.net.URI; 34 import java.util.List; 35 36 /** 37 * A convenience class for accessing the user and default proxy 38 * settings. 39 */ 40 public 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 * @removed 64 */ 65 @Deprecated 66 public static final String EXTRA_PROXY_INFO = "android.intent.extra.PROXY_INFO"; 67 68 private static ConnectivityManager sConnectivityManager = null; 69 70 static { 71 sDefaultProxySelector = ProxySelector.getDefault(); 72 } 73 74 /** 75 * Return the proxy object to be used for the URL given as parameter. 76 * @param ctx A Context used to get the settings for the proxy host. 77 * @param url A URL to be accessed. Used to evaluate exclusion list. 78 * @return Proxy (java.net) object containing the host name. If the 79 * user did not set a hostname it returns the default host. 80 * A null value means that no host is to be used. 81 * {@hide} 82 */ 83 @UnsupportedAppUsage getProxy(Context ctx, String url)84 public static final java.net.Proxy getProxy(Context ctx, String url) { 85 String host = ""; 86 if ((url != null) && !isLocalHost(host)) { 87 URI uri = URI.create(url); 88 ProxySelector proxySelector = ProxySelector.getDefault(); 89 90 List<java.net.Proxy> proxyList = proxySelector.select(uri); 91 92 if (proxyList.size() > 0) { 93 return proxyList.get(0); 94 } 95 } 96 return java.net.Proxy.NO_PROXY; 97 } 98 99 100 /** 101 * Return the proxy host set by the user. 102 * @param ctx A Context used to get the settings for the proxy host. 103 * @return String containing the host name. If the user did not set a host 104 * name it returns the default host. A null value means that no 105 * host is to be used. 106 * @deprecated Use standard java vm proxy values to find the host, port 107 * and exclusion list. This call ignores the exclusion list. 108 */ 109 @Deprecated getHost(Context ctx)110 public static final String getHost(Context ctx) { 111 java.net.Proxy proxy = getProxy(ctx, null); 112 if (proxy == java.net.Proxy.NO_PROXY) return null; 113 try { 114 return ((InetSocketAddress)(proxy.address())).getHostName(); 115 } catch (Exception e) { 116 return null; 117 } 118 } 119 120 /** 121 * Return the proxy port set by the user. 122 * @param ctx A Context used to get the settings for the proxy port. 123 * @return The port number to use or -1 if no proxy is to be used. 124 * @deprecated Use standard java vm proxy values to find the host, port 125 * and exclusion list. This call ignores the exclusion list. 126 */ 127 @Deprecated getPort(Context ctx)128 public static final int getPort(Context ctx) { 129 java.net.Proxy proxy = getProxy(ctx, null); 130 if (proxy == java.net.Proxy.NO_PROXY) return -1; 131 try { 132 return ((InetSocketAddress)(proxy.address())).getPort(); 133 } catch (Exception e) { 134 return -1; 135 } 136 } 137 138 /** 139 * Return the default proxy host specified by the carrier. 140 * @return String containing the host name or null if there is no proxy for 141 * this carrier. 142 * @deprecated Use standard java vm proxy values to find the host, port and 143 * exclusion list. This call ignores the exclusion list and no 144 * longer reports only mobile-data apn-based proxy values. 145 */ 146 @Deprecated getDefaultHost()147 public static final String getDefaultHost() { 148 String host = System.getProperty("http.proxyHost"); 149 if (TextUtils.isEmpty(host)) return null; 150 return host; 151 } 152 153 /** 154 * Return the default proxy port specified by the carrier. 155 * @return The port number to be used with the proxy host or -1 if there is 156 * no proxy for this carrier. 157 * @deprecated Use standard java vm proxy values to find the host, port and 158 * exclusion list. This call ignores the exclusion list and no 159 * longer reports only mobile-data apn-based proxy values. 160 */ 161 @Deprecated getDefaultPort()162 public static final int getDefaultPort() { 163 if (getDefaultHost() == null) return -1; 164 try { 165 return Integer.parseInt(System.getProperty("http.proxyPort")); 166 } catch (NumberFormatException e) { 167 return -1; 168 } 169 } 170 isLocalHost(String host)171 private static final boolean isLocalHost(String host) { 172 if (host == null) { 173 return false; 174 } 175 try { 176 if (host != null) { 177 if (host.equalsIgnoreCase("localhost")) { 178 return true; 179 } 180 if (InetAddresses.parseNumericAddress(host).isLoopbackAddress()) { 181 return true; 182 } 183 } 184 } catch (IllegalArgumentException iex) { 185 } 186 return false; 187 } 188 189 /** @hide */ 190 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) 191 @Deprecated setHttpProxySystemProperty(ProxyInfo p)192 public static void setHttpProxySystemProperty(ProxyInfo p) { 193 setHttpProxyConfiguration(p); 194 } 195 196 /** 197 * Set HTTP proxy configuration for the process to match the provided ProxyInfo. 198 * 199 * If the provided ProxyInfo is null, the proxy configuration will be cleared. 200 * @hide 201 */ 202 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) setHttpProxyConfiguration(@ullable ProxyInfo p)203 public static void setHttpProxyConfiguration(@Nullable ProxyInfo p) { 204 String host = null; 205 String port = null; 206 String exclList = null; 207 Uri pacFileUrl = Uri.EMPTY; 208 if (p != null) { 209 host = p.getHost(); 210 port = Integer.toString(p.getPort()); 211 exclList = ProxyUtils.exclusionListAsString(p.getExclusionList()); 212 pacFileUrl = p.getPacFileUrl(); 213 } 214 setHttpProxyConfiguration(host, port, exclList, pacFileUrl); 215 } 216 217 /** @hide */ setHttpProxyConfiguration(String host, String port, String exclList, Uri pacFileUrl)218 public static void setHttpProxyConfiguration(String host, String port, String exclList, 219 Uri pacFileUrl) { 220 if (exclList != null) exclList = exclList.replace(",", "|"); 221 if (false) Log.d(TAG, "setHttpProxySystemProperty :"+host+":"+port+" - "+exclList); 222 if (host != null) { 223 System.setProperty("http.proxyHost", host); 224 System.setProperty("https.proxyHost", host); 225 } else { 226 System.clearProperty("http.proxyHost"); 227 System.clearProperty("https.proxyHost"); 228 } 229 if (port != null) { 230 System.setProperty("http.proxyPort", port); 231 System.setProperty("https.proxyPort", port); 232 } else { 233 System.clearProperty("http.proxyPort"); 234 System.clearProperty("https.proxyPort"); 235 } 236 if (exclList != null) { 237 System.setProperty("http.nonProxyHosts", exclList); 238 System.setProperty("https.nonProxyHosts", exclList); 239 } else { 240 System.clearProperty("http.nonProxyHosts"); 241 System.clearProperty("https.nonProxyHosts"); 242 } 243 if (!Uri.EMPTY.equals(pacFileUrl)) { 244 ProxySelector.setDefault(new PacProxySelector()); 245 } else { 246 ProxySelector.setDefault(sDefaultProxySelector); 247 } 248 } 249 } 250