Skip to content

Commit 3106ba8

Browse files
committed
fix forward_list
1 parent 1eea76f commit 3106ba8

5 files changed

Lines changed: 80 additions & 28 deletions

File tree

adbutils/_adb.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import subprocess
1212
import typing
1313
import weakref
14-
from typing import Iterator, Union
14+
from typing import Iterator, List, Union
1515

1616
from deprecation import deprecated
1717

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

327-
@deprecated(deprecated_in="0.15.0",
328-
removed_in="1.0.0",
329-
details="use device.forward_list instead",
330-
current_version=__version__)
331-
def forward_list(self, serial: Union[None, str] = None):
327+
def forward_list(self, serial: Union[None, str] = None) -> List[ForwardItem]:
332328
with self.make_connection() as c:
333329
list_cmd = "host:list-forward"
334330
if serial:
335331
list_cmd = "host-serial:{}:list-forward".format(serial)
336332
c.send_command(list_cmd)
337333
c.check_okay()
338334
content = c.read_string_block()
335+
items = []
339336
for line in content.splitlines():
340337
parts = line.split()
341338
if len(parts) != 3:
342339
continue
343340
if serial and parts[0] != serial:
344341
continue
345-
yield ForwardItem(*parts)
342+
items.append(ForwardItem(*parts))
343+
return items
346344

347345
@deprecated(deprecated_in="0.15.0",
348346
removed_in="1.0.0",
@@ -391,18 +389,20 @@ def reverse(self, serial, remote, local, norebind=False):
391389
removed_in="1.0.0",
392390
details="use Device.reverse_list instead",
393391
current_version=__version__)
394-
def reverse_list(self, serial: Union[None, str] = None):
392+
def reverse_list(self, serial: str) -> List[ReverseItem]:
395393
with self.make_connection() as c:
396394
c.send_command("host:transport:" + serial)
397395
c.check_okay()
398396
c.send_command("reverse:list-forward")
399397
c.check_okay()
400398
content = c.read_string_block()
399+
items = []
401400
for line in content.splitlines():
402401
parts = line.split()
403402
if len(parts) != 3:
404403
continue
405-
yield ReverseItem(*parts[1:])
404+
items.append(ReverseItem(*parts[1:]))
405+
return items
406406

407407

408408

adbutils/_device.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from typing import List, Optional, Union
1515

1616
from PIL import Image, UnidentifiedImageError
17+
from deprecation import deprecated
1718

1819
from adbutils._deprecated import DeprecatedExtension
1920
from adbutils.install import InstallExtension
@@ -66,9 +67,7 @@ def open_transport(
6667
self, command: str = None, timeout: float = _DEFAULT_SOCKET_TIMEOUT
6768
) -> AdbConnection:
6869
# connect has it own timeout
69-
c = self._client.make_connection()
70-
if timeout:
71-
c.conn.settimeout(timeout)
70+
c = self._client.make_connection(timeout=timeout)
7271

7372
if command:
7473
if self._transport_id:
@@ -264,15 +263,8 @@ def forward_port(self, remote: Union[int, str]) -> int:
264263
return local_port
265264

266265
def forward_list(self) -> List[ForwardItem]:
267-
c = self.open_transport("list-forward")
268-
content = c.read_string_block()
269-
items = []
270-
for line in content.splitlines():
271-
parts = line.split()
272-
if len(parts) != 3:
273-
continue
274-
items.append(ForwardItem(*parts))
275-
return items
266+
items = self._client.forward_list()
267+
return [item for item in items if item.serial == self._serial]
276268

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

360-
def push(self, local: str, remote: str) -> str:
361-
return self.adb_output("push", local, remote)
352+
@deprecated(deprecated_in="2.6.0", removed_in="3.0.0", current_version=__version__, details="use sync.push instead")
353+
def push(self, local: str, remote: str):
354+
""" alias for sync.push """
355+
self.sync.push(local, remote)
362356

363357
def create_connection(
364358
self, network: Network, address: Union[int, str]

docs/PROTOCOL.md

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
# ADB Protocol
2+
Tested with adb 1.0.41, Capture data with Charles
3+
4+
Charles settings
5+
6+
Proxy->Port Forwarding
7+
8+
```
9+
TCP Local(5555) -> Remote(localhost:5037)
10+
```
11+
12+
execute adb command, e.g. `adb -P 5555 devices`, now data should be seen in charles.
13+
114
## host version
215
```
316
>send
@@ -18,7 +31,33 @@
1831
00000020 30 30 31 34 27 20 6e 6f 74 20 66 6f 75 6e 64 0014' not found
1932
```
2033

21-
## reverse --list
34+
## forward
35+
```
36+
$ adb forward --list
37+
>send
38+
0011host:list-forward
39+
40+
<recv
41+
OKAY0020emulator-5554 tcp:5678 tcp:8765
42+
43+
$ adb forward --remove-all
44+
>send
45+
000ehost:tport:any0014host:killforward-all
46+
47+
<recv
48+
00000000 4f 4b 41 59 08 00 00 00 00 00 00 00 4f 4b 41 59 OKAY OKAY
49+
00000010 4f 4b 41 59 OKAY
50+
51+
$ adb -s emulator-5554 forward tcp:1234 tcp:4321
52+
>send
53+
001fhost:tport:serial:emulator-5554001ehost:forward:tcp:1234;tcp:4321
54+
55+
<recv
56+
00000000 4f 4b 41 59 08 00 00 00 00 00 00 00 4f 4b 41 59 OKAY OKAY
57+
00000010 4f 4b 41 59 30 30 30 34 31 32 33 34 OKAY00041234
58+
```
59+
60+
## reverse
2261
adb help
2362
```
2463
reverse --list list all reverse socket connections from device
@@ -33,14 +72,13 @@ reverse --list list all reverse socket connections from device
3372
```
3473

3574
```
75+
$ adb reverse --list
3676
>send
3777
0022host:tport:serial:GBG5T197110027690014
3878
3979
<recv
4080
00000000 4f 4b 41 59 06 00 00 00 00 00 00 00 OKAY
41-
```
4281
43-
```
4482
>send
4583
reverse:list-forward
4684
@@ -52,8 +90,8 @@ reverse:list-forward
5290
00000040 70 3a 35 31 32 33 20 74 63 70 3a 37 38 39 30 0a p:5123 tcp:7890
5391
```
5492

55-
## adb -s GBG5T197110027690014 reverse localabstract:test tcp:12345
5693
```
94+
$ adb -s GBG5T197110027690014 reverse localabstract:test tcp:12345
5795
>send
5896
001fhost:tport:serial:emulator-5554002creverse:forward:localabstract:test;tcp:12345
5997

tests/adb_server.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,16 @@ async def host_kill(ctx: Context):
9494
await ctx.server.stop()
9595
# os.kill(os.getpid(), signal.SIGINT)
9696

97+
@register_command("host:list-forward")
98+
async def host_list_forward(ctx: Context):
99+
await ctx.send(b"OKAY")
100+
await ctx.send(encode_string("123456 tcp:1234 tcp:4321"))
101+
102+
97103
SHELL_OUTPUTS = {
98104
"pwd": "/",
99105
}
100106

101-
102107
@register_command(re.compile("host:tport:serial:.*"))
103108
async def host_tport_serial(ctx: Context):
104109
serial = ctx.command.split(":")[-1]

tests/test_forward.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
"""Created on Wed May 08 2024 21:45:15 by codeskyblue
5+
"""
6+
7+
import adbutils
8+
9+
10+
def test_forward_list(adb: adbutils.AdbClient):
11+
items = adb.forward_list()
12+
assert len(items) == 1
13+
assert items[0].serial == "123456"
14+
assert items[0].local == "tcp:1234"
15+
assert items[0].remote == "tcp:4321"

0 commit comments

Comments
 (0)