Merge pull request #169 from Panniantong/fix/reddit-doctor-user-agent
fix(reddit): add User-Agent to doctor health check to avoid false 403
This commit is contained in:
@@ -1,9 +1,24 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""Reddit — check if proxy and credentials are configured."""
|
||||
"""Reddit — check connectivity and proxy configuration."""
|
||||
|
||||
import os
|
||||
import urllib.request
|
||||
from .base import Channel
|
||||
|
||||
_UA = "agent-reach/1.0"
|
||||
_TIMEOUT = 10
|
||||
|
||||
|
||||
def _reddit_reachable() -> bool:
|
||||
"""Return True if Reddit JSON API responds with 200 (带 User-Agent)."""
|
||||
url = "https://www.reddit.com/r/linux.json?limit=1"
|
||||
req = urllib.request.Request(url, headers={"User-Agent": _UA})
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=_TIMEOUT) as resp:
|
||||
return resp.status == 200
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
class RedditChannel(Channel):
|
||||
name = "reddit"
|
||||
@@ -20,7 +35,10 @@ class RedditChannel(Channel):
|
||||
proxy = (config.get("reddit_proxy") if config else None) or os.environ.get("REDDIT_PROXY")
|
||||
if proxy:
|
||||
return "ok", "代理已配置,可读取帖子。搜索走 Exa"
|
||||
# 实际探测连通性(带 User-Agent,符合 Reddit API 要求)
|
||||
if _reddit_reachable():
|
||||
return "ok", "直连可用(JSON API 响应正常)。搜索走 Exa"
|
||||
return "warn", (
|
||||
"无代理。服务器 IP 可能被 Reddit 封锁。配置代理:\n"
|
||||
"无代理且 Reddit JSON API 无响应。服务器 IP 可能被封锁。配置代理:\n"
|
||||
" agent-reach configure proxy http://user:pass@ip:port"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user