Search in sources :

Example 1 with DefaultConfig

use of net.schmizz.sshj.DefaultConfig in project dbeaver by dbeaver.

the class SSHImplementationSshj method setupTunnel.

@Override
protected void setupTunnel(DBRProgressMonitor monitor, DBWHandlerConfiguration configuration, String dbHost, String sshHost, String aliveInterval, int sshPortNum, File privKeyFile, int connectTimeout, int dbPort, int localPort) throws DBException, IOException {
    try {
        Config clientConfig = new DefaultConfig();
        clientConfig.setLoggerFactory(LoggerFactory.DEFAULT);
        sshClient = new SSHClient(clientConfig);
        // TODO: make real host verifier
        sshClient.addHostKeyVerifier(new PromiscuousVerifier());
        String sshUser = configuration.getUserName();
        String sshPassword = configuration.getPassword();
        try {
            sshClient.loadKnownHosts();
        } catch (IOException e) {
            log.warn("Error loading known hosts", e);
        }
        sshClient.connect(sshHost);
        if (privKeyFile != null) {
            if (!CommonUtils.isEmpty(sshPassword)) {
                KeyProvider keyProvider = sshClient.loadKeys(privKeyFile.getAbsolutePath(), sshPassword.toCharArray());
                sshClient.authPublickey(sshUser, keyProvider);
            } else {
                sshClient.authPublickey(sshUser, privKeyFile.getAbsolutePath());
            }
        } else {
            sshClient.authPassword(sshUser, sshPassword);
        }
        log.debug("Instantiate SSH tunnel");
        final LocalPortForwarder.Parameters params = new LocalPortForwarder.Parameters(SSHConstants.LOCALHOST_NAME, localPort, dbHost, dbPort);
        portListener = new LocalPortListener(params);
        portListener.start();
        RuntimeUtils.pause(100);
    } catch (Exception e) {
        throw new DBException("Cannot establish tunnel", e);
    }
}
Also used : KeyProvider(net.schmizz.sshj.userauth.keyprovider.KeyProvider) PromiscuousVerifier(net.schmizz.sshj.transport.verification.PromiscuousVerifier) LocalPortForwarder(net.schmizz.sshj.connection.channel.direct.LocalPortForwarder) DBException(org.jkiss.dbeaver.DBException) DefaultConfig(net.schmizz.sshj.DefaultConfig) Config(net.schmizz.sshj.Config) IOException(java.io.IOException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBException(org.jkiss.dbeaver.DBException) DefaultConfig(net.schmizz.sshj.DefaultConfig) SSHClient(net.schmizz.sshj.SSHClient)

Aggregations

IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Config (net.schmizz.sshj.Config)1 DefaultConfig (net.schmizz.sshj.DefaultConfig)1 SSHClient (net.schmizz.sshj.SSHClient)1 LocalPortForwarder (net.schmizz.sshj.connection.channel.direct.LocalPortForwarder)1 PromiscuousVerifier (net.schmizz.sshj.transport.verification.PromiscuousVerifier)1 KeyProvider (net.schmizz.sshj.userauth.keyprovider.KeyProvider)1 DBException (org.jkiss.dbeaver.DBException)1