diff --git a/src/server/config.ts b/src/server/config.ts index e0c5ec7..e8dc66c 100644 --- a/src/server/config.ts +++ b/src/server/config.ts @@ -73,6 +73,7 @@ export function buildConfig(env: NodeJS.ProcessEnv) { telegramApiBaseUrl: 'https://api.telegram.org', telegramBotToken: env.TELEGRAM_BOT_TOKEN || '', telegramChatId: env.TELEGRAM_CHAT_ID || '', + telegramUseSystemProxy: parseBoolean(env.TELEGRAM_USE_SYSTEM_PROXY, false), smtpEnabled: parseBoolean(env.SMTP_ENABLED, false), smtpHost: env.SMTP_HOST || '', smtpPort: parseInt(env.SMTP_PORT || '587'), diff --git a/src/server/routes/api/settings.ts b/src/server/routes/api/settings.ts index 6bb9628..07b7743 100644 --- a/src/server/routes/api/settings.ts +++ b/src/server/routes/api/settings.ts @@ -1,4 +1,4 @@ -import { FastifyInstance } from 'fastify'; +import { FastifyInstance } from 'fastify'; import cron from 'node-cron'; import { config } from '../../config.js'; import { db, runtimeDbDialect, schema } from '../../db/index.js'; @@ -43,6 +43,7 @@ interface RuntimeSettingsBody { telegramApiBaseUrl?: string; telegramBotToken?: string; telegramChatId?: string; + telegramUseSystemProxy?: boolean; smtpEnabled?: boolean; smtpHost?: string; smtpPort?: number; @@ -288,6 +289,10 @@ function applyImportedSettingToRuntime(key: string, value: unknown) { config.telegramChatId = value.trim(); return; } + case 'telegram_use_system_proxy': { + config.telegramUseSystemProxy = !!value; + return; + } case 'smtp_enabled': { config.smtpEnabled = !!value; return; @@ -380,6 +385,7 @@ function getRuntimeSettingsResponse(currentAdminIp = '') { telegramApiBaseUrl: config.telegramApiBaseUrl, telegramBotTokenMasked: maskSecret(config.telegramBotToken), telegramChatId: config.telegramChatId, + telegramUseSystemProxy: config.telegramUseSystemProxy, smtpEnabled: config.smtpEnabled, smtpHost: config.smtpHost, smtpPort: config.smtpPort, @@ -512,7 +518,8 @@ export async function settingsRoutes(app: FastifyInstance) { const telegramTouched = body.telegramEnabled !== undefined || body.telegramApiBaseUrl !== undefined || body.telegramBotToken !== undefined - || body.telegramChatId !== undefined; + || body.telegramChatId !== undefined + || body.telegramUseSystemProxy !== undefined; const nextTelegramEnabled = body.telegramEnabled !== undefined ? !!body.telegramEnabled : config.telegramEnabled; @@ -768,6 +775,14 @@ export async function settingsRoutes(app: FastifyInstance) { upsertSetting('telegram_chat_id', config.telegramChatId); } + if (body.telegramUseSystemProxy !== undefined) { + if (!!body.telegramUseSystemProxy !== config.telegramUseSystemProxy) { + changedLabels.push('Telegram 使用系统代理'); + } + config.telegramUseSystemProxy = !!body.telegramUseSystemProxy; + upsertSetting('telegram_use_system_proxy', config.telegramUseSystemProxy); + } + if (body.smtpEnabled !== undefined) { if (!!body.smtpEnabled !== config.smtpEnabled) { changedLabels.push('SMTP 开关'); diff --git a/src/server/services/notifyService.ts b/src/server/services/notifyService.ts index c1e9888..7af5201 100644 --- a/src/server/services/notifyService.ts +++ b/src/server/services/notifyService.ts @@ -1,5 +1,6 @@ import { fetch } from 'undici'; import { config } from '../config.js'; +import { withExplicitProxyRequestInit } from './siteProxy.js'; import nodemailer, { type Transporter } from 'nodemailer'; import { createNotificationSignature, @@ -224,15 +225,19 @@ export async function sendNotification( tasks.push({ channel: 'telegram', run: async () => { - const response = await fetch(telegramApiUrl, { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ - chat_id: config.telegramChatId, - text, - disable_web_page_preview: true, - }), - }); + const telegramRequestInit = withExplicitProxyRequestInit( + config.telegramUseSystemProxy ? config.systemProxyUrl : null, + { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + chat_id: config.telegramChatId, + text, + disable_web_page_preview: true, + }), + }, + ); + const response = await fetch(telegramApiUrl, telegramRequestInit); if (!response.ok) { throw new Error(`Telegram 响应状态 ${response.status}`); } diff --git a/src/web/pages/NotificationSettings.tsx b/src/web/pages/NotificationSettings.tsx index 75dc1e2..c0917fc 100644 --- a/src/web/pages/NotificationSettings.tsx +++ b/src/web/pages/NotificationSettings.tsx @@ -12,6 +12,7 @@ type RuntimeSettings = { telegramEnabled: boolean; telegramApiBaseUrl: string; telegramChatId: string; + telegramUseSystemProxy: boolean; smtpEnabled: boolean; smtpHost: string; smtpPort: number; @@ -35,6 +36,7 @@ export default function NotificationSettings() { telegramEnabled: false, telegramApiBaseUrl: 'https://api.telegram.org', telegramChatId: '', + telegramUseSystemProxy: false, smtpEnabled: false, smtpHost: '', smtpPort: 587, @@ -78,6 +80,7 @@ export default function NotificationSettings() { telegramEnabled: !!runtimeInfo.telegramEnabled, telegramApiBaseUrl: runtimeInfo.telegramApiBaseUrl || 'https://api.telegram.org', telegramChatId: runtimeInfo.telegramChatId || '', + telegramUseSystemProxy: !!runtimeInfo.telegramUseSystemProxy, smtpEnabled: !!runtimeInfo.smtpEnabled, smtpHost: runtimeInfo.smtpHost || '', smtpPort: Number(runtimeInfo.smtpPort) || 587, @@ -115,6 +118,7 @@ export default function NotificationSettings() { telegramEnabled: runtime.telegramEnabled, telegramApiBaseUrl: runtime.telegramApiBaseUrl, telegramChatId: runtime.telegramChatId, + telegramUseSystemProxy: runtime.telegramUseSystemProxy, smtpEnabled: runtime.smtpEnabled, smtpHost: runtime.smtpHost, smtpPort: runtime.smtpPort, @@ -317,15 +321,26 @@ export default function NotificationSettings() { - +