feat: support real browser auth import
This commit is contained in:
@@ -137,9 +137,12 @@ def _cookie_matches_hostname(cookie_domain: str, hostname: str) -> bool:
|
||||
"""判断 cookie domain 是否适用于给定 hostname。
|
||||
|
||||
支持带点前缀的 domain(如 `.saki.lat` 匹配 `api.saki.lat`)。
|
||||
注意:hostname 为空时,调用方应跳过 cookie 收集而不是调用此函数。
|
||||
"""
|
||||
if not cookie_domain or not hostname:
|
||||
return True # 无 domain 限制时视为全域
|
||||
if not cookie_domain:
|
||||
return True # 无 domain 限制的 cookie 对当前域有效
|
||||
if not hostname:
|
||||
return False # 无法确定当前域,保守拒绝
|
||||
# 去掉前缀点
|
||||
domain = cookie_domain.lstrip(".")
|
||||
return hostname == domain or hostname.endswith("." + domain)
|
||||
@@ -153,14 +156,22 @@ def _build_cookie_bundle(
|
||||
|
||||
返回 (cookie_string, cookie_names_list)。
|
||||
cookie_string 格式:name1=value1; name2=value2; ...
|
||||
过滤掉空值 cookie。
|
||||
过滤掉空值 cookie。若 page_url 为空或无法解析 hostname,返回空结果
|
||||
(不收集全域 cookie 以防误写入无关域凭证)。
|
||||
"""
|
||||
if not page_url:
|
||||
logger.debug("_build_cookie_bundle: no page_url, skipping cookie collection")
|
||||
return "", []
|
||||
|
||||
hostname = ""
|
||||
if page_url:
|
||||
try:
|
||||
hostname = urlparse(page_url).hostname or ""
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
hostname = urlparse(page_url).hostname or ""
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if not hostname:
|
||||
logger.debug("_build_cookie_bundle: cannot parse hostname from %s, skipping", page_url[:80])
|
||||
return "", []
|
||||
|
||||
parts: list[str] = []
|
||||
names: list[str] = []
|
||||
@@ -170,7 +181,7 @@ def _build_cookie_bundle(
|
||||
domain = c.get("domain", "")
|
||||
if not name or not value:
|
||||
continue
|
||||
if hostname and not _cookie_matches_hostname(domain, hostname):
|
||||
if not _cookie_matches_hostname(domain, hostname):
|
||||
continue
|
||||
parts.append(f"{name}={value}")
|
||||
names.append(name)
|
||||
|
||||
Reference in New Issue
Block a user