Merge pull request #179 from Shinku-Chen/tg-proxy

增加TG代理选项
This commit is contained in:
Cita
2026-03-20 11:40:29 +08:00
committed by GitHub
7 changed files with 213 additions and 20 deletions
@@ -55,6 +55,7 @@ describe('settings and auth events', () => {
(config as any).telegramApiBaseUrl = 'https://api.telegram.org';
(config as any).telegramBotToken = '';
(config as any).telegramChatId = '';
(config as any).telegramUseSystemProxy = false;
});
afterAll(async () => {
@@ -219,6 +220,32 @@ describe('settings and auth events', () => {
expect(body.message).toContain('Telegram API Base URL');
});
it('persists and returns telegram use system proxy from runtime settings', async () => {
const updateResponse = await app.inject({
method: 'PUT',
url: '/api/settings/runtime',
payload: {
telegramUseSystemProxy: true,
},
});
expect(updateResponse.statusCode).toBe(200);
const updated = updateResponse.json() as { telegramUseSystemProxy?: boolean };
expect(updated.telegramUseSystemProxy).toBe(true);
expect((config as any).telegramUseSystemProxy).toBe(true);
const saved = await db.select().from(schema.settings).where(eq(schema.settings.key, 'telegram_use_system_proxy')).get();
expect(saved?.value).toBe(JSON.stringify(true));
const getResponse = await app.inject({
method: 'GET',
url: '/api/settings/runtime',
});
expect(getResponse.statusCode).toBe(200);
const runtime = getResponse.json() as { telegramUseSystemProxy?: boolean };
expect(runtime.telegramUseSystemProxy).toBe(true);
});
it('persists and returns routing fallback unit cost from runtime settings', async () => {
const updateResponse = await app.inject({
method: 'PUT',
+17 -2
View File
@@ -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 开关');