优化算法

This commit is contained in:
Jerry 2025-06-23 18:28:39 +08:00
parent 48d68efe6e
commit e894ffd3f9

View File

@ -2,70 +2,205 @@ const configEffect = {
async generateRealisticConfig(command, devices = []) { async generateRealisticConfig(command, devices = []) {
const timestamp = new Date().toLocaleString(); const timestamp = new Date().toLocaleString();
let config = `! 配置生成于 ${timestamp}\n`; let config = `! 配置生成于 ${timestamp}\n`;
const cmd = command.toLowerCase(); const configTemplates = {
// VLAN 配置 vlan: {
if (cmd.includes('vlan')) { pattern: /(vlan|虚拟局域网|虚拟网)\s*(\d+)/i,
const vlanIdMatch = command.match(/vlan\s*(\d+)/i); template: (vlanId) =>
const vlanId = vlanIdMatch?.[1] || '10'; `vlan ${vlanId}\n` +
const isMgmt = cmd.includes('管理') || cmd.includes('mgmt'); ` name VLAN_${vlanId}\n` +
config += ` exit\n` +
`vlan ${vlanId}\n` + `interface Vlan${vlanId}\n` +
` name ${isMgmt ? 'MGMT' : 'USER'}_VLAN\n` + ` description ${vlanId === '10' ? '管理VLAN' : '用户VLAN'}\n` +
` exit\n` + ` ip address 192.168.${vlanId}.1 255.255.255.0\n` +
`interface Vlan${vlanId}\n` + ` exit\n`,
` description ${isMgmt ? 'Management VLAN' : 'User VLAN'}\n` + },
` ip address 192.168.${vlanId}.1 255.255.255.0\n` + ssh: {
` exit\n`; pattern: /(ssh|安全外壳|远程登录)/i,
} template: () => {
// SSH 配置 const password = Math.random().toString(36).slice(2, 10);
if (cmd.includes('ssh') || cmd.includes('安全') || cmd.includes('登录')) { return (
const password = Math.random().toString(36).slice(2, 10); `ip ssh server\n` +
config += `ip ssh version 2\n` +
`ip ssh server\n` + `username admin privilege 15 secret 0 ${password}\n` +
`ip ssh version 2\n` + `line vty 0 4\n` +
`username admin privilege 15 secret 0 ${password}\n` + ` transport input ssh\n` +
`line vty 0 4\n` + ` login local\n` +
` transport input ssh\n` + ` exit\n`
` login local\n` + );
` exit\n`; },
} },
// 端口配置 port: {
if (cmd.includes('端口') || cmd.includes('接口') || cmd.includes('port')) { pattern: /(端口|接口|port|interface)\s*(\d+)/i,
const portMatch = command.match(/端口\s*(\d+)/i) || command.match(/port\s*(\d+)/i); template: (port) => {
const port = portMatch?.[1] || '1'; const isTrunk = /(trunk|干道)/i.test(command);
const isTrunk = cmd.includes('trunk'); const isAccess = /(access|接入)/i.test(command) || !isTrunk;
const isAccess = cmd.includes('access') || !isTrunk; const desc = /(上联|uplink)/i.test(command) ? 'Uplink_Port' : 'Access_Port';
const desc = cmd.includes('上联') || cmd.includes('uplink') ? 'Uplink_Port' : 'Access_Port'; const vlanId = command.match(/vlan\s*(\d+)/i)?.[1] || '10';
const vlanId = '10';
config += return (
`interface GigabitEthernet0/${port}\n` + `interface GigabitEthernet0/${port}\n` +
` description ${desc}\n` + ` description ${desc}\n` +
` switchport mode ${isTrunk ? 'trunk' : 'access'}\n` + ` switchport mode ${isTrunk ? 'trunk' : 'access'}\n` +
` ${isTrunk ? 'switchport trunk allowed vlan all' : `switchport access vlan ${vlanId}`}\n` + ` ${isTrunk ? 'switchport trunk allowed vlan all' : `switchport access vlan ${vlanId}`}\n` +
` no shutdown\n` + ` no shutdown\n` +
` exit\n`; ` exit\n`
} );
// ACL 配置 },
if (cmd.includes('acl') || cmd.includes('访问控制') || cmd.includes('防火墙')) { },
let targetIP = '192.168.10.10'; acl: {
if (devices.length > 0) { pattern: /(acl|访问控制|防火墙)/i,
const randomDevice = devices[Math.floor(Math.random() * devices.length)]; template: () => {
targetIP = randomDevice.ip; 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}`) { if (!matched) {
config += '! 当前命令未识别到任何可配置项目\n'; 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 }; return { config };
}, },
}; };