mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-10-08 23:22:09 +03:00
eec030f86f
* feat(notifications): event bus architecture with Telegram and SMTP subscribers
- Event bus core with buffered channel, fan-out, panic recovery
- Telegram subscriber with HTML formatting and rate limiting
- Email subscriber with SMTP/TLS/STARTTLS support and stage diagnostics
- 5 event types: outbound.down/up, xray.crash, cpu.high, login.attempt
- CPU threshold checks per subscriber (tgCpu for TG, smtpCpu for Email)
- SystemMetricData struct for raw metric values in events
- i18n keys for en-US, ru-RU, and English defaults for other locales
* fix
* fix(notifications): repair crash/CPU alerts, harden secrets, add node alerts
Bug fixes:
- Xray crash notifications were permanently suppressed after the first crash:
XrayStateTracker latched state="down" with no reset and no recovery event,
so only the first crash per process lifetime ever notified. Removed the
tracker; the existing 1/min rate limiter already dedupes crash-loop spam.
- Email CPU alerts could never fire unless Telegram was also enabled, because
the CPU job was registered only inside the tgbot block. Register it whenever
either Telegram or SMTP wants cpu.high (new cpuAlarmWanted gate) and relax
the cadence to @every 1m (cpu.Percent already samples over a full minute).
- SMTP password (and, pre-existing, all other secrets) were shipped to the
browser in plaintext: GetAllSettingView was dead code and /setting/all
returned the raw model. Wire getAllSetting -> GetAllSettingView, redact
smtpPassword with a hasSmtpPassword presence flag, and preserve it on blank
save. Closes the leak for tgBotToken/ldapPassword/2FA token too.
Polish:
- email Send: use nil SMTP auth when no credentials (Go refuses PlainAuth over
the unencrypted "none" transport).
- Remove unused EventClientDepleted; fix inaccurate bus.go doc comments; drop
stale tgBotLoginNotify from the frontend schema; gofmt alignment.
Feature - node online/offline alerts:
- Emit node.down/node.up from the heartbeat job on a real status transition
(with a startup-spam guard), reusing NodeHealthData. Formatted by both the
Telegram and email subscribers and selectable in the settings UI.
Regenerated frontend types (hasSmtpPassword). New i18n keys added to en-US;
other locales fall back to English (bundle default) until translated.
* fix(settings): use antd Space orientation instead of deprecated direction
Ant Design 6 deprecated Space's `direction` prop in favor of `orientation`,
which logged a console warning from the Telegram/Email notification tabs. Brings
these two tabs in line with the rest of the codebase, which already uses
`orientation`.
* i18n(notifications): translate the notification feature into all locales
The notifications PR shipped ~99 new strings (SMTP settings, event labels,
Telegram/email message templates) as English placeholders in every non-English
locale. Translate them — plus the node-alert keys added during this review —
into all 12 locales: Arabic, Spanish, Persian, Indonesian, Japanese,
Portuguese-BR, Russian, Turkish, Ukrainian, Vietnamese, and Simplified/
Traditional Chinese.
Go-template placeholders ({{ .Tag }}, {{ .Name }}, etc.) are preserved exactly;
tgbot message values carry no leading status emoji (the bot/email code adds
those, so an emoji in the value would duplicate it); product/protocol names
(SMTP, STARTTLS, TLS, CPU, Xray, Telegram) are kept as-is.
---------
Co-authored-by: Sanaei <ho3ein.sanaei@gmail.com>
261 lines
10 KiB
TypeScript
261 lines
10 KiB
TypeScript
import { useMemo, useState } from 'react';
|
|
import { useTranslation } from 'react-i18next';
|
|
import { Alert, Button, Input, InputNumber, Select, Space, Switch, Tabs } from 'antd';
|
|
import { BellOutlined, SendOutlined, SettingOutlined } from '@ant-design/icons';
|
|
import { LanguageManager } from '@/utils';
|
|
import { HttpUtil } from '@/utils';
|
|
import type { AllSetting } from '@/models/setting';
|
|
import { SettingListItem, EventBusCheckboxes } from '@/components/ui';
|
|
import { useMediaQuery } from '@/hooks/useMediaQuery';
|
|
import { catTabLabel } from './catTabLabel';
|
|
|
|
interface TelegramTabProps {
|
|
allSetting: AllSetting;
|
|
updateSetting: (patch: Partial<AllSetting>) => void;
|
|
}
|
|
|
|
// The notification schedule is fed straight to robfig/cron's AddJob (see
|
|
// web.go startTask), which accepts @every <duration>, the @hourly/@daily/...
|
|
// macros, and full crontab expressions. This builder covers the common cases
|
|
// with dropdowns so users don't have to memorise the syntax, while "Custom"
|
|
// preserves the raw crontab escape hatch.
|
|
type Unit = 's' | 'm' | 'h';
|
|
type Macro = '@hourly' | '@daily' | '@weekly' | '@monthly';
|
|
type Mode = 'every' | Macro | 'custom';
|
|
const MACROS: Macro[] = ['@hourly', '@daily', '@weekly', '@monthly'];
|
|
const EVERY_RE = /^@every\s+(\d+)\s*([smh])$/i;
|
|
|
|
interface RunTime {
|
|
mode: Mode;
|
|
num: number;
|
|
unit: Unit;
|
|
custom: string;
|
|
}
|
|
|
|
function parseRunTime(raw: string): RunTime {
|
|
const v = (raw ?? '').trim();
|
|
const m = v.match(EVERY_RE);
|
|
if (m) {
|
|
return { mode: 'every', num: Math.max(1, Number(m[1]) || 1), unit: m[2].toLowerCase() as Unit, custom: '' };
|
|
}
|
|
if ((MACROS as string[]).includes(v)) {
|
|
return { mode: v as Macro, num: 1, unit: 'h', custom: '' };
|
|
}
|
|
return { mode: 'custom', num: 1, unit: 'h', custom: v };
|
|
}
|
|
|
|
function composeRunTime(s: RunTime): string {
|
|
if (s.mode === 'every') return `@every ${Math.max(1, s.num || 1)}${s.unit}`;
|
|
if (s.mode === 'custom') return s.custom;
|
|
return s.mode;
|
|
}
|
|
|
|
// The panel's cron runs with seconds enabled (cron.WithSeconds() in web.go), so
|
|
// crontab expressions are 6-field: "second minute hour day month weekday". When
|
|
// the user drops into Custom we seed the box with the crontab equivalent of the
|
|
// current selection rather than a bare @macro, so they get a real expression to
|
|
// edit (and one that the 6-field parser accepts).
|
|
function toCrontab(s: RunTime): string {
|
|
switch (s.mode) {
|
|
case '@hourly': return '0 0 * * * *';
|
|
case '@daily': return '0 0 0 * * *';
|
|
case '@weekly': return '0 0 0 * * 0';
|
|
case '@monthly': return '0 0 0 1 * *';
|
|
case 'every': {
|
|
const n = Math.max(1, s.num || 1);
|
|
if (s.unit === 's') return `*/${n} * * * * *`;
|
|
if (s.unit === 'm') return `0 */${n} * * * *`;
|
|
return `0 0 */${n} * * *`;
|
|
}
|
|
default: return s.custom;
|
|
}
|
|
}
|
|
|
|
function NotifyTimeField({ value, onChange }: { value: string; onChange: (v: string) => void }) {
|
|
const { t } = useTranslation();
|
|
// Init once: the Settings tabs only mount after settings are fetched, so the
|
|
// incoming value is already the persisted one.
|
|
const [state, setState] = useState<RunTime>(() => parseRunTime(value));
|
|
|
|
function update(patch: Partial<RunTime>) {
|
|
const next = { ...state, ...patch };
|
|
setState(next);
|
|
onChange(composeRunTime(next));
|
|
}
|
|
|
|
function onModeChange(mode: Mode) {
|
|
// Seed Custom with the crontab equivalent of the current selection so the
|
|
// box starts from a real expression (e.g. "0 0 0 * * *", not "@daily").
|
|
if (mode === 'custom' && !state.custom.trim()) {
|
|
update({ mode, custom: toCrontab(state) });
|
|
} else {
|
|
update({ mode });
|
|
}
|
|
}
|
|
|
|
const modeOptions = [
|
|
{ value: 'every', label: t('pages.settings.notifyTime.every') },
|
|
{ value: '@hourly', label: t('pages.settings.notifyTime.hourly') },
|
|
{ value: '@daily', label: t('pages.settings.notifyTime.daily') },
|
|
{ value: '@weekly', label: t('pages.settings.notifyTime.weekly') },
|
|
{ value: '@monthly', label: t('pages.settings.notifyTime.monthly') },
|
|
{ value: 'custom', label: t('pages.settings.notifyTime.custom') },
|
|
];
|
|
const unitOptions = [
|
|
{ value: 's', label: t('pages.settings.notifyTime.seconds') },
|
|
{ value: 'm', label: t('pages.settings.notifyTime.minutes') },
|
|
{ value: 'h', label: t('pages.settings.notifyTime.hours') },
|
|
];
|
|
|
|
return (
|
|
<Space orientation="vertical" size="small" style={{ width: '100%' }}>
|
|
<Select<Mode>
|
|
style={{ width: '100%' }}
|
|
value={state.mode}
|
|
options={modeOptions}
|
|
onChange={onModeChange}
|
|
/>
|
|
{state.mode === 'every' && (
|
|
<Space.Compact style={{ width: '100%' }}>
|
|
<InputNumber
|
|
min={1}
|
|
style={{ width: '50%' }}
|
|
value={state.num}
|
|
onChange={(v) => update({ num: Math.max(1, Number(v) || 1) })}
|
|
/>
|
|
<Select<Unit>
|
|
style={{ width: '50%' }}
|
|
value={state.unit}
|
|
options={unitOptions}
|
|
onChange={(unit) => update({ unit })}
|
|
/>
|
|
</Space.Compact>
|
|
)}
|
|
{state.mode === 'custom' && (
|
|
<Input
|
|
value={state.custom}
|
|
placeholder="0 30 8 * * *"
|
|
onChange={(e) => update({ custom: e.target.value })}
|
|
/>
|
|
)}
|
|
</Space>
|
|
);
|
|
}
|
|
|
|
export default function TelegramTab({ allSetting, updateSetting }: TelegramTabProps) {
|
|
const { t } = useTranslation();
|
|
const { isMobile } = useMediaQuery();
|
|
const [testLoading, setTestLoading] = useState(false);
|
|
const [testResult, setTestResult] = useState<{ success: boolean; msg: string } | null>(null);
|
|
|
|
async function handleTestTgBot() {
|
|
setTestLoading(true);
|
|
setTestResult(null);
|
|
try {
|
|
const res = await HttpUtil.post('/panel/api/setting/testTgBot') as { success?: boolean; msg?: string };
|
|
setTestResult({ success: !!res.success, msg: res.msg || '' });
|
|
} catch (e: unknown) {
|
|
setTestResult({ success: false, msg: e instanceof Error ? e.message : t('pages.settings.requestFailed') });
|
|
} finally {
|
|
setTestLoading(false);
|
|
}
|
|
}
|
|
|
|
const langOptions = useMemo(
|
|
() => LanguageManager.supportedLanguages.map((l: { value: string; name: string; icon: string }) => ({
|
|
value: l.value,
|
|
label: (
|
|
<>
|
|
<span role="img" aria-label={l.name}>{l.icon}</span>
|
|
<span>{l.name}</span>
|
|
</>
|
|
),
|
|
})),
|
|
[],
|
|
);
|
|
|
|
return (
|
|
<Tabs defaultActiveKey="1" items={[
|
|
{
|
|
key: '1',
|
|
label: catTabLabel(<SettingOutlined />, t('pages.settings.panelSettings'), isMobile),
|
|
children: (
|
|
<>
|
|
<SettingListItem paddings="small" title={t('pages.settings.telegramBotEnable')} description={t('pages.settings.telegramBotEnableDesc')}>
|
|
<Switch checked={allSetting.tgBotEnable} onChange={(v) => updateSetting({ tgBotEnable: v })} />
|
|
</SettingListItem>
|
|
|
|
<SettingListItem
|
|
paddings="small"
|
|
title={t('pages.settings.telegramToken')}
|
|
description={allSetting.hasTgBotToken ? t('pages.settings.telegramTokenConfigured') : t('pages.settings.telegramTokenDesc')}
|
|
>
|
|
<Input.Password
|
|
value={allSetting.tgBotToken}
|
|
placeholder={allSetting.hasTgBotToken ? t('pages.settings.telegramTokenPlaceholder') : ''}
|
|
onChange={(e) => updateSetting({ tgBotToken: e.target.value })}
|
|
/>
|
|
</SettingListItem>
|
|
|
|
<SettingListItem paddings="small" title={t('pages.settings.telegramChatId')} description={t('pages.settings.telegramChatIdDesc')}>
|
|
<Input value={allSetting.tgBotChatId} onChange={(e) => updateSetting({ tgBotChatId: e.target.value })} />
|
|
</SettingListItem>
|
|
|
|
<SettingListItem paddings="small" title={t('pages.settings.telegramBotLanguage')}>
|
|
<Select
|
|
value={allSetting.tgLang}
|
|
onChange={(v) => updateSetting({ tgLang: v })}
|
|
style={{ width: '100%' }}
|
|
options={langOptions}
|
|
/>
|
|
</SettingListItem>
|
|
|
|
<SettingListItem paddings="small" title={t('pages.settings.telegramAPIServer')} description={t('pages.settings.telegramAPIServerDesc')}>
|
|
<Input value={allSetting.tgBotAPIServer} placeholder="https://api.example.com"
|
|
onChange={(e) => updateSetting({ tgBotAPIServer: e.target.value })} />
|
|
</SettingListItem>
|
|
|
|
<Space orientation="vertical" size={8} style={{ width: '100%', marginTop: 16 }}>
|
|
<Button type="primary" icon={<SendOutlined />} loading={testLoading} onClick={handleTestTgBot}>
|
|
{t('pages.settings.testTgBot')}
|
|
</Button>
|
|
{testResult && (
|
|
<Alert
|
|
type={testResult.success ? 'success' : 'error'}
|
|
message={testResult.msg}
|
|
showIcon
|
|
closable
|
|
onClose={() => setTestResult(null)}
|
|
/>
|
|
)}
|
|
</Space>
|
|
</>
|
|
),
|
|
},
|
|
{
|
|
key: '2',
|
|
label: catTabLabel(<BellOutlined />, t('pages.settings.notifications'), isMobile),
|
|
children: (
|
|
<>
|
|
<SettingListItem paddings="small" title={t('pages.settings.telegramNotifyTime')} description={t('pages.settings.telegramNotifyTimeDesc')}>
|
|
<NotifyTimeField value={allSetting.tgRunTime} onChange={(v) => updateSetting({ tgRunTime: v })} />
|
|
</SettingListItem>
|
|
<SettingListItem paddings="small" title={t('pages.settings.tgNotifyBackup')} description={t('pages.settings.tgNotifyBackupDesc')}>
|
|
<Switch checked={allSetting.tgBotBackup} onChange={(v) => updateSetting({ tgBotBackup: v })} />
|
|
</SettingListItem>
|
|
|
|
<SettingListItem paddings="small" title={t('pages.settings.tgEventBusNotify')} description={t('pages.settings.tgEventBusNotifyDesc')}>
|
|
<EventBusCheckboxes
|
|
value={allSetting.tgEnabledEvents}
|
|
onChange={(v) => updateSetting({ tgEnabledEvents: v })}
|
|
extra={{ 'cpu.high': { key: 'tgCpu', value: allSetting.tgCpu } }}
|
|
onExtraChange={(key, v) => updateSetting({ [key]: Number(v) || 0 })}
|
|
/>
|
|
</SettingListItem>
|
|
</>
|
|
),
|
|
},
|
|
]} />
|
|
);
|
|
}
|