(msg: Api.Message, portStr: string)
| 938 | if (!newPassword) { |
| 939 | await msg.edit({ |
| 940 | text: `❌ <b>请提供新密码</b>\n\n示例: <code>${mainPrefix}ssh passwd 新密码123</code>`, |
| 941 | parseMode: "html" |
| 942 | }); |
| 943 | return; |
| 944 | } |
| 945 | |
| 946 | // 验证密码复杂度 |
| 947 | if (!validatePassword(newPassword)) { |
| 948 | await msg.edit({ |
| 949 | text: `❌ <b>密码不符合要求</b>\n\n密码长度至少8位`, |
| 950 | parseMode: "html" |
| 951 | }); |
| 952 | return; |
| 953 | } |
| 954 | |
| 955 | await msg.edit({ text: "🔄 正在修改root密码...", parseMode: "html" }); |
| 956 | |
| 957 | try { |
| 958 | await runWithInput("chpasswd", [], `root:${newPassword}\n`); |
| 959 | |
| 960 | // 不显示明文密码 |
| 961 | await msg.edit({ |
| 962 | text: `✅ <b>root密码修改成功</b>\n\n⚠️ 请妥善保管新密码`, |
| 963 | parseMode: "html" |
| 964 | }); |
| 965 | } catch (error: any) { |
| 966 | throw new Error(`修改密码失败: ${error.message}`); |
| 967 | } |
| 968 | } |
| 969 | |
| 970 | // 修改SSH端口 |
| 971 | private async changeSSHPort(msg: Api.Message, portStr: string): Promise<void> { |
| 972 | const port = validatePort(portStr); |
| 973 | |
| 974 | if (!port) { |
| 975 | await msg.edit({ |
| 976 | text: `❌ <b>无效的端口号</b>\n\n端口范围: 1-65535\n示例: <code>${mainPrefix}ssh port 2222</code>`, |
| 977 | parseMode: "html" |
| 978 | }); |
| 979 | return; |
| 980 | } |
| 981 | |
| 982 | await msg.edit({ text: `🔄 正在检查端口 ${port} 是否可用...`, parseMode: "html" }); |
| 983 | |
| 984 | try { |
| 985 | // 获取当前SSH端口,用于后续关闭旧端口防火墙 |
| 986 | const currentPort = await ConfigManager.get(CONFIG_KEYS.SSH_PORT, "22"); |
| 987 | |
| 988 | // 检查端口冲突(跳过当前SSH端口检查) |
| 989 | if (String(port) !== currentPort) { |
| 990 | const portCheck = await checkPortInUse(port); |
| 991 | if (portCheck.inUse) { |
| 992 | await msg.edit({ |
| 993 | text: `❌ <b>端口冲突检测失败</b>\n\n端口 <code>${port}</code> 已被占用\n进程信息: <code>${htmlEscape(portCheck.processInfo || '未知')}</code>\n\n💡 <b>建议:</b>\n• 选择其他端口号\n• 停止占用该端口的服务\n• 使用 <code>netstat -tlnp | grep :${port}</code> 查看详情`, |
| 994 | parseMode: "html" |
| 995 | }); |
| 996 | return; |
| 997 | } |
no test coverage detected