Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 20 additions & 53 deletions core/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,19 @@ async def _ensure_browser(self) -> BrowserContext:
return self._context

def _load_cookies(self) -> list[dict[str, Any]]:
path = Path(self.cookies_path)
if not path.exists():
cookies_path = Path(self.cookies_path)
if not cookies_path.exists():
return []

try:
data = json.loads(path.read_text(encoding="utf-8"))
data = json.loads(cookies_path.read_text(encoding="utf-8"))

# Support Cookie-Editor export format: {"url": "...", "cookies": [...]}
if isinstance(data, dict) and isinstance(data.get("cookies"), list):
data = data["cookies"]

if not isinstance(data, list):
log.warning(f"Unsupported cookies format in {path}")
log.warning(f"Unsupported cookies format in {cookies_path}")
return []

cookies: list[dict[str, Any]] = []
Expand All @@ -157,13 +157,13 @@ def _load_cookies(self) -> list[dict[str, Any]]:
continue

domain = c.get("domain")
path = c.get("path")
cookie_path = c.get("path")

cookie: dict[str, Any] = {
"name": name,
"value": value,
"domain": domain if isinstance(domain, str) and domain else ".goofish.com",
"path": path if isinstance(path, str) and path else "/",
"path": cookie_path if isinstance(cookie_path, str) and cookie_path else "/",
"httpOnly": bool(c.get("httpOnly", False)),
"secure": bool(c.get("secure", True)),
"sameSite": _normalize_same_site(c.get("sameSite")),
Expand All @@ -183,14 +183,8 @@ def _load_cookies(self) -> list[dict[str, Any]]:
log.error(f"Failed to load cookies: {e}")
return []

async def close(self) -> None:
if self._context:
await self._context.close()
self._context = None
if self._playwright:
await self._playwright.stop()
self._playwright = None

async def _teardown_qr_session(self) -> None:
"""Clean up QR login resources (page, context, browser, playwright)."""
if self._qr_login_page and not self._qr_login_page.is_closed():
try:
await self._qr_login_page.close()
Expand Down Expand Up @@ -219,6 +213,16 @@ async def close(self) -> None:
pass
self._qr_playwright = None

async def close(self) -> None:
if self._context:
await self._context.close()
self._context = None
if self._playwright:
await self._playwright.stop()
self._playwright = None

await self._teardown_qr_session()

async def export_storage_state(self, output_path: str) -> Any:
"""Export Playwright storage_state (cookies + origins) for ai-goofish-monitor."""

Expand Down Expand Up @@ -284,26 +288,7 @@ async def qr_login_start(self, keyword: str = "iphone") -> dict:
try:
async with self._qr_lock:
# Tear down any previous QR session
if self._qr_login_page and not self._qr_login_page.is_closed():
try:
await self._qr_login_page.close()
except Exception:
pass
self._qr_login_page = None

if self._qr_context:
try:
await self._qr_context.close()
except Exception:
pass
self._qr_context = None

if self._qr_browser:
try:
await self._qr_browser.close()
except Exception:
pass
self._qr_browser = None
await self._teardown_qr_session()

if not self._qr_playwright:
self._qr_playwright = await async_playwright().start()
Expand Down Expand Up @@ -528,25 +513,7 @@ async def qr_login_wait(self, timeout: int = 120) -> dict:
log.error(f"QR login wait failed: {e}", exc_info=True)
return {"success": False, "error": str(e)}
finally:
try:
await page.close()
except Exception:
pass
self._qr_login_page = None

if self._qr_context:
try:
await self._qr_context.close()
except Exception:
pass
self._qr_context = None

if self._qr_browser:
try:
await self._qr_browser.close()
except Exception:
pass
self._qr_browser = None
await self._teardown_qr_session()


goofish_client = GoofishClient()