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