Ian Hickson | 449f4a6 | 2019-11-27 15:04:02 -0800 | [diff] [blame] | 1 | // Copyright 2014 The Flutter Authors. All rights reserved. |
Christopher Fujino | 0b24a5a | 2019-09-18 11:01:08 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Christopher Fujino | 0b24a5a | 2019-09-18 11:01:08 -0700 | [diff] [blame] | 5 | import 'package:meta/meta.dart'; |
| 6 | import 'package:multicast_dns/multicast_dns.dart'; |
| 7 | |
| 8 | import 'base/common.dart'; |
| 9 | import 'base/context.dart'; |
| 10 | import 'base/io.dart'; |
Jonah Williams | 8436c6a | 2020-11-13 13:23:03 -0800 | [diff] [blame] | 11 | import 'base/logger.dart'; |
Zachary Anderson | a72cca1 | 2019-12-16 14:57:29 -0800 | [diff] [blame] | 12 | import 'build_info.dart'; |
Christopher Fujino | 0b24a5a | 2019-09-18 11:01:08 -0700 | [diff] [blame] | 13 | import 'device.dart'; |
Zachary Anderson | a72cca1 | 2019-12-16 14:57:29 -0800 | [diff] [blame] | 14 | import 'reporting/reporting.dart'; |
Christopher Fujino | 0b24a5a | 2019-09-18 11:01:08 -0700 | [diff] [blame] | 15 | |
| 16 | /// A wrapper around [MDnsClient] to find a Dart observatory instance. |
| 17 | class MDnsObservatoryDiscovery { |
| 18 | /// Creates a new [MDnsObservatoryDiscovery] object. |
| 19 | /// |
Jonah Williams | 8436c6a | 2020-11-13 13:23:03 -0800 | [diff] [blame] | 20 | /// The [_client] parameter will be defaulted to a new [MDnsClient] if null. |
Christopher Fujino | 0b24a5a | 2019-09-18 11:01:08 -0700 | [diff] [blame] | 21 | /// The [applicationId] parameter may be null, and can be used to |
| 22 | /// automatically select which application to use if multiple are advertising |
| 23 | /// Dart observatory ports. |
Jonah Williams | 8436c6a | 2020-11-13 13:23:03 -0800 | [diff] [blame] | 24 | MDnsObservatoryDiscovery({ |
Jenn Magder | 53e04de | 2021-10-29 17:18:03 -0700 | [diff] [blame] | 25 | MDnsClient? mdnsClient, |
| 26 | required Logger logger, |
| 27 | required Usage flutterUsage, |
Jonah Williams | 8436c6a | 2020-11-13 13:23:03 -0800 | [diff] [blame] | 28 | }): _client = mdnsClient ?? MDnsClient(), |
| 29 | _logger = logger, |
| 30 | _flutterUsage = flutterUsage; |
Christopher Fujino | 0b24a5a | 2019-09-18 11:01:08 -0700 | [diff] [blame] | 31 | |
Jonah Williams | 8436c6a | 2020-11-13 13:23:03 -0800 | [diff] [blame] | 32 | final MDnsClient _client; |
| 33 | final Logger _logger; |
| 34 | final Usage _flutterUsage; |
Christopher Fujino | 0b24a5a | 2019-09-18 11:01:08 -0700 | [diff] [blame] | 35 | |
| 36 | @visibleForTesting |
| 37 | static const String dartObservatoryName = '_dartobservatory._tcp.local'; |
| 38 | |
Jenn Magder | 53e04de | 2021-10-29 17:18:03 -0700 | [diff] [blame] | 39 | static MDnsObservatoryDiscovery? get instance => context.get<MDnsObservatoryDiscovery>(); |
Christopher Fujino | 0b24a5a | 2019-09-18 11:01:08 -0700 | [diff] [blame] | 40 | |
| 41 | /// Executes an mDNS query for a Dart Observatory. |
| 42 | /// |
| 43 | /// The [applicationId] parameter may be used to specify which application |
Pierre-Louis | 0c2f7bc | 2022-09-02 06:00:58 +0200 | [diff] [blame] | 44 | /// to find. For Android, it refers to the package name; on iOS, it refers to |
Christopher Fujino | 0b24a5a | 2019-09-18 11:01:08 -0700 | [diff] [blame] | 45 | /// the bundle ID. |
| 46 | /// |
| 47 | /// If it is not null, this method will find the port and authentication code |
| 48 | /// of the Dart Observatory for that application. If it cannot find a Dart |
| 49 | /// Observatory matching that application identifier, it will call |
| 50 | /// [throwToolExit]. |
| 51 | /// |
| 52 | /// If it is null and there are multiple ports available, the user will be |
| 53 | /// prompted with a list of available observatory ports and asked to select |
| 54 | /// one. |
| 55 | /// |
| 56 | /// If it is null and there is only one available instance of Observatory, |
| 57 | /// it will return that instance's information regardless of what application |
| 58 | /// the Observatory instance is for. |
Jenn Magder | f8b1de3 | 2020-10-07 08:52:05 -0700 | [diff] [blame] | 59 | @visibleForTesting |
Jenn Magder | 53e04de | 2021-10-29 17:18:03 -0700 | [diff] [blame] | 60 | Future<MDnsObservatoryDiscoveryResult?> query({String? applicationId, int? deviceVmservicePort}) async { |
Jonah Williams | 8436c6a | 2020-11-13 13:23:03 -0800 | [diff] [blame] | 61 | _logger.printTrace('Checking for advertised Dart observatories...'); |
Christopher Fujino | 0b24a5a | 2019-09-18 11:01:08 -0700 | [diff] [blame] | 62 | try { |
Jonah Williams | 8436c6a | 2020-11-13 13:23:03 -0800 | [diff] [blame] | 63 | await _client.start(); |
| 64 | final List<PtrResourceRecord> pointerRecords = await _client |
Zachary Anderson | 0cd2ece | 2020-03-12 14:01:01 -0700 | [diff] [blame] | 65 | .lookup<PtrResourceRecord>( |
| 66 | ResourceRecordQuery.serverPointer(dartObservatoryName), |
| 67 | ) |
| 68 | .toList(); |
Christopher Fujino | 0b24a5a | 2019-09-18 11:01:08 -0700 | [diff] [blame] | 69 | if (pointerRecords.isEmpty) { |
Jonah Williams | 8436c6a | 2020-11-13 13:23:03 -0800 | [diff] [blame] | 70 | _logger.printTrace('No pointer records found.'); |
Christopher Fujino | 0b24a5a | 2019-09-18 11:01:08 -0700 | [diff] [blame] | 71 | return null; |
| 72 | } |
| 73 | // We have no guarantee that we won't get multiple hits from the same |
| 74 | // service on this. |
Jonah Williams | 69ecca5 | 2020-02-06 13:58:03 -0800 | [diff] [blame] | 75 | final Set<String> uniqueDomainNames = pointerRecords |
Zachary Anderson | 0cd2ece | 2020-03-12 14:01:01 -0700 | [diff] [blame] | 76 | .map<String>((PtrResourceRecord record) => record.domainName) |
| 77 | .toSet(); |
Christopher Fujino | 0b24a5a | 2019-09-18 11:01:08 -0700 | [diff] [blame] | 78 | |
Jenn Magder | 53e04de | 2021-10-29 17:18:03 -0700 | [diff] [blame] | 79 | String? domainName; |
Christopher Fujino | 0b24a5a | 2019-09-18 11:01:08 -0700 | [diff] [blame] | 80 | if (applicationId != null) { |
Alexandre Ardhuin | 4f9b6cf | 2020-01-07 16:32:04 +0100 | [diff] [blame] | 81 | for (final String name in uniqueDomainNames) { |
Christopher Fujino | 0b24a5a | 2019-09-18 11:01:08 -0700 | [diff] [blame] | 82 | if (name.toLowerCase().startsWith(applicationId.toLowerCase())) { |
| 83 | domainName = name; |
| 84 | break; |
| 85 | } |
| 86 | } |
| 87 | if (domainName == null) { |
| 88 | throwToolExit('Did not find a observatory port advertised for $applicationId.'); |
| 89 | } |
| 90 | } else if (uniqueDomainNames.length > 1) { |
| 91 | final StringBuffer buffer = StringBuffer(); |
| 92 | buffer.writeln('There are multiple observatory ports available.'); |
| 93 | buffer.writeln('Rerun this command with one of the following passed in as the appId:'); |
Jenn Magder | 53e04de | 2021-10-29 17:18:03 -0700 | [diff] [blame] | 94 | buffer.writeln(); |
Alexandre Ardhuin | b873162 | 2019-09-24 21:03:37 +0200 | [diff] [blame] | 95 | for (final String uniqueDomainName in uniqueDomainNames) { |
Christopher Fujino | 0b24a5a | 2019-09-18 11:01:08 -0700 | [diff] [blame] | 96 | buffer.writeln(' flutter attach --app-id ${uniqueDomainName.replaceAll('.$dartObservatoryName', '')}'); |
| 97 | } |
| 98 | throwToolExit(buffer.toString()); |
| 99 | } else { |
| 100 | domainName = pointerRecords[0].domainName; |
| 101 | } |
Jonah Williams | 8436c6a | 2020-11-13 13:23:03 -0800 | [diff] [blame] | 102 | _logger.printTrace('Checking for available port on $domainName'); |
Christopher Fujino | 0b24a5a | 2019-09-18 11:01:08 -0700 | [diff] [blame] | 103 | // Here, if we get more than one, it should just be a duplicate. |
Jonah Williams | 8436c6a | 2020-11-13 13:23:03 -0800 | [diff] [blame] | 104 | final List<SrvResourceRecord> srv = await _client |
Zachary Anderson | 0cd2ece | 2020-03-12 14:01:01 -0700 | [diff] [blame] | 105 | .lookup<SrvResourceRecord>( |
| 106 | ResourceRecordQuery.service(domainName), |
| 107 | ) |
| 108 | .toList(); |
Christopher Fujino | 0b24a5a | 2019-09-18 11:01:08 -0700 | [diff] [blame] | 109 | if (srv.isEmpty) { |
| 110 | return null; |
| 111 | } |
| 112 | if (srv.length > 1) { |
Greg Spencer | 52ae102 | 2021-11-10 16:13:04 -0800 | [diff] [blame] | 113 | _logger.printWarning('Unexpectedly found more than one observatory report for $domainName ' |
Christopher Fujino | 0b24a5a | 2019-09-18 11:01:08 -0700 | [diff] [blame] | 114 | '- using first one (${srv.first.port}).'); |
| 115 | } |
Jonah Williams | 8436c6a | 2020-11-13 13:23:03 -0800 | [diff] [blame] | 116 | _logger.printTrace('Checking for authentication code for $domainName'); |
| 117 | final List<TxtResourceRecord> txt = await _client |
Christopher Fujino | 0b24a5a | 2019-09-18 11:01:08 -0700 | [diff] [blame] | 118 | .lookup<TxtResourceRecord>( |
| 119 | ResourceRecordQuery.text(domainName), |
| 120 | ) |
Jenn Magder | 53e04de | 2021-10-29 17:18:03 -0700 | [diff] [blame] | 121 | .toList(); |
Christopher Fujino | 0b24a5a | 2019-09-18 11:01:08 -0700 | [diff] [blame] | 122 | if (txt == null || txt.isEmpty) { |
| 123 | return MDnsObservatoryDiscoveryResult(srv.first.port, ''); |
| 124 | } |
Christopher Fujino | 0b24a5a | 2019-09-18 11:01:08 -0700 | [diff] [blame] | 125 | const String authCodePrefix = 'authCode='; |
Jenn Magder | 53e04de | 2021-10-29 17:18:03 -0700 | [diff] [blame] | 126 | String? raw; |
| 127 | for (final String record in txt.first.text.split('\n')) { |
| 128 | if (record.startsWith(authCodePrefix)) { |
| 129 | raw = record; |
| 130 | break; |
| 131 | } |
| 132 | } |
Zachary Anderson | 3b66db6 | 2019-10-07 09:46:57 -0700 | [diff] [blame] | 133 | if (raw == null) { |
| 134 | return MDnsObservatoryDiscoveryResult(srv.first.port, ''); |
| 135 | } |
| 136 | String authCode = raw.substring(authCodePrefix.length); |
| 137 | // The Observatory currently expects a trailing '/' as part of the |
| 138 | // URI, otherwise an invalid authentication code response is given. |
| 139 | if (!authCode.endsWith('/')) { |
| 140 | authCode += '/'; |
Christopher Fujino | 0b24a5a | 2019-09-18 11:01:08 -0700 | [diff] [blame] | 141 | } |
| 142 | return MDnsObservatoryDiscoveryResult(srv.first.port, authCode); |
| 143 | } finally { |
Jonah Williams | 8436c6a | 2020-11-13 13:23:03 -0800 | [diff] [blame] | 144 | _client.stop(); |
Christopher Fujino | 0b24a5a | 2019-09-18 11:01:08 -0700 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
Christopher Fujino | 1371b8d | 2022-07-18 16:10:06 -0700 | [diff] [blame] | 148 | Future<Uri?> getObservatoryUri(String? applicationId, Device device, { |
Jonah Williams | e3cb2c3 | 2019-11-13 16:02:46 -0800 | [diff] [blame] | 149 | bool usesIpv6 = false, |
Jenn Magder | 53e04de | 2021-10-29 17:18:03 -0700 | [diff] [blame] | 150 | int? hostVmservicePort, |
| 151 | int? deviceVmservicePort, |
Jonah Williams | e3cb2c3 | 2019-11-13 16:02:46 -0800 | [diff] [blame] | 152 | }) async { |
Jenn Magder | 53e04de | 2021-10-29 17:18:03 -0700 | [diff] [blame] | 153 | final MDnsObservatoryDiscoveryResult? result = await query( |
Jonah Williams | e3cb2c3 | 2019-11-13 16:02:46 -0800 | [diff] [blame] | 154 | applicationId: applicationId, |
| 155 | deviceVmservicePort: deviceVmservicePort, |
| 156 | ); |
Zachary Anderson | a72cca1 | 2019-12-16 14:57:29 -0800 | [diff] [blame] | 157 | if (result == null) { |
| 158 | await _checkForIPv4LinkLocal(device); |
| 159 | return null; |
Christopher Fujino | 0b24a5a | 2019-09-18 11:01:08 -0700 | [diff] [blame] | 160 | } |
Zachary Anderson | a72cca1 | 2019-12-16 14:57:29 -0800 | [diff] [blame] | 161 | |
| 162 | final String host = usesIpv6 |
| 163 | ? InternetAddress.loopbackIPv6.address |
| 164 | : InternetAddress.loopbackIPv4.address; |
Michael Goderbauer | cb867bb | 2021-03-05 18:38:15 -0800 | [diff] [blame] | 165 | return buildObservatoryUri( |
Zachary Anderson | a72cca1 | 2019-12-16 14:57:29 -0800 | [diff] [blame] | 166 | device, |
| 167 | host, |
| 168 | result.port, |
| 169 | hostVmservicePort, |
| 170 | result.authCode, |
| 171 | ); |
| 172 | } |
| 173 | |
| 174 | // If there's not an ipv4 link local address in `NetworkInterfaces.list`, |
| 175 | // then request user interventions with a `printError()` if possible. |
| 176 | Future<void> _checkForIPv4LinkLocal(Device device) async { |
Jonah Williams | 8436c6a | 2020-11-13 13:23:03 -0800 | [diff] [blame] | 177 | _logger.printTrace( |
Zachary Anderson | a72cca1 | 2019-12-16 14:57:29 -0800 | [diff] [blame] | 178 | 'mDNS query failed. Checking for an interface with a ipv4 link local address.' |
| 179 | ); |
| 180 | final List<NetworkInterface> interfaces = await listNetworkInterfaces( |
| 181 | includeLinkLocal: true, |
| 182 | type: InternetAddressType.IPv4, |
| 183 | ); |
Jonah Williams | 8436c6a | 2020-11-13 13:23:03 -0800 | [diff] [blame] | 184 | if (_logger.isVerbose) { |
Zachary Anderson | a72cca1 | 2019-12-16 14:57:29 -0800 | [diff] [blame] | 185 | _logInterfaces(interfaces); |
| 186 | } |
| 187 | final bool hasIPv4LinkLocal = interfaces.any( |
| 188 | (NetworkInterface interface) => interface.addresses.any( |
| 189 | (InternetAddress address) => address.isLinkLocal, |
| 190 | ), |
| 191 | ); |
| 192 | if (hasIPv4LinkLocal) { |
Jonah Williams | 8436c6a | 2020-11-13 13:23:03 -0800 | [diff] [blame] | 193 | _logger.printTrace('An interface with an ipv4 link local address was found.'); |
Zachary Anderson | a72cca1 | 2019-12-16 14:57:29 -0800 | [diff] [blame] | 194 | return; |
| 195 | } |
| 196 | final TargetPlatform targetPlatform = await device.targetPlatform; |
| 197 | switch (targetPlatform) { |
| 198 | case TargetPlatform.ios: |
Jonah Williams | 8436c6a | 2020-11-13 13:23:03 -0800 | [diff] [blame] | 199 | UsageEvent('ios-mdns', 'no-ipv4-link-local', flutterUsage: _flutterUsage).send(); |
| 200 | _logger.printError( |
Zachary Anderson | a72cca1 | 2019-12-16 14:57:29 -0800 | [diff] [blame] | 201 | 'The mDNS query for an attached iOS device failed. It may ' |
Zachary Anderson | d328e0c | 2019-12-18 09:23:01 -0800 | [diff] [blame] | 202 | 'be necessary to disable the "Personal Hotspot" on the device, and ' |
| 203 | 'to ensure that the "Disable unless needed" setting is unchecked ' |
Alexandre Ardhuin | 3800bb7 | 2020-01-22 01:43:03 +0100 | [diff] [blame] | 204 | 'under System Preferences > Network > iPhone USB. ' |
Zachary Anderson | a72cca1 | 2019-12-16 14:57:29 -0800 | [diff] [blame] | 205 | 'See https://github.com/flutter/flutter/issues/46698 for details.' |
| 206 | ); |
| 207 | break; |
Ian Hickson | 7b01346 | 2021-10-11 10:23:04 -0700 | [diff] [blame] | 208 | case TargetPlatform.android: |
| 209 | case TargetPlatform.android_arm: |
| 210 | case TargetPlatform.android_arm64: |
| 211 | case TargetPlatform.android_x64: |
| 212 | case TargetPlatform.android_x86: |
| 213 | case TargetPlatform.darwin: |
| 214 | case TargetPlatform.fuchsia_arm64: |
| 215 | case TargetPlatform.fuchsia_x64: |
| 216 | case TargetPlatform.linux_arm64: |
| 217 | case TargetPlatform.linux_x64: |
| 218 | case TargetPlatform.tester: |
| 219 | case TargetPlatform.web_javascript: |
Ian Hickson | 7b01346 | 2021-10-11 10:23:04 -0700 | [diff] [blame] | 220 | case TargetPlatform.windows_x64: |
Jonah Williams | 8436c6a | 2020-11-13 13:23:03 -0800 | [diff] [blame] | 221 | _logger.printTrace('No interface with an ipv4 link local address was found.'); |
Zachary Anderson | a72cca1 | 2019-12-16 14:57:29 -0800 | [diff] [blame] | 222 | break; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | void _logInterfaces(List<NetworkInterface> interfaces) { |
Alexandre Ardhuin | 4f9b6cf | 2020-01-07 16:32:04 +0100 | [diff] [blame] | 227 | for (final NetworkInterface interface in interfaces) { |
Jonah Williams | 8436c6a | 2020-11-13 13:23:03 -0800 | [diff] [blame] | 228 | if (_logger.isVerbose) { |
| 229 | _logger.printTrace('Found interface "${interface.name}":'); |
Alexandre Ardhuin | 4f9b6cf | 2020-01-07 16:32:04 +0100 | [diff] [blame] | 230 | for (final InternetAddress address in interface.addresses) { |
Zachary Anderson | a72cca1 | 2019-12-16 14:57:29 -0800 | [diff] [blame] | 231 | final String linkLocal = address.isLinkLocal ? 'link local' : ''; |
Jonah Williams | 8436c6a | 2020-11-13 13:23:03 -0800 | [diff] [blame] | 232 | _logger.printTrace('\tBound address: "${address.address}" $linkLocal'); |
Zachary Anderson | a72cca1 | 2019-12-16 14:57:29 -0800 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | } |
Christopher Fujino | 0b24a5a | 2019-09-18 11:01:08 -0700 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | |
| 239 | class MDnsObservatoryDiscoveryResult { |
| 240 | MDnsObservatoryDiscoveryResult(this.port, this.authCode); |
| 241 | final int port; |
| 242 | final String authCode; |
| 243 | } |
| 244 | |
Alexandre Ardhuin | 890b939 | 2019-10-04 11:00:18 +0200 | [diff] [blame] | 245 | Future<Uri> buildObservatoryUri( |
| 246 | Device device, |
| 247 | String host, |
| 248 | int devicePort, [ |
Jenn Magder | 53e04de | 2021-10-29 17:18:03 -0700 | [diff] [blame] | 249 | int? hostVmservicePort, |
| 250 | String? authCode, |
Alexandre Ardhuin | 890b939 | 2019-10-04 11:00:18 +0200 | [diff] [blame] | 251 | ]) async { |
Christopher Fujino | 0b24a5a | 2019-09-18 11:01:08 -0700 | [diff] [blame] | 252 | String path = '/'; |
| 253 | if (authCode != null) { |
| 254 | path = authCode; |
| 255 | } |
| 256 | // Not having a trailing slash can cause problems in some situations. |
| 257 | // Ensure that there's one present. |
| 258 | if (!path.endsWith('/')) { |
| 259 | path += '/'; |
| 260 | } |
Ben Konyi | a17b330 | 2020-09-16 16:27:42 -0700 | [diff] [blame] | 261 | hostVmservicePort ??= 0; |
Jenn Magder | 53e04de | 2021-10-29 17:18:03 -0700 | [diff] [blame] | 262 | final int? actualHostPort = hostVmservicePort == 0 ? |
| 263 | await device.portForwarder?.forward(devicePort) : |
Ben Konyi | a17b330 | 2020-09-16 16:27:42 -0700 | [diff] [blame] | 264 | hostVmservicePort; |
Jonah Williams | e3cb2c3 | 2019-11-13 16:02:46 -0800 | [diff] [blame] | 265 | return Uri(scheme: 'http', host: host, port: actualHostPort, path: path); |
Christopher Fujino | 0b24a5a | 2019-09-18 11:01:08 -0700 | [diff] [blame] | 266 | } |