Skip to content
Merged
Show file tree
Hide file tree
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
17 changes: 15 additions & 2 deletions objection/console/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ def get_agent() -> Agent:
@click.group()
@click.option('--network', '-N', is_flag=True, help='Connect using a network connection instead of USB.',
show_default=True)
@click.option('--local', '-L', is_flag=True,
help='Connect using a local connection (for iOS Simulator).', show_default=True)
@click.option('--host', '-h', default='127.0.0.1', show_default=True)
@click.option('--port', '-P', required=False, default=27042, show_default=True)
@click.option('--api-host', '-ah', default='127.0.0.1', show_default=True)
Expand All @@ -59,7 +61,7 @@ def get_agent() -> Agent:
@click.option('--foremost', '-f', required=False, is_flag=True, help='Use the current foremost application.')
@click.option('--debugger', required=False, default=False, is_flag=True, help='Enable the Chrome debug port.')
@click.option('--uid', required=False, default=None, help='Specify the uid to run as (Android only).')
def cli(network: bool, host: str, port: int, api_host: str, api_port: int,
def cli(network: bool, local: bool, host: str, port: int, api_host: str, api_port: int,
name: str, gadget: str, serial: str, debug: bool, spawn: bool, no_pause: bool,
foremost: bool, debugger: bool, uid: int) -> None:
"""
Expand All @@ -77,10 +79,21 @@ def cli(network: bool, host: str, port: int, api_host: str, api_port: int,
if debug:
app_state.debug = debug

if network:
if network and local:
raise click.UsageError('The --local flag cannot be used with --network.')

if local:
state_connection.use_local()
state_connection.host = None
state_connection.port = None
elif network:
state_connection.use_network()
state_connection.host = host
state_connection.port = port
else:
state_connection.use_usb()
state_connection.host = None
state_connection.port = None

if serial:
state_connection.device_id = serial
Expand Down
10 changes: 10 additions & 0 deletions objection/state/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ def use_network(self) -> None:
self.network = True
self.device_type = 'remote'

def use_local(self) -> None:
"""
Sets the values required to have a local connection.

:return:
"""

self.network = False
self.device_type = 'local'

def get_comms_type(self) -> int:
"""
Returns the currently configured connection type.
Expand Down
6 changes: 6 additions & 0 deletions objection/utils/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,12 @@ def update_device_state(self):
device_state.set_platform(Ios)
elif params['os']['id'] == 'android':
device_state.set_platform(Android)
else:
rt = self.exports().env_runtime()
if rt == 'ios':
device_state.set_platform(Ios)
elif rt == 'android':
device_state.set_platform(Android)

# set os version
device_state.set_version(params['os']['version'])
Expand Down
36 changes: 35 additions & 1 deletion tests/console/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@
from click.testing import CliRunner

from objection.__init__ import __version__
from objection.console.cli import version, patchipa, patchapk
from objection.console.cli import cli, version, patchipa, patchapk
from objection.state.connection import state_connection


class TestsCommandLineInteractions(unittest.TestCase):
def setUp(self):
state_connection.use_usb()
state_connection.host = None
state_connection.port = None

def test_version(self):
runner = CliRunner()
result = runner.invoke(version)
Expand Down Expand Up @@ -82,3 +88,31 @@ def test_patchipa_fails_and_wants_codesign_signature(self):

self.assertIsNotNone(result.exception)
self.assertEqual(result.exit_code, 2)

def test_cli_uses_local_connection_mode(self):
runner = CliRunner()
result = runner.invoke(cli, ['--local', 'version'])

self.assertIsNone(result.exception)
self.assertEqual(result.exit_code, 0)
self.assertEqual(state_connection.device_type, 'local')
self.assertFalse(state_connection.network)

def test_cli_uses_network_connection_mode(self):
runner = CliRunner()
result = runner.invoke(cli, ['--network', '--host', '10.0.0.5', '--port', '28000', 'version'])

self.assertIsNone(result.exception)
self.assertEqual(result.exit_code, 0)
self.assertEqual(state_connection.device_type, 'remote')
self.assertTrue(state_connection.network)
self.assertEqual(state_connection.host, '10.0.0.5')
self.assertEqual(state_connection.port, 28000)

def test_cli_rejects_local_and_network_together(self):
runner = CliRunner()
result = runner.invoke(cli, ['--local', '--network', 'version'])

self.assertIsNotNone(result.exception)
self.assertEqual(result.exit_code, 2)
self.assertIn('cannot be used with --network', result.output)
Loading