Skip to content

fix(rate-limits): route Claude OAuth usage fetch through HTTP proxy - #803

Merged
Jinwoo-H merged 2 commits into
stablyai:mainfrom
nengqi:fix/claude-usage-respect-http-proxy
Apr 19, 2026
Merged

Jinwoo-H merged 2 commits into
stablyai:mainfrom
nengqi:fix/claude-usage-respect-http-proxy

Conversation

@nengqi

@nengqi nengqi commented Apr 18, 2026

Copy link
Copy Markdown
Contributor

Summary

Node's built-in fetch (undici) does not honor HTTP_PROXY / HTTPS_PROXY / ALL_PROXY envvars unless a dispatcher is passed explicitly. Users in regions where api.anthropic.com is only reachable via proxy saw two problems:

  1. Usage indicator silently fails — the bottom-left Claude usage bar shows nothing / error.
  2. More importantly, Orca hits Anthropic from an unexpected IP, which can trip anti-abuse / rate-limit signals on the user's OAuth account — a hard-to-reverse consequence for what is a cosmetic feature.

src/main/rate-limits/claude-fetcher.ts now resolves a proxy dispatcher from standard envvars and attaches it to the OAuth usage fetch.

Changes

  • Add resolveProxyDispatcher(env) that reads, in precedence order:
    HTTPS_PROXY → https_proxy → ALL_PROXY → all_proxy → HTTP_PROXY → http_proxy
  • Invalid proxy URLs degrade to direct connection rather than crashing polling (the usage bar is cosmetic; a typo'd envvar should not take down rate-limit polling).
  • Wire the resolved dispatcher into fetchViaOAuth().
  • Add undici as an explicit dependency (it is already shipped with Node ≥18 / Electron; adding it lets us import { ProxyAgent } from 'undici' with types).
  • 6 unit tests covering presence, precedence, case-insensitivity, and invalid-URL fallback.

Fixes

Follow-up (not in this PR)

Some users (e.g. #521 reporter with Shadowrocket, or Clash users on macOS) run their proxy as a GUI app and don't export envvars from a shell — a GUI-launched Orca won't inherit those envvars even if .zshrc has them. A Settings UI proxy field (overriding envvars when set) would close this gap. I'm happy to submit that as a follow-up PR once this lands — it's a larger change (settings schema + preferences store + form) and splitting keeps review scope tight.

Test Plan

  • pnpm typecheck:node passes
  • pnpm exec vitest run src/main/rate-limits/claude-fetcher.test.ts — 6/6 green
  • pnpm exec vitest run src/main/rate-limits/ — 10/10 green (no regression)
  • pnpm exec oxlint src/main/rate-limits/claude-fetcher.{ts,test.ts} — 0 warnings / 0 errors

Manual verification by end user (me) once a release build is available:

  • Set HTTPS_PROXY=http://127.0.0.1:7890 before launching Orca; usage bar should load.
  • Unset; usage bar should behave as before (direct fetch).

🤖 Generated with Claude Code

Node's built-in fetch (undici) does not honor HTTP_PROXY / HTTPS_PROXY
envvars unless a dispatcher is passed explicitly. Users in regions where
api.anthropic.com is only reachable via proxy saw the usage indicator
silently fail, and — more importantly — Orca was hitting Anthropic from
an unexpected IP, which can trip anti-abuse / rate-limit signals on the
user's OAuth account.

Adds resolveProxyDispatcher() that reads HTTPS_PROXY / ALL_PROXY /
HTTP_PROXY (upper and lower case) and attaches an undici ProxyAgent to
the usage fetch. Invalid proxy URLs degrade to direct connection rather
than crashing polling, since the usage bar is cosmetic.

Follow-up (not in this PR): a Settings UI field to configure the proxy
for users whose proxy lives in a GUI proxy app (Shadowrocket, Clash)
rather than shell envvars.

Fixes stablyai#521
Fixes stablyai#800
Replace undici dependency with Electron's net.fetch and bridge env var
proxies via session.defaultSession.setProxy(). This aligns with the
existing codebase pattern (updater, changelog, feedback all use net.fetch)
and covers both OS-level proxy and env var proxy users without adding
a new dependency.
@Jinwoo-H

Copy link
Copy Markdown
Contributor

Thanks for the PR! I pushed a commit that swaps the approach slightly: instead of adding undici, it uses Electron's net.fetch() + session.defaultSession.setProxy() to bridge the env vars. Main reason is the rest of the codebase already uses net.fetch for main-process HTTP, which gets us OS-level proxy and cert support for free on top of the env var support you added. No new deps needed.

@Jinwoo-H
Jinwoo-H merged commit 613c5ab into stablyai:main Apr 19, 2026
1 check passed
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants