Per user decision 2026-10-21 (reverses the 2026-10-20 drop-Telegram call): port the upstream 3x-ui v3.7.0-generation Telegram bot wholesale instead of rewriting a minimal set, adapted to this fork. - new internal/web/service/tgbot package (telego): bot lifecycle, commands, reports, callback router, send helpers; token validation via telego; custom API server URL passes the fork's SSRF sanitizer (loopback/private bases fall back to the default endpoint, logged) - settings: full upstream key set (tgBotEnable/Token/Proxy/APIServer/ ChatId, tgRunTime, tgBotBackup, tgCpu/tgMemory, tgLang, tgEnabledEvents) with getters/setters; settings page gains a Telegram tab + sidebar entry; token shown masked, clearTgBotToken honored - wiring (web.go): stats-notify cron registered from tgRunTime when the bot is enabled; CheckHashStorage job; CPU/memory alarm jobs gated on tgEnabledEvents + thresholds; login notifications via event bus; POST /panel/api/setting/testTgBot and /panel/api/backuptotgbot - lite adaptation: StatsNotifyJob no longer gates on a local Xray core (this panel never runs one) so the periodic report and the scheduled DB backup (sendDocument) can actually fire - translations: upstream TG keys restored across all locales End-to-end smoke (real binary + fake Telegram API over HTTPS at a public-looking address, browser form wire format): bot starts on settings change (getMe), testTgBot delivers, panel restart re-arms the bot, tgRunTime='@every 1m' cron really fires sendDocument carrying the panel SQLite DB (magic verified) plus the report sendMessage, login notification delivered, dead-endpoint failure is logged not silent. 13/13 PASS — see P9-REPORT.md.
292 lines
9.7 KiB
TypeScript
292 lines
9.7 KiB
TypeScript
import { useEffect, useMemo, useState } from 'react';
|
|
import { useTranslation } from 'react-i18next';
|
|
import { useLocation } from 'react-router';
|
|
import {
|
|
Alert,
|
|
Button,
|
|
Card,
|
|
Col,
|
|
ConfigProvider,
|
|
FloatButton,
|
|
Layout,
|
|
Modal,
|
|
Row,
|
|
Space,
|
|
Spin,
|
|
message,
|
|
} from 'antd';
|
|
|
|
import { HttpUtil, PromiseUtil } from '@/utils';
|
|
import { setMessageInstance } from '@/utils/messageBus';
|
|
import { useTheme } from '@/hooks/useTheme';
|
|
import { useMediaQuery } from '@/hooks/useMediaQuery';
|
|
import { useAllSettings } from '@/api/queries/useAllSettings';
|
|
import { AllSettingSchema } from '@/schemas/setting';
|
|
import AppSidebar from '@/layouts/AppSidebar';
|
|
import GeneralTab from './GeneralTab';
|
|
import SecurityTab from './SecurityTab';
|
|
import SubscriptionGeneralTab from './SubscriptionGeneralTab';
|
|
import SubscriptionFormatsTab from './SubscriptionFormatsTab';
|
|
import SubscriptionBalancersTab from './SubscriptionBalancersTab';
|
|
import TelegramTab from './TelegramTab';
|
|
import './SettingsPage.css';
|
|
|
|
interface ApiMsg {
|
|
success?: boolean;
|
|
}
|
|
|
|
const tabSlugs = [
|
|
'general',
|
|
'security',
|
|
'telegram',
|
|
'subscription',
|
|
'subscription-formats',
|
|
'subscription-balancers',
|
|
];
|
|
|
|
function isIp(h: string): boolean {
|
|
if (typeof h !== 'string') return false;
|
|
const v4 = h.split('.');
|
|
if (v4.length === 4 && v4.every((p) => /^\d{1,3}$/.test(p) && Number(p) <= 255)) return true;
|
|
if (!h.includes(':') || h.includes(':::')) return false;
|
|
const parts = h.split('::');
|
|
if (parts.length > 2) return false;
|
|
const split = (s: string) => (s ? s.split(':').filter(Boolean) : []);
|
|
const head = split(parts[0]);
|
|
const tail = split(parts[1]);
|
|
const valid = (seg: string) => /^[0-9a-fA-F]{1,4}$/.test(seg);
|
|
if (![...head, ...tail].every(valid)) return false;
|
|
const groups = head.length + tail.length;
|
|
return parts.length === 2 ? groups < 8 : groups === 8;
|
|
}
|
|
|
|
function scrollTarget() {
|
|
return document.getElementById('content-layout') as HTMLElement;
|
|
}
|
|
|
|
export default function SettingsPage() {
|
|
const { t } = useTranslation();
|
|
const { isDark, isUltra, antdThemeConfig } = useTheme();
|
|
const { isMobile } = useMediaQuery();
|
|
const [modal, modalContextHolder] = Modal.useModal();
|
|
const [messageApi, messageContextHolder] = message.useMessage();
|
|
|
|
useEffect(() => {
|
|
setMessageInstance(messageApi);
|
|
}, [messageApi]);
|
|
|
|
const { allSetting, updateSetting, fetched, spinning, setSpinning, saveDisabled, saveAll } =
|
|
useAllSettings();
|
|
|
|
const [entryHost] = useState(() => window.location.hostname);
|
|
const [entryPort] = useState(() => window.location.port);
|
|
const [entryIsIP] = useState(() => isIp(window.location.hostname));
|
|
|
|
const [alertVisible, setAlertVisible] = useState(true);
|
|
const location = useLocation();
|
|
const slug = location.hash.replace(/^#/, '');
|
|
const activeSlug = tabSlugs.includes(slug) ? slug : 'general';
|
|
|
|
function rebuildUrlAfterRestart(): string {
|
|
const { webDomain, webPort, webBasePath, webCertFile, webKeyFile } = allSetting;
|
|
const newProtocol = webCertFile || webKeyFile ? 'https:' : 'http:';
|
|
|
|
let base = webBasePath ? webBasePath.replace(/^\//, '') : '';
|
|
if (base && !base.endsWith('/')) base += '/';
|
|
|
|
if (!entryIsIP) {
|
|
const url = new URL(window.location.href);
|
|
url.pathname = `/${base}panel/settings`;
|
|
url.protocol = newProtocol;
|
|
return url.toString();
|
|
}
|
|
|
|
let finalHost = entryHost;
|
|
let finalPort = entryPort || '';
|
|
if (webDomain && isIp(webDomain)) finalHost = webDomain;
|
|
if (webPort && Number(webPort) !== Number(entryPort)) finalPort = String(webPort);
|
|
|
|
const url = new URL(`${newProtocol}//${finalHost}`);
|
|
if (finalPort) url.port = finalPort;
|
|
url.pathname = `/${base}panel/settings`;
|
|
return url.toString();
|
|
}
|
|
|
|
async function onSave() {
|
|
const result = AllSettingSchema.safeParse(allSetting);
|
|
if (!result.success) {
|
|
const issue = result.error.issues[0];
|
|
const fieldPath = issue?.path.join('.') ?? 'value';
|
|
const msgKey = issue?.message ?? 'somethingWentWrong';
|
|
messageApi.error(`${fieldPath}: ${t(msgKey, { defaultValue: msgKey })}`);
|
|
return;
|
|
}
|
|
await saveAll();
|
|
}
|
|
|
|
function restartPanel() {
|
|
modal.confirm({
|
|
title: t('pages.settings.restartPanel'),
|
|
content: t('pages.settings.restartPanelDesc'),
|
|
okText: t('pages.settings.restartPanel'),
|
|
okButtonProps: { danger: true },
|
|
cancelText: t('cancel'),
|
|
onOk: async () => {
|
|
setSpinning(true);
|
|
try {
|
|
const msg = (await HttpUtil.post('/panel/api/setting/restartPanel')) as ApiMsg;
|
|
if (!msg?.success) return;
|
|
await PromiseUtil.sleep(5000);
|
|
window.location.replace(rebuildUrlAfterRestart());
|
|
} finally {
|
|
setSpinning(false);
|
|
}
|
|
},
|
|
});
|
|
}
|
|
|
|
const confAlerts = useMemo<string[]>(() => {
|
|
const out: string[] = [];
|
|
if (window.location.protocol !== 'https:') {
|
|
out.push(t('pages.settings.warnHttp'));
|
|
}
|
|
if (allSetting.webPort === 2053) {
|
|
out.push(t('pages.settings.warnDefaultPort'));
|
|
}
|
|
const segs = window.location.pathname.split('/').length < 4;
|
|
if (segs && allSetting.webBasePath === '/') {
|
|
out.push(t('pages.settings.warnDefaultBasePath'));
|
|
}
|
|
if (allSetting.subEnable) {
|
|
let subPath = allSetting.subPath;
|
|
if (allSetting.subURI) {
|
|
try {
|
|
subPath = new URL(allSetting.subURI).pathname;
|
|
} catch {
|
|
/* noop */
|
|
}
|
|
}
|
|
if (subPath === '/sub/') {
|
|
out.push(t('pages.settings.warnDefaultSubPath'));
|
|
}
|
|
}
|
|
if (allSetting.subJsonEnable) {
|
|
let p = allSetting.subJsonPath;
|
|
if (allSetting.subJsonURI) {
|
|
try {
|
|
p = new URL(allSetting.subJsonURI).pathname;
|
|
} catch {
|
|
/* noop */
|
|
}
|
|
}
|
|
if (p === '/json/') {
|
|
out.push(t('pages.settings.warnDefaultJsonPath'));
|
|
}
|
|
}
|
|
return out;
|
|
}, [allSetting, t]);
|
|
|
|
const pageClass = useMemo(() => {
|
|
const classes = ['settings-page'];
|
|
if (isDark) classes.push('is-dark');
|
|
if (isUltra) classes.push('is-ultra');
|
|
return classes.join(' ');
|
|
}, [isDark, isUltra]);
|
|
|
|
const categoryBody = useMemo(() => {
|
|
switch (activeSlug) {
|
|
case 'security':
|
|
return <SecurityTab />;
|
|
case 'telegram':
|
|
return <TelegramTab allSetting={allSetting} updateSetting={updateSetting} />;
|
|
case 'subscription':
|
|
return <SubscriptionGeneralTab allSetting={allSetting} updateSetting={updateSetting} />;
|
|
case 'subscription-formats':
|
|
return <SubscriptionFormatsTab allSetting={allSetting} updateSetting={updateSetting} />;
|
|
case 'subscription-balancers':
|
|
return <SubscriptionBalancersTab allSetting={allSetting} updateSetting={updateSetting} />;
|
|
default:
|
|
return <GeneralTab allSetting={allSetting} updateSetting={updateSetting} />;
|
|
}
|
|
}, [activeSlug, allSetting, updateSetting]);
|
|
|
|
return (
|
|
<ConfigProvider theme={antdThemeConfig}>
|
|
{messageContextHolder}
|
|
{modalContextHolder}
|
|
<Layout className={pageClass}>
|
|
<AppSidebar />
|
|
|
|
<Layout className="content-shell">
|
|
<Layout.Content id="content-layout" className="content-area">
|
|
<Spin
|
|
spinning={spinning || !fetched}
|
|
delay={200}
|
|
description={t('loading')}
|
|
size="large"
|
|
>
|
|
{!fetched ? (
|
|
<div className="loading-spacer" />
|
|
) : (
|
|
<>
|
|
{confAlerts.length > 0 && alertVisible && (
|
|
<Alert
|
|
type="error"
|
|
showIcon
|
|
closable={{ onClose: () => setAlertVisible(false) }}
|
|
className="conf-alert"
|
|
title={t('pages.settings.securityWarnings')}
|
|
description={
|
|
<>
|
|
<b>{t('pages.settings.panelExposed')}</b>
|
|
<ul>
|
|
{confAlerts.map((msg, i) => (
|
|
<li key={i}>{msg}</li>
|
|
))}
|
|
</ul>
|
|
</>
|
|
}
|
|
/>
|
|
)}
|
|
|
|
<Row gutter={[isMobile ? 8 : 16, isMobile ? 0 : 12]}>
|
|
<Col span={24}>
|
|
<Card hoverable>
|
|
<Row className="header-row">
|
|
<Col xs={24} sm={10} className="header-actions">
|
|
<Space>
|
|
<Button type="primary" disabled={saveDisabled} onClick={onSave}>
|
|
{t('pages.settings.save')}
|
|
</Button>
|
|
<Button
|
|
type="primary"
|
|
danger
|
|
disabled={!saveDisabled}
|
|
onClick={restartPanel}
|
|
>
|
|
{t('pages.settings.restartPanel')}
|
|
</Button>
|
|
</Space>
|
|
</Col>
|
|
<Col xs={24} sm={14} className="header-info">
|
|
<FloatButton.BackTop target={scrollTarget} visibilityHeight={200} />
|
|
<Alert type="warning" showIcon title={t('pages.settings.infoDesc')} />
|
|
</Col>
|
|
</Row>
|
|
</Card>
|
|
</Col>
|
|
|
|
<Col span={24}>
|
|
<Card hoverable>{categoryBody}</Card>
|
|
</Col>
|
|
</Row>
|
|
</>
|
|
)}
|
|
</Spin>
|
|
</Layout.Content>
|
|
</Layout>
|
|
</Layout>
|
|
</ConfigProvider>
|
|
);
|
|
}
|