首页接口重构

This commit is contained in:
2026-04-17 09:55:42 +08:00
parent b94009c94c
commit 50db270ffc

View File

@@ -10,6 +10,7 @@ import com.jeesite.modules.biz.entity.MySftpAccounts;
import io.micrometer.common.util.StringUtils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.*;
@@ -25,12 +26,10 @@ public class DockerUtil {
ChannelExec channel = null;
InputStream in = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
int port = Optional.ofNullable(account.getHostPort()).orElse(22);
session = jsch.getSession(account.getUsername(), account.getHostIp(), port);
session.setTimeout(SSH_TIMEOUT);
// 认证
if ("key".equalsIgnoreCase(account.getAuthType()) && StringUtils.isNotBlank(account.getPrivateKey())) {
jsch.addIdentity(
@@ -41,52 +40,56 @@ public class DockerUtil {
} else {
session.setPassword(account.getPassword());
}
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
config.put("tcp.nodelay", "yes"); // 加速
session.setConfig(config);
session.connect(SSH_TIMEOUT);
// 执行命令
StringBuilder command = new StringBuilder();
if (StringUtils.isNotBlank(account.getRootPath())) {
command.append("cd ").append(account.getRootPath()).append(" && ");
}
command.append(cmd);
channel = (ChannelExec) session.openChannel("exec");
channel.setCommand(command.toString());
channel.setErrStream(System.err, true);
in = channel.getInputStream();
channel.connect();
byte[] buf = new byte[8192];
int len;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
return out.toString(StandardCharsets.UTF_8).trim();
} catch (Exception e) {
return null;
return e.getMessage();
} finally {
try {
if (in != null) in.close();
} catch (Exception ignored) {
}
try {
if (channel != null) channel.disconnect();
} catch (Exception ignored) {
}
try {
if (session != null) session.disconnect();
} catch (Exception ignored) {
}
allClose(in, channel, session);
}
}
public static void allClose(InputStream inputStream, ChannelExec channel, Session session) {
// 关闭流
Optional.ofNullable(inputStream).ifPresent(stream -> {
try {
stream.close();
} catch (IOException e) {
System.err.println(e.getMessage());
}
});
// 断开通道
Optional.ofNullable(channel)
.filter(ChannelExec::isConnected)
.ifPresent(ChannelExec::disconnect);
// 断开会话
Optional.ofNullable(session)
.filter(Session::isConnected)
.ifPresent(Session::disconnect);
}
public static List<ContainerInfo> listContainers(MySftpAccounts accounts, boolean all) {
String fmt = "{{.ID}}|{{.Image}}|{{.Command}}|{{.CreatedAt}}|{{.Status}}|{{.Ports}}|{{.Names}}";
String cmd = (all ? "docker ps -a" : "docker ps") + " --format \"" + fmt + "\"";