Skip to content

Commit

Permalink
Add --android_tunnel flag to emrun (cross-origin isolation on Android…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
lippinj authored Nov 30, 2023
1 parent 7d639f7 commit 65cd1d8
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion emrun.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.')

Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -1671,6 +1687,9 @@ def run():
# 4. Type 'aapt d xmltree <packagename>.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('/')]
Expand Down

0 comments on commit 65cd1d8

Please sign in to comment.