From 65cd1d880691731ccbc56f10c8b074949d7fdc44 Mon Sep 17 00:00:00 2001 From: lippinj <4030660+lippinj@users.noreply.github.com> Date: Thu, 30 Nov 2023 21:37:56 +0200 Subject: [PATCH] Add --android_tunnel flag to emrun (cross-origin isolation on Android device) (#20783) This flag does two things: 1. we use 'localhost' as the hostname 2. we invoke 'adb reverse tcp:{port} tcp:{port}' before launching This means that the page will run with cross-origin isolation. Without cross-origin isolation, we typically can't have features such as SharedArrayBuffer, which is needed for apps using pthreads. --- emrun.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/emrun.py b/emrun.py index ead752e9dfbd..51652887329e 100644 --- a/emrun.py +++ b/emrun.py @@ -1533,6 +1533,14 @@ def parse_args(): help='Launches the page in a browser of an Android ' 'device connected to an USB on the local system. (via adb)') + parser.add_argument('--android_tunnel', action='store_true', + help='Expose the port directly to the Android device ' + 'and connect to it as localhost, establishing ' + 'cross origin isolation. Implies --android. A ' + 'reverse socket connection is created by adb ' + 'reverse, and remains after emrun terminates (it ' + 'can be removed by adb reverse --remove).') + parser.add_argument('--system_info', action='store_true', help='Prints information about the current system at startup.') @@ -1567,6 +1575,9 @@ def run(): options = emrun_options = parse_args() + if options.android_tunnel: + options.android = True + if options.android: global ADB ADB = which('adb') @@ -1636,7 +1647,12 @@ def run(): if not file_to_serve_is_url: if len(options.cmdlineparams): url += '?' + '&'.join(options.cmdlineparams) - hostname = socket.gethostbyname(socket.gethostname()) if options.android else options.hostname + if options.android_tunnel: + hostname = 'localhost' + elif options.android: + hostname = socket.gethostbyname(socket.gethostname()) + else: + hostname = options.hostname # create url for browser after opening the server so we have the final port number in case we are binding to port 0 url = 'http://' + hostname + ':' + str(options.port) + '/' + url @@ -1671,6 +1687,9 @@ def run(): # 4. Type 'aapt d xmltree .apk AndroidManifest.xml > manifest.txt' to extract the manifest from the package. # 5. Locate the name of the main activity for the browser in manifest.txt and add an entry to above list in form 'appname/mainactivityname' + if options.android_tunnel: + subprocess.check_call([ADB, 'reverse', 'tcp:' + str(options.port), 'tcp:' + str(options.port)]) + url = url.replace('&', '\\&') browser = [ADB, 'shell', 'am', 'start', '-a', 'android.intent.action.VIEW', '-n', browser_app, '-d', url] processname_killed_atexit = browser_app[:browser_app.find('/')]