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