diff --git a/pymobiledevice3/ca.py b/pymobiledevice3/ca.py index f4f6b37cf..49b4ba8c9 100755 --- a/pymobiledevice3/ca.py +++ b/pymobiledevice3/ca.py @@ -15,8 +15,10 @@ def make_cert(key, public_key, common_name=None): cert = cert.issuer_name(issuer) cert = cert.public_key(public_key) cert = cert.serial_number(1) - cert = cert.not_valid_before(datetime.utcnow() - timedelta(minutes=1)) - cert = cert.not_valid_after(datetime.utcnow() + timedelta(days=365 * 10)) + now = datetime.now() + now = now.replace(tzinfo=None) + cert = cert.not_valid_before(now - timedelta(minutes=1)) + cert = cert.not_valid_after(now + timedelta(days=365 * 10)) cert = cert.sign(key, hashes.SHA256()) return cert diff --git a/pymobiledevice3/cli/cli_common.py b/pymobiledevice3/cli/cli_common.py index b67a9dd14..1ae5ba643 100644 --- a/pymobiledevice3/cli/cli_common.py +++ b/pymobiledevice3/cli/cli_common.py @@ -3,6 +3,7 @@ import logging import os import signal +import sys import uuid from typing import Callable, List, Mapping, Optional, Tuple @@ -101,7 +102,7 @@ def wait_return(): def sudo_required(func): def wrapper(*args, **kwargs): - if os.geteuid() != 0: + if sys.platform != 'win32' and os.geteuid() != 0: raise AccessDeniedError() else: func(*args, **kwargs) diff --git a/pymobiledevice3/tunneld.py b/pymobiledevice3/tunneld.py index d2a3b2d2f..5527af3a0 100644 --- a/pymobiledevice3/tunneld.py +++ b/pymobiledevice3/tunneld.py @@ -3,7 +3,7 @@ import logging import os import signal -from contextlib import suppress +from contextlib import asynccontextmanager, suppress from typing import Dict, Tuple import fastapi @@ -162,14 +162,23 @@ async def discover_new_devices(self) -> None: class TunneldRunner: """ TunneldRunner orchestrate between the webserver and TunneldCore """ + @classmethod def create(cls, host: str, port: int) -> None: cls(host, port)._run_app() def __init__(self, host: str, port: int): + @asynccontextmanager + async def lifespan(app: FastAPI): + logging.getLogger('zeroconf').disabled = True + self._tunneld_core.start() + yield + logger.info('Closing tunneld tasks...') + await self._tunneld_core.close() + self.host = host self.port = port - self._app = FastAPI() + self._app = FastAPI(lifespan=lifespan) self._tunneld_core = TunneldCore() @self._app.get('/') @@ -193,16 +202,5 @@ async def clear_tunnels() -> fastapi.Response: self._tunneld_core.clear() return fastapi.Response(status_code=200, content='Cleared tunnels...') - @self._app.on_event('startup') - async def on_startup() -> None: - """ start TunneldCore """ - logging.getLogger('zeroconf').disabled = True - self._tunneld_core.start() - - @self._app.on_event('shutdown') - async def on_close() -> None: - logger.info('Closing tunneld tasks...') - await self._tunneld_core.close() - def _run_app(self) -> None: uvicorn.run(self._app, host=self.host, port=self.port, loop='asyncio')