doronz88 / pymobiledevice3

Pure python3 implementation for working with iDevices (iPhone, etc...).
https://discord.gg/52mZGC3JXJ
GNU General Public License v3.0
1.43k stars 198 forks source link

How to stop a tunnel #746

Closed zhengf0509 closed 9 months ago

zhengf0509 commented 10 months ago

When I start a quick tunnel as follows,how can I stop the tunnel_task?I use the "rsd.close()" 3 second after "asyncio.create_task",but it did not work。

async def tunnel_task(
        service_provider: RemoteServiceDiscoveryService, secrets: TextIO,
        script_mode: bool = False, max_idle_timeout: float = MAX_IDLE_TIMEOUT,
        protocol: TunnelProtocol = TunnelProtocol.QUIC) -> None:
    if start_tunnel is None:
        raise NotImplementedError('failed to start the QUIC tunnel on your platform')

async with start_tunnel(service_provider, secrets=secrets, max_idle_timeout=max_idle_timeout,
                        protocol=protocol) as tunnel_result:
    logger.info('tunnel created')
    print(f"ProductType: {service_provider.product_type}")
    print(f"ProductVersion: {service_provider.product_version}")
    print(f"Interface: {tunnel_result.interface}")
    print(f"Protocol: {tunnel_result.protocol}")
    print(f"RSD Address: {tunnel_result.address}")
    print(f"RSD Port: {tunnel_result.port}")
    print(f"Use the follow connection option:\n--rsd {tunnel_result.address} {tunnel_result.port}")
    sys.stdout.flush()
    await tunnel_result.client.wait_closed()
    logger.info('tunnel was closed')

protocol = TunnelProtocol('quic')
udid = '00008130-0009488E3E10001C'
protocol = TunnelProtocol(protocol)
if not verify_tunnel_imports():
    pass
devices = get_device_list()
rsd:RemoteServiceDiscoveryService|None = None
if not devices:
    # no devices were found
    raise NoDeviceConnectedError()
if len(devices) == 1:
    # only one device found
    rsd = devices[0]
else:
    rsd = [device for device in devices if device.udid == udid]
    if len(rsd) > 0:
        rsd = rsd[0]
    else:
        raise NoDeviceConnectedError()

if udid is not None and rsd.udid != udid:
    raise NoDeviceConnectedError()

# asyncio.run(tunnel_task(rsd, None, False, max_idle_timeout=MAX_IDLE_TIMEOUT, protocol=protocol),
#             debug=True)

async def main():
    asyncio.create_task(tunnel_task(rsd, None, False, max_idle_timeout=MAX_IDLE_TIMEOUT, protocol=protocol))

    print("111-----------------111")

    await asyncio.sleep(3)
    rsd.close()

loop = asyncio.get_event_loop()
loop.set_debug(True)
loop.run_until_complete(main())

while(True):
    pass
doronz88 commented 9 months ago

You need to close the tunnel itself, not the RSD over it. You got a reference to it in tunnel_result