iproxy.dart 1.75 KB
Newer Older
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
// 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.

import 'package:meta/meta.dart';
import 'package:process/process.dart';

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

/// Wraps iproxy command line tool port forwarding.
///
/// See https://github.com/libimobiledevice/libusbmuxd.
class IProxy {
  IProxy({
    @required String iproxyPath,
    @required Logger logger,
    @required ProcessManager processManager,
    @required MapEntry<String, String> dyLdLibEntry,
  }) : _dyLdLibEntry = dyLdLibEntry,
        _processUtils = ProcessUtils(processManager: processManager, logger: logger),
        _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({
    @required Logger logger,
    @required ProcessManager processManager,
  }) {
    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;
  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,
      ],
      environment: Map<String, String>.fromEntries(
        <MapEntry<String, String>>[_dyLdLibEntry],
      ),
    );
  }
}