Search in sources :

Example 1 with CloseFuture

use of org.apache.sshd.common.future.CloseFuture in project gitblit by gitblit.

the class SshServerSessionFactory method createSession.

@Override
protected AbstractSession createSession(final IoSession io) throws Exception {
    log.info("creating ssh session from {}", io.getRemoteAddress());
    if (io instanceof MinaSession) {
        if (((MinaSession) io).getSession().getConfig() instanceof SocketSessionConfig) {
            ((SocketSessionConfig) ((MinaSession) io).getSession().getConfig()).setKeepAlive(true);
        }
    }
    final SshServerSession session = (SshServerSession) super.createSession(io);
    SocketAddress peer = io.getRemoteAddress();
    SshDaemonClient client = new SshDaemonClient(peer);
    session.setAttribute(SshDaemonClient.KEY, client);
    // TODO(davido): Log a session close without authentication as a
    // failure.
    session.addCloseSessionListener(new SshFutureListener<CloseFuture>() {

        @Override
        public void operationComplete(CloseFuture future) {
            log.info("closed ssh session from {}", io.getRemoteAddress());
        }
    });
    return session;
}
Also used : CloseFuture(org.apache.sshd.common.future.CloseFuture) MinaSession(org.apache.sshd.common.io.mina.MinaSession) SocketAddress(java.net.SocketAddress) SocketSessionConfig(org.apache.mina.transport.socket.SocketSessionConfig)

Example 2 with CloseFuture

use of org.apache.sshd.common.future.CloseFuture in project gerrit by GerritCodeReview.

the class CloseConnection method run.

@Override
protected void run() throws Failure {
    IoAcceptor acceptor = sshDaemon.getIoAcceptor();
    if (acceptor == null) {
        throw new Failure(1, "fatal: sshd no longer running");
    }
    for (String sessionId : sessionIds) {
        boolean connectionFound = false;
        int id = (int) Long.parseLong(sessionId, 16);
        for (IoSession io : acceptor.getManagedSessions().values()) {
            AbstractSession serverSession = AbstractSession.getSession(io, true);
            SshSession sshSession = serverSession != null ? serverSession.getAttribute(SshSession.KEY) : null;
            if (sshSession != null && sshSession.getSessionId() == id) {
                connectionFound = true;
                stdout.println("closing connection " + sessionId + "...");
                CloseFuture future = io.close(true);
                if (wait) {
                    try {
                        future.await();
                        stdout.println("closed connection " + sessionId);
                    } catch (IOException e) {
                        log.warn("Wait for connection to close interrupted: " + e.getMessage());
                    }
                }
                break;
            }
        }
        if (!connectionFound) {
            stderr.print("close connection " + sessionId + ": no such connection\n");
        }
    }
}
Also used : CloseFuture(org.apache.sshd.common.future.CloseFuture) IoAcceptor(org.apache.sshd.common.io.IoAcceptor) IOException(java.io.IOException) SshSession(com.google.gerrit.sshd.SshSession) IoSession(org.apache.sshd.common.io.IoSession) AbstractSession(org.apache.sshd.common.session.helpers.AbstractSession)

Example 3 with CloseFuture

use of org.apache.sshd.common.future.CloseFuture in project gerrit by GerritCodeReview.

the class SshUtil method success.

public static boolean success(final String username, final ServerSession session, final SshScope sshScope, final SshLog sshLog, final SshSession sd, final CurrentUser user) {
    if (sd.getUser() == null) {
        sd.authenticationSuccess(username, user);
        // If this is the first time we've authenticated this
        // session, record a login event in the log and add
        // a close listener to record a logout event.
        //
        Context ctx = sshScope.newContext(null, sd, null);
        Context old = sshScope.set(ctx);
        try {
            sshLog.onLogin();
        } finally {
            sshScope.set(old);
        }
        session.addCloseFutureListener(new SshFutureListener<CloseFuture>() {

            @Override
            public void operationComplete(CloseFuture future) {
                final Context ctx = sshScope.newContext(null, sd, null);
                final Context old = sshScope.set(ctx);
                try {
                    sshLog.onLogout();
                } finally {
                    sshScope.set(old);
                }
            }
        });
    }
    return true;
}
Also used : Context(com.google.gerrit.sshd.SshScope.Context) CloseFuture(org.apache.sshd.common.future.CloseFuture)

Aggregations

CloseFuture (org.apache.sshd.common.future.CloseFuture)3 Context (com.google.gerrit.sshd.SshScope.Context)1 SshSession (com.google.gerrit.sshd.SshSession)1 IOException (java.io.IOException)1 SocketAddress (java.net.SocketAddress)1 SocketSessionConfig (org.apache.mina.transport.socket.SocketSessionConfig)1 IoAcceptor (org.apache.sshd.common.io.IoAcceptor)1 IoSession (org.apache.sshd.common.io.IoSession)1 MinaSession (org.apache.sshd.common.io.mina.MinaSession)1 AbstractSession (org.apache.sshd.common.session.helpers.AbstractSession)1