MCPcopy Create free account
hub / github.com/TeleBoxOrg/TeleBox_Plugins / toggleKeyAuth

Method toggleKeyAuth

ssh/ssh.ts:1066–1114  ·  view source on GitHub ↗
(msg: Api.Message, mode: string)

Source from the content-addressed store, hash-verified

1064
1065 // 使用通用函数修改SSH配置
1066 const timestamp = await modifySSHConfig("PasswordAuthentication", authValue);
1067
1068 // 同时设置相关安全选项
1069 await modifySSHConfig("ChallengeResponseAuthentication", "no", false);
1070 await modifySSHConfig("UsePAM", enable ? "yes" : "no", false);
1071
1072 // 重启SSH服务使配置生效
1073 const restartResult = await restartSSHService();
1074 if (!restartResult.success) {
1075 throw new Error("无法重启SSH服务");
1076 }
1077
1078 // 保存配置到插件数据库
1079 await ConfigManager.set(CONFIG_KEYS.PASSWORD_AUTH, authValue);
1080
1081 await msg.edit({
1082 text: `✅ <b>密码登录已${action}</b>\n\n当前状态: ${enable ? "✅ 已开启" : "❌ 已关闭"}\n备份文件: /etc/ssh/sshd_config.backup.${timestamp}`,
1083 parseMode: "html"
1084 });
1085 } catch (error: any) {
1086 throw new Error(`${action}密码登录失败: ${error.message}`);
1087 }
1088 }
1089
1090 // 开关密钥登录
1091 private async toggleKeyAuth(msg: Api.Message, mode: string): Promise<void> {
1092 const enable = mode === "on" || mode === "enable" || mode === "yes";
1093 const disable = mode === "off" || mode === "disable" || mode === "no";
1094
1095 if (!enable && !disable) {
1096 await msg.edit({
1097 text: `❌ <b>无效的参数</b>\n\n使用: <code>${mainPrefix}ssh keyauth on/off</code>`,
1098 parseMode: "html"
1099 });
1100 return;
1101 }
1102
1103 const action = enable ? "开启" : "关闭";
1104 await msg.edit({ text: `🔄 正在${action}密钥登录...`, parseMode: "html" });
1105
1106 try {
1107 const authValue = enable ? "yes" : "no";
1108
1109 // 使用通用函数修改SSH配置
1110 const timestamp = await modifySSHConfig("PubkeyAuthentication", authValue);
1111
1112 // 同时设置相关安全选项
1113 if (enable) {
1114 // 开启密钥登录时确保相关设置正确
1115 await modifySSHConfig("AuthorizedKeysFile", "/root/.ssh/authorized_keys", false);
1116 }
1117

Callers 1

handleSSHMethod · 0.95

Calls 4

modifySSHConfigFunction · 0.85
restartSSHServiceFunction · 0.85
editMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected