Spaces:
Running
Running
Deploy 8b8ec9a từ GitHub Actions
Browse filesFiles changed (3)
6 unmodified lines78910111259 unmodified lines7273747576777854 unmodified lines1331341351361371381396 unmodified lines*/const API = (process.env.TELEGRAM_API_URL ?? 'https://api.telegram.org').replace(/\/$/, '');const TOKEN = process.env.TELEGRAM_BOT_TOKEN ?? '';const CHAT = process.env.TELEGRAM_CHAT_ID ?? '';/** phải < 100s (Cloudflare trả 524 nếu origin không phản hồi trong 100s) */const TIMEOUT_MS = Number(process.env.TELEGRAM_TIMEOUT_MS ?? 25_000);59 unmodified linesres = await fetch(`${API}/bot${TOKEN}/${method}`, {method: 'POST',body: isForm ? body : JSON.stringify(body),headers: isForm ? undefined : { 'content-type': 'application/json' },signal: AbortSignal.timeout(TIMEOUT_MS),});} catch (e) {54 unmodified linesexport async function downloadFile(fileId: string): Promise<Buffer> {const f = await call<{ file_path?: string; file_size?: number }>('getFile', { file_id: fileId });if (!f.file_path) throw new Error('Telegram không trả về đường dẫn file');const res = await fetch(`${API}/file/bot${TOKEN}/${f.file_path}`, { signal: AbortSignal.timeout(Math.max(TIMEOUT_MS, 120_000)) });if (!res.ok) throw new Error(`Tải ảnh từ Telegram lỗi HTTP ${res.status}`);return Buffer.from(await res.arrayBuffer());}6 unmodified lines78910111213141559 unmodified lines7576777879808154 unmodified lines1361371381391401411426 unmodified lines*/const API = (process.env.TELEGRAM_API_URL ?? 'https://api.telegram.org').replace(/\/$/, '');const TOKEN = process.env.TELEGRAM_BOT_TOKEN ?? '';/** Khóa gửi kèm khi gọi qua relay (TELEGRAM_API_URL trỏ tới Cloudflare Worker) — xem telegram-relay/ */const RELAY_KEY = process.env.TELEGRAM_RELAY_KEY ?? '';const relayHeaders = (): Record<string, string> => (RELAY_KEY ? { 'x-relay-key': RELAY_KEY } : {});const CHAT = process.env.TELEGRAM_CHAT_ID ?? '';/** phải < 100s (Cloudflare trả 524 nếu origin không phản hồi trong 100s) */const TIMEOUT_MS = Number(process.env.TELEGRAM_TIMEOUT_MS ?? 25_000);59 unmodified linesres = await fetch(`${API}/bot${TOKEN}/${method}`, {method: 'POST',body: isForm ? body : JSON.stringify(body),headers: { ...relayHeaders(), ...(isForm ? {} : { 'content-type': 'application/json' }) },signal: AbortSignal.timeout(TIMEOUT_MS),});} catch (e) {54 unmodified linesexport async function downloadFile(fileId: string): Promise<Buffer> {const f = await call<{ file_path?: string; file_size?: number }>('getFile', { file_id: fileId });if (!f.file_path) throw new Error('Telegram không trả về đường dẫn file');const res = await fetch(`${API}/file/bot${TOKEN}/${f.file_path}`, { headers: relayHeaders(), signal: AbortSignal.timeout(Math.max(TIMEOUT_MS, 120_000)) });if (!res.ok) throw new Error(`Tải ảnh từ Telegram lỗi HTTP ${res.status}`);return Buffer.from(await res.arrayBuffer());}
1234567891011121314151617181920212223242526272829/** * Relay Telegram Bot API cho backend chạy trên HF Space (HF chặn egress tới api.telegram.org). * Backend gọi: https://<relay>/bot<TOKEN>/<method> và /file/bot<TOKEN>/<path>, kèm header x-relay-key. * Chỉ chuyển tiếp 2 dạng đường dẫn trên tới api.telegram.org; không lưu gì, không log token. * Secret: RELAY_KEY (wrangler secret put RELAY_KEY). */const UPSTREAM = 'https://api.telegram.org';const ALLOWED = /^\/(file\/)?bot[^/]+\/[^/]+(\/.*)?$/;
function safeEqual(a, b) { if (typeof a !== 'string' || typeof b !== 'string' || a.length !== b.length) return false; let r = 0; for (let i = 0; i < a.length; i++) r |= a.charCodeAt(i) ^ b.charCodeAt(i); return r === 0;}
export default { async fetch(request, env) { if (!env.RELAY_KEY) return new Response('relay chưa cấu hình', { status: 503 }); if (!safeEqual(request.headers.get('x-relay-key') ?? '', env.RELAY_KEY)) return new Response('unauthorized', { status: 401 }); const url = new URL(request.url); if (!ALLOWED.test(url.pathname)) return new Response('not found', { status: 404 });
const headers = new Headers(request.headers); for (const h of ['x-relay-key', 'host', 'cf-connecting-ip', 'x-forwarded-for', 'x-real-ip']) headers.delete(h); const body = request.method === 'GET' || request.method === 'HEAD' ? undefined : request.body; return fetch(UPSTREAM + url.pathname + url.search, { method: request.method, headers, body, redirect: 'manual' }); },};1234567{ "$schema": "node_modules/wrangler/config-schema.json", "name": "telegram-relay", "main": "src/index.js", "compatibility_date": "2026-11-01", "observability": { "enabled": true }}passport-check/apps/server/src/telegram.ts+5-2
telegram-relay/src/index.jsAdded+29
telegram-relay/wrangler.jsoncAdded+7