feat: add IP blacklist feature for rate-limited APIs (#753)

This commit is contained in:
Dream Hunter
2025-11-03 20:31:32 +08:00
committed by GitHub
parent fac249ed31
commit be36967b80
7 changed files with 361 additions and 0 deletions

View File

@@ -25,6 +25,7 @@ import Telegram from './admin/Telegram.vue';
import Webhook from './admin/Webhook.vue';
import MailWebhook from './admin/MailWebhook.vue';
import WorkerConfig from './admin/WorkerConfig.vue';
import IpBlacklistSettings from './admin/IpBlacklistSettings.vue';
const {
adminAuth, showAdminAuth, adminTab, loading,
@@ -72,6 +73,7 @@ const { t } = useI18n({
maintenance: 'Maintenance',
database: 'Database',
workerconfig: 'Worker Config',
ipBlacklistSettings: 'IP Blacklist',
appearance: 'Appearance',
about: 'About',
ok: 'OK',
@@ -100,6 +102,7 @@ const { t } = useI18n({
maintenance: '维护',
database: '数据库',
workerconfig: 'Worker 配置',
ipBlacklistSettings: 'IP 黑名单',
appearance: '外观',
about: '关于',
ok: '确定',
@@ -160,6 +163,9 @@ onMounted(async () => {
<n-tab-pane name="senderAccess" :tab="t('senderAccess')">
<SenderAccess />
</n-tab-pane>
<n-tab-pane name="ipBlacklistSettings" :tab="t('ipBlacklistSettings')">
<IpBlacklistSettings />
</n-tab-pane>
<n-tab-pane name="webhook" :tab="t('webhookSettings')">
<Webhook />
</n-tab-pane>

View File

@@ -0,0 +1,126 @@
<script setup>
import { onMounted, ref } from 'vue';
import { useI18n } from 'vue-i18n'
import { useGlobalState } from '../../store'
import { api } from '../../api'
const { loading } = useGlobalState()
const message = useMessage()
const { t } = useI18n({
messages: {
en: {
title: 'IP Blacklist Settings',
tip: 'Block specific IPs from accessing rate-limited APIs. Supports text matching (e.g., "192.168.1") or regex (e.g., "^10\\.0\\.0\\.5$").',
manualInputPrompt: 'Type pattern and press Enter to add',
save: 'Save',
successTip: 'Save Success',
enable_ip_blacklist: 'Enable IP Blacklist',
enable_tip: 'Block IPs matching blacklist patterns from accessing rate-limited APIs',
ip_blacklist: 'IP Blacklist Patterns',
ip_blacklist_placeholder: 'Enter pattern (e.g., 192.168.1 or ^10\\.0\\.0\\.5$)',
},
zh: {
title: 'IP 黑名单设置',
tip: '阻止特定 IP 访问限流 API。支持文本匹配如 "192.168.1")或正则表达式(如 "^10\\.0\\.0\\.5$")。',
manualInputPrompt: '输入匹配模式后按回车键添加',
save: '保存',
successTip: '保存成功',
enable_ip_blacklist: '启用 IP 黑名单',
enable_tip: '阻止匹配黑名单的 IP 访问限流 API',
ip_blacklist: 'IP 黑名单匹配模式',
ip_blacklist_placeholder: '输入匹配模式例如192.168.1 或 ^10\\.0\\.0\\.5$',
}
}
});
const enabled = ref(false)
const ipBlacklist = ref([])
const fetchData = async () => {
try {
loading.value = true
const res = await api.fetch(`/admin/ip_blacklist/settings`)
enabled.value = res.enabled || false
ipBlacklist.value = res.blacklist || []
} catch (error) {
message.error(error.message || "error");
} finally {
loading.value = false
}
}
const save = async () => {
try {
loading.value = true
await api.fetch(`/admin/ip_blacklist/settings`, {
method: 'POST',
body: JSON.stringify({
enabled: enabled.value,
blacklist: ipBlacklist.value || [],
})
})
message.success(t('successTip'))
} catch (error) {
message.error(error.message || "error");
} finally {
loading.value = false
}
}
onMounted(async () => {
await fetchData();
})
</script>
<template>
<div class="center">
<n-card :title="t('title')" :bordered="false" embedded style="max-width: 800px;">
<template #header-extra>
<n-button @click="save" type="primary" :loading="loading">
{{ t('save') }}
</n-button>
</template>
<n-space vertical :size="20">
<n-alert :show-icon="false" :bordered="false" type="info">
<span>{{ t("tip") }}</span>
</n-alert>
<n-form-item-row :label="t('enable_ip_blacklist')">
<n-switch v-model:value="enabled" :round="false" />
<n-text depth="3" style="margin-left: 10px; font-size: 12px;">
{{ t('enable_tip') }}
</n-text>
</n-form-item-row>
<n-form-item-row :label="t('ip_blacklist')">
<n-select
v-model:value="ipBlacklist"
filterable
multiple
tag
:placeholder="t('ip_blacklist_placeholder')"
:disabled="!enabled">
<template #empty>
<n-text depth="3">
{{ t('manualInputPrompt') }}
</n-text>
</template>
</n-select>
</n-form-item-row>
</n-space>
</n-card>
</div>
</template>
<style scoped>
.center {
display: flex;
text-align: left;
place-items: center;
justify-content: center;
margin: 20px;
}
</style>