Proxy.java revision 434203a277cd2f237a71508a3d5a7d1602126cd5
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    // TODO: deprecate this function
135    /**
136     * Return the proxy host set by the user.
137     * @param ctx A Context used to get the settings for the proxy host.
138     * @return String containing the host name. If the user did not set a host
139     *         name it returns the default host. A null value means that no
140     *         host is to be used.
141     */
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    // TODO: deprecate this function
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     */
158    public static final int getPort(Context ctx) {
159        java.net.Proxy proxy = getProxy(ctx, null);
160        if (proxy == java.net.Proxy.NO_PROXY) return -1;
161        try {
162            return ((InetSocketAddress)(proxy.address())).getPort();
163        } catch (Exception e) {
164            return -1;
165        }
166    }
167
168    // TODO: deprecate this function
169    /**
170     * Return the default proxy host specified by the carrier.
171     * @return String containing the host name or null if there is no proxy for
172     * this carrier.
173     */
174    public static final String getDefaultHost() {
175        return null;
176    }
177
178    // TODO: deprecate this function
179    /**
180     * Return the default proxy port specified by the carrier.
181     * @return The port number to be used with the proxy host or -1 if there is
182     * no proxy for this carrier.
183     */
184    public static final int getDefaultPort() {
185        return -1;
186    }
187
188    // TODO: remove this function / deprecate
189    /**
190     * Returns the preferred proxy to be used by clients. This is a wrapper
191     * around {@link android.net.Proxy#getHost()}. Currently no proxy will
192     * be returned for localhost or if the active network is Wi-Fi.
193     *
194     * @param context the context which will be passed to
195     * {@link android.net.Proxy#getHost()}
196     * @param url the target URL for the request
197     * @note Calling this method requires permission
198     * android.permission.ACCESS_NETWORK_STATE
199     * @return The preferred proxy to be used by clients, or null if there
200     * is no proxy.
201     * {@hide}
202     */
203    public static final HttpHost getPreferredHttpHost(Context context,
204            String url) {
205        java.net.Proxy prefProxy = getProxy(context, url);
206        if (prefProxy.equals(java.net.Proxy.NO_PROXY)) {
207            return null;
208        } else {
209            InetSocketAddress sa = (InetSocketAddress)prefProxy.address();
210            return new HttpHost(sa.getHostName(), sa.getPort(), "http");
211        }
212    }
213
214    private static final boolean isLocalHost(String host) {
215        if (host == null) {
216            return false;
217        }
218        try {
219            if (host != null) {
220                if (host.equalsIgnoreCase("localhost")) {
221                    return true;
222                }
223                // Check we have a numeric address so we don't cause a DNS lookup in getByName.
224                if (InetAddress.isNumeric(host)) {
225                    if (InetAddress.getByName(host).isLoopbackAddress()) {
226                        return true;
227                    }
228                }
229            }
230        } catch (UnknownHostException ignored) {
231            // Can't happen for a numeric address (InetAddress.getByName).
232        } catch (IllegalArgumentException iex) {
233            // Ignore (URI.create)
234        }
235        return false;
236    }
237
238    /**
239     * Validate syntax of hostname, port and exclusion list entries
240     * {@hide}
241     */
242    public static void validate(String hostname, String port, String exclList) {
243        Matcher match = HOSTNAME_PATTERN.matcher(hostname);
244        Matcher listMatch = EXCLLIST_PATTERN.matcher(exclList);
245
246        if (!match.matches()) {
247            throw new IllegalArgumentException();
248        }
249
250        if (!listMatch.matches()) {
251            throw new IllegalArgumentException();
252        }
253
254        if (hostname.length() > 0 && port.length() == 0) {
255            throw new IllegalArgumentException();
256        }
257
258        if (port.length() > 0) {
259            if (hostname.length() == 0) {
260                throw new IllegalArgumentException();
261            }
262            int portVal = -1;
263            try {
264                portVal = Integer.parseInt(port);
265            } catch (NumberFormatException ex) {
266                throw new IllegalArgumentException();
267            }
268            if (portVal <= 0 || portVal > 0xFFFF) {
269                throw new IllegalArgumentException();
270            }
271        }
272    }
273
274    static class AndroidProxySelectorRoutePlanner
275            extends org.apache.http.impl.conn.ProxySelectorRoutePlanner {
276
277        private Context mContext;
278
279        public AndroidProxySelectorRoutePlanner(SchemeRegistry schreg, ProxySelector prosel,
280                Context context) {
281            super(schreg, prosel);
282            mContext = context;
283        }
284
285        @Override
286        protected java.net.Proxy chooseProxy(List<java.net.Proxy> proxies, HttpHost target,
287                HttpRequest request, HttpContext context) {
288            return getProxy(mContext, target.getHostName());
289        }
290
291        @Override
292        protected HttpHost determineProxy(HttpHost target, HttpRequest request,
293                HttpContext context) {
294            return getPreferredHttpHost(mContext, target.getHostName());
295        }
296
297        @Override
298        public HttpRoute determineRoute(HttpHost target, HttpRequest request,
299                HttpContext context) {
300            HttpHost proxy = getPreferredHttpHost(mContext, target.getHostName());
301            if (proxy == null) {
302                return new HttpRoute(target);
303            } else {
304                return new HttpRoute(target, null, proxy, false);
305            }
306        }
307    }
308
309    /** @hide */
310    public static final HttpRoutePlanner getAndroidProxySelectorRoutePlanner(Context context) {
311        AndroidProxySelectorRoutePlanner ret = new AndroidProxySelectorRoutePlanner(
312                new SchemeRegistry(), ProxySelector.getDefault(), context);
313        return ret;
314    }
315
316    /** @hide */
317    public static final void setHttpProxySystemProperty(ProxyProperties p) {
318        String host = null;
319        String port = null;
320        String exclList = null;
321        if (p != null) {
322            host = p.getHost();
323            port = Integer.toString(p.getPort());
324            exclList = p.getExclusionList();
325        }
326        setHttpProxySystemProperty(host, port, exclList);
327    }
328
329    /** @hide */
330    public static final void setHttpProxySystemProperty(String host, String port, String exclList) {
331        if (exclList != null) exclList = exclList.replace(",", "|");
332        if (false) Log.d(TAG, "setHttpProxySystemProperty :"+host+":"+port+" - "+exclList);
333        if (host != null) {
334            System.setProperty("http.proxyHost", host);
335            System.setProperty("https.proxyHost", host);
336        } else {
337            System.clearProperty("http.proxyHost");
338            System.clearProperty("https.proxyHost");
339        }
340        if (port != null) {
341            System.setProperty("http.proxyPort", port);
342            System.setProperty("https.proxyPort", port);
343        } else {
344            System.clearProperty("http.proxyPort");
345            System.clearProperty("https.proxyPort");
346        }
347        if (exclList != null) {
348            System.setProperty("http.nonProxyHosts", exclList);
349            System.setProperty("https.nonProxyHosts", exclList);
350        } else {
351            System.clearProperty("http.nonProxyHosts");
352            System.clearProperty("https.nonProxyHosts");
353        }
354    }
355}
356