Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | |
| 17 | package com.android.server.connectivity; |
| 18 | |
Paul Jensen | 49e3edf | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 19 | import static android.net.CaptivePortal.APP_RETURN_DISMISSED; |
| 20 | import static android.net.CaptivePortal.APP_RETURN_UNWANTED; |
| 21 | import static android.net.CaptivePortal.APP_RETURN_WANTED_AS_IS; |
| 22 | |
Paul Jensen | 79a0805 | 2014-08-21 12:44:07 -0400 | [diff] [blame] | 23 | import android.app.AlarmManager; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 24 | import android.app.PendingIntent; |
| 25 | import android.content.BroadcastReceiver; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 26 | import android.content.Context; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 27 | import android.content.Intent; |
| 28 | import android.content.IntentFilter; |
Paul Jensen | 49e3edf | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 29 | import android.net.CaptivePortal; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 30 | import android.net.ConnectivityManager; |
Paul Jensen | 49e3edf | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 31 | import android.net.ICaptivePortal; |
Paul Jensen | 2c311d6 | 2014-11-17 12:34:51 -0500 | [diff] [blame] | 32 | import android.net.NetworkRequest; |
Paul Jensen | 8fe1742 | 2015-02-02 11:03:03 -0500 | [diff] [blame] | 33 | import android.net.ProxyInfo; |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 34 | import android.net.TrafficStats; |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 35 | import android.net.Uri; |
Hugo Benichi | cfddd68 | 2016-05-31 16:28:06 +0900 | [diff] [blame] | 36 | import android.net.metrics.IpConnectivityLog; |
Hugo Benichi | cc92c6e | 2016-04-21 15:02:38 +0900 | [diff] [blame] | 37 | import android.net.metrics.NetworkEvent; |
Hugo Benichi | cfddd68 | 2016-05-31 16:28:06 +0900 | [diff] [blame] | 38 | import android.net.metrics.ValidationProbeEvent; |
Hugo Benichi | d953bf8 | 2016-09-27 09:22:35 +0900 | [diff] [blame] | 39 | import android.net.util.Stopwatch; |
Paul Jensen | 306f1a4 | 2014-08-04 10:59:01 -0400 | [diff] [blame] | 40 | import android.net.wifi.WifiInfo; |
| 41 | import android.net.wifi.WifiManager; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 42 | import android.os.Handler; |
| 43 | import android.os.Message; |
Paul Jensen | 306f1a4 | 2014-08-04 10:59:01 -0400 | [diff] [blame] | 44 | import android.os.SystemClock; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 45 | import android.os.UserHandle; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 46 | import android.provider.Settings; |
Paul Jensen | 306f1a4 | 2014-08-04 10:59:01 -0400 | [diff] [blame] | 47 | import android.telephony.CellIdentityCdma; |
| 48 | import android.telephony.CellIdentityGsm; |
| 49 | import android.telephony.CellIdentityLte; |
| 50 | import android.telephony.CellIdentityWcdma; |
| 51 | import android.telephony.CellInfo; |
| 52 | import android.telephony.CellInfoCdma; |
| 53 | import android.telephony.CellInfoGsm; |
| 54 | import android.telephony.CellInfoLte; |
| 55 | import android.telephony.CellInfoWcdma; |
| 56 | import android.telephony.TelephonyManager; |
Paul Jensen | 2f0a897 | 2015-06-25 10:07:14 -0400 | [diff] [blame] | 57 | import android.text.TextUtils; |
Robert Greenwalt | 22b4c6a | 2015-06-23 15:03:33 -0700 | [diff] [blame] | 58 | import android.util.LocalLog; |
| 59 | import android.util.LocalLog.ReadOnlyLocalLog; |
Paul Jensen | 532b6143 | 2014-11-10 09:50:02 -0500 | [diff] [blame] | 60 | import android.util.Log; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 61 | |
Paul Jensen | d7b6ca9 | 2015-05-13 14:05:12 -0400 | [diff] [blame] | 62 | import com.android.internal.annotations.VisibleForTesting; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 63 | import com.android.internal.util.Protocol; |
| 64 | import com.android.internal.util.State; |
| 65 | import com.android.internal.util.StateMachine; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 66 | |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 67 | import java.io.IOException; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 68 | import java.net.HttpURLConnection; |
Paul Jensen | 2f0a897 | 2015-06-25 10:07:14 -0400 | [diff] [blame] | 69 | import java.net.InetAddress; |
Lorenzo Colitti | c5be12e | 2016-04-19 21:57:31 +0900 | [diff] [blame] | 70 | import java.net.MalformedURLException; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 71 | import java.net.URL; |
Hugo Benichi | d953bf8 | 2016-09-27 09:22:35 +0900 | [diff] [blame] | 72 | import java.net.UnknownHostException; |
Paul Jensen | 306f1a4 | 2014-08-04 10:59:01 -0400 | [diff] [blame] | 73 | import java.util.List; |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 74 | import java.util.Random; |
Hugo Benichi | d953bf8 | 2016-09-27 09:22:35 +0900 | [diff] [blame] | 75 | import java.util.concurrent.CountDownLatch; |
| 76 | import java.util.concurrent.TimeUnit; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 77 | |
| 78 | /** |
| 79 | * {@hide} |
| 80 | */ |
| 81 | public class NetworkMonitor extends StateMachine { |
Erik Kline | a488c23 | 2016-04-15 15:49:42 +0900 | [diff] [blame] | 82 | private static final String TAG = NetworkMonitor.class.getSimpleName(); |
Hugo Benichi | a4f17bc | 2016-11-21 13:50:05 +0900 | [diff] [blame] | 83 | private static final boolean DBG = true; |
| 84 | private static final boolean VDBG = false; |
Hugo Benichi | 92eb22fd | 2016-09-27 13:01:41 +0900 | [diff] [blame] | 85 | |
Hugo Benichi | 11ae28f | 2016-09-27 13:16:19 +0900 | [diff] [blame] | 86 | // Default configuration values for captive portal detection probes. |
| 87 | // TODO: append a random length parameter to the default HTTPS url. |
| 88 | // TODO: randomize browser version ids in the default User-Agent String. |
| 89 | private static final String DEFAULT_HTTPS_URL = "https://www.google.com/generate_204"; |
Hugo Benichi | 92eb22fd | 2016-09-27 13:01:41 +0900 | [diff] [blame] | 90 | private static final String DEFAULT_HTTP_URL = |
| 91 | "http://connectivitycheck.gstatic.com/generate_204"; |
Hugo Benichi | 11ae28f | 2016-09-27 13:16:19 +0900 | [diff] [blame] | 92 | private static final String DEFAULT_FALLBACK_URL = "http://www.google.com/gen_204"; |
| 93 | private static final String DEFAULT_USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64) " |
| 94 | + "AppleWebKit/537.36 (KHTML, like Gecko) " |
| 95 | + "Chrome/52.0.2743.82 Safari/537.36"; |
Hugo Benichi | 92eb22fd | 2016-09-27 13:01:41 +0900 | [diff] [blame] | 96 | |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 97 | private static final int SOCKET_TIMEOUT_MS = 10000; |
Hugo Benichi | 92eb22fd | 2016-09-27 13:01:41 +0900 | [diff] [blame] | 98 | private static final int PROBE_TIMEOUT_MS = 3000; |
| 99 | |
Hugo Benichi | dd22982 | 2016-11-15 23:23:24 +0900 | [diff] [blame^] | 100 | static enum EvaluationResult { |
| 101 | VALIDATED(true), |
| 102 | CAPTIVE_PORTAL(false); |
| 103 | final boolean isValidated; |
| 104 | EvaluationResult(boolean isValidated) { |
| 105 | this.isValidated = isValidated; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | static enum ValidationStage { |
| 110 | FIRST_VALIDATION(true), |
| 111 | REVALIDATION(false); |
| 112 | final boolean isFirstValidation; |
| 113 | ValidationStage(boolean isFirstValidation) { |
| 114 | this.isFirstValidation = isFirstValidation; |
| 115 | } |
| 116 | } |
| 117 | |
Paul Jensen | 306f1a4 | 2014-08-04 10:59:01 -0400 | [diff] [blame] | 118 | public static final String ACTION_NETWORK_CONDITIONS_MEASURED = |
| 119 | "android.net.conn.NETWORK_CONDITIONS_MEASURED"; |
| 120 | public static final String EXTRA_CONNECTIVITY_TYPE = "extra_connectivity_type"; |
| 121 | public static final String EXTRA_NETWORK_TYPE = "extra_network_type"; |
| 122 | public static final String EXTRA_RESPONSE_RECEIVED = "extra_response_received"; |
| 123 | public static final String EXTRA_IS_CAPTIVE_PORTAL = "extra_is_captive_portal"; |
| 124 | public static final String EXTRA_CELL_ID = "extra_cellid"; |
| 125 | public static final String EXTRA_SSID = "extra_ssid"; |
| 126 | public static final String EXTRA_BSSID = "extra_bssid"; |
| 127 | /** real time since boot */ |
| 128 | public static final String EXTRA_REQUEST_TIMESTAMP_MS = "extra_request_timestamp_ms"; |
| 129 | public static final String EXTRA_RESPONSE_TIMESTAMP_MS = "extra_response_timestamp_ms"; |
| 130 | |
| 131 | private static final String PERMISSION_ACCESS_NETWORK_CONDITIONS = |
| 132 | "android.permission.ACCESS_NETWORK_CONDITIONS"; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 133 | |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 134 | // After a network has been tested this result can be sent with EVENT_NETWORK_TESTED. |
| 135 | // The network should be used as a default internet connection. It was found to be: |
| 136 | // 1. a functioning network providing internet access, or |
| 137 | // 2. a captive portal and the user decided to use it as is. |
| 138 | public static final int NETWORK_TEST_RESULT_VALID = 0; |
| 139 | // After a network has been tested this result can be sent with EVENT_NETWORK_TESTED. |
| 140 | // The network should not be used as a default internet connection. It was found to be: |
| 141 | // 1. a captive portal and the user is prompted to sign-in, or |
| 142 | // 2. a captive portal and the user did not want to use it, or |
| 143 | // 3. a broken network (e.g. DNS failed, connect failed, HTTP request failed). |
| 144 | public static final int NETWORK_TEST_RESULT_INVALID = 1; |
| 145 | |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 146 | private static final int BASE = Protocol.BASE_NETWORK_MONITOR; |
| 147 | |
| 148 | /** |
| 149 | * Inform NetworkMonitor that their network is connected. |
| 150 | * Initiates Network Validation. |
| 151 | */ |
| 152 | public static final int CMD_NETWORK_CONNECTED = BASE + 1; |
| 153 | |
| 154 | /** |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 155 | * Inform ConnectivityService that the network has been tested. |
Paul Jensen | 23243731 | 2016-04-06 09:51:26 -0400 | [diff] [blame] | 156 | * obj = String representing URL that Internet probe was redirect to, if it was redirected. |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 157 | * arg1 = One of the NETWORK_TESTED_RESULT_* constants. |
Paul Jensen | 23243731 | 2016-04-06 09:51:26 -0400 | [diff] [blame] | 158 | * arg2 = NetID. |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 159 | */ |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 160 | public static final int EVENT_NETWORK_TESTED = BASE + 2; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 161 | |
| 162 | /** |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 163 | * Message to self indicating it's time to evaluate a network's connectivity. |
| 164 | * arg1 = Token to ignore old messages. |
| 165 | */ |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 166 | private static final int CMD_REEVALUATE = BASE + 6; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 167 | |
| 168 | /** |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 169 | * Inform NetworkMonitor that the network has disconnected. |
| 170 | */ |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 171 | public static final int CMD_NETWORK_DISCONNECTED = BASE + 7; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 172 | |
| 173 | /** |
| 174 | * Force evaluation even if it has succeeded in the past. |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 175 | * arg1 = UID responsible for requesting this reeval. Will be billed for data. |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 176 | */ |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 177 | public static final int CMD_FORCE_REEVALUATION = BASE + 8; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 178 | |
| 179 | /** |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 180 | * Message to self indicating captive portal app finished. |
Paul Jensen | 49e3edf | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 181 | * arg1 = one of: APP_RETURN_DISMISSED, |
| 182 | * APP_RETURN_UNWANTED, |
| 183 | * APP_RETURN_WANTED_AS_IS |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 184 | * obj = mCaptivePortalLoggedInResponseToken as String |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 185 | */ |
Paul Jensen | 49e3edf | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 186 | private static final int CMD_CAPTIVE_PORTAL_APP_FINISHED = BASE + 9; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 187 | |
| 188 | /** |
| 189 | * Request ConnectivityService display provisioning notification. |
| 190 | * arg1 = Whether to make the notification visible. |
Paul Jensen | fdc4e4a | 2014-07-15 12:07:36 -0400 | [diff] [blame] | 191 | * arg2 = NetID. |
| 192 | * obj = Intent to be launched when notification selected by user, null if !arg1. |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 193 | */ |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 194 | public static final int EVENT_PROVISIONING_NOTIFICATION = BASE + 10; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 195 | |
| 196 | /** |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 197 | * Message to self indicating sign-in app should be launched. |
| 198 | * Sent by mLaunchCaptivePortalAppBroadcastReceiver when the |
| 199 | * user touches the sign in notification. |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 200 | */ |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 201 | private static final int CMD_LAUNCH_CAPTIVE_PORTAL_APP = BASE + 11; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 202 | |
Paul Jensen | ee3e2ce | 2015-06-17 15:02:54 -0400 | [diff] [blame] | 203 | /** |
| 204 | * Retest network to see if captive portal is still in place. |
| 205 | * arg1 = UID responsible for requesting this reeval. Will be billed for data. |
| 206 | * 0 indicates self-initiated, so nobody to blame. |
| 207 | */ |
| 208 | private static final int CMD_CAPTIVE_PORTAL_RECHECK = BASE + 12; |
| 209 | |
Paul Jensen | d0491e9a | 2015-05-05 14:52:22 -0400 | [diff] [blame] | 210 | // Start mReevaluateDelayMs at this value and double. |
| 211 | private static final int INITIAL_REEVALUATE_DELAY_MS = 1000; |
| 212 | private static final int MAX_REEVALUATE_DELAY_MS = 10*60*1000; |
| 213 | // Before network has been evaluated this many times, ignore repeated reevaluate requests. |
| 214 | private static final int IGNORE_REEVALUATE_ATTEMPTS = 5; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 215 | private int mReevaluateToken = 0; |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 216 | private static final int INVALID_UID = -1; |
| 217 | private int mUidResponsibleForReeval = INVALID_UID; |
Paul Jensen | d9be23f | 2015-05-19 14:51:47 -0400 | [diff] [blame] | 218 | // Stop blaming UID that requested re-evaluation after this many attempts. |
| 219 | private static final int BLAME_FOR_EVALUATION_ATTEMPTS = 5; |
Paul Jensen | ee3e2ce | 2015-06-17 15:02:54 -0400 | [diff] [blame] | 220 | // Delay between reevaluations once a captive portal has been found. |
| 221 | private static final int CAPTIVE_PORTAL_REEVALUATE_DELAY_MS = 10*60*1000; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 222 | |
| 223 | private final Context mContext; |
| 224 | private final Handler mConnectivityServiceHandler; |
| 225 | private final NetworkAgentInfo mNetworkAgentInfo; |
Erik Kline | a488c23 | 2016-04-15 15:49:42 +0900 | [diff] [blame] | 226 | private final int mNetId; |
Paul Jensen | 306f1a4 | 2014-08-04 10:59:01 -0400 | [diff] [blame] | 227 | private final TelephonyManager mTelephonyManager; |
| 228 | private final WifiManager mWifiManager; |
Paul Jensen | 79a0805 | 2014-08-21 12:44:07 -0400 | [diff] [blame] | 229 | private final AlarmManager mAlarmManager; |
Paul Jensen | 2c311d6 | 2014-11-17 12:34:51 -0500 | [diff] [blame] | 230 | private final NetworkRequest mDefaultRequest; |
Hugo Benichi | f9fdf87 | 2016-07-28 17:53:06 +0900 | [diff] [blame] | 231 | private final IpConnectivityLog mMetricsLog; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 232 | |
Calvin On | 4bc78eb | 2016-10-11 15:10:46 -0700 | [diff] [blame] | 233 | @VisibleForTesting |
| 234 | protected boolean mIsCaptivePortalCheckEnabled; |
| 235 | |
Lorenzo Colitti | c5be12e | 2016-04-19 21:57:31 +0900 | [diff] [blame] | 236 | private boolean mUseHttps; |
Hugo Benichi | dd22982 | 2016-11-15 23:23:24 +0900 | [diff] [blame^] | 237 | // The total number of captive portal detection attempts for this NetworkMonitor instance. |
| 238 | private int mValidations = 0; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 239 | |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 240 | // Set if the user explicitly selected "Do not use this network" in captive portal sign-in app. |
| 241 | private boolean mUserDoesNotWant = false; |
Paul Jensen | 700f236 | 2015-05-05 14:56:10 -0400 | [diff] [blame] | 242 | // Avoids surfacing "Sign in to network" notification. |
| 243 | private boolean mDontDisplaySigninNotification = false; |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 244 | |
Robert Greenwalt | fb68f8f | 2014-08-13 13:43:32 -0700 | [diff] [blame] | 245 | public boolean systemReady = false; |
| 246 | |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 247 | private final State mDefaultState = new DefaultState(); |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 248 | private final State mValidatedState = new ValidatedState(); |
| 249 | private final State mMaybeNotifyState = new MaybeNotifyState(); |
| 250 | private final State mEvaluatingState = new EvaluatingState(); |
| 251 | private final State mCaptivePortalState = new CaptivePortalState(); |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 252 | |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 253 | private CustomIntentReceiver mLaunchCaptivePortalAppBroadcastReceiver = null; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 254 | |
Robert Greenwalt | 22b4c6a | 2015-06-23 15:03:33 -0700 | [diff] [blame] | 255 | private final LocalLog validationLogs = new LocalLog(20); // 20 lines |
| 256 | |
Erik Kline | a488c23 | 2016-04-15 15:49:42 +0900 | [diff] [blame] | 257 | private final Stopwatch mEvaluationTimer = new Stopwatch(); |
| 258 | |
Hugo Benichi | d953bf8 | 2016-09-27 09:22:35 +0900 | [diff] [blame] | 259 | // This variable is set before transitioning to the mCaptivePortalState. |
| 260 | private CaptivePortalProbeResult mLastPortalProbeResult = CaptivePortalProbeResult.FAILED; |
| 261 | |
Paul Jensen | 2c311d6 | 2014-11-17 12:34:51 -0500 | [diff] [blame] | 262 | public NetworkMonitor(Context context, Handler handler, NetworkAgentInfo networkAgentInfo, |
| 263 | NetworkRequest defaultRequest) { |
Hugo Benichi | f9fdf87 | 2016-07-28 17:53:06 +0900 | [diff] [blame] | 264 | this(context, handler, networkAgentInfo, defaultRequest, new IpConnectivityLog()); |
| 265 | } |
| 266 | |
| 267 | @VisibleForTesting |
| 268 | protected NetworkMonitor(Context context, Handler handler, NetworkAgentInfo networkAgentInfo, |
| 269 | NetworkRequest defaultRequest, IpConnectivityLog logger) { |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 270 | // Add suffix indicating which NetworkMonitor we're talking about. |
| 271 | super(TAG + networkAgentInfo.name()); |
| 272 | |
| 273 | mContext = context; |
Hugo Benichi | f9fdf87 | 2016-07-28 17:53:06 +0900 | [diff] [blame] | 274 | mMetricsLog = logger; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 275 | mConnectivityServiceHandler = handler; |
| 276 | mNetworkAgentInfo = networkAgentInfo; |
Erik Kline | a488c23 | 2016-04-15 15:49:42 +0900 | [diff] [blame] | 277 | mNetId = mNetworkAgentInfo.network.netId; |
Paul Jensen | 306f1a4 | 2014-08-04 10:59:01 -0400 | [diff] [blame] | 278 | mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); |
| 279 | mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); |
Paul Jensen | 79a0805 | 2014-08-21 12:44:07 -0400 | [diff] [blame] | 280 | mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); |
Paul Jensen | 2c311d6 | 2014-11-17 12:34:51 -0500 | [diff] [blame] | 281 | mDefaultRequest = defaultRequest; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 282 | |
| 283 | addState(mDefaultState); |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 284 | addState(mValidatedState, mDefaultState); |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 285 | addState(mMaybeNotifyState, mDefaultState); |
| 286 | addState(mEvaluatingState, mMaybeNotifyState); |
| 287 | addState(mCaptivePortalState, mMaybeNotifyState); |
Robert Greenwalt | 49f63fb | 2014-09-13 12:04:12 -0700 | [diff] [blame] | 288 | setInitialState(mDefaultState); |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 289 | |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 290 | mIsCaptivePortalCheckEnabled = Settings.Global.getInt(mContext.getContentResolver(), |
Calvin On | 4bc78eb | 2016-10-11 15:10:46 -0700 | [diff] [blame] | 291 | Settings.Global.CAPTIVE_PORTAL_MODE, Settings.Global.CAPTIVE_PORTAL_MODE_PROMPT) |
| 292 | != Settings.Global.CAPTIVE_PORTAL_MODE_IGNORE; |
Lorenzo Colitti | c5be12e | 2016-04-19 21:57:31 +0900 | [diff] [blame] | 293 | mUseHttps = Settings.Global.getInt(mContext.getContentResolver(), |
| 294 | Settings.Global.CAPTIVE_PORTAL_USE_HTTPS, 1) == 1; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 295 | |
| 296 | start(); |
| 297 | } |
| 298 | |
Paul Jensen | 532b6143 | 2014-11-10 09:50:02 -0500 | [diff] [blame] | 299 | @Override |
| 300 | protected void log(String s) { |
Paul Jensen | 22e547a | 2015-06-25 09:17:53 -0400 | [diff] [blame] | 301 | if (DBG) Log.d(TAG + "/" + mNetworkAgentInfo.name(), s); |
Paul Jensen | 532b6143 | 2014-11-10 09:50:02 -0500 | [diff] [blame] | 302 | } |
| 303 | |
Robert Greenwalt | 22b4c6a | 2015-06-23 15:03:33 -0700 | [diff] [blame] | 304 | private void validationLog(String s) { |
| 305 | if (DBG) log(s); |
| 306 | validationLogs.log(s); |
| 307 | } |
| 308 | |
| 309 | public ReadOnlyLocalLog getValidationLogs() { |
| 310 | return validationLogs.readOnlyLocalLog(); |
| 311 | } |
| 312 | |
Hugo Benichi | dd22982 | 2016-11-15 23:23:24 +0900 | [diff] [blame^] | 313 | private ValidationStage validationStage() { |
| 314 | return 0 == mValidations ? ValidationStage.FIRST_VALIDATION : ValidationStage.REVALIDATION; |
| 315 | } |
| 316 | |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 317 | // DefaultState is the parent of all States. It exists only to handle CMD_* messages but |
| 318 | // does not entail any real state (hence no enter() or exit() routines). |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 319 | private class DefaultState extends State { |
| 320 | @Override |
| 321 | public boolean processMessage(Message message) { |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 322 | switch (message.what) { |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 323 | case CMD_NETWORK_CONNECTED: |
Hugo Benichi | cfddd68 | 2016-05-31 16:28:06 +0900 | [diff] [blame] | 324 | logNetworkEvent(NetworkEvent.NETWORK_CONNECTED); |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 325 | transitionTo(mEvaluatingState); |
Paul Jensen | d6a3f7e | 2014-08-19 09:40:11 -0400 | [diff] [blame] | 326 | return HANDLED; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 327 | case CMD_NETWORK_DISCONNECTED: |
Hugo Benichi | cfddd68 | 2016-05-31 16:28:06 +0900 | [diff] [blame] | 328 | logNetworkEvent(NetworkEvent.NETWORK_DISCONNECTED); |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 329 | if (mLaunchCaptivePortalAppBroadcastReceiver != null) { |
| 330 | mContext.unregisterReceiver(mLaunchCaptivePortalAppBroadcastReceiver); |
| 331 | mLaunchCaptivePortalAppBroadcastReceiver = null; |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 332 | } |
Robert Greenwalt | 1fd9aee | 2014-07-17 16:11:38 -0700 | [diff] [blame] | 333 | quit(); |
Paul Jensen | d6a3f7e | 2014-08-19 09:40:11 -0400 | [diff] [blame] | 334 | return HANDLED; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 335 | case CMD_FORCE_REEVALUATION: |
Paul Jensen | ee3e2ce | 2015-06-17 15:02:54 -0400 | [diff] [blame] | 336 | case CMD_CAPTIVE_PORTAL_RECHECK: |
Paul Jensen | 22e547a | 2015-06-25 09:17:53 -0400 | [diff] [blame] | 337 | log("Forcing reevaluation for UID " + message.arg1); |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 338 | mUidResponsibleForReeval = message.arg1; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 339 | transitionTo(mEvaluatingState); |
Paul Jensen | d6a3f7e | 2014-08-19 09:40:11 -0400 | [diff] [blame] | 340 | return HANDLED; |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 341 | case CMD_CAPTIVE_PORTAL_APP_FINISHED: |
Paul Jensen | 22e547a | 2015-06-25 09:17:53 -0400 | [diff] [blame] | 342 | log("CaptivePortal App responded with " + message.arg1); |
Lorenzo Colitti | c5be12e | 2016-04-19 21:57:31 +0900 | [diff] [blame] | 343 | |
| 344 | // If the user has seen and acted on a captive portal notification, and the |
| 345 | // captive portal app is now closed, disable HTTPS probes. This avoids the |
| 346 | // following pathological situation: |
| 347 | // |
| 348 | // 1. HTTP probe returns a captive portal, HTTPS probe fails or times out. |
| 349 | // 2. User opens the app and logs into the captive portal. |
| 350 | // 3. HTTP starts working, but HTTPS still doesn't work for some other reason - |
| 351 | // perhaps due to the network blocking HTTPS? |
| 352 | // |
| 353 | // In this case, we'll fail to validate the network even after the app is |
| 354 | // dismissed. There is now no way to use this network, because the app is now |
| 355 | // gone, so the user cannot select "Use this network as is". |
| 356 | mUseHttps = false; |
| 357 | |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 358 | switch (message.arg1) { |
Paul Jensen | 49e3edf | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 359 | case APP_RETURN_DISMISSED: |
Paul Jensen | d0491e9a | 2015-05-05 14:52:22 -0400 | [diff] [blame] | 360 | sendMessage(CMD_FORCE_REEVALUATION, 0 /* no UID */, 0); |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 361 | break; |
Paul Jensen | 49e3edf | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 362 | case APP_RETURN_WANTED_AS_IS: |
Paul Jensen | 700f236 | 2015-05-05 14:56:10 -0400 | [diff] [blame] | 363 | mDontDisplaySigninNotification = true; |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 364 | // TODO: Distinguish this from a network that actually validates. |
| 365 | // Displaying the "!" on the system UI icon may still be a good idea. |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 366 | transitionTo(mValidatedState); |
| 367 | break; |
Paul Jensen | 49e3edf | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 368 | case APP_RETURN_UNWANTED: |
Paul Jensen | 700f236 | 2015-05-05 14:56:10 -0400 | [diff] [blame] | 369 | mDontDisplaySigninNotification = true; |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 370 | mUserDoesNotWant = true; |
Paul Jensen | d0491e9a | 2015-05-05 14:52:22 -0400 | [diff] [blame] | 371 | mConnectivityServiceHandler.sendMessage(obtainMessage( |
Paul Jensen | 23243731 | 2016-04-06 09:51:26 -0400 | [diff] [blame] | 372 | EVENT_NETWORK_TESTED, NETWORK_TEST_RESULT_INVALID, |
Erik Kline | a488c23 | 2016-04-15 15:49:42 +0900 | [diff] [blame] | 373 | mNetId, null)); |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 374 | // TODO: Should teardown network. |
Paul Jensen | d0491e9a | 2015-05-05 14:52:22 -0400 | [diff] [blame] | 375 | mUidResponsibleForReeval = 0; |
| 376 | transitionTo(mEvaluatingState); |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 377 | break; |
| 378 | } |
| 379 | return HANDLED; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 380 | default: |
Paul Jensen | d6a3f7e | 2014-08-19 09:40:11 -0400 | [diff] [blame] | 381 | return HANDLED; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 382 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 386 | // Being in the ValidatedState State indicates a Network is: |
| 387 | // - Successfully validated, or |
| 388 | // - Wanted "as is" by the user, or |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 389 | // - Does not satisfy the default NetworkRequest and so validation has been skipped. |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 390 | private class ValidatedState extends State { |
| 391 | @Override |
| 392 | public void enter() { |
Hugo Benichi | dd22982 | 2016-11-15 23:23:24 +0900 | [diff] [blame^] | 393 | maybeLogEvaluationResult( |
| 394 | networkEventType(validationStage(), EvaluationResult.VALIDATED)); |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 395 | mConnectivityServiceHandler.sendMessage(obtainMessage(EVENT_NETWORK_TESTED, |
Paul Jensen | 23243731 | 2016-04-06 09:51:26 -0400 | [diff] [blame] | 396 | NETWORK_TEST_RESULT_VALID, mNetworkAgentInfo.network.netId, null)); |
Hugo Benichi | dd22982 | 2016-11-15 23:23:24 +0900 | [diff] [blame^] | 397 | mValidations++; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | @Override |
| 401 | public boolean processMessage(Message message) { |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 402 | switch (message.what) { |
| 403 | case CMD_NETWORK_CONNECTED: |
| 404 | transitionTo(mValidatedState); |
Paul Jensen | d6a3f7e | 2014-08-19 09:40:11 -0400 | [diff] [blame] | 405 | return HANDLED; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 406 | default: |
| 407 | return NOT_HANDLED; |
| 408 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 412 | // Being in the MaybeNotifyState State indicates the user may have been notified that sign-in |
| 413 | // is required. This State takes care to clear the notification upon exit from the State. |
| 414 | private class MaybeNotifyState extends State { |
| 415 | @Override |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 416 | public boolean processMessage(Message message) { |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 417 | switch (message.what) { |
| 418 | case CMD_LAUNCH_CAPTIVE_PORTAL_APP: |
| 419 | final Intent intent = new Intent( |
| 420 | ConnectivityManager.ACTION_CAPTIVE_PORTAL_SIGN_IN); |
| 421 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK, mNetworkAgentInfo.network); |
Paul Jensen | 49e3edf | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 422 | intent.putExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL, |
| 423 | new CaptivePortal(new ICaptivePortal.Stub() { |
| 424 | @Override |
| 425 | public void appResponse(int response) { |
| 426 | if (response == APP_RETURN_WANTED_AS_IS) { |
| 427 | mContext.enforceCallingPermission( |
| 428 | android.Manifest.permission.CONNECTIVITY_INTERNAL, |
| 429 | "CaptivePortal"); |
| 430 | } |
| 431 | sendMessage(CMD_CAPTIVE_PORTAL_APP_FINISHED, response); |
| 432 | } |
| 433 | })); |
Hugo Benichi | d953bf8 | 2016-09-27 09:22:35 +0900 | [diff] [blame] | 434 | intent.putExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL_URL, |
| 435 | mLastPortalProbeResult.detectUrl); |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 436 | intent.setFlags( |
| 437 | Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK); |
| 438 | mContext.startActivityAsUser(intent, UserHandle.CURRENT); |
| 439 | return HANDLED; |
| 440 | default: |
| 441 | return NOT_HANDLED; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | @Override |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 446 | public void exit() { |
| 447 | Message message = obtainMessage(EVENT_PROVISIONING_NOTIFICATION, 0, |
| 448 | mNetworkAgentInfo.network.netId, null); |
| 449 | mConnectivityServiceHandler.sendMessage(message); |
| 450 | } |
| 451 | } |
| 452 | |
Paul Jensen | 23243731 | 2016-04-06 09:51:26 -0400 | [diff] [blame] | 453 | /** |
| 454 | * Result of calling isCaptivePortal(). |
| 455 | * @hide |
| 456 | */ |
| 457 | @VisibleForTesting |
| 458 | public static final class CaptivePortalProbeResult { |
Hugo Benichi | d953bf8 | 2016-09-27 09:22:35 +0900 | [diff] [blame] | 459 | static final CaptivePortalProbeResult FAILED = new CaptivePortalProbeResult(599); |
Lorenzo Colitti | c5be12e | 2016-04-19 21:57:31 +0900 | [diff] [blame] | 460 | |
Hugo Benichi | d953bf8 | 2016-09-27 09:22:35 +0900 | [diff] [blame] | 461 | private final int mHttpResponseCode; // HTTP response code returned from Internet probe. |
| 462 | final String redirectUrl; // Redirect destination returned from Internet probe. |
| 463 | final String detectUrl; // URL where a 204 response code indicates |
| 464 | // captive portal has been appeased. |
Paul Jensen | 23243731 | 2016-04-06 09:51:26 -0400 | [diff] [blame] | 465 | |
Hugo Benichi | d953bf8 | 2016-09-27 09:22:35 +0900 | [diff] [blame] | 466 | public CaptivePortalProbeResult( |
| 467 | int httpResponseCode, String redirectUrl, String detectUrl) { |
Paul Jensen | 23243731 | 2016-04-06 09:51:26 -0400 | [diff] [blame] | 468 | mHttpResponseCode = httpResponseCode; |
Hugo Benichi | d953bf8 | 2016-09-27 09:22:35 +0900 | [diff] [blame] | 469 | this.redirectUrl = redirectUrl; |
| 470 | this.detectUrl = detectUrl; |
| 471 | } |
| 472 | |
| 473 | public CaptivePortalProbeResult(int httpResponseCode) { |
| 474 | this(httpResponseCode, null, null); |
Paul Jensen | 23243731 | 2016-04-06 09:51:26 -0400 | [diff] [blame] | 475 | } |
Lorenzo Colitti | c5be12e | 2016-04-19 21:57:31 +0900 | [diff] [blame] | 476 | |
| 477 | boolean isSuccessful() { return mHttpResponseCode == 204; } |
| 478 | boolean isPortal() { |
| 479 | return !isSuccessful() && mHttpResponseCode >= 200 && mHttpResponseCode <= 399; |
| 480 | } |
Paul Jensen | 23243731 | 2016-04-06 09:51:26 -0400 | [diff] [blame] | 481 | } |
| 482 | |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 483 | // Being in the EvaluatingState State indicates the Network is being evaluated for internet |
Paul Jensen | d0491e9a | 2015-05-05 14:52:22 -0400 | [diff] [blame] | 484 | // connectivity, or that the user has indicated that this network is unwanted. |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 485 | private class EvaluatingState extends State { |
Paul Jensen | d0491e9a | 2015-05-05 14:52:22 -0400 | [diff] [blame] | 486 | private int mReevaluateDelayMs; |
| 487 | private int mAttempts; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 488 | |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 489 | @Override |
| 490 | public void enter() { |
Erik Kline | a488c23 | 2016-04-15 15:49:42 +0900 | [diff] [blame] | 491 | // If we have already started to track time spent in EvaluatingState |
| 492 | // don't reset the timer due simply to, say, commands or events that |
| 493 | // cause us to exit and re-enter EvaluatingState. |
| 494 | if (!mEvaluationTimer.isStarted()) { |
| 495 | mEvaluationTimer.start(); |
| 496 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 497 | sendMessage(CMD_REEVALUATE, ++mReevaluateToken, 0); |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 498 | if (mUidResponsibleForReeval != INVALID_UID) { |
| 499 | TrafficStats.setThreadStatsUid(mUidResponsibleForReeval); |
| 500 | mUidResponsibleForReeval = INVALID_UID; |
| 501 | } |
Paul Jensen | d0491e9a | 2015-05-05 14:52:22 -0400 | [diff] [blame] | 502 | mReevaluateDelayMs = INITIAL_REEVALUATE_DELAY_MS; |
| 503 | mAttempts = 0; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | @Override |
| 507 | public boolean processMessage(Message message) { |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 508 | switch (message.what) { |
| 509 | case CMD_REEVALUATE: |
Paul Jensen | d0491e9a | 2015-05-05 14:52:22 -0400 | [diff] [blame] | 510 | if (message.arg1 != mReevaluateToken || mUserDoesNotWant) |
Paul Jensen | d6a3f7e | 2014-08-19 09:40:11 -0400 | [diff] [blame] | 511 | return HANDLED; |
Paul Jensen | 2c311d6 | 2014-11-17 12:34:51 -0500 | [diff] [blame] | 512 | // Don't bother validating networks that don't satisify the default request. |
| 513 | // This includes: |
| 514 | // - VPNs which can be considered explicitly desired by the user and the |
| 515 | // user's desire trumps whether the network validates. |
| 516 | // - Networks that don't provide internet access. It's unclear how to |
| 517 | // validate such networks. |
| 518 | // - Untrusted networks. It's unsafe to prompt the user to sign-in to |
| 519 | // such networks and the user didn't express interest in connecting to |
| 520 | // such networks (an app did) so the user may be unhappily surprised when |
| 521 | // asked to sign-in to a network they didn't want to connect to in the |
| 522 | // first place. Validation could be done to adjust the network scores |
| 523 | // however these networks are app-requested and may not be intended for |
| 524 | // general usage, in which case general validation may not be an accurate |
| 525 | // measure of the network's quality. Only the app knows how to evaluate |
| 526 | // the network so don't bother validating here. Furthermore sending HTTP |
| 527 | // packets over the network may be undesirable, for example an extremely |
| 528 | // expensive metered network, or unwanted leaking of the User Agent string. |
| 529 | if (!mDefaultRequest.networkCapabilities.satisfiedByNetworkCapabilities( |
| 530 | mNetworkAgentInfo.networkCapabilities)) { |
Lorenzo Colitti | c5be12e | 2016-04-19 21:57:31 +0900 | [diff] [blame] | 531 | validationLog("Network would not satisfy default request, not validating"); |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 532 | transitionTo(mValidatedState); |
Paul Jensen | d6a3f7e | 2014-08-19 09:40:11 -0400 | [diff] [blame] | 533 | return HANDLED; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 534 | } |
Paul Jensen | d0491e9a | 2015-05-05 14:52:22 -0400 | [diff] [blame] | 535 | mAttempts++; |
Lorenzo Colitti | 351bfad | 2015-01-22 22:36:50 +0900 | [diff] [blame] | 536 | // Note: This call to isCaptivePortal() could take up to a minute. Resolving the |
| 537 | // server's IP addresses could hit the DNS timeout, and attempting connections |
| 538 | // to each of the server's several IP addresses (currently one IPv4 and one |
| 539 | // IPv6) could each take SOCKET_TIMEOUT_MS. During this time this StateMachine |
| 540 | // will be unresponsive. isCaptivePortal() could be executed on another Thread |
| 541 | // if this is found to cause problems. |
Paul Jensen | 23243731 | 2016-04-06 09:51:26 -0400 | [diff] [blame] | 542 | CaptivePortalProbeResult probeResult = isCaptivePortal(); |
Lorenzo Colitti | c5be12e | 2016-04-19 21:57:31 +0900 | [diff] [blame] | 543 | if (probeResult.isSuccessful()) { |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 544 | transitionTo(mValidatedState); |
Lorenzo Colitti | c5be12e | 2016-04-19 21:57:31 +0900 | [diff] [blame] | 545 | } else if (probeResult.isPortal()) { |
Paul Jensen | 23243731 | 2016-04-06 09:51:26 -0400 | [diff] [blame] | 546 | mConnectivityServiceHandler.sendMessage(obtainMessage(EVENT_NETWORK_TESTED, |
Hugo Benichi | d953bf8 | 2016-09-27 09:22:35 +0900 | [diff] [blame] | 547 | NETWORK_TEST_RESULT_INVALID, mNetId, probeResult.redirectUrl)); |
| 548 | mLastPortalProbeResult = probeResult; |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 549 | transitionTo(mCaptivePortalState); |
Paul Jensen | d0491e9a | 2015-05-05 14:52:22 -0400 | [diff] [blame] | 550 | } else { |
Paul Jensen | d9be23f | 2015-05-19 14:51:47 -0400 | [diff] [blame] | 551 | final Message msg = obtainMessage(CMD_REEVALUATE, ++mReevaluateToken, 0); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 552 | sendMessageDelayed(msg, mReevaluateDelayMs); |
Hugo Benichi | cfddd68 | 2016-05-31 16:28:06 +0900 | [diff] [blame] | 553 | logNetworkEvent(NetworkEvent.NETWORK_VALIDATION_FAILED); |
Paul Jensen | d9be23f | 2015-05-19 14:51:47 -0400 | [diff] [blame] | 554 | mConnectivityServiceHandler.sendMessage(obtainMessage( |
Erik Kline | a488c23 | 2016-04-15 15:49:42 +0900 | [diff] [blame] | 555 | EVENT_NETWORK_TESTED, NETWORK_TEST_RESULT_INVALID, mNetId, |
Hugo Benichi | d953bf8 | 2016-09-27 09:22:35 +0900 | [diff] [blame] | 556 | probeResult.redirectUrl)); |
Paul Jensen | d9be23f | 2015-05-19 14:51:47 -0400 | [diff] [blame] | 557 | if (mAttempts >= BLAME_FOR_EVALUATION_ATTEMPTS) { |
Paul Jensen | d0491e9a | 2015-05-05 14:52:22 -0400 | [diff] [blame] | 558 | // Don't continue to blame UID forever. |
| 559 | TrafficStats.clearThreadStatsUid(); |
| 560 | } |
| 561 | mReevaluateDelayMs *= 2; |
| 562 | if (mReevaluateDelayMs > MAX_REEVALUATE_DELAY_MS) { |
| 563 | mReevaluateDelayMs = MAX_REEVALUATE_DELAY_MS; |
| 564 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 565 | } |
Paul Jensen | d6a3f7e | 2014-08-19 09:40:11 -0400 | [diff] [blame] | 566 | return HANDLED; |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 567 | case CMD_FORCE_REEVALUATION: |
Paul Jensen | d0491e9a | 2015-05-05 14:52:22 -0400 | [diff] [blame] | 568 | // Before IGNORE_REEVALUATE_ATTEMPTS attempts are made, |
| 569 | // ignore any re-evaluation requests. After, restart the |
| 570 | // evaluation process via EvaluatingState#enter. |
Erik Kline | a488c23 | 2016-04-15 15:49:42 +0900 | [diff] [blame] | 571 | return (mAttempts < IGNORE_REEVALUATE_ATTEMPTS) ? HANDLED : NOT_HANDLED; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 572 | default: |
| 573 | return NOT_HANDLED; |
| 574 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 575 | } |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 576 | |
| 577 | @Override |
| 578 | public void exit() { |
| 579 | TrafficStats.clearThreadStatsUid(); |
| 580 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 581 | } |
| 582 | |
Paul Jensen | dcbe835 | 2014-09-16 16:28:34 -0400 | [diff] [blame] | 583 | // BroadcastReceiver that waits for a particular Intent and then posts a message. |
| 584 | private class CustomIntentReceiver extends BroadcastReceiver { |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 585 | private final int mToken; |
| 586 | private final int mWhat; |
Paul Jensen | dcbe835 | 2014-09-16 16:28:34 -0400 | [diff] [blame] | 587 | private final String mAction; |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 588 | CustomIntentReceiver(String action, int token, int what) { |
| 589 | mToken = token; |
| 590 | mWhat = what; |
Paul Jensen | dcbe835 | 2014-09-16 16:28:34 -0400 | [diff] [blame] | 591 | mAction = action + "_" + mNetworkAgentInfo.network.netId + "_" + token; |
| 592 | mContext.registerReceiver(this, new IntentFilter(mAction)); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 593 | } |
Paul Jensen | dcbe835 | 2014-09-16 16:28:34 -0400 | [diff] [blame] | 594 | public PendingIntent getPendingIntent() { |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 595 | final Intent intent = new Intent(mAction); |
| 596 | intent.setPackage(mContext.getPackageName()); |
| 597 | return PendingIntent.getBroadcast(mContext, 0, intent, 0); |
Paul Jensen | dcbe835 | 2014-09-16 16:28:34 -0400 | [diff] [blame] | 598 | } |
| 599 | @Override |
| 600 | public void onReceive(Context context, Intent intent) { |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 601 | if (intent.getAction().equals(mAction)) sendMessage(obtainMessage(mWhat, mToken)); |
Paul Jensen | dcbe835 | 2014-09-16 16:28:34 -0400 | [diff] [blame] | 602 | } |
| 603 | } |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 604 | |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 605 | // Being in the CaptivePortalState State indicates a captive portal was detected and the user |
| 606 | // has been shown a notification to sign-in. |
| 607 | private class CaptivePortalState extends State { |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 608 | private static final String ACTION_LAUNCH_CAPTIVE_PORTAL_APP = |
| 609 | "android.net.netmon.launchCaptivePortalApp"; |
| 610 | |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 611 | @Override |
| 612 | public void enter() { |
Hugo Benichi | dd22982 | 2016-11-15 23:23:24 +0900 | [diff] [blame^] | 613 | maybeLogEvaluationResult( |
| 614 | networkEventType(validationStage(), EvaluationResult.CAPTIVE_PORTAL)); |
Paul Jensen | d0491e9a | 2015-05-05 14:52:22 -0400 | [diff] [blame] | 615 | // Don't annoy user with sign-in notifications. |
Paul Jensen | 700f236 | 2015-05-05 14:56:10 -0400 | [diff] [blame] | 616 | if (mDontDisplaySigninNotification) return; |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 617 | // Create a CustomIntentReceiver that sends us a |
| 618 | // CMD_LAUNCH_CAPTIVE_PORTAL_APP message when the user |
| 619 | // touches the notification. |
| 620 | if (mLaunchCaptivePortalAppBroadcastReceiver == null) { |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 621 | // Wait for result. |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 622 | mLaunchCaptivePortalAppBroadcastReceiver = new CustomIntentReceiver( |
| 623 | ACTION_LAUNCH_CAPTIVE_PORTAL_APP, new Random().nextInt(), |
| 624 | CMD_LAUNCH_CAPTIVE_PORTAL_APP); |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 625 | } |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 626 | // Display the sign in notification. |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 627 | Message message = obtainMessage(EVENT_PROVISIONING_NOTIFICATION, 1, |
| 628 | mNetworkAgentInfo.network.netId, |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 629 | mLaunchCaptivePortalAppBroadcastReceiver.getPendingIntent()); |
Paul Jensen | 71b645f | 2014-10-13 14:13:07 -0400 | [diff] [blame] | 630 | mConnectivityServiceHandler.sendMessage(message); |
Paul Jensen | ee3e2ce | 2015-06-17 15:02:54 -0400 | [diff] [blame] | 631 | // Retest for captive portal occasionally. |
| 632 | sendMessageDelayed(CMD_CAPTIVE_PORTAL_RECHECK, 0 /* no UID */, |
| 633 | CAPTIVE_PORTAL_REEVALUATE_DELAY_MS); |
Hugo Benichi | dd22982 | 2016-11-15 23:23:24 +0900 | [diff] [blame^] | 634 | mValidations++; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | @Override |
Paul Jensen | ee3e2ce | 2015-06-17 15:02:54 -0400 | [diff] [blame] | 638 | public void exit() { |
fionaxu | 1bf6ec2 | 2016-05-23 16:33:16 -0700 | [diff] [blame] | 639 | removeMessages(CMD_CAPTIVE_PORTAL_RECHECK); |
Paul Jensen | ee3e2ce | 2015-06-17 15:02:54 -0400 | [diff] [blame] | 640 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 641 | } |
| 642 | |
Hugo Benichi | 92eb22fd | 2016-09-27 13:01:41 +0900 | [diff] [blame] | 643 | private static String getCaptivePortalServerHttpsUrl(Context context) { |
| 644 | return getSetting(context, Settings.Global.CAPTIVE_PORTAL_HTTPS_URL, DEFAULT_HTTPS_URL); |
Lorenzo Colitti | c5be12e | 2016-04-19 21:57:31 +0900 | [diff] [blame] | 645 | } |
| 646 | |
Hugo Benichi | 92eb22fd | 2016-09-27 13:01:41 +0900 | [diff] [blame] | 647 | public static String getCaptivePortalServerHttpUrl(Context context) { |
| 648 | return getSetting(context, Settings.Global.CAPTIVE_PORTAL_HTTP_URL, DEFAULT_HTTP_URL); |
| 649 | } |
| 650 | |
| 651 | private static String getCaptivePortalFallbackUrl(Context context) { |
| 652 | return getSetting(context, |
| 653 | Settings.Global.CAPTIVE_PORTAL_FALLBACK_URL, DEFAULT_FALLBACK_URL); |
| 654 | } |
| 655 | |
| 656 | private static String getCaptivePortalUserAgent(Context context) { |
| 657 | return getSetting(context, Settings.Global.CAPTIVE_PORTAL_USER_AGENT, DEFAULT_USER_AGENT); |
| 658 | } |
| 659 | |
| 660 | private static String getSetting(Context context, String symbol, String defaultValue) { |
| 661 | final String value = Settings.Global.getString(context.getContentResolver(), symbol); |
| 662 | return value != null ? value : defaultValue; |
Lorenzo Colitti | c5be12e | 2016-04-19 21:57:31 +0900 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | @VisibleForTesting |
| 666 | protected CaptivePortalProbeResult isCaptivePortal() { |
Calvin On | 4bc78eb | 2016-10-11 15:10:46 -0700 | [diff] [blame] | 667 | if (!mIsCaptivePortalCheckEnabled) { |
| 668 | validationLog("Validation disabled."); |
| 669 | return new CaptivePortalProbeResult(204); |
| 670 | } |
Lorenzo Colitti | c5be12e | 2016-04-19 21:57:31 +0900 | [diff] [blame] | 671 | |
Hugo Benichi | 92eb22fd | 2016-09-27 13:01:41 +0900 | [diff] [blame] | 672 | URL pacUrl = null, httpsUrl = null, httpUrl = null, fallbackUrl = null; |
Lorenzo Colitti | c5be12e | 2016-04-19 21:57:31 +0900 | [diff] [blame] | 673 | |
| 674 | // On networks with a PAC instead of fetching a URL that should result in a 204 |
| 675 | // response, we instead simply fetch the PAC script. This is done for a few reasons: |
| 676 | // 1. At present our PAC code does not yet handle multiple PACs on multiple networks |
| 677 | // until something like https://android-review.googlesource.com/#/c/115180/ lands. |
| 678 | // Network.openConnection() will ignore network-specific PACs and instead fetch |
| 679 | // using NO_PROXY. If a PAC is in place, the only fetch we know will succeed with |
| 680 | // NO_PROXY is the fetch of the PAC itself. |
| 681 | // 2. To proxy the generate_204 fetch through a PAC would require a number of things |
| 682 | // happen before the fetch can commence, namely: |
| 683 | // a) the PAC script be fetched |
| 684 | // b) a PAC script resolver service be fired up and resolve the captive portal |
| 685 | // server. |
| 686 | // Network validation could be delayed until these prerequisities are satisifed or |
| 687 | // could simply be left to race them. Neither is an optimal solution. |
| 688 | // 3. PAC scripts are sometimes used to block or restrict Internet access and may in |
| 689 | // fact block fetching of the generate_204 URL which would lead to false negative |
| 690 | // results for network validation. |
| 691 | final ProxyInfo proxyInfo = mNetworkAgentInfo.linkProperties.getHttpProxy(); |
| 692 | if (proxyInfo != null && !Uri.EMPTY.equals(proxyInfo.getPacFileUrl())) { |
Hugo Benichi | 92eb22fd | 2016-09-27 13:01:41 +0900 | [diff] [blame] | 693 | pacUrl = makeURL(proxyInfo.getPacFileUrl().toString()); |
| 694 | if (pacUrl == null) { |
Lorenzo Colitti | c5be12e | 2016-04-19 21:57:31 +0900 | [diff] [blame] | 695 | return CaptivePortalProbeResult.FAILED; |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | if (pacUrl == null) { |
Hugo Benichi | 92eb22fd | 2016-09-27 13:01:41 +0900 | [diff] [blame] | 700 | httpsUrl = makeURL(getCaptivePortalServerHttpsUrl(mContext)); |
| 701 | httpUrl = makeURL(getCaptivePortalServerHttpUrl(mContext)); |
| 702 | fallbackUrl = makeURL(getCaptivePortalFallbackUrl(mContext)); |
| 703 | if (httpUrl == null || httpsUrl == null) { |
Lorenzo Colitti | c5be12e | 2016-04-19 21:57:31 +0900 | [diff] [blame] | 704 | return CaptivePortalProbeResult.FAILED; |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | long startTime = SystemClock.elapsedRealtime(); |
| 709 | |
| 710 | // Pre-resolve the captive portal server host so we can log it. |
| 711 | // Only do this if HttpURLConnection is about to, to avoid any potentially |
| 712 | // unnecessary resolution. |
| 713 | String hostToResolve = null; |
| 714 | if (pacUrl != null) { |
| 715 | hostToResolve = pacUrl.getHost(); |
| 716 | } else if (proxyInfo != null) { |
| 717 | hostToResolve = proxyInfo.getHost(); |
| 718 | } else { |
| 719 | hostToResolve = httpUrl.getHost(); |
| 720 | } |
| 721 | |
| 722 | if (!TextUtils.isEmpty(hostToResolve)) { |
| 723 | String probeName = ValidationProbeEvent.getProbeName(ValidationProbeEvent.PROBE_DNS); |
| 724 | final Stopwatch dnsTimer = new Stopwatch().start(); |
Hugo Benichi | cfddd68 | 2016-05-31 16:28:06 +0900 | [diff] [blame] | 725 | int dnsResult; |
| 726 | long dnsLatency; |
Lorenzo Colitti | c5be12e | 2016-04-19 21:57:31 +0900 | [diff] [blame] | 727 | try { |
| 728 | InetAddress[] addresses = mNetworkAgentInfo.network.getAllByName(hostToResolve); |
Hugo Benichi | cfddd68 | 2016-05-31 16:28:06 +0900 | [diff] [blame] | 729 | dnsResult = ValidationProbeEvent.DNS_SUCCESS; |
| 730 | dnsLatency = dnsTimer.stop(); |
Lorenzo Colitti | c5be12e | 2016-04-19 21:57:31 +0900 | [diff] [blame] | 731 | final StringBuffer connectInfo = new StringBuffer(", " + hostToResolve + "="); |
| 732 | for (InetAddress address : addresses) { |
| 733 | connectInfo.append(address.getHostAddress()); |
| 734 | if (address != addresses[addresses.length-1]) connectInfo.append(","); |
| 735 | } |
| 736 | validationLog(probeName + " OK " + dnsLatency + "ms" + connectInfo); |
| 737 | } catch (UnknownHostException e) { |
Hugo Benichi | cfddd68 | 2016-05-31 16:28:06 +0900 | [diff] [blame] | 738 | dnsResult = ValidationProbeEvent.DNS_FAILURE; |
| 739 | dnsLatency = dnsTimer.stop(); |
Lorenzo Colitti | c5be12e | 2016-04-19 21:57:31 +0900 | [diff] [blame] | 740 | validationLog(probeName + " FAIL " + dnsLatency + "ms, " + hostToResolve); |
| 741 | } |
Hugo Benichi | cfddd68 | 2016-05-31 16:28:06 +0900 | [diff] [blame] | 742 | logValidationProbe(dnsLatency, ValidationProbeEvent.PROBE_DNS, dnsResult); |
Lorenzo Colitti | c5be12e | 2016-04-19 21:57:31 +0900 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | CaptivePortalProbeResult result; |
| 746 | if (pacUrl != null) { |
| 747 | result = sendHttpProbe(pacUrl, ValidationProbeEvent.PROBE_PAC); |
| 748 | } else if (mUseHttps) { |
Hugo Benichi | 92eb22fd | 2016-09-27 13:01:41 +0900 | [diff] [blame] | 749 | result = sendParallelHttpProbes(httpsUrl, httpUrl, fallbackUrl); |
Lorenzo Colitti | c5be12e | 2016-04-19 21:57:31 +0900 | [diff] [blame] | 750 | } else { |
| 751 | result = sendHttpProbe(httpUrl, ValidationProbeEvent.PROBE_HTTP); |
| 752 | } |
| 753 | |
| 754 | long endTime = SystemClock.elapsedRealtime(); |
| 755 | |
| 756 | sendNetworkConditionsBroadcast(true /* response received */, |
| 757 | result.isPortal() /* isCaptivePortal */, |
| 758 | startTime, endTime); |
| 759 | |
| 760 | return result; |
Udam Saini | b7c2487 | 2016-01-04 12:16:14 -0800 | [diff] [blame] | 761 | } |
| 762 | |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 763 | /** |
| 764 | * Do a URL fetch on a known server to see if we get the data we expect. |
| 765 | * Returns HTTP response code. |
| 766 | */ |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 767 | @VisibleForTesting |
Lorenzo Colitti | c5be12e | 2016-04-19 21:57:31 +0900 | [diff] [blame] | 768 | protected CaptivePortalProbeResult sendHttpProbe(URL url, int probeType) { |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 769 | HttpURLConnection urlConnection = null; |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 770 | int httpResponseCode = 599; |
Paul Jensen | 23243731 | 2016-04-06 09:51:26 -0400 | [diff] [blame] | 771 | String redirectUrl = null; |
Erik Kline | a488c23 | 2016-04-15 15:49:42 +0900 | [diff] [blame] | 772 | final Stopwatch probeTimer = new Stopwatch().start(); |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 773 | try { |
Lorenzo Colitti | 9f1274b | 2014-08-21 11:45:54 -0700 | [diff] [blame] | 774 | urlConnection = (HttpURLConnection) mNetworkAgentInfo.network.openConnection(url); |
Lorenzo Colitti | c5be12e | 2016-04-19 21:57:31 +0900 | [diff] [blame] | 775 | urlConnection.setInstanceFollowRedirects(probeType == ValidationProbeEvent.PROBE_PAC); |
Paul Jensen | e547ff2 | 2014-08-04 09:12:24 -0400 | [diff] [blame] | 776 | urlConnection.setConnectTimeout(SOCKET_TIMEOUT_MS); |
| 777 | urlConnection.setReadTimeout(SOCKET_TIMEOUT_MS); |
| 778 | urlConnection.setUseCaches(false); |
Hugo Benichi | 92eb22fd | 2016-09-27 13:01:41 +0900 | [diff] [blame] | 779 | final String userAgent = getCaptivePortalUserAgent(mContext); |
| 780 | if (userAgent != null) { |
| 781 | urlConnection.setRequestProperty("User-Agent", userAgent); |
| 782 | } |
Paul Jensen | 306f1a4 | 2014-08-04 10:59:01 -0400 | [diff] [blame] | 783 | |
| 784 | // Time how long it takes to get a response to our request |
| 785 | long requestTimestamp = SystemClock.elapsedRealtime(); |
| 786 | |
Pierre Imai | be12d76 | 2016-03-10 17:00:50 +0900 | [diff] [blame] | 787 | httpResponseCode = urlConnection.getResponseCode(); |
Paul Jensen | 23243731 | 2016-04-06 09:51:26 -0400 | [diff] [blame] | 788 | redirectUrl = urlConnection.getHeaderField("location"); |
Paul Jensen | 306f1a4 | 2014-08-04 10:59:01 -0400 | [diff] [blame] | 789 | |
| 790 | // Time how long it takes to get a response to our request |
| 791 | long responseTimestamp = SystemClock.elapsedRealtime(); |
| 792 | |
Lorenzo Colitti | c5be12e | 2016-04-19 21:57:31 +0900 | [diff] [blame] | 793 | validationLog(ValidationProbeEvent.getProbeName(probeType) + " " + url + |
| 794 | " time=" + (responseTimestamp - requestTimestamp) + "ms" + |
| 795 | " ret=" + httpResponseCode + |
Robert Greenwalt | 22b4c6a | 2015-06-23 15:03:33 -0700 | [diff] [blame] | 796 | " headers=" + urlConnection.getHeaderFields()); |
Paul Jensen | e547ff2 | 2014-08-04 09:12:24 -0400 | [diff] [blame] | 797 | // NOTE: We may want to consider an "HTTP/1.0 204" response to be a captive |
| 798 | // portal. The only example of this seen so far was a captive portal. For |
| 799 | // the time being go with prior behavior of assuming it's not a captive |
| 800 | // portal. If it is considered a captive portal, a different sign-in URL |
| 801 | // is needed (i.e. can't browse a 204). This could be the result of an HTTP |
| 802 | // proxy server. |
| 803 | |
| 804 | // Consider 200 response with "Content-length=0" to not be a captive portal. |
| 805 | // There's no point in considering this a captive portal as the user cannot |
| 806 | // sign-in to an empty page. Probably the result of a broken transparent proxy. |
| 807 | // See http://b/9972012. |
| 808 | if (httpResponseCode == 200 && urlConnection.getContentLength() == 0) { |
Robert Greenwalt | 22b4c6a | 2015-06-23 15:03:33 -0700 | [diff] [blame] | 809 | validationLog("Empty 200 response interpreted as 204 response."); |
Paul Jensen | e547ff2 | 2014-08-04 09:12:24 -0400 | [diff] [blame] | 810 | httpResponseCode = 204; |
| 811 | } |
Paul Jensen | 306f1a4 | 2014-08-04 10:59:01 -0400 | [diff] [blame] | 812 | |
Lorenzo Colitti | c5be12e | 2016-04-19 21:57:31 +0900 | [diff] [blame] | 813 | if (httpResponseCode == 200 && probeType == ValidationProbeEvent.PROBE_PAC) { |
Robert Greenwalt | 22b4c6a | 2015-06-23 15:03:33 -0700 | [diff] [blame] | 814 | validationLog("PAC fetch 200 response interpreted as 204 response."); |
Paul Jensen | 8fe1742 | 2015-02-02 11:03:03 -0500 | [diff] [blame] | 815 | httpResponseCode = 204; |
| 816 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 817 | } catch (IOException e) { |
Robert Greenwalt | 22b4c6a | 2015-06-23 15:03:33 -0700 | [diff] [blame] | 818 | validationLog("Probably not a portal: exception " + e); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 819 | if (httpResponseCode == 599) { |
| 820 | // TODO: Ping gateway and DNS server and log results. |
| 821 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 822 | } finally { |
| 823 | if (urlConnection != null) { |
| 824 | urlConnection.disconnect(); |
| 825 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 826 | } |
Hugo Benichi | cfddd68 | 2016-05-31 16:28:06 +0900 | [diff] [blame] | 827 | logValidationProbe(probeTimer.stop(), probeType, httpResponseCode); |
Hugo Benichi | d953bf8 | 2016-09-27 09:22:35 +0900 | [diff] [blame] | 828 | return new CaptivePortalProbeResult(httpResponseCode, redirectUrl, url.toString()); |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 829 | } |
Paul Jensen | 306f1a4 | 2014-08-04 10:59:01 -0400 | [diff] [blame] | 830 | |
Hugo Benichi | d953bf8 | 2016-09-27 09:22:35 +0900 | [diff] [blame] | 831 | private CaptivePortalProbeResult sendParallelHttpProbes( |
| 832 | URL httpsUrl, URL httpUrl, URL fallbackUrl) { |
| 833 | // Number of probes to wait for. If a probe completes with a conclusive answer |
| 834 | // it shortcuts the latch immediately by forcing the count to 0. |
Lorenzo Colitti | c5be12e | 2016-04-19 21:57:31 +0900 | [diff] [blame] | 835 | final CountDownLatch latch = new CountDownLatch(2); |
| 836 | |
Lorenzo Colitti | c5be12e | 2016-04-19 21:57:31 +0900 | [diff] [blame] | 837 | final class ProbeThread extends Thread { |
| 838 | private final boolean mIsHttps; |
Hugo Benichi | d953bf8 | 2016-09-27 09:22:35 +0900 | [diff] [blame] | 839 | private volatile CaptivePortalProbeResult mResult = CaptivePortalProbeResult.FAILED; |
Lorenzo Colitti | c5be12e | 2016-04-19 21:57:31 +0900 | [diff] [blame] | 840 | |
| 841 | public ProbeThread(boolean isHttps) { |
| 842 | mIsHttps = isHttps; |
| 843 | } |
| 844 | |
Hugo Benichi | d953bf8 | 2016-09-27 09:22:35 +0900 | [diff] [blame] | 845 | public CaptivePortalProbeResult result() { |
Lorenzo Colitti | c5be12e | 2016-04-19 21:57:31 +0900 | [diff] [blame] | 846 | return mResult; |
| 847 | } |
| 848 | |
| 849 | @Override |
| 850 | public void run() { |
| 851 | if (mIsHttps) { |
| 852 | mResult = sendHttpProbe(httpsUrl, ValidationProbeEvent.PROBE_HTTPS); |
| 853 | } else { |
| 854 | mResult = sendHttpProbe(httpUrl, ValidationProbeEvent.PROBE_HTTP); |
| 855 | } |
| 856 | if ((mIsHttps && mResult.isSuccessful()) || (!mIsHttps && mResult.isPortal())) { |
Hugo Benichi | d953bf8 | 2016-09-27 09:22:35 +0900 | [diff] [blame] | 857 | // Stop waiting immediately if https succeeds or if http finds a portal. |
| 858 | while (latch.getCount() > 0) { |
| 859 | latch.countDown(); |
| 860 | } |
Lorenzo Colitti | c5be12e | 2016-04-19 21:57:31 +0900 | [diff] [blame] | 861 | } |
Hugo Benichi | d953bf8 | 2016-09-27 09:22:35 +0900 | [diff] [blame] | 862 | // Signal this probe has completed. |
Lorenzo Colitti | c5be12e | 2016-04-19 21:57:31 +0900 | [diff] [blame] | 863 | latch.countDown(); |
| 864 | } |
| 865 | } |
| 866 | |
Hugo Benichi | d953bf8 | 2016-09-27 09:22:35 +0900 | [diff] [blame] | 867 | final ProbeThread httpsProbe = new ProbeThread(true); |
| 868 | final ProbeThread httpProbe = new ProbeThread(false); |
Lorenzo Colitti | c5be12e | 2016-04-19 21:57:31 +0900 | [diff] [blame] | 869 | |
| 870 | try { |
Hugo Benichi | d953bf8 | 2016-09-27 09:22:35 +0900 | [diff] [blame] | 871 | httpsProbe.start(); |
| 872 | httpProbe.start(); |
| 873 | latch.await(PROBE_TIMEOUT_MS, TimeUnit.MILLISECONDS); |
Lorenzo Colitti | c5be12e | 2016-04-19 21:57:31 +0900 | [diff] [blame] | 874 | } catch (InterruptedException e) { |
Hugo Benichi | d953bf8 | 2016-09-27 09:22:35 +0900 | [diff] [blame] | 875 | validationLog("Error: probes wait interrupted!"); |
Lorenzo Colitti | c5be12e | 2016-04-19 21:57:31 +0900 | [diff] [blame] | 876 | return CaptivePortalProbeResult.FAILED; |
| 877 | } |
| 878 | |
Hugo Benichi | d953bf8 | 2016-09-27 09:22:35 +0900 | [diff] [blame] | 879 | final CaptivePortalProbeResult httpsResult = httpsProbe.result(); |
| 880 | final CaptivePortalProbeResult httpResult = httpProbe.result(); |
Lorenzo Colitti | c5be12e | 2016-04-19 21:57:31 +0900 | [diff] [blame] | 881 | |
Hugo Benichi | d953bf8 | 2016-09-27 09:22:35 +0900 | [diff] [blame] | 882 | // Look for a conclusive probe result first. |
| 883 | if (httpResult.isPortal()) { |
| 884 | return httpResult; |
| 885 | } |
| 886 | // httpsResult.isPortal() is not expected, but check it nonetheless. |
| 887 | if (httpsResult.isPortal() || httpsResult.isSuccessful()) { |
| 888 | return httpsResult; |
| 889 | } |
| 890 | // If a fallback url is specified, use a fallback probe to try again portal detection. |
| 891 | if (fallbackUrl != null) { |
| 892 | CaptivePortalProbeResult result = |
| 893 | sendHttpProbe(fallbackUrl, ValidationProbeEvent.PROBE_FALLBACK); |
| 894 | if (result.isPortal()) { |
| 895 | return result; |
| 896 | } |
| 897 | } |
| 898 | // Otherwise wait until https probe completes and use its result. |
| 899 | try { |
| 900 | httpsProbe.join(); |
| 901 | } catch (InterruptedException e) { |
| 902 | validationLog("Error: https probe wait interrupted!"); |
| 903 | return CaptivePortalProbeResult.FAILED; |
| 904 | } |
| 905 | return httpsProbe.result(); |
Lorenzo Colitti | c5be12e | 2016-04-19 21:57:31 +0900 | [diff] [blame] | 906 | } |
| 907 | |
Hugo Benichi | 92eb22fd | 2016-09-27 13:01:41 +0900 | [diff] [blame] | 908 | private URL makeURL(String url) { |
| 909 | if (url != null) { |
| 910 | try { |
| 911 | return new URL(url); |
| 912 | } catch (MalformedURLException e) { |
| 913 | validationLog("Bad URL: " + url); |
| 914 | } |
| 915 | } |
| 916 | return null; |
| 917 | } |
| 918 | |
Paul Jensen | 306f1a4 | 2014-08-04 10:59:01 -0400 | [diff] [blame] | 919 | /** |
| 920 | * @param responseReceived - whether or not we received a valid HTTP response to our request. |
| 921 | * If false, isCaptivePortal and responseTimestampMs are ignored |
| 922 | * TODO: This should be moved to the transports. The latency could be passed to the transports |
| 923 | * along with the captive portal result. Currently the TYPE_MOBILE broadcasts appear unused so |
| 924 | * perhaps this could just be added to the WiFi transport only. |
| 925 | */ |
| 926 | private void sendNetworkConditionsBroadcast(boolean responseReceived, boolean isCaptivePortal, |
| 927 | long requestTimestampMs, long responseTimestampMs) { |
| 928 | if (Settings.Global.getInt(mContext.getContentResolver(), |
| 929 | Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE, 0) == 0) { |
Paul Jensen | 306f1a4 | 2014-08-04 10:59:01 -0400 | [diff] [blame] | 930 | return; |
| 931 | } |
| 932 | |
Robert Greenwalt | fb68f8f | 2014-08-13 13:43:32 -0700 | [diff] [blame] | 933 | if (systemReady == false) return; |
| 934 | |
Paul Jensen | 306f1a4 | 2014-08-04 10:59:01 -0400 | [diff] [blame] | 935 | Intent latencyBroadcast = new Intent(ACTION_NETWORK_CONDITIONS_MEASURED); |
| 936 | switch (mNetworkAgentInfo.networkInfo.getType()) { |
| 937 | case ConnectivityManager.TYPE_WIFI: |
| 938 | WifiInfo currentWifiInfo = mWifiManager.getConnectionInfo(); |
| 939 | if (currentWifiInfo != null) { |
| 940 | // NOTE: getSSID()'s behavior changed in API 17; before that, SSIDs were not |
| 941 | // surrounded by double quotation marks (thus violating the Javadoc), but this |
| 942 | // was changed to match the Javadoc in API 17. Since clients may have started |
| 943 | // sanitizing the output of this method since API 17 was released, we should |
| 944 | // not change it here as it would become impossible to tell whether the SSID is |
| 945 | // simply being surrounded by quotes due to the API, or whether those quotes |
| 946 | // are actually part of the SSID. |
| 947 | latencyBroadcast.putExtra(EXTRA_SSID, currentWifiInfo.getSSID()); |
| 948 | latencyBroadcast.putExtra(EXTRA_BSSID, currentWifiInfo.getBSSID()); |
| 949 | } else { |
Hugo Benichi | a4f17bc | 2016-11-21 13:50:05 +0900 | [diff] [blame] | 950 | if (VDBG) logw("network info is TYPE_WIFI but no ConnectionInfo found"); |
Paul Jensen | 306f1a4 | 2014-08-04 10:59:01 -0400 | [diff] [blame] | 951 | return; |
| 952 | } |
| 953 | break; |
| 954 | case ConnectivityManager.TYPE_MOBILE: |
| 955 | latencyBroadcast.putExtra(EXTRA_NETWORK_TYPE, mTelephonyManager.getNetworkType()); |
| 956 | List<CellInfo> info = mTelephonyManager.getAllCellInfo(); |
| 957 | if (info == null) return; |
| 958 | int numRegisteredCellInfo = 0; |
| 959 | for (CellInfo cellInfo : info) { |
| 960 | if (cellInfo.isRegistered()) { |
| 961 | numRegisteredCellInfo++; |
| 962 | if (numRegisteredCellInfo > 1) { |
Hugo Benichi | a4f17bc | 2016-11-21 13:50:05 +0900 | [diff] [blame] | 963 | if (VDBG) logw("more than one registered CellInfo." + |
| 964 | " Can't tell which is active. Bailing."); |
Paul Jensen | 306f1a4 | 2014-08-04 10:59:01 -0400 | [diff] [blame] | 965 | return; |
| 966 | } |
| 967 | if (cellInfo instanceof CellInfoCdma) { |
| 968 | CellIdentityCdma cellId = ((CellInfoCdma) cellInfo).getCellIdentity(); |
| 969 | latencyBroadcast.putExtra(EXTRA_CELL_ID, cellId); |
| 970 | } else if (cellInfo instanceof CellInfoGsm) { |
| 971 | CellIdentityGsm cellId = ((CellInfoGsm) cellInfo).getCellIdentity(); |
| 972 | latencyBroadcast.putExtra(EXTRA_CELL_ID, cellId); |
| 973 | } else if (cellInfo instanceof CellInfoLte) { |
| 974 | CellIdentityLte cellId = ((CellInfoLte) cellInfo).getCellIdentity(); |
| 975 | latencyBroadcast.putExtra(EXTRA_CELL_ID, cellId); |
| 976 | } else if (cellInfo instanceof CellInfoWcdma) { |
| 977 | CellIdentityWcdma cellId = ((CellInfoWcdma) cellInfo).getCellIdentity(); |
| 978 | latencyBroadcast.putExtra(EXTRA_CELL_ID, cellId); |
| 979 | } else { |
Hugo Benichi | a4f17bc | 2016-11-21 13:50:05 +0900 | [diff] [blame] | 980 | if (VDBG) logw("Registered cellinfo is unrecognized"); |
Paul Jensen | 306f1a4 | 2014-08-04 10:59:01 -0400 | [diff] [blame] | 981 | return; |
| 982 | } |
| 983 | } |
| 984 | } |
| 985 | break; |
| 986 | default: |
| 987 | return; |
| 988 | } |
| 989 | latencyBroadcast.putExtra(EXTRA_CONNECTIVITY_TYPE, mNetworkAgentInfo.networkInfo.getType()); |
| 990 | latencyBroadcast.putExtra(EXTRA_RESPONSE_RECEIVED, responseReceived); |
| 991 | latencyBroadcast.putExtra(EXTRA_REQUEST_TIMESTAMP_MS, requestTimestampMs); |
| 992 | |
| 993 | if (responseReceived) { |
| 994 | latencyBroadcast.putExtra(EXTRA_IS_CAPTIVE_PORTAL, isCaptivePortal); |
| 995 | latencyBroadcast.putExtra(EXTRA_RESPONSE_TIMESTAMP_MS, responseTimestampMs); |
| 996 | } |
Paul Jensen | 5529858 | 2014-08-20 11:01:41 -0400 | [diff] [blame] | 997 | mContext.sendBroadcastAsUser(latencyBroadcast, UserHandle.CURRENT, |
| 998 | PERMISSION_ACCESS_NETWORK_CONDITIONS); |
Paul Jensen | 306f1a4 | 2014-08-04 10:59:01 -0400 | [diff] [blame] | 999 | } |
Paul Jensen | d7b6ca9 | 2015-05-13 14:05:12 -0400 | [diff] [blame] | 1000 | |
Hugo Benichi | cfddd68 | 2016-05-31 16:28:06 +0900 | [diff] [blame] | 1001 | private void logNetworkEvent(int evtype) { |
| 1002 | mMetricsLog.log(new NetworkEvent(mNetId, evtype)); |
| 1003 | } |
| 1004 | |
Hugo Benichi | dd22982 | 2016-11-15 23:23:24 +0900 | [diff] [blame^] | 1005 | private int networkEventType(ValidationStage s, EvaluationResult r) { |
| 1006 | if (s.isFirstValidation) { |
| 1007 | if (r.isValidated) { |
| 1008 | return NetworkEvent.NETWORK_FIRST_VALIDATION_SUCCESS; |
| 1009 | } else { |
| 1010 | return NetworkEvent.NETWORK_FIRST_VALIDATION_PORTAL_FOUND; |
| 1011 | } |
| 1012 | } else { |
| 1013 | if (r.isValidated) { |
| 1014 | return NetworkEvent.NETWORK_REVALIDATION_SUCCESS; |
| 1015 | } else { |
| 1016 | return NetworkEvent.NETWORK_REVALIDATION_PORTAL_FOUND; |
| 1017 | } |
| 1018 | } |
| 1019 | } |
| 1020 | |
Hugo Benichi | cfddd68 | 2016-05-31 16:28:06 +0900 | [diff] [blame] | 1021 | private void maybeLogEvaluationResult(int evtype) { |
| 1022 | if (mEvaluationTimer.isRunning()) { |
| 1023 | mMetricsLog.log(new NetworkEvent(mNetId, evtype, mEvaluationTimer.stop())); |
| 1024 | mEvaluationTimer.reset(); |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | private void logValidationProbe(long durationMs, int probeType, int probeResult) { |
Hugo Benichi | dd22982 | 2016-11-15 23:23:24 +0900 | [diff] [blame^] | 1029 | probeType = |
| 1030 | ValidationProbeEvent.makeProbeType(probeType, validationStage().isFirstValidation); |
Hugo Benichi | cfddd68 | 2016-05-31 16:28:06 +0900 | [diff] [blame] | 1031 | mMetricsLog.log(new ValidationProbeEvent(mNetId, durationMs, probeType, probeResult)); |
| 1032 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 1033 | } |