Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
terefang opened this issue Nov 19, 2019 · 1 comment

Comments

@terefang
Copy link

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"));
        }
    }
}
@terefang terefang changed the title ssh support stuck at apache sshd 1.0 does interop with newer ssh clients ssh support stuck at apache sshd 1.0 does not interop with newer ssh clients Nov 19, 2019
@stalep
Copy link
Member

stalep commented Jun 4, 2020

Hi, sorry I've managed to miss this. I'll have a look!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants