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

Method toggleRootLogin

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

Source from the content-addressed store, hash-verified

1115 await modifySSHConfig("AuthorizedKeysFile", "/root/.ssh/authorized_keys", false);
1116 }
1117
1118 // 重启SSH服务使配置生效
1119 const restartResult = await restartSSHService();
1120 if (!restartResult.success) {
1121 throw new Error("无法重启SSH服务");
1122 }
1123
1124 // 保存配置到插件数据库
1125 await ConfigManager.set(CONFIG_KEYS.PUBKEY_AUTH, authValue);
1126
1127 let warningText = "";
1128 if (!enable) {
1129 warningText = "\n\n⚠️ <b>警告</b>: 关闭密钥登录可能会造成无法使用SSH密钥连接";
1130 }
1131
1132 await msg.edit({
1133 text: `✅ <b>密钥登录已${action}</b>\n\n当前状态: ${enable ? "✅ 已开启" : "❌ 已关闭"}\n备份文件: /etc/ssh/sshd_config.backup.${timestamp}${warningText}`,
1134 parseMode: "html"
1135 });
1136 } catch (error: any) {
1137 throw new Error(`${action}密钥登录失败: ${error.message}`);
1138 }
1139 }
1140
1141 // 开关Root登录
1142 private async toggleRootLogin(msg: Api.Message, mode: string): Promise<void> {
1143 const enable = mode === "on" || mode === "enable" || mode === "yes";
1144 const disable = mode === "off" || mode === "disable" || mode === "no";
1145 const keyOnly = mode === "keyonly" || mode === "key-only" || mode === "keys";
1146
1147 if (!enable && !disable && !keyOnly) {
1148 await msg.edit({
1149 text: `❌ <b>无效的参数</b>\n\n用法:\n• <code>${mainPrefix}ssh rootlogin on</code> - 允许所有root登录方式\n• <code>${mainPrefix}ssh rootlogin off</code> - 完全禁止root登录\n• <code>${mainPrefix}ssh rootlogin keyonly</code> - 仅允许密钥登录root`,
1150 parseMode: "html"
1151 });
1152 return;
1153 }
1154
1155 let action: string;
1156 let authValue: string;
1157
1158 if (enable) {
1159 action = "开启所有Root登录方式";
1160 authValue = "yes";
1161 } else if (keyOnly) {
1162 action = "设置Root仅密钥登录";
1163 authValue = "prohibit-password";
1164 } else {
1165 action = "完全禁止Root登录";
1166 authValue = "no";
1167 }
1168
1169 await msg.edit({ text: `🔄 正在${action}...`, parseMode: "html" });
1170
1171 try {
1172 // 检查当前是否有其他登录方式
1173 if (disable) {
1174 // 完全禁用root登录前检查是否有其他用户

Callers 1

handleSSHMethod · 0.95

Calls 3

modifySSHConfigFunction · 0.85
restartSSHServiceFunction · 0.85
editMethod · 0.45

Tested by

no test coverage detected