mirror of
https://github.com/violettoolssite/CFspider.git
synced 2026-04-05 03:09:01 +08:00
v1.8.5: fix multiple bugs in Session and StealthSession
This commit is contained in:
@@ -217,7 +217,7 @@ class PlaywrightNotInstalledError(CFSpiderError):
|
||||
pass
|
||||
|
||||
|
||||
__version__ = "1.8.4"
|
||||
__version__ = "1.8.5"
|
||||
__all__ = [
|
||||
# 同步 API (requests)
|
||||
"get", "post", "put", "delete", "head", "options", "patch", "request",
|
||||
|
||||
@@ -465,6 +465,9 @@ def request(method, url, cf_proxies=None, uuid=None, http2=False, impersonate=No
|
||||
... )
|
||||
... print(response.json()['origin']) # 每次都是不同 IP
|
||||
"""
|
||||
# 移除不支持的旧版参数(保持向后兼容)
|
||||
kwargs.pop("token", None)
|
||||
|
||||
# 应用随机延迟
|
||||
if delay:
|
||||
from .stealth import random_delay
|
||||
|
||||
@@ -31,6 +31,7 @@ class AsyncSession:
|
||||
timeout: float = 30,
|
||||
headers: Optional[Dict[str, str]] = None,
|
||||
cookies: Optional[Dict[str, str]] = None,
|
||||
token: Optional[str] = None,
|
||||
**kwargs
|
||||
):
|
||||
"""
|
||||
@@ -43,6 +44,7 @@ class AsyncSession:
|
||||
timeout: 默认超时时间(秒)
|
||||
headers: 默认请求头
|
||||
cookies: 默认 Cookies
|
||||
token: CFspider Workers API token(选填)
|
||||
**kwargs: 传递给 httpx.AsyncClient 的其他参数
|
||||
"""
|
||||
self.cf_proxies = cf_proxies
|
||||
|
||||
@@ -47,7 +47,7 @@ class Session:
|
||||
please use cfspider.StealthSession.
|
||||
"""
|
||||
|
||||
def __init__(self, cf_proxies=None, uuid=None):
|
||||
def __init__(self, cf_proxies=None, uuid=None, static_ip=False, two_proxy=None):
|
||||
"""
|
||||
初始化会话 / Initialize session
|
||||
|
||||
@@ -61,6 +61,11 @@ class Session:
|
||||
uuid (str, optional): VLESS UUID(可选)
|
||||
如果不填写,会自动从 Workers 首页获取
|
||||
If not provided, will be auto-fetched from Workers homepage
|
||||
static_ip (bool): 是否使用固定 IP 模式(默认 False)
|
||||
/ Whether to use static IP mode (default False)
|
||||
two_proxy (str, optional): 第二层代理配置
|
||||
/ Second layer proxy configuration
|
||||
格式:host:port:user:pass 或 host:port
|
||||
|
||||
Raises:
|
||||
ValueError: 当 cf_proxies 为空时
|
||||
@@ -74,6 +79,12 @@ class Session:
|
||||
... cf_proxies="https://cfspider.violetqqcom.workers.dev",
|
||||
... uuid="c373c80c-58e4-4e64-8db5-40096905ec58"
|
||||
... )
|
||||
>>>
|
||||
>>> # 固定 IP 模式
|
||||
>>> session = cfspider.Session(
|
||||
... cf_proxies="https://cfspider.violetqqcom.workers.dev",
|
||||
... static_ip=True
|
||||
... )
|
||||
"""
|
||||
if not cf_proxies:
|
||||
raise ValueError(
|
||||
@@ -86,6 +97,8 @@ class Session:
|
||||
)
|
||||
self.cf_proxies = cf_proxies.rstrip("/") if cf_proxies else None
|
||||
self.uuid = uuid
|
||||
self.static_ip = static_ip
|
||||
self.two_proxy = two_proxy
|
||||
self.headers = {}
|
||||
self.cookies = {}
|
||||
self._base_headers = {} # 兼容 StealthSession API
|
||||
@@ -226,11 +239,24 @@ class Session:
|
||||
cookies = self.cookies.copy()
|
||||
cookies.update(kwargs.pop("cookies", {}))
|
||||
|
||||
# 如果用户在请求中指定了 uuid,使用用户指定的,否则使用 Session 的
|
||||
uuid = kwargs.pop("uuid", None) or self.uuid
|
||||
# 如果用户在请求中指定了 cf_proxies,使用用户指定的,否则使用 Session 的
|
||||
cf_proxies = kwargs.pop("cf_proxies", None) or self.cf_proxies
|
||||
# 如果用户在请求中指定了 static_ip,使用用户指定的,否则使用 Session 的
|
||||
static_ip = kwargs.pop("static_ip", None)
|
||||
if static_ip is None:
|
||||
static_ip = self.static_ip
|
||||
# 如果用户在请求中指定了 two_proxy,使用用户指定的,否则使用 Session 的
|
||||
two_proxy = kwargs.pop("two_proxy", None) or self.two_proxy
|
||||
|
||||
response = request(
|
||||
method,
|
||||
url,
|
||||
cf_proxies=self.cf_proxies,
|
||||
uuid=self.uuid,
|
||||
cf_proxies=cf_proxies,
|
||||
uuid=uuid,
|
||||
static_ip=static_ip,
|
||||
two_proxy=two_proxy,
|
||||
headers=headers,
|
||||
cookies=cookies,
|
||||
**kwargs
|
||||
|
||||
@@ -295,6 +295,8 @@ class StealthSession:
|
||||
uuid: str = None,
|
||||
delay: Tuple[float, float] = None,
|
||||
auto_referer: bool = True,
|
||||
static_ip: bool = False,
|
||||
two_proxy: str = None,
|
||||
**kwargs
|
||||
):
|
||||
"""
|
||||
@@ -322,6 +324,11 @@ class StealthSession:
|
||||
- 第一次请求不会延迟 / First request won't be delayed
|
||||
auto_referer (bool): 是否自动添加 Referer(默认 True)
|
||||
/ Whether to auto-add Referer (default: True)
|
||||
static_ip (bool): 是否使用固定 IP 模式(默认 False)
|
||||
/ Whether to use static IP mode (default False)
|
||||
two_proxy (str, optional): 第二层代理配置
|
||||
/ Second layer proxy configuration
|
||||
格式:host:port:user:pass 或 host:port
|
||||
**kwargs: 保留参数,用于未来扩展 / Reserved for future extensions
|
||||
|
||||
Example:
|
||||
@@ -337,6 +344,8 @@ class StealthSession:
|
||||
self.uuid = uuid
|
||||
self.delay = delay
|
||||
self.auto_referer = auto_referer
|
||||
self.static_ip = static_ip
|
||||
self.two_proxy = two_proxy
|
||||
self.last_url = None
|
||||
self.request_count = 0
|
||||
self._extra_kwargs = kwargs
|
||||
@@ -457,10 +466,20 @@ class StealthSession:
|
||||
cookies = kwargs.pop('cookies', {})
|
||||
cookies.update(self._cookies)
|
||||
|
||||
# 提取可覆盖的参数,避免重复传递
|
||||
uuid = kwargs.pop('uuid', None) or self.uuid
|
||||
cf_proxies = kwargs.pop('cf_proxies', None) or self.cf_proxies
|
||||
static_ip = kwargs.pop('static_ip', None)
|
||||
if static_ip is None:
|
||||
static_ip = getattr(self, 'static_ip', False)
|
||||
two_proxy = kwargs.pop('two_proxy', None) or getattr(self, 'two_proxy', None)
|
||||
|
||||
response = _get(
|
||||
url,
|
||||
cf_proxies=self.cf_proxies,
|
||||
uuid=self.uuid,
|
||||
cf_proxies=cf_proxies,
|
||||
uuid=uuid,
|
||||
static_ip=static_ip,
|
||||
two_proxy=two_proxy,
|
||||
headers=headers,
|
||||
cookies=cookies,
|
||||
**kwargs
|
||||
@@ -496,10 +515,20 @@ class StealthSession:
|
||||
cookies = kwargs.pop('cookies', {})
|
||||
cookies.update(self._cookies)
|
||||
|
||||
# 提取可覆盖的参数,避免重复传递
|
||||
uuid = kwargs.pop('uuid', None) or self.uuid
|
||||
cf_proxies = kwargs.pop('cf_proxies', None) or self.cf_proxies
|
||||
static_ip = kwargs.pop('static_ip', None)
|
||||
if static_ip is None:
|
||||
static_ip = getattr(self, 'static_ip', False)
|
||||
two_proxy = kwargs.pop('two_proxy', None) or getattr(self, 'two_proxy', None)
|
||||
|
||||
response = _post(
|
||||
url,
|
||||
cf_proxies=self.cf_proxies,
|
||||
uuid=self.uuid,
|
||||
cf_proxies=cf_proxies,
|
||||
uuid=uuid,
|
||||
static_ip=static_ip,
|
||||
two_proxy=two_proxy,
|
||||
headers=headers,
|
||||
cookies=cookies,
|
||||
**kwargs
|
||||
@@ -519,10 +548,21 @@ class StealthSession:
|
||||
headers = self._prepare_headers(url, kwargs.pop('headers', None))
|
||||
cookies = kwargs.pop('cookies', {})
|
||||
cookies.update(self._cookies)
|
||||
|
||||
# 提取可覆盖的参数,避免重复传递
|
||||
uuid = kwargs.pop('uuid', None) or self.uuid
|
||||
cf_proxies = kwargs.pop('cf_proxies', None) or self.cf_proxies
|
||||
static_ip = kwargs.pop('static_ip', None)
|
||||
if static_ip is None:
|
||||
static_ip = getattr(self, 'static_ip', False)
|
||||
two_proxy = kwargs.pop('two_proxy', None) or getattr(self, 'two_proxy', None)
|
||||
|
||||
response = _put(
|
||||
url,
|
||||
cf_proxies=self.cf_proxies,
|
||||
uuid=self.uuid,
|
||||
cf_proxies=cf_proxies,
|
||||
uuid=uuid,
|
||||
static_ip=static_ip,
|
||||
two_proxy=two_proxy,
|
||||
headers=headers,
|
||||
cookies=cookies,
|
||||
**kwargs
|
||||
@@ -540,10 +580,21 @@ class StealthSession:
|
||||
headers = self._prepare_headers(url, kwargs.pop('headers', None))
|
||||
cookies = kwargs.pop('cookies', {})
|
||||
cookies.update(self._cookies)
|
||||
|
||||
# 提取可覆盖的参数,避免重复传递
|
||||
uuid = kwargs.pop('uuid', None) or self.uuid
|
||||
cf_proxies = kwargs.pop('cf_proxies', None) or self.cf_proxies
|
||||
static_ip = kwargs.pop('static_ip', None)
|
||||
if static_ip is None:
|
||||
static_ip = getattr(self, 'static_ip', False)
|
||||
two_proxy = kwargs.pop('two_proxy', None) or getattr(self, 'two_proxy', None)
|
||||
|
||||
response = _delete(
|
||||
url,
|
||||
cf_proxies=self.cf_proxies,
|
||||
uuid=self.uuid,
|
||||
cf_proxies=cf_proxies,
|
||||
uuid=uuid,
|
||||
static_ip=static_ip,
|
||||
two_proxy=two_proxy,
|
||||
headers=headers,
|
||||
cookies=cookies,
|
||||
**kwargs
|
||||
@@ -561,10 +612,21 @@ class StealthSession:
|
||||
headers = self._prepare_headers(url, kwargs.pop('headers', None))
|
||||
cookies = kwargs.pop('cookies', {})
|
||||
cookies.update(self._cookies)
|
||||
|
||||
# 提取可覆盖的参数,避免重复传递
|
||||
uuid = kwargs.pop('uuid', None) or self.uuid
|
||||
cf_proxies = kwargs.pop('cf_proxies', None) or self.cf_proxies
|
||||
static_ip = kwargs.pop('static_ip', None)
|
||||
if static_ip is None:
|
||||
static_ip = getattr(self, 'static_ip', False)
|
||||
two_proxy = kwargs.pop('two_proxy', None) or getattr(self, 'two_proxy', None)
|
||||
|
||||
response = _head(
|
||||
url,
|
||||
cf_proxies=self.cf_proxies,
|
||||
uuid=self.uuid,
|
||||
cf_proxies=cf_proxies,
|
||||
uuid=uuid,
|
||||
static_ip=static_ip,
|
||||
two_proxy=two_proxy,
|
||||
headers=headers,
|
||||
cookies=cookies,
|
||||
**kwargs
|
||||
|
||||
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
||||
|
||||
[project]
|
||||
name = "cfspider"
|
||||
version = "1.8.4"
|
||||
version = "1.8.5"
|
||||
description = "Cloudflare Workers proxy IP pool client"
|
||||
readme = "README.md"
|
||||
license = {text = "Apache-2.0"}
|
||||
|
||||
Reference in New Issue
Block a user