Skip to content

ssh support stuck at apache sshd 1.0 does not interop with newer ssh clients #51

@terefang

Description

@terefang

keeping SSHAttributesBuilder, SSHDevice and TtyCommand delete the netty sub-package and use the following bridge class :

package org.aesh.terminal.ssh.mina;

import org.aesh.terminal.Connection;
import org.aesh.terminal.ssh.TtyCommand;

import org.aesh.terminal.telnet.util.Helper;
import org.apache.sshd.common.keyprovider.KeyPairProvider;
import org.apache.sshd.server.SshServer;
import org.apache.sshd.server.auth.password.PasswordAuthenticator;
import org.apache.sshd.server.auth.pubkey.PublickeyAuthenticator;
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;

/**
 * @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
 */
public class SshTtyBootstrap {

    private String host;
    private int port;
    private Charset charset;

    private SshServer server;
    private KeyPairProvider keyPairProvider;
    private PasswordAuthenticator passwordAuthenticator;
    private PublickeyAuthenticator publicKeyAuthenticator;

    public SshTtyBootstrap() {
        this.host = "localhost";
        this.port = 5000;
        this.charset = StandardCharsets.UTF_8;

        this.keyPairProvider = new SimpleGeneratorHostKeyProvider(new File("hostkey.ser").toPath());
        this.passwordAuthenticator = (username, password, session) -> true;
    }

    public String getHost() {
        return host;
    }

    public SshTtyBootstrap setHost(String host) {
        this.host = host;
        return this;
    }

    public int getPort() {
        return port;
    }

    public SshTtyBootstrap setPort(int port) {
        this.port = port;
        return this;
    }

    public SshTtyBootstrap setPasswordAuthenticator(PasswordAuthenticator passwordAuthenticator) {
        this.passwordAuthenticator = passwordAuthenticator;
        return this;
    }

    public SshTtyBootstrap setPublicKeyAuthenticator(PublickeyAuthenticator publicKeyAuthenticator) {
        this.publicKeyAuthenticator = publicKeyAuthenticator;
        return this;
    }

    public CompletableFuture<Void> start(Consumer<Connection> handler) throws Exception {
        CompletableFuture<Void> fut = new CompletableFuture<>();
        start(handler, Helper.startedHandler(fut));
        return fut;
    }

    public KeyPairProvider getKeyPairProvider() {
        return keyPairProvider;
    }

    public SshTtyBootstrap setKeyPairProvider(KeyPairProvider keyPairProvider) {
        this.keyPairProvider = keyPairProvider;
        return this;
    }

    public Charset getCharset() {
        return charset;
    }

    public void setCharset(Charset charset) {
        this.charset = charset;
    }

    public void start(Consumer<Connection> factory, Consumer<Throwable> doneHandler) {
        server = SshServer.setUpDefaultServer();
        server.setPort(port);
        server.setHost(host);
        server.setKeyPairProvider(keyPairProvider);
        server.setPasswordAuthenticator(passwordAuthenticator);
        if (publicKeyAuthenticator != null) {
            server.setPublickeyAuthenticator(publicKeyAuthenticator);
        }
        server.setShellFactory((channelSession) -> new TtyCommand(charset, factory));
        try {
            server.start();
        } catch (Exception e) {
            doneHandler.accept(e);
            return;
        }
        doneHandler.accept(null);
    }

    public CompletableFuture<Void> stop() throws InterruptedException {
        CompletableFuture<Void> fut = new CompletableFuture<>();
        stop(Helper.stoppedHandler(fut));
        return fut;
    }

    public void stop(Consumer<Throwable> doneHandler) {
        if (server != null) {
            try {
                server.stop();
            } catch (IOException e) {
                doneHandler.accept(e);
                return;
            }
            doneHandler.accept(null);
        } else {
            doneHandler.accept(new IllegalStateException("Server not started"));
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions