Peter Wen | b51e454 | 2021-06-30 17:42:57 | [diff] [blame] | 1 | #!/usr/bin/env vpython3 |
[email protected] | 47bd760 | 2013-01-11 16:37:17 | [diff] [blame] | 2 | # |
Avi Drissman | 73a09d1 | 2022-09-08 20:33:38 | [diff] [blame] | 3 | # Copyright 2013 The Chromium Authors |
[email protected] | 47bd760 | 2013-01-11 16:37:17 | [diff] [blame] | 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | """Command line tool for forwarding ports from a device to the host. |
| 8 | |
| 9 | Allows an Android device to connect to services running on the host machine, |
| 10 | i.e., "adb forward" in reverse. Requires |host_forwarder| and |device_forwarder| |
| 11 | to be built. |
| 12 | """ |
| 13 | |
jbudorick | d9bce390 | 2016-09-23 02:39:22 | [diff] [blame] | 14 | import argparse |
[email protected] | 47bd760 | 2013-01-11 16:37:17 | [diff] [blame] | 15 | import sys |
| 16 | import time |
| 17 | |
jbudorick | d28554a | 2016-01-11 16:22:59 | [diff] [blame] | 18 | import devil_chromium |
| 19 | |
John Budorick | 8b57e60 | 2020-09-01 16:57:50 | [diff] [blame] | 20 | from devil.android import device_denylist |
jbudorick | 06162944 | 2015-09-03 18:00:57 | [diff] [blame] | 21 | from devil.android import device_utils |
jbudorick | d645efe8 | 2015-12-11 19:09:59 | [diff] [blame] | 22 | from devil.android import forwarder |
jbudorick | 06162944 | 2015-09-03 18:00:57 | [diff] [blame] | 23 | from devil.utils import run_tests_helper |
jbudorick | d28554a | 2016-01-11 16:22:59 | [diff] [blame] | 24 | |
jbudorick | bfffb22e | 2015-04-16 14:10:02 | [diff] [blame] | 25 | from pylib import constants |
[email protected] | 47bd760 | 2013-01-11 16:37:17 | [diff] [blame] | 26 | |
| 27 | |
| 28 | def main(argv): |
jbudorick | d9bce390 | 2016-09-23 02:39:22 | [diff] [blame] | 29 | parser = argparse.ArgumentParser( |
| 30 | usage='Usage: %(prog)s [options] device_port ' |
| 31 | 'host_port [device_port_2 host_port_2] ...', |
| 32 | description=__doc__) |
| 33 | parser.add_argument( |
| 34 | '-v', '--verbose', |
| 35 | dest='verbose_count', |
| 36 | default=0, |
| 37 | action='count', |
| 38 | help='Verbose level (multiple times for more)') |
| 39 | parser.add_argument( |
| 40 | '--device', |
| 41 | help='Serial number of device we should use.') |
John Budorick | 8b57e60 | 2020-09-01 16:57:50 | [diff] [blame] | 42 | parser.add_argument('--denylist-file', help='Device denylist JSON file.') |
jbudorick | d9bce390 | 2016-09-23 02:39:22 | [diff] [blame] | 43 | parser.add_argument( |
| 44 | '--debug', |
| 45 | action='store_const', |
| 46 | const='Debug', |
| 47 | dest='build_type', |
| 48 | default='Release', |
| 49 | help='DEPRECATED: use --output-directory instead.') |
| 50 | parser.add_argument( |
| 51 | '--output-directory', |
| 52 | help='Path to the root build directory.') |
| 53 | parser.add_argument( |
| 54 | 'ports', |
| 55 | nargs='+', |
| 56 | type=int, |
| 57 | help='Port pair to reverse forward.') |
[email protected] | 47bd760 | 2013-01-11 16:37:17 | [diff] [blame] | 58 | |
jbudorick | d9bce390 | 2016-09-23 02:39:22 | [diff] [blame] | 59 | args = parser.parse_args(argv) |
| 60 | run_tests_helper.SetLogLevel(args.verbose_count) |
[email protected] | 47bd760 | 2013-01-11 16:37:17 | [diff] [blame] | 61 | |
jbudorick | d9bce390 | 2016-09-23 02:39:22 | [diff] [blame] | 62 | if len(args.ports) < 2 or len(args.ports) % 2: |
[email protected] | 47bd760 | 2013-01-11 16:37:17 | [diff] [blame] | 63 | parser.error('Need even number of port pairs') |
[email protected] | 47bd760 | 2013-01-11 16:37:17 | [diff] [blame] | 64 | |
Peter Wen | b51e454 | 2021-06-30 17:42:57 | [diff] [blame] | 65 | port_pairs = list(zip(args.ports[::2], args.ports[1::2])) |
[email protected] | 47bd760 | 2013-01-11 16:37:17 | [diff] [blame] | 66 | |
jbudorick | d9bce390 | 2016-09-23 02:39:22 | [diff] [blame] | 67 | if args.build_type: |
| 68 | constants.SetBuildType(args.build_type) |
| 69 | if args.output_directory: |
| 70 | constants.SetOutputDirectory(args.output_directory) |
| 71 | devil_chromium.Initialize(output_directory=constants.GetOutDirectory()) |
| 72 | |
John Budorick | 8b57e60 | 2020-09-01 16:57:50 | [diff] [blame] | 73 | denylist = (device_denylist.Denylist(args.denylist_file) |
| 74 | if args.denylist_file else None) |
| 75 | device = device_utils.DeviceUtils.HealthyDevices(denylist=denylist, |
| 76 | device_arg=args.device)[0] |
[email protected] | 47bd760 | 2013-01-11 16:37:17 | [diff] [blame] | 77 | try: |
[email protected] | 044d79b | 2014-04-10 19:37:30 | [diff] [blame] | 78 | forwarder.Forwarder.Map(port_pairs, device) |
[email protected] | 47bd760 | 2013-01-11 16:37:17 | [diff] [blame] | 79 | while True: |
| 80 | time.sleep(60) |
| 81 | except KeyboardInterrupt: |
| 82 | sys.exit(0) |
| 83 | finally: |
[email protected] | 044d79b | 2014-04-10 19:37:30 | [diff] [blame] | 84 | forwarder.Forwarder.UnmapAllDevicePorts(device) |
[email protected] | 47bd760 | 2013-01-11 16:37:17 | [diff] [blame] | 85 | |
| 86 | if __name__ == '__main__': |
jbudorick | d9bce390 | 2016-09-23 02:39:22 | [diff] [blame] | 87 | sys.exit(main(sys.argv[1:])) |