fix(douyu): 修复斗鱼链路 P0/P1 隐患并增强 P2 运维队列可观测性

This commit is contained in:
yml
2026-11-29 14:10:24 +08:00
parent be7146a9ef
commit d12d9e02b3
51 changed files with 2510 additions and 561 deletions
+39 -35
View File
@@ -155,19 +155,21 @@ def _get_local_exit_ip() -> str | None:
"https://4.ipw.cn",
"https://api.ipify.org",
]
for url in targets:
try:
response = requests.get(
url, timeout=8, headers={"User-Agent": "Mozilla/5.0"}
)
response.raise_for_status()
text = response.text.strip()
match = re.search(r"(\d{1,3}(?:\.\d{1,3}){3})", text)
if match:
return match.group(1)
except Exception as exc: # noqa: BLE001
logger.debug(f"本机出口 IP 查询失败: {url}: {exc}")
continue
with requests.Session() as sess:
sess.trust_env = False
for url in targets:
try:
with sess.get(
url, timeout=8, headers={"User-Agent": "Mozilla/5.0"}
) as response:
response.raise_for_status()
text = response.text.strip()
match = re.search(r"(\d{1,3}(?:\.\d{1,3}){3})", text)
if match:
return match.group(1)
except Exception as exc: # noqa: BLE001
logger.debug(f"本机出口 IP 查询失败: {url}: {exc}")
continue
return None
@@ -179,26 +181,28 @@ def get_exit_ip_via_proxy(proxy: str) -> str | None:
"https://4.ipw.cn",
]
proxies = {"http": proxy, "https": proxy}
for url in targets:
try:
response = requests.get(
url,
proxies=proxies,
timeout=6,
headers={"User-Agent": "Mozilla/5.0"},
)
response.raise_for_status()
content_type = response.headers.get("content-type", "").lower()
if "json" in content_type:
data = response.json()
ip = data.get("ip") or data.get("origin")
if ip:
return str(ip).split(",")[0].strip()
text = response.text.strip()
match = re.search(r"(\d{1,3}(?:\.\d{1,3}){3})", text)
if match:
return match.group(1)
except Exception as exc: # noqa: BLE001
logger.debug(f"代理出口 IP 查询失败: {url}: {exc}")
continue
with requests.Session() as sess:
sess.trust_env = False
for url in targets:
try:
with sess.get(
url,
proxies=proxies,
timeout=6,
headers={"User-Agent": "Mozilla/5.0"},
) as response:
response.raise_for_status()
content_type = response.headers.get("content-type", "").lower()
if "json" in content_type:
data = response.json()
ip = data.get("ip") or data.get("origin")
if ip:
return str(ip).split(",")[0].strip()
text = response.text.strip()
match = re.search(r"(\d{1,3}(?:\.\d{1,3}){3})", text)
if match:
return match.group(1)
except Exception as exc: # noqa: BLE001
logger.debug(f"代理出口 IP 查询失败: {url}: {exc}")
continue
return None
+15 -12
View File
@@ -52,10 +52,11 @@ class XiequAdapter(BaseWhitelistAdapter):
"""
try:
url = self._build_url(act="getjson")
response = requests.get(url, timeout=10)
response.raise_for_status()
text = response.text.strip()
with requests.Session() as sess:
sess.trust_env = False
with sess.get(url, timeout=10) as response:
response.raise_for_status()
text = response.text.strip()
if not text or text == "[]":
return []
@@ -83,10 +84,11 @@ class XiequAdapter(BaseWhitelistAdapter):
"""添加IP到白名单。"""
try:
url = self._build_url(act="add", ip=ip, meno=self.memo)
response = requests.get(url, timeout=10)
response.raise_for_status()
text = response.text.strip()
with requests.Session() as sess:
sess.trust_env = False
with sess.get(url, timeout=10) as response:
response.raise_for_status()
text = response.text.strip()
logger.debug(f"添加白名单响应: {text}")
if "ok" in text.lower() or "success" in text.lower() or "添加成功" in text:
@@ -128,10 +130,11 @@ class XiequAdapter(BaseWhitelistAdapter):
"""删除白名单IP。"""
try:
url = self._build_url(act="del", ip=ip)
response = requests.get(url, timeout=10)
response.raise_for_status()
text = response.text.strip()
with requests.Session() as sess:
sess.trust_env = False
with sess.get(url, timeout=10) as response:
response.raise_for_status()
text = response.text.strip()
logger.debug(f"删除白名单响应: {text}")
if "ok" in text.lower() or "success" in text.lower() or "删除成功" in text:
+15 -12
View File
@@ -102,10 +102,11 @@ class XkdailiAdapter(BaseWhitelistAdapter):
"""添加IP到白名单。多个IP用英文逗号隔开。"""
try:
url = self._build_url(type="addwhiteip", ip=ip)
response = requests.get(url, timeout=10)
response.raise_for_status()
text = response.text.strip()
with requests.Session() as sess:
sess.trust_env = False
with sess.get(url, timeout=10) as response:
response.raise_for_status()
text = response.text.strip()
logger.debug(f"星空添加白名单响应: {text}")
ok, msg = self._parse_response(text)
@@ -123,10 +124,11 @@ class XkdailiAdapter(BaseWhitelistAdapter):
"""删除白名单IP。多个IP用英文逗号隔开。"""
try:
url = self._build_url(type="delwhiteip", ip=ip)
response = requests.get(url, timeout=10)
response.raise_for_status()
text = response.text.strip()
with requests.Session() as sess:
sess.trust_env = False
with sess.get(url, timeout=10) as response:
response.raise_for_status()
text = response.text.strip()
logger.debug(f"星空删除白名单响应: {text}")
ok, msg = self._parse_response(text)
@@ -145,10 +147,11 @@ class XkdailiAdapter(BaseWhitelistAdapter):
try:
# 尝试添加无效IP来验证认证
url = self._build_url(type="addwhiteip", ip="0.0.0.0")
response = requests.get(url, timeout=10)
response.raise_for_status()
text = response.text.strip()
with requests.Session() as sess:
sess.trust_env = False
with sess.get(url, timeout=10) as response:
response.raise_for_status()
text = response.text.strip()
ok, msg = self._parse_response(text)
if ok: