Skip to content

Commit

Permalink
try to fix tunneld error for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed May 7, 2024
1 parent 7f2d185 commit 2e22b66
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions tidevice3/cli/tunneld.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import sys
import threading
import time
from typing import Mapping, NamedTuple, Tuple
from typing import List, Mapping, NamedTuple, Tuple

import click
import fastapi
Expand Down Expand Up @@ -43,19 +43,19 @@ def get_connected_devices() -> list[str]:
return [d.Identifier for d in devices if Version(d.ProductVersion) >= Version("17")]


def guess_pymobiledevice3_path() -> str:
def guess_pymobiledevice3_cmd() -> List[str]:
pmd3path = shutil.which("pymobiledevice3")
if not pmd3path:
pmd3path = sys.executable + " -m pymobiledevice3"
return pmd3path
return [sys.executable, '-m', 'pymobiledevice3']
return [pmd3path]


class TunnelError(Exception):
pass


@threadsafe_function
def start_tunnel(pmd3_path: str, udid: str) -> Tuple[Address, subprocess.Popen]:
def start_tunnel(pmd3_path: List[str], udid: str) -> Tuple[Address, subprocess.Popen]:
"""
Start program, should be killed when the main program quit
Expand All @@ -64,10 +64,10 @@ def start_tunnel(pmd3_path: str, udid: str) -> Tuple[Address, subprocess.Popen]:
"""
# cmd = ["bash", "-c", "echo ::1 1234; sleep 10001"]
log_prefix = f"[{udid}]"
cmd = f"{pmd3_path} remote start-tunnel --script-mode --udid {udid}"
logger.info("%s cmd: %s", log_prefix, cmd)
cmdargs = pmd3_path + f"remote start-tunnel --script-mode --udid {udid}".split()
logger.info("%s cmd: %s", log_prefix, shlex.join(cmdargs))
process = subprocess.Popen(
shlex.split(cmd), stdin=subprocess.DEVNULL, stdout=subprocess.PIPE
cmdargs, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE
)
output_str = process.stdout.readline().decode("utf-8").strip()
if output_str == "":
Expand All @@ -84,7 +84,7 @@ def __init__(self):
self.active_monitors: Mapping[str, subprocess.Popen] = {}
self.running = True
self.addresses: Mapping[str, Address] = {}
self.pmd3_path = "pymobiledevice3"
self.pmd3_cmd = ["pymobiledevice3"]

def update_devices(self):
current_devices = set(get_connected_devices())
Expand Down Expand Up @@ -113,7 +113,7 @@ def update_devices(self):
def _start_tunnel_keeper(self, udid: str):
while udid in self.active_monitors:
try:
addr, process = start_tunnel(self.pmd3_path, udid)
addr, process = start_tunnel(self.pmd3_cmd, udid)
self.active_monitors[udid] = process
self.addresses[udid] = addr
self._wait_process_exit(process, udid)
Expand Down Expand Up @@ -152,7 +152,7 @@ def run_forever(self):
"--pmd3-path",
"pmd3_path",
help="pymobiledevice3 cli path",
default=guess_pymobiledevice3_path(),
default=None,
)
@click.option("--port", "port", help="listen port", default=5555)
def tunneld(pmd3_path: str, port: int):
Expand All @@ -174,7 +174,10 @@ def shutdown():
os.kill(os.getpid(), signal.SIGINT)
return fastapi.Response(status_code=200, content="Server shutting down...")

manager.pmd3_path = pmd3_path
if pmd3_path is None:
manager.pmd3_cmd = guess_pymobiledevice3_cmd()
else:
manager.pmd3_cmd = [pmd3_path]

threading.Thread(
target=manager.run_forever, daemon=True, name="device_manager"
Expand Down

0 comments on commit 2e22b66

Please sign in to comment.