iproxy.dart 1.87 KB
Newer Older
1 2 3 4
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'package:process/process.dart';
6 7 8 9 10

import '../base/io.dart';
import '../base/logger.dart';
import '../base/process.dart';

11 12 13 14 15 16
enum IOSDeviceConnectionInterface {
  none,
  usb,
  network,
}

17 18 19 20 21
/// Wraps iproxy command line tool port forwarding.
///
/// See https://github.com/libimobiledevice/libusbmuxd.
class IProxy {
  IProxy({
22 23 24 25
    required String iproxyPath,
    required Logger logger,
    required ProcessManager processManager,
    required MapEntry<String, String> dyLdLibEntry,
26 27
  }) : _dyLdLibEntry = dyLdLibEntry,
        _processUtils = ProcessUtils(processManager: processManager, logger: logger),
28
        _logger = logger,
29 30 31 32 33 34 35
        _iproxyPath = iproxyPath;

  /// Create a [IProxy] for testing.
  ///
  /// This specifies the path to iproxy as 'iproxy` and the dyLdLibEntry as
  /// 'DYLD_LIBRARY_PATH: /path/to/libs'.
  factory IProxy.test({
36 37
    required Logger logger,
    required ProcessManager processManager,
38 39 40 41 42 43 44 45 46 47 48 49 50
  }) {
    return IProxy(
      iproxyPath: 'iproxy',
      logger: logger,
      processManager: processManager,
      dyLdLibEntry: const MapEntry<String, String>(
        'DYLD_LIBRARY_PATH', '/path/to/libs',
      ),
    );
  }

  final String _iproxyPath;
  final ProcessUtils _processUtils;
51
  final Logger _logger;
52 53 54 55 56 57 58 59 60 61
  final MapEntry<String, String> _dyLdLibEntry;

  Future<Process> forward(int devicePort, int hostPort, String deviceId) {
    // Usage: iproxy LOCAL_PORT:DEVICE_PORT --udid UDID
    return _processUtils.start(
      <String>[
        _iproxyPath,
        '$hostPort:$devicePort',
        '--udid',
        deviceId,
62 63
        if (_logger.isVerbose)
          '--debug',
64 65 66 67 68 69 70
      ],
      environment: Map<String, String>.fromEntries(
        <MapEntry<String, String>>[_dyLdLibEntry],
      ),
    );
  }
}