From 223d43f0d9c7cbb7209d2b1df3f320473ac801ea Mon Sep 17 00:00:00 2001 From: Jerry Date: Sat, 21 Jun 2025 17:17:46 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96vlan=E5=8C=B9=E9=85=8D?= =?UTF-8?q?=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/frontend/src/pages/ConfigPage.jsx | 55 ++++++++++++++++++--------- src/frontend/src/pages/ScanPage.jsx | 4 +- 2 files changed, 38 insertions(+), 21 deletions(-) diff --git a/src/frontend/src/pages/ConfigPage.jsx b/src/frontend/src/pages/ConfigPage.jsx index c4c74a8..5b6782c 100644 --- a/src/frontend/src/pages/ConfigPage.jsx +++ b/src/frontend/src/pages/ConfigPage.jsx @@ -44,49 +44,63 @@ const ConfigPage = () => { setDevices(config.devices || []); }, []); - const generateRealisticConfig = (command) => { + const generateRealisticConfig = (command, devices = []) => { const timestamp = new Date().toLocaleString(); let config = `! 配置生成于 ${timestamp}\n`; - - if (command.includes('VLAN')) { - const vlanId = command.match(/VLAN\s*(\d+)/)?.[1] || '10'; + 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 ${command.includes('管理') ? 'MGMT' : 'USER'}_VLAN\n` + + ` name ${isMgmt ? 'MGMT' : 'USER'}_VLAN\n` + ` exit\n` + `interface Vlan${vlanId}\n` + - ` description ${command.includes('管理') ? 'Management' : 'User'} VLAN\n` + + ` description ${isMgmt ? 'Management VLAN' : 'User VLAN'}\n` + ` ip address 192.168.${vlanId}.1 255.255.255.0\n` + ` exit\n`; } - - if (command.includes('SSH') || command.includes('安全')) { + // 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 ${Math.random().toString(36).slice(2, 10)}\n` + + `username admin privilege 15 secret 0 ${password}\n` + `line vty 0 4\n` + ` transport input ssh\n` + ` login local\n` + ` exit\n`; } - - if (command.includes('端口') || command.includes('接口')) { - const port = command.match(/端口\s*(\d+)/)?.[1] || '1'; + // 端口配置 + 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 ${command.includes('接入') ? 'Access_Port' : 'Uplink_Port'}\n` + - ` switchport mode ${command.includes('trunk') ? 'trunk' : 'access'}\n` + - ` ${command.includes('trunk') ? 'switchport trunk allowed vlan all' : 'switchport access vlan 10'}\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`; } - - if (command.includes('ACL') || command.includes('访问控制')) { + // 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; + } config += `ip access-list extended PROTECT_SERVERS\n` + - ` permit tcp any host 192.168.10.10 eq 22\n` + - ` permit tcp any host 192.168.10.10 eq 80\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` + @@ -94,6 +108,9 @@ const ConfigPage = () => { ` exit\n`; } + if (config.trim() === `! 配置生成于 ${timestamp}`) { + config += '! 当前命令未识别到任何可配置项目\n'; + } return { config }; }; diff --git a/src/frontend/src/pages/ScanPage.jsx b/src/frontend/src/pages/ScanPage.jsx index bdf8eb5..9ce3ff8 100644 --- a/src/frontend/src/pages/ScanPage.jsx +++ b/src/frontend/src/pages/ScanPage.jsx @@ -36,9 +36,9 @@ const ScanPage = () => { useEffect(() => { const fetchLocalInfo = async () => { - setLocalIp('172.17.99.208'); + setLocalIp('192.168.1.100'); if (!subnet) { - const ipParts = '172.17.99.208'.split('.'); + const ipParts = '192.168.1.0'.split('.'); setSubnet(`${ipParts[0]}.${ipParts[1]}.${ipParts[2]}.0/24`); } };