[Android] Add a command line tool for reverse port forwarding (issue 11828051)

Showing 1-6 of 6 messages
[Android] Add a command line tool for reverse port forwarding (issue 11828051) skyo...@chromium.org 1/10/13 11:30 AM
Reviewers: bulach, Philippe,

Message:
Hi guys,

While working with browsers that don't have telemetry support yet I found it
useful to be able to run the forwarder from the command line. Have a look  
if you
agree :)

Description:
[Android] Add a command line tool for reverse port forwarding

This patch adds a command line interface to
build/android/pylib/forwarder.py. It allows an Android device to access
services running on the host machine or elsewhere. This is essentially
the reverse of "adb forward".


Please review this at https://codereview.chromium.org/11828051/

SVN Base: svn://svn.chromium.org/chrome/trunk/src

Affected files:
   A build/android/adb_reverse_forwarder.py


Index: build/android/adb_reverse_forwarder.py
diff --git a/build/android/adb_reverse_forwarder.py  
b/build/android/adb_reverse_forwarder.py
new file mode 100755
index  
0000000000000000000000000000000000000000..38eacc6bff76e45650f3350b4cd37a311cf71ba1
--- /dev/null
+++ b/build/android/adb_reverse_forwarder.py
@@ -0,0 +1,67 @@
+#!/usr/bin/env python
+#
+# Copyright (c) 2013 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.
+
+"""Command line tool for forwarding ports from a device to the host.
+
+Allows an Android device to connect to services running on the host  
machine,
+i.e., "adb forward" in reverse.
+"""
+
+import optparse
+import sys
+import time
+
+from pylib import android_commands, forwarder
+from pylib.utils import run_tests_helper
+
+
+def main(argv):
+  parser = optparse.OptionParser(usage='Usage: %prog [options]  
device_port '
+                                 'host_port [device_port_2  
host_port_2] ...',
+                                 description=__doc__)
+  parser.add_option('-v',
+                    '--verbose',
+                    dest='verbose_count',
+                    default=0,
+                    action='count',
+                    help='Verbose level (multiple times for more)')
+  parser.add_option('--device',
+                    help='Serial number of device we should use.')
+  parser.add_option('--host',
+                    help='Host address to forward to from the host  
machine. '
+                    '127.0.0.1 by default', default='127.0.0.1')
+  parser.add_option('--debug', action='store_const', const='Debug',
+                    dest='build_type', default='Release',
+                    help='Use Debug build of host tools instead of  
Release.')
+
+  options, args = parser.parse_args(argv)
+  run_tests_helper.SetLogLevel(options.verbose_count)
+
+  if len(args) < 2 or not len(args) % 2:
+    parser.error('Need even number of port pairs')
+    sys.exit(1)
+
+  try:
+    port_pairs = map(int, args[1:])
+    port_pairs = zip(port_pairs[::2], port_pairs[1::2])
+  except ValueError:
+    parser.error('Bad port number')
+    sys.exit(1)
+
+  adb = android_commands.AndroidCommands(options.device)
+  forwarder_instance = forwarder.Forwarder(adb, options.build_type)
+  try:
+    forwarder_instance.Run(port_pairs, None, options.host)
+    while True:
+      time.sleep(60)
+  except KeyboardInterrupt:
+    sys.exit(0)
+  finally:
+    forwarder_instance.Close()
+
+
+if __name__ == '__main__':
+  main(sys.argv)


Re: [Android] Add a command line tool for reverse port forwarding (issue 11828051) bul...@chromium.org 1/10/13 11:43 AM
lgtm, this is very handy!!

make sure phillippe is happy with it, and a tiny nit below:


https://codereview.chromium.org/11828051/diff/1/build/android/adb_reverse_forwarder.py
File build/android/adb_reverse_forwarder.py (right):

https://codereview.chromium.org/11828051/diff/1/build/android/adb_reverse_forwarder.py#newcode10
build/android/adb_reverse_forwarder.py:10
: i.e., "adb forward" in
reverse.
Requires |host_forwarder| and |device_forwarder| to be built.

https://codereview.chromium.org/11828051/
Re: [Android] Add a command line tool for reverse port forwarding (issue 11828051) skyo...@chromium.org 1/10/13 11:48 AM
On 2013/01/10 19:43:23, bulach wrote:
> Requires |host_forwarder| and |device_forwarder| to be built.

Good point, added a note about that.

https://codereview.chromium.org/11828051/
Re: [Android] Add a command line tool for reverse port forwarding (issue 11828051) pli...@chromium.org 1/11/13 5:50 AM
LGTM, thanks Sami! Very useful indeed.


https://codereview.chromium.org/11828051/diff/4001/build/android/adb_reverse_forwarder.py
File build/android/adb_reverse_forwarder.py (right):

https://codereview.chromium.org/11828051/diff/4001/build/android/adb_reverse_forwarder.py#newcode64
build/android/adb_reverse_forwarder.py:64
: forwarder_instance.Close()
Forwarder::Close() should have been removed. It's a no-op right now. I
will send you a CL updating this code once I did the cleanup (no need to
change anything here now).

https://codereview.chromium.org/11828051/
Re: [Android] Add a command line tool for reverse port forwarding (issue 11828051) commi...@chromium.org 1/11/13 6:08 AM
Re: [Android] Add a command line tool for reverse port forwarding (issue 11828051) commi...@chromium.org 1/11/13 8:37 AM