Skip to content

Commit

Permalink
Upgrade sshd to 2.7.0 (#715)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Oct 14, 2021
1 parent deb7469 commit 9908d12
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 31 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
<jna.version>5.8.0</jna.version>
<jansi.version>2.3.2</jansi.version>
<juniversalchardet.version>1.0.3</juniversalchardet.version>
<sshd.version>2.1.0</sshd.version>
<sshd.version>2.7.0</sshd.version>
<easymock.version>3.3.1</easymock.version>
<junit.version>4.13.1</junit.version>
<gogo.runtime.version>1.1.2</gogo.runtime.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@

import org.apache.sshd.server.Environment;
import org.apache.sshd.server.ExitCallback;
import org.apache.sshd.server.SessionAware;
import org.apache.sshd.server.channel.ChannelSession;
import org.apache.sshd.server.command.Command;
import org.apache.sshd.server.session.ServerSession;

public class ShellCommand implements Command, SessionAware {
public class ShellCommand implements Command {

private static final Logger LOGGER = Logger.getLogger(ShellCommand.class.getName());

Expand Down Expand Up @@ -55,11 +55,9 @@ public void setExitCallback(ExitCallback callback) {
this.callback = callback;
}

public void setSession(ServerSession session) {
this.session = session;
}

public void start(final Environment env) throws IOException {
@Override
public void start(ChannelSession channel, Environment env) throws IOException {
this.session = channel.getSession();
this.env = env;
new Thread(this::run).start();
}
Expand All @@ -84,7 +82,7 @@ private void run() {
}
}

public void destroy() {
public void destroy(ChannelSession channel) {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
import java.util.Map;
import java.util.function.Consumer;

import org.apache.sshd.common.Factory;
import org.apache.sshd.common.channel.PtyMode;
import org.apache.sshd.server.Environment;
import org.apache.sshd.server.ExitCallback;
import org.apache.sshd.server.SessionAware;
import org.apache.sshd.server.Signal;
import org.apache.sshd.server.channel.ChannelSession;
import org.apache.sshd.server.command.Command;
import org.apache.sshd.server.session.ServerSession;
import org.apache.sshd.server.shell.ShellFactory;
import org.jline.terminal.Attributes;
import org.jline.terminal.Attributes.ControlChar;
import org.jline.terminal.Attributes.InputFlag;
Expand All @@ -36,7 +35,7 @@
* SSHD {@link org.apache.sshd.server.command.Command} factory which provides access to
* Shell.
*/
public class ShellFactoryImpl implements Factory<Command> {
public class ShellFactoryImpl implements ShellFactory {
private final Consumer<Ssh.ShellParams> shell;

public ShellFactoryImpl(Consumer<Ssh.ShellParams> shell) {
Expand All @@ -63,11 +62,11 @@ static void close(Closeable... closeables) {
}
}

public Command create() {
public Command createShell(ChannelSession session) {
return new ShellImpl();
}

public class ShellImpl implements Command, SessionAware {
public class ShellImpl implements Command {
private InputStream in;

private OutputStream out;
Expand All @@ -76,8 +75,6 @@ public class ShellImpl implements Command, SessionAware {

private ExitCallback callback;

private ServerSession session;

private boolean closed;

public void setInputStream(final InputStream in) {
Expand All @@ -96,15 +93,11 @@ public void setExitCallback(ExitCallback callback) {
this.callback = callback;
}

public void setSession(ServerSession session) {
this.session = session;
}

public void start(final Environment env) throws IOException {
public void start(final ChannelSession session, final Environment env) throws IOException {
try {
new Thread(() -> {
try {
ShellImpl.this.run(env);
ShellImpl.this.run(session, env);
} catch (Throwable t) {
t.printStackTrace();
}
Expand All @@ -114,7 +107,7 @@ public void start(final Environment env) throws IOException {
}
}

public void run(Environment env) throws Exception {
public void run(ChannelSession session, Environment env) throws Exception {
try {
Terminal terminal = TerminalBuilder.builder()
.name("JLine SSH")
Expand Down Expand Up @@ -216,19 +209,19 @@ public void run(Environment env) throws Exception {
}
}
terminal.setAttributes(attr);
env.addSignalListener(signals -> {
env.addSignalListener((channel, signals) -> {
terminal.setSize(new Size(Integer.parseInt(env.getEnv().get("COLUMNS")),
Integer.parseInt(env.getEnv().get("LINES"))));
terminal.raise(Terminal.Signal.WINCH);
}, Signal.WINCH);

shell.accept(new Ssh.ShellParams(env.getEnv(), session, terminal, this::destroy));
shell.accept(new Ssh.ShellParams(env.getEnv(), session.getSession(), terminal, () -> destroy(session)));
} catch (Throwable t) {
t.printStackTrace();
}
}

public void destroy() {
public void destroy(ChannelSession session) {
if (!closed) {
closed = true;
flush(out, err);
Expand Down
10 changes: 6 additions & 4 deletions remote-ssh/src/main/java/org/jline/builtins/ssh/Ssh.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@
import org.apache.sshd.client.channel.ClientChannelEvent;
import org.apache.sshd.client.future.ConnectFuture;
import org.apache.sshd.client.session.ClientSession;
import org.apache.sshd.common.NamedResource;
import org.apache.sshd.common.channel.PtyMode;
import org.apache.sshd.common.config.keys.FilePasswordProvider;
import org.apache.sshd.common.session.SessionContext;
import org.apache.sshd.common.util.io.NoCloseInputStream;
import org.apache.sshd.common.util.io.NoCloseOutputStream;
import org.apache.sshd.scp.server.ScpCommandFactory;
import org.apache.sshd.server.SshServer;
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
import org.apache.sshd.server.scp.ScpCommandFactory;
import org.apache.sshd.server.session.ServerSession;
import org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory;
import org.apache.sshd.sftp.server.SftpSubsystemFactory;
import org.jline.builtins.Options;
import org.jline.builtins.Options.HelpException;
import org.jline.reader.LineReader;
Expand Down Expand Up @@ -362,7 +364,7 @@ private void start() throws IOException {
server.setHost(ip);
server.setShellFactory(new ShellFactoryImpl(shell));
server.setCommandFactory(new ScpCommandFactory.Builder()
.withDelegate(command -> new ShellCommand(execute, command)).build());
.withDelegate((channel, command) -> new ShellCommand(execute, command)).build());
server.setSubsystemFactories(Collections.singletonList(
new SftpSubsystemFactory.Builder().build()
));
Expand Down Expand Up @@ -390,7 +392,7 @@ public JLineUserInteraction(Terminal terminal, LineReader reader, PrintStream st
}

@Override
public String getPassword(String resourceKey) throws IOException {
public String getPassword(SessionContext session, NamedResource resourceKey, int retryIndex) throws IOException {
return readLine("Enter password for " + resourceKey + ":", false);
}

Expand Down

0 comments on commit 9908d12

Please sign in to comment.