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

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