diff --git a/objection/console/cli.py b/objection/console/cli.py index ff490b49..420de841 100644 --- a/objection/console/cli.py +++ b/objection/console/cli.py @@ -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) @@ -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: """ @@ -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 diff --git a/objection/state/connection.py b/objection/state/connection.py index 93929ac6..b67ebcf4 100644 --- a/objection/state/connection.py +++ b/objection/state/connection.py @@ -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. diff --git a/objection/utils/agent.py b/objection/utils/agent.py index 5cda4883..65836a39 100644 --- a/objection/utils/agent.py +++ b/objection/utils/agent.py @@ -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']) diff --git a/tests/console/test_cli.py b/tests/console/test_cli.py index 3e935b64..2a56189b 100644 --- a/tests/console/test_cli.py +++ b/tests/console/test_cli.py @@ -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) @@ -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)