Skip to content

Commit

Permalink
fix forward_list
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed May 8, 2024
1 parent 1eea76f commit 3106ba8
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 28 deletions.
18 changes: 9 additions & 9 deletions adbutils/_adb.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import subprocess
import typing
import weakref
from typing import Iterator, Union
from typing import Iterator, List, Union

from deprecation import deprecated

Expand Down Expand Up @@ -324,25 +324,23 @@ def _diff_devices(self, orig: typing.List[DeviceEvent], curr: typing.List[Device
for d in set(curr).difference(orig):
yield DeviceEvent(True, d.serial, d.status)

@deprecated(deprecated_in="0.15.0",
removed_in="1.0.0",
details="use device.forward_list instead",
current_version=__version__)
def forward_list(self, serial: Union[None, str] = None):
def forward_list(self, serial: Union[None, str] = None) -> List[ForwardItem]:
with self.make_connection() as c:
list_cmd = "host:list-forward"
if serial:
list_cmd = "host-serial:{}:list-forward".format(serial)
c.send_command(list_cmd)
c.check_okay()
content = c.read_string_block()
items = []
for line in content.splitlines():
parts = line.split()
if len(parts) != 3:
continue
if serial and parts[0] != serial:
continue
yield ForwardItem(*parts)
items.append(ForwardItem(*parts))
return items

@deprecated(deprecated_in="0.15.0",
removed_in="1.0.0",
Expand Down Expand Up @@ -391,18 +389,20 @@ def reverse(self, serial, remote, local, norebind=False):
removed_in="1.0.0",
details="use Device.reverse_list instead",
current_version=__version__)
def reverse_list(self, serial: Union[None, str] = None):
def reverse_list(self, serial: str) -> List[ReverseItem]:
with self.make_connection() as c:
c.send_command("host:transport:" + serial)
c.check_okay()
c.send_command("reverse:list-forward")
c.check_okay()
content = c.read_string_block()
items = []
for line in content.splitlines():
parts = line.split()
if len(parts) != 3:
continue
yield ReverseItem(*parts[1:])
items.append(ReverseItem(*parts[1:]))
return items



22 changes: 8 additions & 14 deletions adbutils/_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from typing import List, Optional, Union

from PIL import Image, UnidentifiedImageError
from deprecation import deprecated

from adbutils._deprecated import DeprecatedExtension
from adbutils.install import InstallExtension
Expand Down Expand Up @@ -66,9 +67,7 @@ def open_transport(
self, command: str = None, timeout: float = _DEFAULT_SOCKET_TIMEOUT
) -> AdbConnection:
# connect has it own timeout
c = self._client.make_connection()
if timeout:
c.conn.settimeout(timeout)
c = self._client.make_connection(timeout=timeout)

if command:
if self._transport_id:
Expand Down Expand Up @@ -264,15 +263,8 @@ def forward_port(self, remote: Union[int, str]) -> int:
return local_port

def forward_list(self) -> List[ForwardItem]:
c = self.open_transport("list-forward")
content = c.read_string_block()
items = []
for line in content.splitlines():
parts = line.split()
if len(parts) != 3:
continue
items.append(ForwardItem(*parts))
return items
items = self._client.forward_list()
return [item for item in items if item.serial == self._serial]

def reverse(self, remote: str, local: str, norebind: bool = False):
"""
Expand Down Expand Up @@ -357,8 +349,10 @@ def framebuffer(self) -> Image.Image:
image = Image.frombytes(color_format, (width, height), buffer)
return image

def push(self, local: str, remote: str) -> str:
return self.adb_output("push", local, remote)
@deprecated(deprecated_in="2.6.0", removed_in="3.0.0", current_version=__version__, details="use sync.push instead")
def push(self, local: str, remote: str):
""" alias for sync.push """
self.sync.push(local, remote)

def create_connection(
self, network: Network, address: Union[int, str]
Expand Down
46 changes: 42 additions & 4 deletions docs/PROTOCOL.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
# ADB Protocol
Tested with adb 1.0.41, Capture data with Charles

Charles settings

Proxy->Port Forwarding

```
TCP Local(5555) -> Remote(localhost:5037)
```

execute adb command, e.g. `adb -P 5555 devices`, now data should be seen in charles.

## host version
```
>send
Expand All @@ -18,7 +31,33 @@
00000020 30 30 31 34 27 20 6e 6f 74 20 66 6f 75 6e 64 0014' not found
```

## reverse --list
## forward
```
$ adb forward --list
>send
0011host:list-forward
<recv
OKAY0020emulator-5554 tcp:5678 tcp:8765
$ adb forward --remove-all
>send
000ehost:tport:any0014host:killforward-all
<recv
00000000 4f 4b 41 59 08 00 00 00 00 00 00 00 4f 4b 41 59 OKAY OKAY
00000010 4f 4b 41 59 OKAY
$ adb -s emulator-5554 forward tcp:1234 tcp:4321
>send
001fhost:tport:serial:emulator-5554001ehost:forward:tcp:1234;tcp:4321
<recv
00000000 4f 4b 41 59 08 00 00 00 00 00 00 00 4f 4b 41 59 OKAY OKAY
00000010 4f 4b 41 59 30 30 30 34 31 32 33 34 OKAY00041234
```

## reverse
adb help
```
reverse --list list all reverse socket connections from device
Expand All @@ -33,14 +72,13 @@ reverse --list list all reverse socket connections from device
```

```
$ adb reverse --list
>send
0022host:tport:serial:GBG5T197110027690014
<recv
00000000 4f 4b 41 59 06 00 00 00 00 00 00 00 OKAY
```
```
>send
reverse:list-forward
Expand All @@ -52,8 +90,8 @@ reverse:list-forward
00000040 70 3a 35 31 32 33 20 74 63 70 3a 37 38 39 30 0a p:5123 tcp:7890
```

## adb -s GBG5T197110027690014 reverse localabstract:test tcp:12345
```
$ adb -s GBG5T197110027690014 reverse localabstract:test tcp:12345
>send
001fhost:tport:serial:emulator-5554002creverse:forward:localabstract:test;tcp:12345
Expand Down
7 changes: 6 additions & 1 deletion tests/adb_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,16 @@ async def host_kill(ctx: Context):
await ctx.server.stop()
# os.kill(os.getpid(), signal.SIGINT)

@register_command("host:list-forward")
async def host_list_forward(ctx: Context):
await ctx.send(b"OKAY")
await ctx.send(encode_string("123456 tcp:1234 tcp:4321"))


SHELL_OUTPUTS = {
"pwd": "/",
}


@register_command(re.compile("host:tport:serial:.*"))
async def host_tport_serial(ctx: Context):
serial = ctx.command.split(":")[-1]
Expand Down
15 changes: 15 additions & 0 deletions tests/test_forward.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""Created on Wed May 08 2024 21:45:15 by codeskyblue
"""

import adbutils


def test_forward_list(adb: adbutils.AdbClient):
items = adb.forward_list()
assert len(items) == 1
assert items[0].serial == "123456"
assert items[0].local == "tcp:1234"
assert items[0].remote == "tcp:4321"

0 comments on commit 3106ba8

Please sign in to comment.