diff --git a/jpos/src/main/java/org/jpos/q2/CLI.java b/jpos/src/main/java/org/jpos/q2/CLI.java index f5a7b5c631..9a4fe92dde 100644 --- a/jpos/src/main/java/org/jpos/q2/CLI.java +++ b/jpos/src/main/java/org/jpos/q2/CLI.java @@ -18,9 +18,12 @@ package org.jpos.q2; +import org.apache.sshd.server.Environment; +import org.apache.sshd.server.Signal; import org.jline.reader.*; import org.jline.reader.impl.history.DefaultHistory; import org.jline.terminal.Attributes; +import org.jline.terminal.Size; import org.jline.terminal.Terminal; import org.jline.terminal.TerminalBuilder; @@ -45,10 +48,10 @@ public class CLI implements Runnable { private History mainHistory; public CLI(Q2 q2, String line, boolean keepRunning) throws IOException { - this(q2, System.in, System.out, line, keepRunning, true); + this(q2, System.in, System.out, null, line, keepRunning, true); } - public CLI(Q2 q2, InputStream in, OutputStream rawout, String line, boolean keepRunning, boolean interactive) throws IOException { + public CLI(Q2 q2, InputStream in, OutputStream rawout, Environment env, String line, boolean keepRunning, boolean interactive) throws IOException { Logger.getLogger("org.jline").setLevel(Level.SEVERE); this.q2 = q2; PrintStream out = rawout instanceof PrintStream ? (PrintStream) rawout : new PrintStream(rawout); @@ -58,7 +61,7 @@ public CLI(Q2 q2, InputStream in, OutputStream rawout, String line, boolean keep this.interactive = interactive; this.mainHistory = new DefaultHistory(); if (interactive) { - terminal = buildTerminal(in, out); + terminal = buildTerminal(in, out, env); } initCmdInterface(getCompletionPrefixes(), mainHistory); } @@ -186,7 +189,7 @@ public LineReader getReader() { } public static void exec (InputStream in, OutputStream out, String command) throws Exception { - CLI cli = new CLI(Q2.getQ2(), in, out, command, false, false); + CLI cli = new CLI(Q2.getQ2(), in, out, null, command, false, false); cli.start(); cli.stop(); } @@ -197,20 +200,35 @@ public static String exec (String command) throws Exception { return out.toString(); } - private Terminal buildTerminal (InputStream in, OutputStream out) throws IOException { + protected Terminal buildTerminal (InputStream in, OutputStream out, Environment env) throws IOException { TerminalBuilder builder = TerminalBuilder.builder() .streams(in,out) .system(System.in == in); + if (env != null) { + builder.size(getSize(env)); + env.addSignalListener((_, _) -> { + terminal.setSize(getSize(env)); + terminal.raise(Terminal.Signal.WINCH); + }, Signal.WINCH); + } Terminal t = builder.build(); Attributes attr = t.getAttributes(); attr.getOutputFlags().addAll( EnumSet.of(Attributes.OutputFlag.ONLCR, Attributes.OutputFlag.OPOST) ); + t.setAttributes(attr); return t; } + private Size getSize (Environment env) { + return new Size( + Integer.parseInt(env.getEnv().get(Environment.ENV_COLUMNS)), + Integer.parseInt(env.getEnv().get(Environment.ENV_LINES)) + ); + } + private LineReader buildReader(Terminal terminal, String[] completionPrefixes, History history) throws IOException { LineReader reader = LineReaderBuilder.builder() .terminal(terminal) diff --git a/jpos/src/main/java/org/jpos/q2/ssh/CliShellFactory.java b/jpos/src/main/java/org/jpos/q2/ssh/CliShellFactory.java index a1046482cb..bff3b47e95 100644 --- a/jpos/src/main/java/org/jpos/q2/ssh/CliShellFactory.java +++ b/jpos/src/main/java/org/jpos/q2/ssh/CliShellFactory.java @@ -28,6 +28,9 @@ import org.apache.sshd.server.session.ServerSessionAware; import org.apache.sshd.server.shell.ShellFactory; import org.apache.sshd.server.ExitCallback; +import org.jline.terminal.Attributes; +import org.jline.terminal.Terminal; +import org.jline.terminal.TerminalBuilder; import org.jpos.q2.CLI; import org.jpos.q2.Q2; import org.jpos.util.Log; @@ -35,6 +38,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.util.EnumSet; public class CliShellFactory implements Factory, CommandFactory, ShellFactory { Q2 q2; @@ -89,7 +93,7 @@ public void setSession(ServerSession serverSession) { } public void start(ChannelSession channel, Environment env) throws IOException { - cli = new SshCLI(q2, args != null ? null : in, out, args, args == null); + cli = new SshCLI(q2, args != null ? null : in, out, env, args, args == null); try { cli.setServerSession(serverSession); cli.start(); @@ -108,8 +112,8 @@ public void destroy(ChannelSession channel) { public class SshCLI extends CLI { ServerSession serverSession = null; - public SshCLI(Q2 q2, InputStream in, OutputStream out, String line, boolean keepRunning) throws IOException { - super(q2, in, out, line, keepRunning, true); + public SshCLI(Q2 q2, InputStream in, OutputStream out, Environment env, String line, boolean keepRunning) throws IOException { + super(q2, in, out, env, line, keepRunning, true); } protected boolean running() { diff --git a/jpos/src/main/java/org/jpos/q2/ssh/SshService.java b/jpos/src/main/java/org/jpos/q2/ssh/SshService.java index 2ea84d3eef..e73c87351e 100644 --- a/jpos/src/main/java/org/jpos/q2/ssh/SshService.java +++ b/jpos/src/main/java/org/jpos/q2/ssh/SshService.java @@ -59,8 +59,7 @@ protected void startService() throws Exception { CliShellFactory csf = new CliShellFactory(getServer(), prefixes); sshd.setShellFactory(csf); sshd.setCommandFactory(csf); - - + sshd.setUserAuthFactories(Collections.singletonList(new UserAuthPublicKeyFactory())); sshd.setPublickeyAuthenticator(new AuthorizedKeysFileBasedPKA(username, authorizedKeysFilename)); sshd.start(); @@ -72,14 +71,12 @@ protected void stopService() throws Exception { log.info("Stopping SSHD"); if(sshd!=null) { - new Thread() { - public void run() { - try { - sshd.stop(true); - } catch (IOException ignored) { } - sshd=null; - } - }.start(); + new Thread(() -> { + try { + sshd.stop(true); + } catch (IOException ignored) { } + sshd=null; + }).start(); } }