From 50db270ffcb62da0f665da35a2615f5675339df7 Mon Sep 17 00:00:00 2001 From: gaoxq <376340421@qq.com> Date: Fri, 17 Apr 2026 09:55:42 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A6=96=E9=A1=B5=E6=8E=A5=E5=8F=A3=E9=87=8D?= =?UTF-8?q?=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/jeesite/modules/utils/DockerUtil.java | 45 ++++++++++--------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/web-api/src/main/java/com/jeesite/modules/utils/DockerUtil.java b/web-api/src/main/java/com/jeesite/modules/utils/DockerUtil.java index 1a86a6e..e8ccf34 100644 --- a/web-api/src/main/java/com/jeesite/modules/utils/DockerUtil.java +++ b/web-api/src/main/java/com/jeesite/modules/utils/DockerUtil.java @@ -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 listContainers(MySftpAccounts accounts, boolean all) { String fmt = "{{.ID}}|{{.Image}}|{{.Command}}|{{.CreatedAt}}|{{.Status}}|{{.Ports}}|{{.Names}}"; String cmd = (all ? "docker ps -a" : "docker ps") + " --format \"" + fmt + "\"";