Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --android_tunnel flag to emrun (cross-origin isolation on Android device) #20783

Merged
merged 4 commits into from
Nov 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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