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 | package com.android.server.connectivity; |
| 17 | |
Daichi Hirono | 4010fe4 | 2017-03-17 09:12:12 +0900 | [diff] [blame] | 18 | import android.annotation.WorkerThread; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 19 | import android.app.AlarmManager; |
| 20 | import android.app.PendingIntent; |
| 21 | import android.content.BroadcastReceiver; |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 22 | import android.content.ComponentName; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 23 | import android.content.ContentResolver; |
| 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
| 26 | import android.content.IntentFilter; |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 27 | import android.content.ServiceConnection; |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 28 | import android.net.ProxyInfo; |
Jeff Sharkey | 619a511 | 2017-01-19 11:55:54 -0700 | [diff] [blame] | 29 | import android.net.TrafficStats; |
Jason Monk | 83520b9 | 2014-05-09 15:16:06 -0400 | [diff] [blame] | 30 | import android.net.Uri; |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 31 | import android.os.Handler; |
Paul Jensen | 7d2198b | 2016-08-22 09:15:40 -0400 | [diff] [blame] | 32 | import android.os.HandlerThread; |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 33 | import android.os.IBinder; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 34 | import android.os.RemoteException; |
| 35 | import android.os.ServiceManager; |
| 36 | import android.os.SystemClock; |
| 37 | import android.os.SystemProperties; |
| 38 | import android.provider.Settings; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 39 | import android.util.Log; |
chiachangwang | ba482c6 | 2022-06-02 10:22:20 +0000 | [diff] [blame^] | 40 | import android.webkit.URLUtil; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 41 | |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 42 | import com.android.internal.annotations.GuardedBy; |
Chalard Jean | 6a76b7e | 2019-04-09 11:16:56 +0900 | [diff] [blame] | 43 | import com.android.internal.util.TrafficStatsConstants; |
Jason Monk | 6f8a68f | 2013-08-23 19:21:25 -0400 | [diff] [blame] | 44 | import com.android.net.IProxyCallback; |
| 45 | import com.android.net.IProxyPortListener; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 46 | import com.android.net.IProxyService; |
| 47 | |
Paul Jensen | 7d2198b | 2016-08-22 09:15:40 -0400 | [diff] [blame] | 48 | import java.io.ByteArrayOutputStream; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 49 | import java.io.IOException; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 50 | import java.net.URL; |
| 51 | import java.net.URLConnection; |
| 52 | |
| 53 | /** |
| 54 | * @hide |
| 55 | */ |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 56 | public class PacManager { |
Chalard Jean | 918a68b | 2018-01-19 17:00:47 +0900 | [diff] [blame] | 57 | private static final String PAC_PACKAGE = "com.android.pacprocessor"; |
| 58 | private static final String PAC_SERVICE = "com.android.pacprocessor.PacService"; |
| 59 | private static final String PAC_SERVICE_NAME = "com.android.net.IProxyService"; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 60 | |
Chalard Jean | 918a68b | 2018-01-19 17:00:47 +0900 | [diff] [blame] | 61 | private static final String PROXY_PACKAGE = "com.android.proxyhandler"; |
| 62 | private static final String PROXY_SERVICE = "com.android.proxyhandler.ProxyService"; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 63 | |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 64 | private static final String TAG = "PacManager"; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 65 | |
| 66 | private static final String ACTION_PAC_REFRESH = "android.net.proxy.PAC_REFRESH"; |
| 67 | |
| 68 | private static final String DEFAULT_DELAYS = "8 32 120 14400 43200"; |
| 69 | private static final int DELAY_1 = 0; |
| 70 | private static final int DELAY_4 = 3; |
| 71 | private static final int DELAY_LONG = 4; |
Paul Jensen | 7d2198b | 2016-08-22 09:15:40 -0400 | [diff] [blame] | 72 | private static final long MAX_PAC_SIZE = 20 * 1000 * 1000; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 73 | |
Chalard Jean | 6945231 | 2018-06-11 16:58:52 +0900 | [diff] [blame] | 74 | // Return values for #setCurrentProxyScriptUrl |
Chalard Jean | 29f63af | 2018-10-15 14:27:15 +0900 | [diff] [blame] | 75 | public static final boolean DONT_SEND_BROADCAST = false; |
| 76 | public static final boolean DO_SEND_BROADCAST = true; |
Chalard Jean | 6945231 | 2018-06-11 16:58:52 +0900 | [diff] [blame] | 77 | |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 78 | private String mCurrentPac; |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 79 | @GuardedBy("mProxyLock") |
Daichi Hirono | 4010fe4 | 2017-03-17 09:12:12 +0900 | [diff] [blame] | 80 | private volatile Uri mPacUrl = Uri.EMPTY; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 81 | |
| 82 | private AlarmManager mAlarmManager; |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 83 | @GuardedBy("mProxyLock") |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 84 | private IProxyService mProxyService; |
| 85 | private PendingIntent mPacRefreshIntent; |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 86 | private ServiceConnection mConnection; |
Jason Monk | da205a7 | 2013-08-21 15:57:30 -0400 | [diff] [blame] | 87 | private ServiceConnection mProxyConnection; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 88 | private Context mContext; |
| 89 | |
| 90 | private int mCurrentDelay; |
Jason Monk | 6f8a68f | 2013-08-23 19:21:25 -0400 | [diff] [blame] | 91 | private int mLastPort; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 92 | |
Daichi Hirono | 4010fe4 | 2017-03-17 09:12:12 +0900 | [diff] [blame] | 93 | private volatile boolean mHasSentBroadcast; |
| 94 | private volatile boolean mHasDownloaded; |
Jason Monk | d443479 | 2013-09-23 13:32:39 -0400 | [diff] [blame] | 95 | |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 96 | private Handler mConnectivityHandler; |
Chalard Jean | 3c443fc | 2018-06-07 18:37:59 +0900 | [diff] [blame] | 97 | private final int mProxyMessage; |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 98 | |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 99 | /** |
Daichi Hirono | 4010fe4 | 2017-03-17 09:12:12 +0900 | [diff] [blame] | 100 | * Used for locking when setting mProxyService and all references to mCurrentPac. |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 101 | */ |
| 102 | private final Object mProxyLock = new Object(); |
| 103 | |
Daichi Hirono | 4010fe4 | 2017-03-17 09:12:12 +0900 | [diff] [blame] | 104 | /** |
| 105 | * Runnable to download PAC script. |
Chalard Jean | 3c443fc | 2018-06-07 18:37:59 +0900 | [diff] [blame] | 106 | * The behavior relies on the assumption it always runs on mNetThread to guarantee that the |
Daichi Hirono | 4010fe4 | 2017-03-17 09:12:12 +0900 | [diff] [blame] | 107 | * latest data fetched from mPacUrl is stored in mProxyService. |
| 108 | */ |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 109 | private Runnable mPacDownloader = new Runnable() { |
| 110 | @Override |
Daichi Hirono | 4010fe4 | 2017-03-17 09:12:12 +0900 | [diff] [blame] | 111 | @WorkerThread |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 112 | public void run() { |
| 113 | String file; |
Daichi Hirono | 4010fe4 | 2017-03-17 09:12:12 +0900 | [diff] [blame] | 114 | final Uri pacUrl = mPacUrl; |
| 115 | if (Uri.EMPTY.equals(pacUrl)) return; |
Chalard Jean | 6a76b7e | 2019-04-09 11:16:56 +0900 | [diff] [blame] | 116 | final int oldTag = TrafficStats.getAndSetThreadStatsTag( |
| 117 | TrafficStatsConstants.TAG_SYSTEM_PAC); |
Daichi Hirono | 4010fe4 | 2017-03-17 09:12:12 +0900 | [diff] [blame] | 118 | try { |
| 119 | file = get(pacUrl); |
| 120 | } catch (IOException ioe) { |
| 121 | file = null; |
| 122 | Log.w(TAG, "Failed to load PAC file: " + ioe); |
| 123 | } finally { |
| 124 | TrafficStats.setThreadStatsTag(oldTag); |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 125 | } |
| 126 | if (file != null) { |
| 127 | synchronized (mProxyLock) { |
| 128 | if (!file.equals(mCurrentPac)) { |
| 129 | setCurrentProxyScript(file); |
| 130 | } |
| 131 | } |
Jason Monk | d443479 | 2013-09-23 13:32:39 -0400 | [diff] [blame] | 132 | mHasDownloaded = true; |
| 133 | sendProxyIfNeeded(); |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 134 | longSchedule(); |
| 135 | } else { |
| 136 | reschedule(); |
| 137 | } |
| 138 | } |
| 139 | }; |
| 140 | |
Paul Jensen | 7d2198b | 2016-08-22 09:15:40 -0400 | [diff] [blame] | 141 | private final Handler mNetThreadHandler; |
| 142 | |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 143 | class PacRefreshIntentReceiver extends BroadcastReceiver { |
| 144 | public void onReceive(Context context, Intent intent) { |
Paul Jensen | 7d2198b | 2016-08-22 09:15:40 -0400 | [diff] [blame] | 145 | mNetThreadHandler.post(mPacDownloader); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 149 | public PacManager(Context context, Handler handler, int proxyMessage) { |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 150 | mContext = context; |
Jason Monk | 6f8a68f | 2013-08-23 19:21:25 -0400 | [diff] [blame] | 151 | mLastPort = -1; |
Chalard Jean | 3c443fc | 2018-06-07 18:37:59 +0900 | [diff] [blame] | 152 | final HandlerThread netThread = new HandlerThread("android.pacmanager", |
| 153 | android.os.Process.THREAD_PRIORITY_DEFAULT); |
| 154 | netThread.start(); |
| 155 | mNetThreadHandler = new Handler(netThread.getLooper()); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 156 | |
| 157 | mPacRefreshIntent = PendingIntent.getBroadcast( |
| 158 | context, 0, new Intent(ACTION_PAC_REFRESH), 0); |
| 159 | context.registerReceiver(new PacRefreshIntentReceiver(), |
| 160 | new IntentFilter(ACTION_PAC_REFRESH)); |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 161 | mConnectivityHandler = handler; |
| 162 | mProxyMessage = proxyMessage; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | private AlarmManager getAlarmManager() { |
| 166 | if (mAlarmManager == null) { |
| 167 | mAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE); |
| 168 | } |
| 169 | return mAlarmManager; |
| 170 | } |
| 171 | |
Jason Monk | 6f8a68f | 2013-08-23 19:21:25 -0400 | [diff] [blame] | 172 | /** |
| 173 | * Updates the PAC Manager with current Proxy information. This is called by |
Chalard Jean | d82ba6d | 2018-06-08 12:41:21 +0900 | [diff] [blame] | 174 | * the ProxyTracker directly before a broadcast takes place to allow |
Jason Monk | 6f8a68f | 2013-08-23 19:21:25 -0400 | [diff] [blame] | 175 | * the PacManager to indicate that the broadcast should not be sent and the |
| 176 | * PacManager will trigger a new broadcast when it is ready. |
| 177 | * |
| 178 | * @param proxy Proxy information that is about to be broadcast. |
Chalard Jean | 6945231 | 2018-06-11 16:58:52 +0900 | [diff] [blame] | 179 | * @return Returns whether the broadcast should be sent : either DO_ or DONT_SEND_BROADCAST |
Jason Monk | 6f8a68f | 2013-08-23 19:21:25 -0400 | [diff] [blame] | 180 | */ |
Chalard Jean | 29f63af | 2018-10-15 14:27:15 +0900 | [diff] [blame] | 181 | synchronized boolean setCurrentProxyScriptUrl(ProxyInfo proxy) { |
Jason Monk | 83520b9 | 2014-05-09 15:16:06 -0400 | [diff] [blame] | 182 | if (!Uri.EMPTY.equals(proxy.getPacFileUrl())) { |
Jason Monk | 90760c8 | 2013-10-14 18:24:13 -0400 | [diff] [blame] | 183 | if (proxy.getPacFileUrl().equals(mPacUrl) && (proxy.getPort() > 0)) { |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 184 | // Allow to send broadcast, nothing to do. |
Chalard Jean | 29f63af | 2018-10-15 14:27:15 +0900 | [diff] [blame] | 185 | return DO_SEND_BROADCAST; |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 186 | } |
Daichi Hirono | 4010fe4 | 2017-03-17 09:12:12 +0900 | [diff] [blame] | 187 | mPacUrl = proxy.getPacFileUrl(); |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 188 | mCurrentDelay = DELAY_1; |
Jason Monk | d443479 | 2013-09-23 13:32:39 -0400 | [diff] [blame] | 189 | mHasSentBroadcast = false; |
| 190 | mHasDownloaded = false; |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 191 | getAlarmManager().cancel(mPacRefreshIntent); |
| 192 | bind(); |
Chalard Jean | 29f63af | 2018-10-15 14:27:15 +0900 | [diff] [blame] | 193 | return DONT_SEND_BROADCAST; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 194 | } else { |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 195 | getAlarmManager().cancel(mPacRefreshIntent); |
| 196 | synchronized (mProxyLock) { |
Jason Monk | f6b46cb | 2014-09-19 12:46:19 -0400 | [diff] [blame] | 197 | mPacUrl = Uri.EMPTY; |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 198 | mCurrentPac = null; |
| 199 | if (mProxyService != null) { |
| 200 | try { |
| 201 | mProxyService.stopPacSystem(); |
| 202 | } catch (RemoteException e) { |
| 203 | Log.w(TAG, "Failed to stop PAC service", e); |
| 204 | } finally { |
| 205 | unbind(); |
| 206 | } |
| 207 | } |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 208 | } |
Chalard Jean | 29f63af | 2018-10-15 14:27:15 +0900 | [diff] [blame] | 209 | return DO_SEND_BROADCAST; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Does a post and reports back the status code. |
| 215 | * |
Chalard Jean | 3c443fc | 2018-06-07 18:37:59 +0900 | [diff] [blame] | 216 | * @throws IOException if the URL is malformed, or the PAC file is too big. |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 217 | */ |
Jason Monk | 83520b9 | 2014-05-09 15:16:06 -0400 | [diff] [blame] | 218 | private static String get(Uri pacUri) throws IOException { |
chiachangwang | ba482c6 | 2022-06-02 10:22:20 +0000 | [diff] [blame^] | 219 | if (!URLUtil.isValidUrl(pacUri.toString())) { |
| 220 | throw new IOException("Malformed URL:" + pacUri); |
| 221 | } |
| 222 | |
| 223 | final URL url = new URL(pacUri.toString()); |
| 224 | URLConnection urlConnection; |
| 225 | try { |
| 226 | urlConnection = url.openConnection(java.net.Proxy.NO_PROXY); |
| 227 | // Catch the possible exceptions and rethrow as IOException to not to crash the system |
| 228 | // for illegal input. |
| 229 | } catch (IllegalArgumentException e) { |
| 230 | throw new IOException("Incorrect proxy type for " + pacUri); |
| 231 | } catch (UnsupportedOperationException e) { |
| 232 | throw new IOException("Unsupported URL connection type for " + pacUri); |
| 233 | } |
| 234 | |
Paul Jensen | 7d2198b | 2016-08-22 09:15:40 -0400 | [diff] [blame] | 235 | long contentLength = -1; |
| 236 | try { |
| 237 | contentLength = Long.parseLong(urlConnection.getHeaderField("Content-Length")); |
| 238 | } catch (NumberFormatException e) { |
| 239 | // Ignore |
| 240 | } |
| 241 | if (contentLength > MAX_PAC_SIZE) { |
| 242 | throw new IOException("PAC too big: " + contentLength + " bytes"); |
| 243 | } |
| 244 | ByteArrayOutputStream bytes = new ByteArrayOutputStream(); |
| 245 | byte[] buffer = new byte[1024]; |
| 246 | int count; |
| 247 | while ((count = urlConnection.getInputStream().read(buffer)) != -1) { |
| 248 | bytes.write(buffer, 0, count); |
| 249 | if (bytes.size() > MAX_PAC_SIZE) { |
| 250 | throw new IOException("PAC too big"); |
| 251 | } |
| 252 | } |
| 253 | return bytes.toString(); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | private int getNextDelay(int currentDelay) { |
| 257 | if (++currentDelay > DELAY_4) { |
| 258 | return DELAY_4; |
| 259 | } |
| 260 | return currentDelay; |
| 261 | } |
| 262 | |
| 263 | private void longSchedule() { |
| 264 | mCurrentDelay = DELAY_1; |
| 265 | setDownloadIn(DELAY_LONG); |
| 266 | } |
| 267 | |
| 268 | private void reschedule() { |
| 269 | mCurrentDelay = getNextDelay(mCurrentDelay); |
| 270 | setDownloadIn(mCurrentDelay); |
| 271 | } |
| 272 | |
| 273 | private String getPacChangeDelay() { |
| 274 | final ContentResolver cr = mContext.getContentResolver(); |
| 275 | |
Chalard Jean | 3c443fc | 2018-06-07 18:37:59 +0900 | [diff] [blame] | 276 | // Check system properties for the default value then use secure settings value, if any. |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 277 | String defaultDelay = SystemProperties.get( |
| 278 | "conn." + Settings.Global.PAC_CHANGE_DELAY, |
| 279 | DEFAULT_DELAYS); |
| 280 | String val = Settings.Global.getString(cr, Settings.Global.PAC_CHANGE_DELAY); |
| 281 | return (val == null) ? defaultDelay : val; |
| 282 | } |
| 283 | |
| 284 | private long getDownloadDelay(int delayIndex) { |
| 285 | String[] list = getPacChangeDelay().split(" "); |
| 286 | if (delayIndex < list.length) { |
| 287 | return Long.parseLong(list[delayIndex]); |
| 288 | } |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | private void setDownloadIn(int delayIndex) { |
| 293 | long delay = getDownloadDelay(delayIndex); |
| 294 | long timeTillTrigger = 1000 * delay + SystemClock.elapsedRealtime(); |
| 295 | getAlarmManager().set(AlarmManager.ELAPSED_REALTIME, timeTillTrigger, mPacRefreshIntent); |
| 296 | } |
| 297 | |
Chalard Jean | 3c443fc | 2018-06-07 18:37:59 +0900 | [diff] [blame] | 298 | private void setCurrentProxyScript(String script) { |
Wink Saville | a48ad8b | 2013-08-10 11:22:31 -0700 | [diff] [blame] | 299 | if (mProxyService == null) { |
| 300 | Log.e(TAG, "setCurrentProxyScript: no proxy service"); |
Chiachang Wang | c517a6f | 2019-01-17 10:18:39 +0800 | [diff] [blame] | 301 | return; |
Wink Saville | a48ad8b | 2013-08-10 11:22:31 -0700 | [diff] [blame] | 302 | } |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 303 | try { |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 304 | mProxyService.setPacFile(script); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 305 | mCurrentPac = script; |
| 306 | } catch (RemoteException e) { |
| 307 | Log.e(TAG, "Unable to set PAC file", e); |
| 308 | } |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 309 | } |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 310 | |
| 311 | private void bind() { |
| 312 | if (mContext == null) { |
| 313 | Log.e(TAG, "No context for binding"); |
| 314 | return; |
| 315 | } |
| 316 | Intent intent = new Intent(); |
Jason Monk | da205a7 | 2013-08-21 15:57:30 -0400 | [diff] [blame] | 317 | intent.setClassName(PAC_PACKAGE, PAC_SERVICE); |
Jason Monk | bc018d8 | 2013-09-17 16:37:38 -0400 | [diff] [blame] | 318 | if ((mProxyConnection != null) && (mConnection != null)) { |
Chalard Jean | 6945231 | 2018-06-11 16:58:52 +0900 | [diff] [blame] | 319 | // Already bound: no need to bind again, just download the new file. |
Paul Jensen | baae57a | 2016-09-19 08:53:20 -0400 | [diff] [blame] | 320 | mNetThreadHandler.post(mPacDownloader); |
Jason Monk | 6f8a68f | 2013-08-23 19:21:25 -0400 | [diff] [blame] | 321 | return; |
| 322 | } |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 323 | mConnection = new ServiceConnection() { |
| 324 | @Override |
| 325 | public void onServiceDisconnected(ComponentName component) { |
| 326 | synchronized (mProxyLock) { |
| 327 | mProxyService = null; |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | @Override |
| 332 | public void onServiceConnected(ComponentName component, IBinder binder) { |
| 333 | synchronized (mProxyLock) { |
| 334 | try { |
Jason Monk | da205a7 | 2013-08-21 15:57:30 -0400 | [diff] [blame] | 335 | Log.d(TAG, "Adding service " + PAC_SERVICE_NAME + " " |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 336 | + binder.getInterfaceDescriptor()); |
| 337 | } catch (RemoteException e1) { |
| 338 | Log.e(TAG, "Remote Exception", e1); |
| 339 | } |
Jason Monk | da205a7 | 2013-08-21 15:57:30 -0400 | [diff] [blame] | 340 | ServiceManager.addService(PAC_SERVICE_NAME, binder); |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 341 | mProxyService = IProxyService.Stub.asInterface(binder); |
| 342 | if (mProxyService == null) { |
| 343 | Log.e(TAG, "No proxy service"); |
| 344 | } else { |
| 345 | try { |
| 346 | mProxyService.startPacSystem(); |
| 347 | } catch (RemoteException e) { |
| 348 | Log.e(TAG, "Unable to reach ProxyService - PAC will not be started", e); |
| 349 | } |
Paul Jensen | 7d2198b | 2016-08-22 09:15:40 -0400 | [diff] [blame] | 350 | mNetThreadHandler.post(mPacDownloader); |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | } |
| 354 | }; |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 355 | mContext.bindService(intent, mConnection, |
| 356 | Context.BIND_AUTO_CREATE | Context.BIND_NOT_FOREGROUND | Context.BIND_NOT_VISIBLE); |
Jason Monk | da205a7 | 2013-08-21 15:57:30 -0400 | [diff] [blame] | 357 | |
| 358 | intent = new Intent(); |
| 359 | intent.setClassName(PROXY_PACKAGE, PROXY_SERVICE); |
| 360 | mProxyConnection = new ServiceConnection() { |
| 361 | @Override |
| 362 | public void onServiceDisconnected(ComponentName component) { |
| 363 | } |
| 364 | |
| 365 | @Override |
| 366 | public void onServiceConnected(ComponentName component, IBinder binder) { |
Jason Monk | 6f8a68f | 2013-08-23 19:21:25 -0400 | [diff] [blame] | 367 | IProxyCallback callbackService = IProxyCallback.Stub.asInterface(binder); |
| 368 | if (callbackService != null) { |
| 369 | try { |
| 370 | callbackService.getProxyPort(new IProxyPortListener.Stub() { |
| 371 | @Override |
Chalard Jean | 3c443fc | 2018-06-07 18:37:59 +0900 | [diff] [blame] | 372 | public void setProxyPort(int port) { |
Jason Monk | d443479 | 2013-09-23 13:32:39 -0400 | [diff] [blame] | 373 | if (mLastPort != -1) { |
| 374 | // Always need to send if port changed |
| 375 | mHasSentBroadcast = false; |
| 376 | } |
Jason Monk | 6f8a68f | 2013-08-23 19:21:25 -0400 | [diff] [blame] | 377 | mLastPort = port; |
| 378 | if (port != -1) { |
| 379 | Log.d(TAG, "Local proxy is bound on " + port); |
Jason Monk | d443479 | 2013-09-23 13:32:39 -0400 | [diff] [blame] | 380 | sendProxyIfNeeded(); |
Jason Monk | 6f8a68f | 2013-08-23 19:21:25 -0400 | [diff] [blame] | 381 | } else { |
| 382 | Log.e(TAG, "Received invalid port from Local Proxy," |
| 383 | + " PAC will not be operational"); |
| 384 | } |
| 385 | } |
| 386 | }); |
| 387 | } catch (RemoteException e) { |
| 388 | e.printStackTrace(); |
| 389 | } |
| 390 | } |
Jason Monk | da205a7 | 2013-08-21 15:57:30 -0400 | [diff] [blame] | 391 | } |
| 392 | }; |
| 393 | mContext.bindService(intent, mProxyConnection, |
| 394 | Context.BIND_AUTO_CREATE | Context.BIND_NOT_FOREGROUND | Context.BIND_NOT_VISIBLE); |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | private void unbind() { |
Jason Monk | bc018d8 | 2013-09-17 16:37:38 -0400 | [diff] [blame] | 398 | if (mConnection != null) { |
| 399 | mContext.unbindService(mConnection); |
| 400 | mConnection = null; |
| 401 | } |
| 402 | if (mProxyConnection != null) { |
| 403 | mContext.unbindService(mProxyConnection); |
| 404 | mProxyConnection = null; |
| 405 | } |
| 406 | mProxyService = null; |
Jason Monk | d443479 | 2013-09-23 13:32:39 -0400 | [diff] [blame] | 407 | mLastPort = -1; |
Jason Monk | 6f8a68f | 2013-08-23 19:21:25 -0400 | [diff] [blame] | 408 | } |
| 409 | |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 410 | private void sendPacBroadcast(ProxyInfo proxy) { |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 411 | mConnectivityHandler.sendMessage(mConnectivityHandler.obtainMessage(mProxyMessage, proxy)); |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 412 | } |
Jason Monk | d443479 | 2013-09-23 13:32:39 -0400 | [diff] [blame] | 413 | |
| 414 | private synchronized void sendProxyIfNeeded() { |
| 415 | if (!mHasDownloaded || (mLastPort == -1)) { |
| 416 | return; |
| 417 | } |
| 418 | if (!mHasSentBroadcast) { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 419 | sendPacBroadcast(new ProxyInfo(mPacUrl, mLastPort)); |
Jason Monk | d443479 | 2013-09-23 13:32:39 -0400 | [diff] [blame] | 420 | mHasSentBroadcast = true; |
| 421 | } |
| 422 | } |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 423 | } |