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