From e894ffd3f9ac03f7f6926dff48a94379e1d17caa Mon Sep 17 00:00:00 2001 From: Jerry Date: Mon, 23 Jun 2025 18:28:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../libs/script/configPage/configEffect.js | 255 +++++++++++++----- 1 file changed, 195 insertions(+), 60 deletions(-) diff --git a/src/frontend/src/libs/script/configPage/configEffect.js b/src/frontend/src/libs/script/configPage/configEffect.js index 76607c2..6cef92e 100644 --- a/src/frontend/src/libs/script/configPage/configEffect.js +++ b/src/frontend/src/libs/script/configPage/configEffect.js @@ -2,70 +2,205 @@ const configEffect = { async generateRealisticConfig(command, devices = []) { const timestamp = new Date().toLocaleString(); let config = `! 配置生成于 ${timestamp}\n`; - const cmd = command.toLowerCase(); - // VLAN 配置 - if (cmd.includes('vlan')) { - const vlanIdMatch = command.match(/vlan\s*(\d+)/i); - const vlanId = vlanIdMatch?.[1] || '10'; - const isMgmt = cmd.includes('管理') || cmd.includes('mgmt'); - config += - `vlan ${vlanId}\n` + - ` name ${isMgmt ? 'MGMT' : 'USER'}_VLAN\n` + - ` exit\n` + - `interface Vlan${vlanId}\n` + - ` description ${isMgmt ? 'Management VLAN' : 'User VLAN'}\n` + - ` ip address 192.168.${vlanId}.1 255.255.255.0\n` + - ` exit\n`; - } - // SSH 配置 - if (cmd.includes('ssh') || cmd.includes('安全') || cmd.includes('登录')) { - const password = Math.random().toString(36).slice(2, 10); - config += - `ip ssh server\n` + - `ip ssh version 2\n` + - `username admin privilege 15 secret 0 ${password}\n` + - `line vty 0 4\n` + - ` transport input ssh\n` + - ` login local\n` + - ` exit\n`; - } - // 端口配置 - if (cmd.includes('端口') || cmd.includes('接口') || cmd.includes('port')) { - const portMatch = command.match(/端口\s*(\d+)/i) || command.match(/port\s*(\d+)/i); - const port = portMatch?.[1] || '1'; - const isTrunk = cmd.includes('trunk'); - const isAccess = cmd.includes('access') || !isTrunk; - const desc = cmd.includes('上联') || cmd.includes('uplink') ? 'Uplink_Port' : 'Access_Port'; - const vlanId = '10'; - config += - `interface GigabitEthernet0/${port}\n` + - ` description ${desc}\n` + - ` switchport mode ${isTrunk ? 'trunk' : 'access'}\n` + - ` ${isTrunk ? 'switchport trunk allowed vlan all' : `switchport access vlan ${vlanId}`}\n` + - ` no shutdown\n` + - ` exit\n`; - } - // ACL 配置 - if (cmd.includes('acl') || cmd.includes('访问控制') || cmd.includes('防火墙')) { - let targetIP = '192.168.10.10'; - if (devices.length > 0) { - const randomDevice = devices[Math.floor(Math.random() * devices.length)]; - targetIP = randomDevice.ip; + const configTemplates = { + vlan: { + pattern: /(vlan|虚拟局域网|虚拟网)\s*(\d+)/i, + template: (vlanId) => + `vlan ${vlanId}\n` + + ` name VLAN_${vlanId}\n` + + ` exit\n` + + `interface Vlan${vlanId}\n` + + ` description ${vlanId === '10' ? '管理VLAN' : '用户VLAN'}\n` + + ` ip address 192.168.${vlanId}.1 255.255.255.0\n` + + ` exit\n`, + }, + ssh: { + pattern: /(ssh|安全外壳|远程登录)/i, + template: () => { + const password = Math.random().toString(36).slice(2, 10); + return ( + `ip ssh server\n` + + `ip ssh version 2\n` + + `username admin privilege 15 secret 0 ${password}\n` + + `line vty 0 4\n` + + ` transport input ssh\n` + + ` login local\n` + + ` exit\n` + ); + }, + }, + port: { + pattern: /(端口|接口|port|interface)\s*(\d+)/i, + template: (port) => { + const isTrunk = /(trunk|干道)/i.test(command); + const isAccess = /(access|接入)/i.test(command) || !isTrunk; + const desc = /(上联|uplink)/i.test(command) ? 'Uplink_Port' : 'Access_Port'; + const vlanId = command.match(/vlan\s*(\d+)/i)?.[1] || '10'; + + return ( + `interface GigabitEthernet0/${port}\n` + + ` description ${desc}\n` + + ` switchport mode ${isTrunk ? 'trunk' : 'access'}\n` + + ` ${isTrunk ? 'switchport trunk allowed vlan all' : `switchport access vlan ${vlanId}`}\n` + + ` no shutdown\n` + + ` exit\n` + ); + }, + }, + acl: { + pattern: /(acl|访问控制|防火墙)/i, + template: () => { + let targetIP = '192.168.10.10'; + if (devices.length > 0) { + const randomDevice = devices[Math.floor(Math.random() * devices.length)]; + targetIP = randomDevice.ip; + } + return ( + `ip access-list extended PROTECT_SERVERS\n` + + ` permit tcp any host ${targetIP} eq 22\n` + + ` permit tcp any host ${targetIP} eq 80\n` + + ` permit tcp any host ${targetIP} eq 443\n` + + ` deny ip any any\n` + + ` exit\n` + + `interface Vlan10\n` + + ` ip access-group PROTECT_SERVERS in\n` + + ` exit\n` + ); + }, + }, + dhcp: { + pattern: /(dhcp|动态主机配置)/i, + template: () => { + const vlanId = command.match(/vlan\s*(\d+)/i)?.[1] || '10'; + return ( + `ip dhcp pool VLAN_${vlanId}\n` + + ` network 192.168.${vlanId}.0 255.255.255.0\n` + + ` default-router 192.168.${vlanId}.1\n` + + ` dns-server 8.8.8.8 8.8.4.4\n` + + ` exit\n` + + `ip dhcp excluded-address 192.168.${vlanId}.1 192.168.${vlanId}.10\n` + ); + }, + }, + nat: { + pattern: /(nat|网络地址转换)/i, + template: () => { + const publicIp = `203.0.113.${Math.floor(Math.random() * 10) + 1}`; + return ( + `ip access-list standard NAT_ACL\n` + + ` permit 192.168.0.0 0.0.255.255\n` + + ` exit\n` + + `ip nat inside source list NAT_ACL interface GigabitEthernet0/1 overload\n` + + `interface GigabitEthernet0/1\n` + + ` ip address ${publicIp} 255.255.255.248\n` + + ` ip nat outside\n` + + ` exit\n` + + `interface Vlan10\n` + + ` ip nat inside\n` + + ` exit\n` + ); + }, + }, + stp: { + pattern: /(stp|生成树|spanning-tree)/i, + template: () => { + return ( + `spanning-tree mode rapid-pvst\n` + + `spanning-tree vlan 1-4094 priority 4096\n` + + `spanning-tree portfast default\n` + + `spanning-tree portfast bpduguard default\n` + ); + }, + }, + portSecurity: { + pattern: /(端口安全|port-security)/i, + template: () => { + const port = command.match(/端口\s*(\d+)/i)?.[1] || '1'; + return ( + `interface GigabitEthernet0/${port}\n` + + ` switchport port-security\n` + + ` switchport port-security maximum 5\n` + + ` switchport port-security violation restrict\n` + + ` switchport port-security mac-address sticky\n` + + ` exit\n` + ); + }, + }, + qos: { + pattern: /(qos|服务质量|流量控制)/i, + template: () => { + return ( + `class-map match-all VOICE\n` + + ` match ip dscp ef\n` + + ` exit\n` + + `policy-map QOS_POLICY\n` + + ` class VOICE\n` + + ` priority percent 20\n` + + ` class class-default\n` + + ` bandwidth percent 80\n` + + ` exit\n` + + `interface GigabitEthernet0/1\n` + + ` service-policy output QOS_POLICY\n` + + ` exit\n` + ); + }, + }, + vpn: { + pattern: /(vpn|虚拟专用网)/i, + template: () => { + const vpnId = Math.floor(Math.random() * 1000); + return ( + `crypto isakmp policy ${vpnId}\n` + + ` encryption aes 256\n` + + ` hash sha256\n` + + ` authentication pre-share\n` + + ` group 14\n` + + ` exit\n` + + `crypto ipsec transform-set VPN_TRANSFORM esp-aes 256 esp-sha256-hmac\n` + + ` mode tunnel\n` + + ` exit\n` + + `crypto map VPN_MAP 10 ipsec-isakmp\n` + + ` set peer 203.0.113.5\n` + + ` set transform-set VPN_TRANSFORM\n` + + ` match address VPN_ACL\n` + + ` exit\n` + ); + }, + }, + }; + let matched = false; + if (/(完整配置|全部配置|all config)/i.test(command)) { + matched = true; + config += '! 生成完整校园网络配置\n'; + Object.values(configTemplates).forEach((template) => { + const result = template.template(); + if (result) { + config += result; + } + }); + } else { + for (const [key, { pattern, template }] of Object.entries(configTemplates)) { + const match = command.match(pattern); + if (match) { + matched = true; + config += template(match[2] || match[1] || ''); + } } - config += - `ip access-list extended PROTECT_SERVERS\n` + - ` permit tcp any host ${targetIP} eq 22\n` + - ` permit tcp any host ${targetIP} eq 80\n` + - ` deny ip any any\n` + - ` exit\n` + - `interface Vlan10\n` + - ` ip access-group PROTECT_SERVERS in\n` + - ` exit\n`; } - if (config.trim() === `! 配置生成于 ${timestamp}`) { - config += '! 当前命令未识别到任何可配置项目\n'; + if (!matched) { + config += 'hostname SCHOOL_SWITCH\n'; + config += 'ip domain-name school.local\n'; + config += 'snmp-server community SCHOOL_RO RO\n'; + config += 'ntp server 192.168.1.1\n'; + config += 'logging trap informational\n'; + config += 'logging 192.168.1.10\n'; + config += 'service password-encryption\n'; + config += 'enable secret 0 ' + Math.random().toString(36).slice(2, 12) + '\n'; + config += 'no ip http server\n'; + config += 'no ip http secure-server\n'; } + return { config }; }, };