From 45036ff400a5b53fe1bed297ffcbcc3df6241350 Mon Sep 17 00:00:00 2001 From: jingrow Date: Sat, 9 Aug 2025 21:12:31 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Eget=5Fjingrow=5Ffirewall=5Fru?= =?UTF-8?q?les=E7=AB=AF=E7=82=B9=EF=BC=8C=E5=89=8D=E7=AB=AF=E9=98=B2?= =?UTF-8?q?=E7=81=AB=E5=A2=99=E5=88=97=E8=A1=A8=E6=94=B9=E4=B8=BA=E4=BD=BF?= =?UTF-8?q?=E7=94=A8get=5Fjingrow=5Ffirewall=5Frules=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E6=9C=AC=E5=9C=B0=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/JsiteServerFirewallRules.vue | 24 +++---- jcloud/api/aliyun_server_light.py | 71 ++++++++++++++++++- 2 files changed, 78 insertions(+), 17 deletions(-) diff --git a/dashboard/src2/components/JsiteServerFirewallRules.vue b/dashboard/src2/components/JsiteServerFirewallRules.vue index bd6d482..1dde3c4 100644 --- a/dashboard/src2/components/JsiteServerFirewallRules.vue +++ b/dashboard/src2/components/JsiteServerFirewallRules.vue @@ -291,10 +291,11 @@ export default { resources: { firewallRules() { return { - url: 'jcloud.api.aliyun_server_light.get_aliyun_firewall_rules', + url: 'jcloud.api.aliyun_server_light.get_jingrow_firewall_rules', params: { instance_id: this.$jsiteServer.pg?.instance_id, - region_id: this.$jsiteServer.pg?.region + limit: this.pagination.limit, + pageno: this.pagination.pageno }, auto: true, onSuccess: (response) => { @@ -311,22 +312,13 @@ export default { isNew: false })); - // 简单分页处理 - const total = this.firewallRules.length; - const limit = this.pagination.limit; - const pageno = this.pagination.pageno; - const start = (pageno - 1) * limit; - const end = start + limit; - + // 使用API返回的分页信息 this.pagination = { - pageno: pageno, - limit: limit, - total: total, - pagecount: Math.ceil(total / limit) + pageno: response.data.pageno, + limit: response.data.limit, + total: response.data.total, + pagecount: response.data.pagecount }; - - // 对当前页的数据进行切片 - this.firewallRules = this.firewallRules.slice(start, end); } else { this.error = response.message || '获取防火墙规则失败'; } diff --git a/jcloud/api/aliyun_server_light.py b/jcloud/api/aliyun_server_light.py index e3916d1..da5352b 100644 --- a/jcloud/api/aliyun_server_light.py +++ b/jcloud/api/aliyun_server_light.py @@ -1781,4 +1781,73 @@ def sync_firewall_rules(instance_id): except Exception as e: jingrow.log_error("同步防火墙规则失败", f"同步实例 {instance_id} 防火墙规则时发生错误: {str(e)}") - return {"success": False, "error": str(e), "message": "同步防火墙规则失败"} \ No newline at end of file + return {"success": False, "error": str(e), "message": "同步防火墙规则失败"} + +@jingrow.whitelist() +def get_jingrow_firewall_rules(**data): + """获取防火墙规则(从本地数据库获取,支持分页)""" + try: + instance_id = data.get('instance_id') + limit = data.get('limit', 20) + pageno = data.get('pageno', 1) + + if not instance_id: + return {"success": False, "message": "缺少实例ID参数"} + + # 查找对应的Jsite Server记录 + server_records = jingrow.get_all( + "Jsite Server", + {"instance_id": instance_id}, + ["name"] + ) + + if not server_records: + return {"success": False, "message": "未找到指定的服务器记录"} + + server_record = server_records[0] + + # 获取该服务器的所有防火墙规则 + firewall_rules = jingrow.get_all( + "Firewall Rules", + {"parent": server_record.name}, + ["name", "rule_id", "rule_protocol", "port", "source_cidr_ip", "remark"] + ) + + # 转换为API格式的数据结构 + all_items = [] + for rule in firewall_rules: + item = { + "rule_id": rule.rule_id or rule.name, + "rule_protocol": rule.rule_protocol or "", + "port": rule.port or "", + "source_cidr_ip": rule.source_cidr_ip or "0.0.0.0/0", + "remark": rule.remark or "" + } + all_items.append(item) + + # 对所有记录按协议排序 + sorted_items = sorted(all_items, key=lambda x: x.get('rule_protocol', '')) + + # 计算分页 + total = len(sorted_items) + pagecount = (total + limit - 1) // limit + start_index = (pageno - 1) * limit + end_index = start_index + limit + + # 返回当前页的记录 + current_page_items = sorted_items[start_index:end_index] + + # 返回格式化的防火墙规则信息 + return { + "success": True, + "data": { + "pageno": pageno, + "limit": limit, + "total": total, + "pagecount": pagecount, + "firewall_rules": current_page_items + } + } + + except Exception as e: + return {"success": False, "message": f"防火墙规则查询失败: {str(e)}"} \ No newline at end of file