(msg: Api.Message)
| 781 | throw new Error(`查看授权密钥失败: ${error.message}`); |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | // 清空授权密钥 |
| 786 | private async clearAuthorizedKeys(msg: Api.Message): Promise<void> { |
| 787 | await msg.edit({ text: "⚠️ 正在清空所有授权密钥...", parseMode: "html" }); |
| 788 | |
| 789 | try { |
| 790 | const authorizedKeysPath = "/root/.ssh/authorized_keys"; |
| 791 | |
| 792 | // 备份现有密钥 |
| 793 | const timestamp = dayjs().format("YYYYMMDD_HHmmss"); |
| 794 | try { |
| 795 | fs.copyFileSync(authorizedKeysPath, `${authorizedKeysPath}.backup.${timestamp}`); |
| 796 | } catch { |
| 797 | // 文件不存在时忽略备份错误 |
| 798 | } |
| 799 | |
| 800 | // 清空密钥文件 |
| 801 | fs.mkdirSync("/root/.ssh", { recursive: true, mode: 0o700 }); |
| 802 | fs.chmodSync("/root/.ssh", 0o700); |
| 803 | fs.writeFileSync(authorizedKeysPath, "", { mode: 0o600 }); |
| 804 | fs.chmodSync(authorizedKeysPath, 0o600); |
| 805 | |
| 806 | await msg.edit({ |
| 807 | text: `✅ <b>授权密钥已清空</b>\n\n🗂️ 备份文件: <code>${authorizedKeysPath}.backup.${timestamp}</code>\n\n⚠️ <b>警告:</b> 所有SSH密钥登录已失效,请确保有其他方式访问服务器`, |
| 808 | parseMode: "html" |
| 809 | }); |
| 810 | |
| 811 | } catch (error: any) { |
| 812 | throw new Error(`清空授权密钥失败: ${error.message}`); |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | // 导出授权密钥 |
| 817 | private async exportAuthorizedKeys(msg: Api.Message): Promise<void> { |
| 818 | await msg.edit({ text: "🔄 正在导出授权密钥...", parseMode: "html" }); |
| 819 | |
| 820 | try { |
| 821 | const authorizedKeysPath = "/root/.ssh/authorized_keys"; |
| 822 | |
| 823 | // 检查文件是否存在 |
| 824 | if (!fs.existsSync(authorizedKeysPath)) { |
| 825 | await msg.edit({ |
| 826 | text: "❌ <b>未找到授权密钥文件</b>\n\n文件路径: <code>/root/.ssh/authorized_keys</code>\n状态: 不存在", |
| 827 | parseMode: "html" |
| 828 | }); |
| 829 | return; |
| 830 | } |
| 831 | |
| 832 | // 读取密钥内容 |
| 833 | const keysContent = fs.readFileSync(authorizedKeysPath, "utf8").trim(); |
| 834 | |
| 835 | if (!keysContent) { |
| 836 | await msg.edit({ |
| 837 | text: "📋 <b>授权密钥为空</b>\n\n当前没有任何授权密钥可导出", |
| 838 | parseMode: "html" |
| 839 | }); |
| 840 | return; |
no test coverage detected