1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'package:meta/meta.dart';
import '../application_package.dart';
import '../base/common.dart';
import '../base/io.dart';
import '../base/platform.dart';
import '../base/process.dart';
import '../base/process_manager.dart';
import '../build_info.dart';
import '../device.dart';
import '../globals.dart';
import '../vmservice.dart';
import 'fuchsia_sdk.dart';
import 'fuchsia_workflow.dart';
final String _ipv4Loopback = InternetAddress.loopbackIPv4.address;
final String _ipv6Loopback = InternetAddress.loopbackIPv6.address;
/// Read the log for a particular device.
class _FuchsiaLogReader extends DeviceLogReader {
_FuchsiaLogReader(this._device);
// TODO(jonahwilliams): handle filtering log output from different modules.
static final Pattern flutterLogOutput = RegExp(r'\[\d+\.\d+\]\[\d+\]\[\d+\]\[klog\] INFO: \w+\(flutter\): ');
FuchsiaDevice _device;
@override String get name => _device.name;
Stream<String> _logLines;
@override
Stream<String> get logLines {
_logLines ??= fuchsiaSdk.syslogs()
.where((String line) => flutterLogOutput.matchAsPrefix(line) != null);
return _logLines;
}
@override
String toString() => name;
}
class FuchsiaDevices extends PollingDeviceDiscovery {
FuchsiaDevices() : super('Fuchsia devices');
@override
bool get supportsPlatform => platform.isLinux || platform.isMacOS;
@override
bool get canListAnything => fuchsiaWorkflow.canListDevices;
@override
Future<List<Device>> pollingGetDevices() async {
if (!fuchsiaWorkflow.canListDevices) {
return <Device>[];
}
final String text = await fuchsiaSdk.netls();
final List<FuchsiaDevice> devices = <FuchsiaDevice>[];
for (String name in parseFuchsiaDeviceOutput(text)) {
final String id = await fuchsiaSdk.netaddr();
devices.add(FuchsiaDevice(id, name: name));
}
return devices;
}
@override
Future<List<String>> getDiagnostics() async => const <String>[];
}
/// Parses output from the netls tool into fuchsia devices names.
///
/// Example output:
/// $ ./netls
/// > device liliac-shore-only-last (fe80::82e4:da4d:fe81:227d/3)
@visibleForTesting
List<String> parseFuchsiaDeviceOutput(String text) {
final List<String> names = <String>[];
for (String rawLine in text.trim().split('\n')) {
final String line = rawLine.trim();
if (!line.startsWith('device'))
continue;
// ['device', 'device name', '(id)']
final List<String> words = line.split(' ');
final String name = words[1];
names.add(name);
}
return names;
}
class FuchsiaDevice extends Device {
FuchsiaDevice(String id, { this.name }) : super(id);
@override
bool get supportsHotMode => true;
@override
final String name;
@override
Future<bool> get isLocalEmulator async => false;
@override
bool get supportsStartPaused => false;
@override
Future<bool> isAppInstalled(ApplicationPackage app) async => false;
@override
Future<bool> isLatestBuildInstalled(ApplicationPackage app) async => false;
@override
Future<bool> installApp(ApplicationPackage app) => Future<bool>.value(false);
@override
Future<bool> uninstallApp(ApplicationPackage app) async => false;
@override
bool isSupported() => true;
@override
Future<LaunchResult> startApp(
ApplicationPackage package, {
String mainPath,
String route,
DebuggingOptions debuggingOptions,
Map<String, dynamic> platformArgs,
bool prebuiltApplication = false,
bool applicationNeedsRebuild = false,
bool usesTerminalUi = false,
bool ipv6 = false,
}) => Future<void>.error('unimplemented');
@override
Future<bool> stopApp(ApplicationPackage app) async {
// Currently we don't have a way to stop an app running on Fuchsia.
return false;
}
@override
Future<TargetPlatform> get targetPlatform async => TargetPlatform.fuchsia;
@override
Future<String> get sdkNameAndVersion async => 'Fuchsia';
@override
DeviceLogReader getLogReader({ApplicationPackage app}) => _logReader ??= _FuchsiaLogReader(this);
_FuchsiaLogReader _logReader;
@override
DevicePortForwarder get portForwarder => _portForwarder ??= _FuchsiaPortForwarder(this);
_FuchsiaPortForwarder _portForwarder;
@override
void clearLogs() {
}
@override
bool get supportsScreenshot => false;
/// List the ports currently running a dart observatory.
Future<List<int>> servicePorts() async {
final String lsOutput = await shell('ls /tmp/dart.services');
return parseFuchsiaDartPortOutput(lsOutput);
}
/// Run `command` on the Fuchsia device shell.
Future<String> shell(String command) async {
final RunResult result = await runAsync(<String>[
'ssh', '-F', fuchsiaArtifacts.sshConfig.absolute.path, id, command]);
if (result.exitCode != 0) {
throwToolExit('Command failed: $command\nstdout: ${result.stdout}\nstderr: ${result.stderr}');
return null;
}
return result.stdout;
}
/// Finds the first port running a VM matching `isolateName` from the
/// provided set of `ports`.
///
/// Returns null if no isolate port can be found.
///
// TODO(jonahwilliams): replacing this with the hub will require an update
// to the flutter_runner.
Future<int> findIsolatePort(String isolateName, List<int> ports) async {
for (int port in ports) {
try {
// Note: The square-bracket enclosure for using the IPv6 loopback
// didn't appear to work, but when assigning to the IPv4 loopback device,
// netstat shows that the local port is actually being used on the IPv6
// loopback (::1).
final Uri uri = Uri.parse('http://[$_ipv6Loopback]:$port');
final VMService vmService = await VMService.connect(uri);
await vmService.getVM();
await vmService.refreshViews();
for (FlutterView flutterView in vmService.vm.views) {
if (flutterView.uiIsolate == null) {
continue;
}
final Uri address = flutterView.owner.vmService.httpAddress;
if (flutterView.uiIsolate.name.contains(isolateName)) {
return address.port;
}
}
} on SocketException catch (err) {
printTrace('Failed to connect to $port: $err');
}
}
throwToolExit('No ports found running $isolateName');
return null;
}
}
class _FuchsiaPortForwarder extends DevicePortForwarder {
_FuchsiaPortForwarder(this.device);
final FuchsiaDevice device;
final Map<int, Process> _processes = <int, Process>{};
@override
Future<int> forward(int devicePort, {int hostPort}) async {
hostPort ??= await _findPort();
// Note: the provided command works around a bug in -N, see US-515
// for more explanation.
final List<String> command = <String>[
'ssh', '-6', '-F', fuchsiaArtifacts.sshConfig.absolute.path, '-nNT', '-vvv', '-f',
'-L', '$hostPort:$_ipv4Loopback:$devicePort', device.id, 'true'
];
final Process process = await processManager.start(command);
process.exitCode.then((int exitCode) { // ignore: unawaited_futures
if (exitCode != 0) {
throwToolExit('Failed to forward port:$devicePort');
}
});
_processes[hostPort] = process;
_forwardedPorts.add(ForwardedPort(hostPort, devicePort));
return hostPort;
}
@override
List<ForwardedPort> get forwardedPorts => _forwardedPorts;
final List<ForwardedPort> _forwardedPorts = <ForwardedPort>[];
@override
Future<void> unforward(ForwardedPort forwardedPort) async {
_forwardedPorts.remove(forwardedPort);
final Process process = _processes.remove(forwardedPort.hostPort);
process?.kill();
final List<String> command = <String>[
'ssh', '-F', fuchsiaArtifacts.sshConfig.absolute.path, '-O', 'cancel', '-vvv',
'-L', '${forwardedPort.hostPort}:$_ipv4Loopback:${forwardedPort.devicePort}', device.id];
final ProcessResult result = await processManager.run(command);
if (result.exitCode != 0) {
throwToolExit(result.stderr);
}
}
static Future<int> _findPort() async {
int port = 0;
ServerSocket serverSocket;
try {
serverSocket = await ServerSocket.bind(_ipv4Loopback, 0);
port = serverSocket.port;
} catch (e) {
// Failures are signaled by a return value of 0 from this function.
printTrace('_findPort failed: $e');
}
if (serverSocket != null)
await serverSocket.close();
return port;
}
}
/// Parses output from `dart.services` output on a fuchsia device.
///
/// Example output:
/// $ ls /tmp/dart.services
/// > d 2 0 .
/// > - 1 0 36780
@visibleForTesting
List<int> parseFuchsiaDartPortOutput(String text) {
final List<int> ports = <int>[];
if (text == null)
return ports;
for (String line in text.split('\n')) {
final String trimmed = line.trim();
final int lastSpace = trimmed.lastIndexOf(' ');
final String lastWord = trimmed.substring(lastSpace + 1);
if ((lastWord != '.') && (lastWord != '..')) {
final int value = int.tryParse(lastWord);
if (value != null) {
ports.add(value);
}
}
}
return ports;
}