Skip to content

Commit

Permalink
add SSH remote resize (WINCH) support
Browse files Browse the repository at this point in the history
  • Loading branch information
ar committed Sep 20, 2024
1 parent 64169ca commit 5392936
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
28 changes: 23 additions & 5 deletions jpos/src/main/java/org/jpos/q2/CLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand All @@ -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);
}
Expand Down Expand Up @@ -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();
}
Expand All @@ -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)
Expand Down
10 changes: 7 additions & 3 deletions jpos/src/main/java/org/jpos/q2/ssh/CliShellFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@
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;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.EnumSet;

public class CliShellFactory implements Factory<Command>, CommandFactory, ShellFactory {
Q2 q2;
Expand Down Expand Up @@ -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();
Expand All @@ -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() {
Expand Down
17 changes: 7 additions & 10 deletions jpos/src/main/java/org/jpos/q2/ssh/SshService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
}
}

Expand Down

0 comments on commit 5392936

Please sign in to comment.