feat(bot): TELEGRAM_API_ROOT + TELEGRAM_PROXY_SECRET for reverse-proxy setups#92
Conversation
|
@avfirsov thanks for contribution! Please update readme file and add new env variables there. Also these two variables should be near TELEGRAM_PROXY_URL in env.example |
Per @grinev review on PR grinev#92: * Add both env vars to the Environment Variables table, in the Telegram block right after TELEGRAM_PROXY_URL. * Add a new "Reverse Proxy (Optional)" subsection under Configuration with motivation, .env snippet, and a copy-pasteable nginx config. * Resync README.md to current main (gains /detach, /ls, /mcps, TTS provider columns, OPENCODE_AUTO_RESTART_ENABLED, etc.) so the diff is clean against main.
|
@grinev thanks for the review — addressed both points and rebased on current Review feedback
Drive-by — rebased the branch Branch was
Source files ( PR is now |
|
@avfirsov thanks for the update. Could you please try to apply changes on the clean main branch? Right now the branch still diverges from an old base commit, so the PR diff includes changes that are already present in main. This makes the review hard to follow and also mixes the reverse-proxy changes with unrelated history. After a clean rebase, the PR should ideally show only the TELEGRAM_API_ROOT / TELEGRAM_PROXY_SECRET changes and the related docs updates. |
55a0627 to
f07b968
Compare
|
@grinev fair point — done. Force-pushed a clean rebase: branch is now exactly one commit ( Diff against main is now strictly the reverse-proxy feature: No more (Side note: GitHub's PR view of the previous state was a bit deceptive — it counted lines that already existed on |
|
@avfirsov the PR history looks clean now: I see one commit on top I still have a few requests before merge:
|
…y setups Adds two new environment variables for routing Telegram Bot API calls and file downloads through a custom HTTPS reverse-proxy (e.g. nginx proxying api.telegram.org) with an optional shared-secret header. * TELEGRAM_API_ROOT replaces https://api.telegram.org for both Bot API calls (via grammY's client.apiRoot) and file downloads. Defaults to empty -> existing behaviour preserved. Trailing slashes are stripped at config load (grammY rejects apiRoot ending with '/'). * TELEGRAM_PROXY_SECRET, if set, is sent as the X-Proxy-Secret header on every Bot API call and every file-download fetch so the reverse proxy can authorize callers. Defaults to empty. Startup validation: * TELEGRAM_PROXY_URL and TELEGRAM_API_ROOT cannot be combined; they are alternative connectivity modes (forward vs reverse proxy). * TELEGRAM_PROXY_SECRET requires TELEGRAM_API_ROOT to avoid sending the secret header to api.telegram.org. Touched: src/config.ts (apiRoot/proxySecret + buildTelegramConfig with validation), src/bot/index.ts (apiRoot wired into grammY client + X-Proxy-Secret via fetch wrapper), src/bot/utils/file-download.ts and src/bot/handlers/voice.ts (file URL base + secret header). Tests: tests/config.test.ts covers validation + trailing-slash normalization. tests/bot/utils/file-download.test.ts covers default URL base, custom apiRoot URL base, slash-tolerant URL build, and X-Proxy-Secret injection on/off. node-fetch is now a direct dependency rather than a transitive one through grammY, since src/bot/index.ts imports it explicitly. Docs: .env.example block lives next to TELEGRAM_PROXY_URL; README gains the two env vars in the Environment Variables table and a new 'Reverse Proxy (Optional)' subsection with a copy-pasteable nginx config.
f07b968 to
7a32b0b
Compare
|
@grinev all four points addressed and rebased onto current 1. Startup validation —
2. 3. Trailing-slash normalization — done once at config load: 4. Tests — added focused coverage:
Aside: I exported Diff summary against main: Local |
|
@avfirsov thanks for contribution! |
|
@grinev спасибо за быстрый мерж и за внимательное ревью — все четыре пункта были по делу, фича стала чище. ❤️ |
|
@avfirsov спасибо что довел до конца ПР, фича реально полезная в нынешних реалиях. |
Summary
Adds two new environment variables for routing Telegram Bot API calls and file downloads through a custom HTTPS reverse-proxy (e.g. nginx proxying
api.telegram.org) with an optional shared-secret header.TELEGRAM_API_ROOT— replaceshttps://api.telegram.orgfor both Bot API calls (via grammY'sclient.apiRoot) and file downloads. Defaults to empty → existing behaviour preserved.TELEGRAM_PROXY_SECRET— if set,X-Proxy-Secretheader is sent on every request so the reverse proxy can authorise callers. Defaults to empty.Motivation
Corporate networks frequently block
api.telegram.orgat the DNS/IP level but allow the operator's own HTTPS endpoint. The existingTELEGRAM_PROXY_URLcovers the SOCKS/HTTP-CONNECT forward-proxy case (tunnel TCP toapi.telegram.orgvia a proxy). This PR covers the orthogonal reverse-proxy / URL-rewrite case: the bot connects directly to a reverse proxy that forwards toapi.telegram.orgserver-side, with a shared secret gating access.Typical nginx config on the operator's VPS:
Bot-side
.env:Changes
src/config.ts— newtelegram.apiRoot+telegram.proxySecretfields.src/bot/index.ts— passapiRootinto grammY'sclientoptions; injectX-Proxy-SecretviabaseFetchConfig.headers. Composes with the existingTELEGRAM_PROXY_URLpath.src/bot/utils/file-download.ts— derive the file URL base fromapiRoot; add the secret header to thefetch()call.src/bot/handlers/voice.ts— same for the rawhttp.get()code path that downloads voice files (URL base + header)..env.example— documents the two new variables with a short motivation.Note on branch contents
This branch also carries a second commit that pins
remarkto14.0.3to fix an unrelatedERR_REQUIRE_ESMcrash on Node 20 — I've opened that as a separate single-commit PR #93 so it can be reviewed and merged independently on its own merits. If #93 lands first, GitHub will auto-close that commit here and this PR will rebase cleanly. If you prefer I drop the remark commit from this branch while #93 is under review, let me know and I'll force-push.Backward compatibility
Both new env vars default to empty. When unset, the bot behaves exactly as before: base URL stays
https://api.telegram.org, no extra headers, no logs. Existing installs are unaffected.Test plan
api.telegram.orgdirectly.TELEGRAM_API_ROOTset — Bot API calls go to the new root; file downloads use the new root.TELEGRAM_PROXY_SECRETset — header is attached to API calls AND to both file-download code paths.if ($http_x_proxy_secret != "suckit") { return 403; }— getMe/sendMessage/getFile all round-trip correctly.Notes
Composition with
TELEGRAM_PROXY_URL: the reverse-proxy and forward-proxy features are orthogonal and can be used together (forward proxy to reach the reverse proxy, though that's an unusual combination). No code path conflict.