blob: 9bdf4f6db8e65d8e0360026c4971a31e6a1c5ee9 [file] [log] [blame]
Jason Monk9ced3cd2013-08-12 16:42:38 -04001/*
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 Monk602b2322013-07-03 17:04:33 -040016
17package android.net;
18
Jason Monk602b2322013-07-03 17:04:33 -040019import android.os.ServiceManager;
Wink Savillea48ad8b2013-08-10 11:22:31 -070020import android.util.Log;
Jason Monk602b2322013-07-03 17:04:33 -040021
22import com.android.net.IProxyService;
23import com.google.android.collect.Lists;
24
25import java.io.IOException;
26import java.net.InetSocketAddress;
27import java.net.MalformedURLException;
28import java.net.Proxy;
29import java.net.Proxy.Type;
30import java.net.ProxySelector;
31import java.net.SocketAddress;
32import java.net.URI;
33import java.util.List;
34
35/**
36 * @hide
37 */
38public class PacProxySelector extends ProxySelector {
Wink Savillea48ad8b2013-08-10 11:22:31 -070039 private static final String TAG = "PacProxySelector";
Jason Monk602b2322013-07-03 17:04:33 -040040 public static final String PROXY_SERVICE = "com.android.net.IProxyService";
Jason Monk4385af32014-08-18 13:32:34 -040041 private static final String SOCKS = "SOCKS ";
42 private static final String PROXY = "PROXY ";
43
Jason Monk602b2322013-07-03 17:04:33 -040044 private IProxyService mProxyService;
Jason Monk9ced3cd2013-08-12 16:42:38 -040045 private final List<Proxy> mDefaultList;
Jason Monk602b2322013-07-03 17:04:33 -040046
47 public PacProxySelector() {
48 mProxyService = IProxyService.Stub.asInterface(
49 ServiceManager.getService(PROXY_SERVICE));
Wink Savillea48ad8b2013-08-10 11:22:31 -070050 if (mProxyService == null) {
51 // Added because of b10267814 where mako is restarting.
Jason Monk9ced3cd2013-08-12 16:42:38 -040052 Log.e(TAG, "PacManager: no proxy service");
Wink Savillea48ad8b2013-08-10 11:22:31 -070053 }
Jason Monk9ced3cd2013-08-12 16:42:38 -040054 mDefaultList = Lists.newArrayList(java.net.Proxy.NO_PROXY);
Jason Monk602b2322013-07-03 17:04:33 -040055 }
56
57 @Override
58 public List<Proxy> select(URI uri) {
Wink Savillea48ad8b2013-08-10 11:22:31 -070059 if (mProxyService == null) {
Jason Monk9ced3cd2013-08-12 16:42:38 -040060 mProxyService = IProxyService.Stub.asInterface(
61 ServiceManager.getService(PROXY_SERVICE));
62 }
63 if (mProxyService == null) {
Wink Savillea48ad8b2013-08-10 11:22:31 -070064 Log.e(TAG, "select: no proxy service return NO_PROXY");
65 return Lists.newArrayList(java.net.Proxy.NO_PROXY);
66 }
Jason Monk602b2322013-07-03 17:04:33 -040067 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 Kapishnikov44c02592014-12-22 12:40:48 -050076 } catch (Exception e) {
77 Log.e(TAG, "Error resolving PAC File", e);
Jason Monk602b2322013-07-03 17:04:33 -040078 }
Jason Monk9ced3cd2013-08-12 16:42:38 -040079 if (response == null) {
80 return mDefaultList;
81 }
Jason Monk602b2322013-07-03 17:04:33 -040082
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 Monk4385af32014-08-18 13:32:34 -040093 } else if (trimmed.startsWith(PROXY)) {
94 Proxy proxy = proxyFromHostPort(Type.HTTP, trimmed.substring(PROXY.length()));
95 if (proxy != null) {
96 ret.add(proxy);
Jason Monk602b2322013-07-03 17:04:33 -040097 }
Jason Monk4385af32014-08-18 13:32:34 -040098 } 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 Monk602b2322013-07-03 17:04:33 -0400103 }
104 }
105 if (ret.size() == 0) {
106 ret.add(java.net.Proxy.NO_PROXY);
107 }
108 return ret;
109 }
110
Jason Monk4385af32014-08-18 13:32:34 -0400111 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 Monk602b2322013-07-03 17:04:33 -0400123 @Override
124 public void connectFailed(URI uri, SocketAddress address, IOException failure) {
125
126 }
127
128}