feat:新增IP个性化配置

This commit is contained in:
2026-07-10 11:57:28 +08:00
parent 46bdff3734
commit 0223db88b7
9 changed files with 848 additions and 23 deletions
+324
View File
@@ -299,6 +299,72 @@
</el-form>
</el-card>
</el-tab-pane>
<el-tab-pane label="IP管理" name="ip">
<el-card class="section-card">
<div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:12px;">
<div>
<strong>IP 访问策略</strong>
<div class="small-text" style="margin-top:4px;">
按客户端 IP 拉黑或配置独立目标环境(不含本地代理端口),便于开发/测试分流。
通过 localhost 访问时会自动匹配本机唯一 LAN IP 规则。
<a href="/__client-ip" target="_blank" style="color:#409EFF;">查看当前请求识别到的 IP</a>
</div>
</div>
<div style="display:flex;gap:8px;align-items:center;">
<el-input size="small" v-model="ipRuleSearch" placeholder="搜索 IP 或备注" clearable style="width:220px;" prefix-icon="el-icon-search"></el-input>
<el-button size="mini" icon="el-icon-refresh" :loading="ipRulesRefreshing" @click="refreshIpRules">刷新</el-button>
<el-button size="mini" type="primary" @click="openIpRuleDialogForCreate">新增 IP 规则</el-button>
</div>
</div>
<el-table :data="pagedIpRules" border style="width: 100%;">
<el-table-column label="IP 地址" min-width="160">
<template slot-scope="scope">
<span>{{ scope.row.ip }}</span>
</template>
</el-table-column>
<el-table-column label="备注" min-width="140" show-overflow-tooltip>
<template slot-scope="scope">
<span>{{ scope.row.remark || "-" }}</span>
</template>
</el-table-column>
<el-table-column label="策略" width="120" align="center">
<template slot-scope="scope">
<el-tag v-if="scope.row.blocked" type="danger" size="mini">拉黑</el-tag>
<el-tag v-else-if="scope.row.useCustomConfig" type="warning" size="mini">个性化</el-tag>
<el-tag v-else type="info" size="mini">默认</el-tag>
</template>
</el-table-column>
<el-table-column label="目标环境" min-width="260" show-overflow-tooltip>
<template slot-scope="scope">
<span>{{ getIpRuleTargetSummary(scope.row) }}</span>
</template>
</el-table-column>
<el-table-column label="Mock" width="90" align="center">
<template slot-scope="scope">
<span v-if="scope.row.blocked">-</span>
<span v-else-if="scope.row.useCustomConfig">{{ scope.row.config.mockEnabled === false ? "关闭" : "开启" }}</span>
<span v-else>全局</span>
</template>
</el-table-column>
<el-table-column label="操作" width="160">
<template slot-scope="scope">
<el-button size="mini" type="primary" plain @click="openIpRuleDialogForEdit(scope.row)">修改</el-button>
<el-button size="mini" type="danger" @click="removeIpRule(scope.row)">删除</el-button>
</template>
</el-table-column>
</el-table>
<el-pagination
class="table-pagination"
background
layout="total, prev, pager, next"
:current-page="ipRulePagination.currentPage"
:page-size="ipRulePagination.pageSize"
:total="filteredIpRules.length"
@current-change="handleIpRulePageChange"
></el-pagination>
</el-card>
</el-tab-pane>
</el-tabs>
<el-dialog
@@ -554,6 +620,88 @@
<el-button type="danger" @click="submitDeleteGroup">确认删除</el-button>
</span>
</el-dialog>
<el-dialog
:title="ipRuleDialog.mode === 'edit' ? '修改 IP 规则' : '新增 IP 规则'"
:visible.sync="ipRuleDialog.visible"
width="760px"
>
<el-form :model="ipRuleDialog.form" label-width="180px">
<el-form-item label="IP 地址">
<el-input
v-model="ipRuleDialog.form.ip"
placeholder="如 192.168.1.100"
:disabled="ipRuleDialog.mode === 'edit'"
></el-input>
</el-form-item>
<el-form-item label="备注">
<el-input v-model="ipRuleDialog.form.remark" placeholder="如:测试组、开发组"></el-input>
</el-form-item>
<el-form-item label="拉黑访问">
<el-switch v-model="ipRuleDialog.form.blocked" @change="onIpRuleBlockedChange"></el-switch>
<div class="small-text" style="margin-top:4px;">开启后该 IP 的所有请求将被拒绝(403)</div>
</el-form-item>
<template v-if="!ipRuleDialog.form.blocked">
<el-form-item label="个性化配置">
<el-switch v-model="ipRuleDialog.form.useCustomConfig"></el-switch>
<div class="small-text" style="margin-top:4px;">开启后可为此 IP 单独配置目标环境,覆盖全局基础配置</div>
</el-form-item>
<template v-if="ipRuleDialog.form.useCustomConfig">
<el-divider content-position="left">独立环境配置</el-divider>
<el-row :gutter="16">
<el-col :span="12">
<el-form-item label="Mock 开关">
<el-switch v-model="ipRuleDialog.form.config.mockEnabled"></el-switch>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="默认 Content-Type">
<el-select
v-model="ipRuleDialog.form.config.defaultContentType"
filterable
allow-create
default-first-option
placeholder="请选择"
style="width: 100%;"
>
<el-option
v-for="item in contentTypeOptions"
:key="item"
:label="item"
:value="item"
></el-option>
</el-select>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="目标主机">
<el-input v-model="ipRuleDialog.form.config.targetHost" placeholder="如 dev.example.com"></el-input>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="目标端口">
<el-input-number
v-model="ipRuleDialog.form.config.targetPort"
:min="1"
:max="65535"
style="width: 100%;"
></el-input-number>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="目标 HTTPS">
<el-switch v-model="ipRuleDialog.form.config.targetHttps"></el-switch>
</el-form-item>
</el-col>
</el-row>
</template>
</template>
</el-form>
<span slot="footer" class="dialog-footer">
<el-button @click="ipRuleDialog.visible = false">取消</el-button>
<el-button type="primary" @click="submitIpRuleDialog">保存</el-button>
</span>
</el-dialog>
</div>
</div>
@@ -569,6 +717,27 @@
mocksRefreshing: false,
apiRefreshing: false,
configRefreshing: false,
ipRulesRefreshing: false,
ipRules: [],
ipRuleSearch: "",
ipRulePagination: { currentPage: 1, pageSize: 10 },
ipRuleDialog: {
visible: false,
mode: "create",
form: {
ip: "",
remark: "",
blocked: false,
useCustomConfig: true,
config: {
mockEnabled: true,
defaultContentType: "application/json",
targetHost: "",
targetPort: 443,
targetHttps: true,
},
},
},
apiList: [],
apiPagination: { currentPage: 1, pageSize: 10 },
apiDialog: {
@@ -675,11 +844,13 @@
else if (tab === "mocks") { this.loadMockFiles(); this.loadMockGroups(); }
else if (tab === "api") { this.loadApiList(); }
else if (tab === "basic") { this.loadConfig(); }
else if (tab === "ip") { this.loadIpRules(); }
},
routeSearch: function () { this.routePagination.currentPage = 1; },
mockFileSearch: function () { this.mockFilePagination.currentPage = 1; },
mockGroupFilter: function () { this.mockFilePagination.currentPage = 1; },
apiSearch: function () { this.apiPagination.currentPage = 1; },
ipRuleSearch: function () { this.ipRulePagination.currentPage = 1; },
},
created: async function () {
await this.loadApiList();
@@ -744,6 +915,145 @@
this.configRefreshing = false;
}
},
refreshIpRules: async function () {
this.ipRulesRefreshing = true;
try {
await this.loadIpRules();
this.$message.success("IP 规则已刷新");
} finally {
this.ipRulesRefreshing = false;
}
},
loadIpRules: async function () {
try {
var resp = await fetch("/__ip-rules");
var data = await resp.json();
if (!resp.ok || data.success === false) {
throw new Error(data.error || "加载 IP 规则失败");
}
this.ipRules = Array.isArray(data.list) ? data.list : [];
this.ipRulePagination.currentPage = 1;
} catch (err) {
this.ipRules = [];
this.$message.error("加载 IP 规则失败: " + err.message);
}
},
getDefaultIpRuleForm: function () {
var global = this.form.config || {};
return {
ip: "",
remark: "",
blocked: false,
useCustomConfig: true,
config: {
mockEnabled: global.mockEnabled !== false,
defaultContentType: global.defaultContentType || "application/json",
targetHost: global.targetHost || "",
targetPort: global.targetPort || 443,
targetHttps: global.targetHttps !== false,
},
};
},
getIpRuleTargetSummary: function (rule) {
if (!rule) return "-";
if (rule.blocked) return "已拉黑,禁止访问";
if (!rule.useCustomConfig) return "使用全局配置";
var cfg = rule.config || {};
var host = cfg.targetHost || this.form.config.targetHost || "-";
var port = cfg.targetPort != null ? cfg.targetPort : this.form.config.targetPort;
var https = cfg.targetHttps !== undefined ? cfg.targetHttps !== false : this.form.config.targetHttps !== false;
var proto = https ? "https" : "http";
return proto + "://" + host + (port ? ":" + port : "");
},
onIpRuleBlockedChange: function (blocked) {
if (blocked) {
this.ipRuleDialog.form.useCustomConfig = false;
}
},
openIpRuleDialogForCreate: function () {
this.ipRuleDialog.mode = "create";
this.ipRuleDialog.form = this.getDefaultIpRuleForm();
this.ipRuleDialog.visible = true;
},
openIpRuleDialogForEdit: function (rule) {
var cfg = rule.config || {};
this.ipRuleDialog.mode = "edit";
this.ipRuleDialog.form = {
ip: rule.ip || "",
remark: rule.remark || "",
blocked: !!rule.blocked,
useCustomConfig: !!rule.useCustomConfig,
config: {
mockEnabled: cfg.mockEnabled !== false,
defaultContentType: cfg.defaultContentType || this.form.config.defaultContentType || "application/json",
targetHost: cfg.targetHost || "",
targetPort: cfg.targetPort != null ? cfg.targetPort : this.form.config.targetPort,
targetHttps: cfg.targetHttps !== undefined ? cfg.targetHttps !== false : this.form.config.targetHttps !== false,
},
};
this.ipRuleDialog.visible = true;
},
submitIpRuleDialog: async function () {
var form = this.ipRuleDialog.form;
var ip = (form.ip || "").trim();
if (!ip) {
this.$message.error("IP 地址不能为空");
return;
}
if (!form.blocked && !form.useCustomConfig) {
this.$message.error("请至少启用「拉黑访问」或「个性化配置」");
return;
}
try {
var resp = await fetch("/__ip-rules", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
ip: ip,
remark: form.remark,
blocked: !!form.blocked,
useCustomConfig: !!form.useCustomConfig,
config: form.useCustomConfig ? form.config : {},
}),
});
var data = await resp.json();
if (!resp.ok || data.success === false) {
throw new Error(data.error || "保存 IP 规则失败");
}
this.$message.success("IP 规则已保存");
this.ipRuleDialog.visible = false;
await this.loadIpRules();
} catch (err) {
this.$message.error("保存 IP 规则失败: " + err.message);
}
},
removeIpRule: async function (rule) {
try {
await this.$confirm("确定删除 IP「" + rule.ip + "」的规则吗?", "提示", {
type: "warning",
});
} catch (e) {
return;
}
try {
var resp = await fetch("/__ip-rules", {
method: "DELETE",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ ip: rule.ip }),
});
var data = await resp.json();
if (!resp.ok || data.success === false) {
throw new Error(data.error || "删除 IP 规则失败");
}
this.$message.success("IP 规则已删除");
await this.loadIpRules();
} catch (err) {
this.$message.error("删除 IP 规则失败: " + err.message);
}
},
handleIpRulePageChange: function (page) {
this.ipRulePagination.currentPage = page;
},
getPreviewText: function (content) {
var text = String(content || "").replace(/\s+/g, " ").trim();
if (!text) return "(空文件)";
@@ -1440,6 +1750,17 @@
);
});
},
filteredIpRules: function () {
var q = (this.ipRuleSearch || "").toLowerCase().trim();
var list = this.ipRules || [];
if (!q) return list;
return list.filter(function (item) {
return (
(item.ip || "").toLowerCase().indexOf(q) !== -1 ||
(item.remark || "").toLowerCase().indexOf(q) !== -1
);
});
},
pagedRoutes: function () {
return this.getPagedData(this.filteredRoutes, this.routePagination);
},
@@ -1449,6 +1770,9 @@
pagedApiList: function () {
return this.getPagedData(this.filteredApiList, this.apiPagination);
},
pagedIpRules: function () {
return this.getPagedData(this.filteredIpRules, this.ipRulePagination);
},
routeDialogMockFiles: function () {
var groupFilter = this.routeDialog.form.groupFilter;
if (groupFilter === null || groupFilter === undefined) {