(msg: Api.Message, portStr: string)
| 1331 | await msg.edit({ |
| 1332 | text: `✅ <b>端口 ${port} 已开放</b>\n\n协议: TCP/UDP\n\n💡 提示: 规则已添加到iptables,重启后可能需要重新设置`, |
| 1333 | parseMode: "html" |
| 1334 | }); |
| 1335 | } catch (error: any) { |
| 1336 | throw new Error(`开放端口失败: ${error.message}`); |
| 1337 | } |
| 1338 | } |
| 1339 | |
| 1340 | // 关闭防火墙端口 |
| 1341 | private async closePort(msg: Api.Message, portStr: string): Promise<void> { |
| 1342 | const port = validatePort(portStr); |
| 1343 | |
| 1344 | if (!port) { |
| 1345 | await msg.edit({ |
| 1346 | text: `❌ <b>无效的端口号</b>\n\n端口范围: 1-65535\n示例: <code>${mainPrefix}ssh close 80</code>`, |
| 1347 | parseMode: "html" |
| 1348 | }); |
| 1349 | return; |
| 1350 | } |
| 1351 | |
| 1352 | await msg.edit({ text: `🔄 正在关闭端口 ${port}...`, parseMode: "html" }); |
| 1353 | |
| 1354 | try { |
| 1355 | // 使用iptables关闭端口 |
| 1356 | for (const protocol of ["tcp", "udp"]) { |
| 1357 | try { |
| 1358 | await execFileAsync("iptables", ["-D", "INPUT", "-p", protocol, "--dport", String(port), "-j", "ACCEPT"]); |
| 1359 | } catch { } |
| 1360 | await execFileAsync("iptables", ["-A", "INPUT", "-p", protocol, "--dport", String(port), "-j", "DROP"]); |
| 1361 | } |
| 1362 | |
| 1363 | // 尝试保存iptables规则 |
| 1364 | try { |
| 1365 | await saveIptablesRules(); |
| 1366 | } catch { |
| 1367 | console.log("[sshkey] 无法持久化iptables规则"); |
| 1368 | } |
| 1369 | |
| 1370 | await msg.edit({ |
| 1371 | text: `✅ <b>端口 ${port} 已关闭</b>\n\n协议: TCP/UDP\n\n💡 提示: 规则已添加到iptables,重启后可能需要重新设置`, |
| 1372 | parseMode: "html" |
| 1373 | }); |
| 1374 | } catch (error: any) { |
| 1375 | throw new Error(`关闭端口失败: ${error.message}`); |
| 1376 | } |
no test coverage detected