Proxy.java revision 778c0ba8586a4494ed617c2d3cb3692311df1086
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
17package android.net;
18
19import android.annotation.SdkConstant;
20import android.annotation.SdkConstant.SdkConstantType;
21import android.content.ContentResolver;
22import android.content.Context;
23import android.database.ContentObserver;
24import android.net.ProxyProperties;
25import android.os.Handler;
26import android.os.SystemProperties;
27import android.text.TextUtils;
28import android.provider.Settings;
29import android.util.Log;
30
31import java.net.InetAddress;
32import java.net.InetSocketAddress;
33import java.net.ProxySelector;
34import java.net.SocketAddress;
35import java.net.URI;
36import java.net.UnknownHostException;
37import java.util.concurrent.locks.ReadWriteLock;
38import java.util.concurrent.locks.ReentrantReadWriteLock;
39import java.util.List;
40import java.util.regex.Matcher;
41import java.util.regex.Pattern;
42
43import junit.framework.Assert;
44
45import org.apache.http.conn.routing.HttpRoute;
46import org.apache.http.conn.routing.HttpRoutePlanner;
47import org.apache.http.conn.scheme.SchemeRegistry;
48import org.apache.http.HttpHost;
49import org.apache.http.HttpRequest;
50import org.apache.http.impl.conn.ProxySelectorRoutePlanner;
51import org.apache.http.protocol.HttpContext;
52
53/**
54 * A convenience class for accessing the user and default proxy
55 * settings.
56 */
57public final class Proxy {
58
59    // Set to true to enable extra debugging.
60    private static final boolean DEBUG = false;
61    private static final String TAG = "Proxy";
62
63    /**
64     * Used to notify an app that's caching the default connection proxy
65     * that either the default connection or its proxy has changed.
66     * The intent will have the following extra value:</p>
67     * <ul>
68     *   <li><em>EXTRA_PROXY_INFO</em> - The ProxyProperties for the proxy
69     * </ul>
70     *
71     * <p class="note">This is a protected intent that can only be sent by the system
72     */
73    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
74    public static final String PROXY_CHANGE_ACTION = "android.intent.action.PROXY_CHANGE";
75    /** {@hide} **/
76    public static final String EXTRA_PROXY_INFO = "proxy";
77
78    private static ConnectivityManager sConnectivityManager = null;
79
80    // Hostname / IP REGEX validation
81    // Matches blank input, ips, and domain names
82    private static final String NAME_IP_REGEX =
83        "[a-zA-Z0-9]+(\\-[a-zA-Z0-9]+)*(\\.[a-zA-Z0-9]+(\\-[a-zA-Z0-9]+)*)*";
84
85    private static final String HOSTNAME_REGEXP = "^$|^" + NAME_IP_REGEX + "$";
86
87    private static final Pattern HOSTNAME_PATTERN;
88
89    private static final String EXCLLIST_REGEXP = "$|^(.?" + NAME_IP_REGEX
90        + ")+(,(.?" + NAME_IP_REGEX + "))*$";
91
92    private static final Pattern EXCLLIST_PATTERN;
93
94    static {
95        HOSTNAME_PATTERN = Pattern.compile(HOSTNAME_REGEXP);
96        EXCLLIST_PATTERN = Pattern.compile(EXCLLIST_REGEXP);
97    }
98
99    /**
100     * Return the proxy object to be used for the URL given as parameter.
101     * @param ctx A Context used to get the settings for the proxy host.
102     * @param url A URL to be accessed. Used to evaluate exclusion list.
103     * @return Proxy (java.net) object containing the host name. If the
104     *         user did not set a hostname it returns the default host.
105     *         A null value means that no host is to be used.
106     * {@hide}
107     */
108    public static final java.net.Proxy getProxy(Context ctx, String url) {
109        String host = "";
110        if (url != null) {
111            URI uri = URI.create(url);
112            host = uri.getHost();
113        }
114
115        if (!isLocalHost(host)) {
116            if (sConnectivityManager == null) {
117                sConnectivityManager = (ConnectivityManager)ctx.getSystemService(
118                        Context.CONNECTIVITY_SERVICE);
119            }
120            if (sConnectivityManager == null) return java.net.Proxy.NO_PROXY;
121
122            ProxyProperties proxyProperties = sConnectivityManager.getProxy();
123
124            if (proxyProperties != null) {
125                if (!proxyProperties.isExcluded(host)) {
126                    return proxyProperties.makeProxy();
127                }
128            }
129        }
130        return java.net.Proxy.NO_PROXY;
131    }
132
133
134    /**
135     * Return the proxy host set by the user.
136     * @param ctx A Context used to get the settings for the proxy host.
137     * @return String containing the host name. If the user did not set a host
138     *         name it returns the default host. A null value means that no
139     *         host is to be used.
140     * @deprecated Use standard java vm proxy values to find the host, port
141     *         and exclusion list.  This call ignores the exclusion list.
142     */
143    public static final String getHost(Context ctx) {
144        java.net.Proxy proxy = getProxy(ctx, null);
145        if (proxy == java.net.Proxy.NO_PROXY) return null;
146        try {
147            return ((InetSocketAddress)(proxy.address())).getHostName();
148        } catch (Exception e) {
149            return null;
150        }
151    }
152
153    /**
154     * Return the proxy port set by the user.
155     * @param ctx A Context used to get the settings for the proxy port.
156     * @return The port number to use or -1 if no proxy is to be used.
157     * @deprecated Use standard java vm proxy values to find the host, port
158     *         and exclusion list.  This call ignores the exclusion list.
159     */
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    public static final String getDefaultHost() {
179        String host = System.getProperty("http.proxyHost");
180        if (TextUtils.isEmpty(host)) return null;
181        return host;
182    }
183
184    /**
185     * Return the default proxy port specified by the carrier.
186     * @return The port number to be used with the proxy host or -1 if there is
187     * no proxy for this carrier.
188     * @deprecated Use standard java vm proxy values to find the host, port and
189     *         exclusion list.  This call ignores the exclusion list and no
190     *         longer reports only mobile-data apn-based proxy values.
191     */
192    public static final int getDefaultPort() {
193        if (getDefaultHost() == null) return -1;
194        try {
195            return Integer.parseInt(System.getProperty("http.proxyPort"));
196        } catch (NumberFormatException e) {
197            return -1;
198        }
199    }
200
201    /**
202     * Returns the preferred proxy to be used by clients. This is a wrapper
203     * around {@link android.net.Proxy#getHost()}.
204     *
205     * @param context the context which will be passed to
206     * {@link android.net.Proxy#getHost()}
207     * @param url the target URL for the request
208     * @note Calling this method requires permission
209     * android.permission.ACCESS_NETWORK_STATE
210     * @return The preferred proxy to be used by clients, or null if there
211     * is no proxy.
212     * {@hide}
213     */
214    public static final HttpHost getPreferredHttpHost(Context context,
215            String url) {
216        java.net.Proxy prefProxy = getProxy(context, url);
217        if (prefProxy.equals(java.net.Proxy.NO_PROXY)) {
218            return null;
219        } else {
220            InetSocketAddress sa = (InetSocketAddress)prefProxy.address();
221            return new HttpHost(sa.getHostName(), sa.getPort(), "http");
222        }
223    }
224
225    private static final boolean isLocalHost(String host) {
226        if (host == null) {
227            return false;
228        }
229        try {
230            if (host != null) {
231                if (host.equalsIgnoreCase("localhost")) {
232                    return true;
233                }
234                // Check we have a numeric address so we don't cause a DNS lookup in getByName.
235                if (InetAddress.isNumeric(host)) {
236                    if (InetAddress.getByName(host).isLoopbackAddress()) {
237                        return true;
238                    }
239                }
240            }
241        } catch (UnknownHostException ignored) {
242            // Can't happen for a numeric address (InetAddress.getByName).
243        } catch (IllegalArgumentException iex) {
244            // Ignore (URI.create)
245        }
246        return false;
247    }
248
249    /**
250     * Validate syntax of hostname, port and exclusion list entries
251     * {@hide}
252     */
253    public static void validate(String hostname, String port, String exclList) {
254        Matcher match = HOSTNAME_PATTERN.matcher(hostname);
255        Matcher listMatch = EXCLLIST_PATTERN.matcher(exclList);
256
257        if (!match.matches()) {
258            throw new IllegalArgumentException();
259        }
260
261        if (!listMatch.matches()) {
262            throw new IllegalArgumentException();
263        }
264
265        if (hostname.length() > 0 && port.length() == 0) {
266            throw new IllegalArgumentException();
267        }
268
269        if (port.length() > 0) {
270            if (hostname.length() == 0) {
271                throw new IllegalArgumentException();
272            }
273            int portVal = -1;
274            try {
275                portVal = Integer.parseInt(port);
276            } catch (NumberFormatException ex) {
277                throw new IllegalArgumentException();
278            }
279            if (portVal <= 0 || portVal > 0xFFFF) {
280                throw new IllegalArgumentException();
281            }
282        }
283    }
284
285    static class AndroidProxySelectorRoutePlanner
286            extends org.apache.http.impl.conn.ProxySelectorRoutePlanner {
287
288        private Context mContext;
289
290        public AndroidProxySelectorRoutePlanner(SchemeRegistry schreg, ProxySelector prosel,
291                Context context) {
292            super(schreg, prosel);
293            mContext = context;
294        }
295
296        @Override
297        protected java.net.Proxy chooseProxy(List<java.net.Proxy> proxies, HttpHost target,
298                HttpRequest request, HttpContext context) {
299            return getProxy(mContext, target.getHostName());
300        }
301
302        @Override
303        protected HttpHost determineProxy(HttpHost target, HttpRequest request,
304                HttpContext context) {
305            return getPreferredHttpHost(mContext, target.getHostName());
306        }
307
308        @Override
309        public HttpRoute determineRoute(HttpHost target, HttpRequest request,
310                HttpContext context) {
311            HttpHost proxy = getPreferredHttpHost(mContext, target.getHostName());
312            if (proxy == null) {
313                return new HttpRoute(target);
314            } else {
315                return new HttpRoute(target, null, proxy, false);
316            }
317        }
318    }
319
320    /** @hide */
321    public static final HttpRoutePlanner getAndroidProxySelectorRoutePlanner(Context context) {
322        AndroidProxySelectorRoutePlanner ret = new AndroidProxySelectorRoutePlanner(
323                new SchemeRegistry(), ProxySelector.getDefault(), context);
324        return ret;
325    }
326
327    /** @hide */
328    public static final void setHttpProxySystemProperty(ProxyProperties p) {
329        String host = null;
330        String port = null;
331        String exclList = null;
332        if (p != null) {
333            host = p.getHost();
334            port = Integer.toString(p.getPort());
335            exclList = p.getExclusionList();
336        }
337        setHttpProxySystemProperty(host, port, exclList);
338    }
339
340    /** @hide */
341    public static final void setHttpProxySystemProperty(String host, String port, String exclList) {
342        if (exclList != null) exclList = exclList.replace(",", "|");
343        if (false) Log.d(TAG, "setHttpProxySystemProperty :"+host+":"+port+" - "+exclList);
344        if (host != null) {
345            System.setProperty("http.proxyHost", host);
346            System.setProperty("https.proxyHost", host);
347        } else {
348            System.clearProperty("http.proxyHost");
349            System.clearProperty("https.proxyHost");
350        }
351        if (port != null) {
352            System.setProperty("http.proxyPort", port);
353            System.setProperty("https.proxyPort", port);
354        } else {
355            System.clearProperty("http.proxyPort");
356            System.clearProperty("https.proxyPort");
357        }
358        if (exclList != null) {
359            System.setProperty("http.nonProxyHosts", exclList);
360            System.setProperty("https.nonProxyHosts", exclList);
361        } else {
362            System.clearProperty("http.nonProxyHosts");
363            System.clearProperty("https.nonProxyHosts");
364        }
365    }
366}
367