-
Notifications
You must be signed in to change notification settings - Fork 42
feat: support start-tunnel over Wi-Fi #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
2088669827
wants to merge
2
commits into
codeskyblue:master
Choose a base branch
from
2088669827:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,14 +34,18 @@ | |
port: int | ||
|
||
|
||
def get_connected_devices() -> list[str]: | ||
def get_connected_devices(wifi: bool) -> list[str]: | ||
"""return list of udid""" | ||
try: | ||
devices = list_devices(usb=True, network=False) | ||
usb_devices = list_devices(usb=True, network=False) | ||
devices = ["usb_" + d.Identifier for d in usb_devices if Version(d.ProductVersion) >= Version("17")] | ||
if wifi: | ||
wifi_devices = list_devices(usb=False, network=True) | ||
devices.extend(["wifi_" + d.Identifier for d in wifi_devices if Version(d.ProductVersion) >= Version("17")]) | ||
except MuxException as e: | ||
logger.error("list_devices failed: %s", e) | ||
return [] | ||
return [d.Identifier for d in devices if Version(d.ProductVersion) >= Version("17")] | ||
return devices | ||
|
||
|
||
def get_need_lockdown_devices() -> list[str]: | ||
|
@@ -74,11 +78,16 @@ | |
TunnelError | ||
""" | ||
# cmd = ["bash", "-c", "echo ::1 1234; sleep 10001"] | ||
log_prefix = f"[{udid}]" | ||
device_type, _udid = udid.split("_")[0], udid.split("_")[1] | ||
log_prefix = f"[{_udid}]" | ||
start_tunnel_cmd = "remote" | ||
if udid in get_need_lockdown_devices(): | ||
start_tunnel_cmd = "lockdown" | ||
cmdargs = pmd3_path + f"{start_tunnel_cmd} start-tunnel --script-mode --udid {udid}".split() | ||
lockdown_devices = get_need_lockdown_devices() | ||
if device_type == "wifi" and _udid not in lockdown_devices: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 17.4以下,Wi-Fi模式,用remote加-t wifi;17.4以上,Wi-Fi模式和USB模式都用lockdown,没有-t wifi选项 |
||
cmdargs = pmd3_path + f"{start_tunnel_cmd} start-tunnel --script-mode --udid {_udid} -t wifi".split() | ||
else: | ||
if _udid in lockdown_devices: | ||
start_tunnel_cmd = "lockdown" | ||
cmdargs = pmd3_path + f"{start_tunnel_cmd} start-tunnel --script-mode --udid {_udid}".split() | ||
logger.info("%s cmd: %s", log_prefix, shlex.join(cmdargs)) | ||
process = subprocess.Popen( | ||
cmdargs, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE | ||
|
@@ -100,12 +109,16 @@ | |
self.addresses: Mapping[str, Address] = {} | ||
self.pmd3_cmd = ["pymobiledevice3"] | ||
|
||
def update_devices(self): | ||
current_devices = set(get_connected_devices()) | ||
active_udids = set(self.active_monitors.keys()) | ||
def update_devices(self, wifi: bool): | ||
current_devices = get_connected_devices(wifi) | ||
active_udids = self.active_monitors.keys() | ||
|
||
# Start monitors for new devices | ||
for udid in current_devices - active_udids: | ||
for udid in current_devices: | ||
if udid in active_udids: | ||
continue | ||
if udid.replace("wifi", "usb") in active_udids: # skip if device already monitered by usb | ||
continue | ||
self.active_monitors[udid] = None | ||
try: | ||
threading.Thread(name=f"{udid} keeper", | ||
|
@@ -116,7 +129,9 @@ | |
logger.error("udid: %s start-tunnel failed: %s", udid, e) | ||
|
||
# Stop monitors for disconnected devices | ||
for udid in active_udids - current_devices: | ||
for udid in active_udids: | ||
if udid in current_devices: | ||
continue | ||
logger.info("udid: %s quit, terminate related process", udid) | ||
process = self.active_monitors[udid] | ||
if process: | ||
|
@@ -152,10 +167,10 @@ | |
process.terminate() | ||
self.running = False | ||
|
||
def run_forever(self): | ||
def run_forever(self, wifi: bool): | ||
while self.running: | ||
try: | ||
self.update_devices() | ||
self.update_devices(wifi) | ||
except Exception as e: | ||
logger.exception("update_devices failed: %s", e) | ||
time.sleep(1) | ||
|
@@ -169,7 +184,8 @@ | |
default=None, | ||
) | ||
@click.option("--port", "port", help="listen port", default=5555) | ||
def tunneld(pmd3_path: str, port: int): | ||
@click.option("--wifi", is_flag=True, help="start-tunnel for network devices") | ||
def tunneld(pmd3_path: str, port: int, wifi: bool): | ||
"""start server for iOS >= 17 auto start-tunnel, function like pymobiledevice3 remote tunneld""" | ||
if not os_utils.is_admin: | ||
logger.error("Please run as root(Mac) or administrator(Windows)") | ||
|
@@ -194,7 +210,7 @@ | |
manager.pmd3_cmd = [pmd3_path] | ||
|
||
threading.Thread( | ||
target=manager.run_forever, daemon=True, name="device_manager" | ||
target=manager.run_forever, args=(wifi,), daemon=True, name="device_manager" | ||
).start() | ||
try: | ||
uvicorn.run(app, host="0.0.0.0", port=port) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里感觉不太好,udid加上前缀 usb_, wifi_ 感觉不如直接定义一个新的类型,比如
get_connected_devices(usb: bool, network: bool) -> List[DeviceInfo]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
以提交新版本