GeezerVison is showing github.com — fix: retry webfetch with simple UA on 403 (#10328) · bpd1069/opencode@b978ca1
File treeExpand file treeCollapse file tree Open diff view settings packages/opencode/src/tool
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/tool/webfetch.ts +15-9Lines changed: 15 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -56,15 +56,21 @@ export const WebFetchTool = Tool.define("webfetch", { | 56 | 56 | "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8" | 57 | 57 | } | 58 | 58 |
| 59 | | - const response = await fetch(params.url, { | 60 | | - signal: AbortSignal.any([controller.signal, ctx.abort]), | 61 | | - headers: { | 62 | | - "User-Agent": | 63 | | - "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36", | 64 | | - Accept: acceptHeader, | 65 | | - "Accept-Language": "en-US,en;q=0.9", | 66 | | - }, | 67 | | - }) | | 59 | + const signal = AbortSignal.any([controller.signal, ctx.abort]) | | 60 | + const headers = { | | 61 | + "User-Agent": | | 62 | + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36", | | 63 | + Accept: acceptHeader, | | 64 | + "Accept-Language": "en-US,en;q=0.9", | | 65 | + } | | 66 | + | | 67 | + const initial = await fetch(params.url, { signal, headers }) | | 68 | + | | 69 | + // Retry with honest UA if blocked by Cloudflare bot detection (TLS fingerprint mismatch) | | 70 | + const response = | | 71 | + initial.status === 403 && initial.headers.get("cf-mitigated") === "challenge" | | 72 | + ? await fetch(params.url, { signal, headers: { ...headers, "User-Agent": "opencode" } }) | | 73 | + : initial | 68 | 74 |
| 69 | 75 | clearTimeout(timeoutId) | 70 | 76 |
| |
|