mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-03-30 04:40:59 +00:00
Merge pull request #2647 from seefs001/feature/status-code-auto-disable
feat: status code auto-disable configuration
This commit is contained in:
@@ -70,6 +70,7 @@ const OperationSetting = () => {
|
||||
AutomaticDisableChannelEnabled: false,
|
||||
AutomaticEnableChannelEnabled: false,
|
||||
AutomaticDisableKeywords: '',
|
||||
AutomaticDisableStatusCodes: '401',
|
||||
'monitor_setting.auto_test_channel_enabled': false,
|
||||
'monitor_setting.auto_test_channel_minutes': 10 /* 签到设置 */,
|
||||
'checkin_setting.enabled': false,
|
||||
|
||||
@@ -29,3 +29,4 @@ export * from './token';
|
||||
export * from './boolean';
|
||||
export * from './dashboard';
|
||||
export * from './passkey';
|
||||
export * from './statusCodeRules';
|
||||
|
||||
96
web/src/helpers/statusCodeRules.js
Normal file
96
web/src/helpers/statusCodeRules.js
Normal file
@@ -0,0 +1,96 @@
|
||||
export function parseHttpStatusCodeRules(input) {
|
||||
const raw = (input ?? '').toString().trim();
|
||||
if (raw.length === 0) {
|
||||
return {
|
||||
ok: true,
|
||||
ranges: [],
|
||||
tokens: [],
|
||||
normalized: '',
|
||||
invalidTokens: [],
|
||||
};
|
||||
}
|
||||
|
||||
const sanitized = raw.replace(/[,]/g, ',');
|
||||
const segments = sanitized.split(/[,]/g);
|
||||
|
||||
const ranges = [];
|
||||
const invalidTokens = [];
|
||||
|
||||
for (const segment of segments) {
|
||||
const trimmed = segment.trim();
|
||||
if (!trimmed) continue;
|
||||
const parsed = parseToken(trimmed);
|
||||
if (!parsed) invalidTokens.push(trimmed);
|
||||
else ranges.push(parsed);
|
||||
}
|
||||
|
||||
if (invalidTokens.length > 0) {
|
||||
return {
|
||||
ok: false,
|
||||
ranges: [],
|
||||
tokens: [],
|
||||
normalized: raw,
|
||||
invalidTokens,
|
||||
};
|
||||
}
|
||||
|
||||
const merged = mergeRanges(ranges);
|
||||
const tokens = merged.map((r) => (r.start === r.end ? `${r.start}` : `${r.start}-${r.end}`));
|
||||
const normalized = tokens.join(',');
|
||||
|
||||
return {
|
||||
ok: true,
|
||||
ranges: merged,
|
||||
tokens,
|
||||
normalized,
|
||||
invalidTokens: [],
|
||||
};
|
||||
}
|
||||
|
||||
function parseToken(token) {
|
||||
const cleaned = (token ?? '').toString().trim().replaceAll(' ', '');
|
||||
if (!cleaned) return null;
|
||||
|
||||
if (cleaned.includes('-')) {
|
||||
const parts = cleaned.split('-');
|
||||
if (parts.length !== 2) return null;
|
||||
const [a, b] = parts;
|
||||
if (!isNumber(a) || !isNumber(b)) return null;
|
||||
const start = Number.parseInt(a, 10);
|
||||
const end = Number.parseInt(b, 10);
|
||||
if (!Number.isFinite(start) || !Number.isFinite(end)) return null;
|
||||
if (start > end) return null;
|
||||
if (start < 100 || end > 599) return null;
|
||||
return { start, end };
|
||||
}
|
||||
|
||||
if (!isNumber(cleaned)) return null;
|
||||
const code = Number.parseInt(cleaned, 10);
|
||||
if (!Number.isFinite(code)) return null;
|
||||
if (code < 100 || code > 599) return null;
|
||||
return { start: code, end: code };
|
||||
}
|
||||
|
||||
function isNumber(s) {
|
||||
return typeof s === 'string' && /^\d+$/.test(s);
|
||||
}
|
||||
|
||||
function mergeRanges(ranges) {
|
||||
if (!Array.isArray(ranges) || ranges.length === 0) return [];
|
||||
|
||||
const sorted = [...ranges].sort((a, b) => (a.start !== b.start ? a.start - b.start : a.end - b.end));
|
||||
const merged = [sorted[0]];
|
||||
|
||||
for (let i = 1; i < sorted.length; i += 1) {
|
||||
const current = sorted[i];
|
||||
const last = merged[merged.length - 1];
|
||||
|
||||
if (current.start <= last.end + 1) {
|
||||
last.end = Math.max(last.end, current.end);
|
||||
continue;
|
||||
}
|
||||
merged.push({ ...current });
|
||||
}
|
||||
|
||||
return merged;
|
||||
}
|
||||
@@ -1923,6 +1923,10 @@
|
||||
"自动测试所有通道间隔时间": "Auto test interval for all channels",
|
||||
"自动禁用": "Auto disabled",
|
||||
"自动禁用关键词": "Automatic disable keywords",
|
||||
"自动禁用状态码": "Auto-disable status codes",
|
||||
"自动禁用状态码格式不正确": "Invalid auto-disable status code format",
|
||||
"支持填写单个状态码或范围(含首尾),使用逗号分隔": "Supports single status codes or inclusive ranges; separate with commas",
|
||||
"例如:401, 403, 429, 500-599": "e.g. 401,403,429,500-599",
|
||||
"自动选择": "Auto Select",
|
||||
"自定义充值数量选项": "Custom Recharge Amount Options",
|
||||
"自定义充值数量选项不是合法的 JSON 数组": "Custom recharge amount options is not a valid JSON array",
|
||||
|
||||
@@ -1909,6 +1909,10 @@
|
||||
"自动测试所有通道间隔时间": "自动测试所有通道间隔时间",
|
||||
"自动禁用": "自动禁用",
|
||||
"自动禁用关键词": "自动禁用关键词",
|
||||
"自动禁用状态码": "自动禁用状态码",
|
||||
"自动禁用状态码格式不正确": "自动禁用状态码格式不正确",
|
||||
"支持填写单个状态码或范围(含首尾),使用逗号分隔": "支持填写单个状态码或范围(含首尾),使用逗号分隔",
|
||||
"例如:401, 403, 429, 500-599": "例如:401,403,429,500-599",
|
||||
"自动选择": "自动选择",
|
||||
"自定义充值数量选项": "自定义充值数量选项",
|
||||
"自定义充值数量选项不是合法的 JSON 数组": "自定义充值数量选项不是合法的 JSON 数组",
|
||||
|
||||
@@ -18,19 +18,29 @@ For commercial licensing, please contact support@quantumnous.com
|
||||
*/
|
||||
|
||||
import React, { useEffect, useState, useRef } from 'react';
|
||||
import { Button, Col, Form, Row, Spin } from '@douyinfe/semi-ui';
|
||||
import {
|
||||
Button,
|
||||
Col,
|
||||
Form,
|
||||
Row,
|
||||
Spin,
|
||||
Tag,
|
||||
Typography,
|
||||
} from '@douyinfe/semi-ui';
|
||||
import {
|
||||
compareObjects,
|
||||
API,
|
||||
showError,
|
||||
showSuccess,
|
||||
showWarning,
|
||||
parseHttpStatusCodeRules,
|
||||
verifyJSON,
|
||||
} from '../../../helpers';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
|
||||
export default function SettingsMonitoring(props) {
|
||||
const { t } = useTranslation();
|
||||
const { Text } = Typography;
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [inputs, setInputs] = useState({
|
||||
ChannelDisableThreshold: '',
|
||||
@@ -38,21 +48,37 @@ export default function SettingsMonitoring(props) {
|
||||
AutomaticDisableChannelEnabled: false,
|
||||
AutomaticEnableChannelEnabled: false,
|
||||
AutomaticDisableKeywords: '',
|
||||
AutomaticDisableStatusCodes: '401',
|
||||
'monitor_setting.auto_test_channel_enabled': false,
|
||||
'monitor_setting.auto_test_channel_minutes': 10,
|
||||
});
|
||||
const refForm = useRef();
|
||||
const [inputsRow, setInputsRow] = useState(inputs);
|
||||
const parsedAutoDisableStatusCodes = parseHttpStatusCodeRules(
|
||||
inputs.AutomaticDisableStatusCodes || '',
|
||||
);
|
||||
|
||||
function onSubmit() {
|
||||
const updateArray = compareObjects(inputs, inputsRow);
|
||||
if (!updateArray.length) return showWarning(t('你似乎并没有修改什么'));
|
||||
if (!parsedAutoDisableStatusCodes.ok) {
|
||||
const details =
|
||||
parsedAutoDisableStatusCodes.invalidTokens &&
|
||||
parsedAutoDisableStatusCodes.invalidTokens.length > 0
|
||||
? `: ${parsedAutoDisableStatusCodes.invalidTokens.join(', ')}`
|
||||
: '';
|
||||
return showError(`${t('自动禁用状态码格式不正确')}${details}`);
|
||||
}
|
||||
const requestQueue = updateArray.map((item) => {
|
||||
let value = '';
|
||||
if (typeof inputs[item.key] === 'boolean') {
|
||||
value = String(inputs[item.key]);
|
||||
} else {
|
||||
value = inputs[item.key];
|
||||
if (item.key === 'AutomaticDisableStatusCodes') {
|
||||
value = parsedAutoDisableStatusCodes.normalized;
|
||||
} else {
|
||||
value = inputs[item.key];
|
||||
}
|
||||
}
|
||||
return API.put('/api/option/', {
|
||||
key: item.key,
|
||||
@@ -207,6 +233,45 @@ export default function SettingsMonitoring(props) {
|
||||
</Row>
|
||||
<Row gutter={16}>
|
||||
<Col xs={24} sm={16}>
|
||||
<Form.Input
|
||||
label={t('自动禁用状态码')}
|
||||
placeholder={t('例如:401, 403, 429, 500-599')}
|
||||
extraText={t(
|
||||
'支持填写单个状态码或范围(含首尾),使用逗号分隔',
|
||||
)}
|
||||
field={'AutomaticDisableStatusCodes'}
|
||||
onChange={(value) =>
|
||||
setInputs({ ...inputs, AutomaticDisableStatusCodes: value })
|
||||
}
|
||||
/>
|
||||
{parsedAutoDisableStatusCodes.ok &&
|
||||
parsedAutoDisableStatusCodes.tokens.length > 0 && (
|
||||
<div
|
||||
style={{
|
||||
display: 'flex',
|
||||
flexWrap: 'wrap',
|
||||
gap: 8,
|
||||
marginTop: 8,
|
||||
}}
|
||||
>
|
||||
{parsedAutoDisableStatusCodes.tokens.map((token) => (
|
||||
<Tag key={token} size='small'>
|
||||
{token}
|
||||
</Tag>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
{!parsedAutoDisableStatusCodes.ok && (
|
||||
<Text type='danger' style={{ display: 'block', marginTop: 8 }}>
|
||||
{t('自动禁用状态码格式不正确')}
|
||||
{parsedAutoDisableStatusCodes.invalidTokens &&
|
||||
parsedAutoDisableStatusCodes.invalidTokens.length > 0
|
||||
? `: ${parsedAutoDisableStatusCodes.invalidTokens.join(
|
||||
', ',
|
||||
)}`
|
||||
: ''}
|
||||
</Text>
|
||||
)}
|
||||
<Form.TextArea
|
||||
label={t('自动禁用关键词')}
|
||||
placeholder={t('一行一个,不区分大小写')}
|
||||
|
||||
Reference in New Issue
Block a user