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