Skip to content

Commit

Permalink
update fake_useragent
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-black-tea committed Jul 2, 2024
1 parent 82348c1 commit b2a2c2a
Show file tree
Hide file tree
Showing 9 changed files with 303 additions and 127 deletions.
12 changes: 8 additions & 4 deletions src/linktools/android/adb.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,19 @@
import json
import re
import time
from typing import Any, Generator, List
from typing import Any, Generator, List, Callable, TYPE_CHECKING, TypeVar

from .struct import App, UnixSocket, InetSocket, Process
from .. import utils, environ
from ..decorator import cached_property, cached_classproperty
from ..device import BridgeError, Bridge, BaseDevice
from ..reactor import Stoppable

_logger = environ.get_logger("android.adb")
if TYPE_CHECKING:
DEVICE_TYPE = TypeVar("DEVICE_TYPE", bound="Device")

_logger = environ.get_logger("android.adb")
_agent_output_pattern = re.compile(
r""
r"┌──+──┐[^\n]*\n"
r"│[^|]*│[^\n]*\n"
r"└──+──┘[^\n]*\n",
Expand Down Expand Up @@ -138,6 +139,9 @@ def uid(self) -> int:
"""
return self.get_uid()

def copy(self, type: "Callable[[str, Adb], DEVICE_TYPE]" = None) -> "DEVICE_TYPE":
return (type or Device)(self._id, self._adb)

def popen(self, *args: Any, **kwargs) -> utils.Process:
"""
执行命令
Expand Down Expand Up @@ -203,7 +207,7 @@ def install(self, path_or_url: str, opts: [str] = (), **kwargs):
apk_path = file.download()
environ.logger.info(f"Save file to local: {apk_path}")

remote_path = self.get_data_path("apk", f"{int(time.time())}.apk")
remote_path = self.get_data_path("apk", f"installed_{int(time.time())}.apk")
try:
self.push(apk_path, remote_path, **kwargs)
if self.uid >= 10000:
Expand Down
165 changes: 165 additions & 0 deletions src/linktools/assets/browsers.json

Large diffs are not rendered by default.

52 changes: 0 additions & 52 deletions src/linktools/assets/fake_useragent.json

This file was deleted.

55 changes: 31 additions & 24 deletions src/linktools/cli/commands/android/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@
from linktools.cli import AndroidCommand, CommandError


class AgentDevice(Device):

def get_agent_path(self, *name: str) -> str:
return self.get_data_path("agent", *name)

def push_agent_plugin(self, src_path: str = None) -> Optional[str]:
if not src_path:
return None
if not os.path.exists(src_path):
raise CommandError(f"Plugin file not found: {src_path}")
dest_name = os.path.basename(src_path)
dest_path = self.get_agent_path("plugin", dest_name)
self.push(src_path, dest_path)
return dest_path


class Command(AndroidCommand):
"""
Debug and interact with android-tools.apk for troubleshooting
Expand All @@ -53,32 +69,23 @@ def init_arguments(self, parser: ArgumentParser) -> None:
parser.add_argument("agent_args", nargs="...", help="agent args")

def run(self, args: Namespace) -> Optional[int]:
device = args.device_picker.pick()

process = device.popen(
*device.make_shell_args(
*device.make_agent_args(
*args.agent_args,
data_path=args.data or device.get_data_path('agent', 'data'),
library_path=args.library,
plugin_path=self._push_plugin(device, args.plugin),
),
privilege=args.privilege,
user=args.user
)
device = args.device_picker.pick().copy(AgentDevice)

agent_args = device.make_agent_args(
*args.agent_args,
data_path=args.data or device.get_agent_path("data"),
library_path=args.library or device.get_agent_path("data", "lib"),
plugin_path=device.push_agent_plugin(args.plugin),
)
return process.call()

@classmethod
def _push_plugin(cls, device: Device, path: str = None) -> Optional[str]:
if not path:
return None
if not os.path.exists(path):
raise CommandError(f"Plugin file not found: {path}")
plugin_name = os.path.basename(path)
plugin_path = device.get_data_path("agent", "plugin", plugin_name)
device.push(path, plugin_path)
return plugin_path
shell_args = device.make_shell_args(
*agent_args,
privilege=args.privilege,
user=args.user
)

process = device.popen(*shell_args)
return process.call()


command = Command()
Expand Down
10 changes: 8 additions & 2 deletions src/linktools/ios/sib.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import subprocess
import time
from subprocess import TimeoutExpired
from typing import Any, Generator, List
from typing import Any, Generator, List, Callable, Dict, TYPE_CHECKING, TypeVar

from .struct import Process, App
from .. import utils
Expand All @@ -14,6 +14,9 @@
from ..device import BridgeError, Bridge, BaseDevice
from ..reactor import Stoppable

if TYPE_CHECKING:
DEVICE_TYPE = TypeVar("DEVICE_TYPE", bound="Device")

_logger = environ.get_logger("android.adb")


Expand Down Expand Up @@ -49,7 +52,7 @@ def list_devices(self, alive: bool = None) -> Generator["Device", None, None]:

class Device(BaseDevice):

def __init__(self, id: str = None, info: dict = None, sib: Sib = None):
def __init__(self, id: str = None, info: Dict = None, sib: Sib = None):
"""
:param id: 设备号
"""
Expand Down Expand Up @@ -103,6 +106,9 @@ def info(self) -> dict:
def detail(self) -> dict:
return self.info.get("deviceDetail")

def copy(self, type: "Callable[[str, Dict, Sib], DEVICE_TYPE]" = None) -> "DEVICE_TYPE":
return (type or Device)(self._id, self._info, self._sib)

def popen(self, *args: [Any], **kwargs) -> utils.Process:
"""
执行命令
Expand Down
Loading

0 comments on commit b2a2c2a

Please sign in to comment.