1/*
2 * Copyright (C) 2013 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.os.RemoteException;
20import android.os.ServiceManager;
21import android.util.Log;
22
23import com.android.net.IProxyService;
24import com.google.android.collect.Lists;
25
26import java.io.IOException;
27import java.net.InetSocketAddress;
28import java.net.MalformedURLException;
29import java.net.Proxy;
30import java.net.Proxy.Type;
31import java.net.ProxySelector;
32import java.net.SocketAddress;
33import java.net.URI;
34import java.util.List;
35
36/**
37 * @hide
38 */
39public class PacProxySelector extends ProxySelector {
40    private static final String TAG = "PacProxySelector";
41    public static final String PROXY_SERVICE = "com.android.net.IProxyService";
42    private static final String SOCKS = "SOCKS ";
43    private static final String PROXY = "PROXY ";
44
45    private IProxyService mProxyService;
46    private final List<Proxy> mDefaultList;
47
48    public PacProxySelector() {
49        mProxyService = IProxyService.Stub.asInterface(
50                ServiceManager.getService(PROXY_SERVICE));
51        if (mProxyService == null) {
52            // Added because of b10267814 where mako is restarting.
53            Log.e(TAG, "PacManager: no proxy service");
54        }
55        mDefaultList = Lists.newArrayList(java.net.Proxy.NO_PROXY);
56    }
57
58    @Override
59    public List<Proxy> select(URI uri) {
60        if (mProxyService == null) {
61            mProxyService = IProxyService.Stub.asInterface(
62                    ServiceManager.getService(PROXY_SERVICE));
63        }
64        if (mProxyService == null) {
65            Log.e(TAG, "select: no proxy service return NO_PROXY");
66            return Lists.newArrayList(java.net.Proxy.NO_PROXY);
67        }
68        String response = null;
69        String urlString;
70        try {
71            urlString = uri.toURL().toString();
72        } catch (MalformedURLException e) {
73            urlString = uri.getHost();
74        }
75        try {
76            response = mProxyService.resolvePacFile(uri.getHost(), urlString);
77        } catch (RemoteException e) {
78            e.printStackTrace();
79        }
80        if (response == null) {
81            return mDefaultList;
82        }
83
84        return parseResponse(response);
85    }
86
87    private static List<Proxy> parseResponse(String response) {
88        String[] split = response.split(";");
89        List<Proxy> ret = Lists.newArrayList();
90        for (String s : split) {
91            String trimmed = s.trim();
92            if (trimmed.equals("DIRECT")) {
93                ret.add(java.net.Proxy.NO_PROXY);
94            } else if (trimmed.startsWith(PROXY)) {
95                Proxy proxy = proxyFromHostPort(Type.HTTP, trimmed.substring(PROXY.length()));
96                if (proxy != null) {
97                    ret.add(proxy);
98                }
99            } else if (trimmed.startsWith(SOCKS)) {
100                Proxy proxy = proxyFromHostPort(Type.SOCKS, trimmed.substring(SOCKS.length()));
101                if (proxy != null) {
102                    ret.add(proxy);
103                }
104            }
105        }
106        if (ret.size() == 0) {
107            ret.add(java.net.Proxy.NO_PROXY);
108        }
109        return ret;
110    }
111
112    private static Proxy proxyFromHostPort(Proxy.Type type, String hostPortString) {
113        try {
114            String[] hostPort = hostPortString.split(":");
115            String host = hostPort[0];
116            int port = Integer.parseInt(hostPort[1]);
117            return new Proxy(type, InetSocketAddress.createUnresolved(host, port));
118        } catch (NumberFormatException|ArrayIndexOutOfBoundsException e) {
119            Log.d(TAG, "Unable to parse proxy " + hostPortString + " " + e);
120            return null;
121        }
122    }
123
124    @Override
125    public void connectFailed(URI uri, SocketAddress address, IOException failure) {
126
127    }
128
129}
130