James Moger
2015-11-22 ed552ba47c02779c270ffd62841d6d1048dade70
commit | author | age
924c9b 1 /*
JM 2  * Copyright 2014 gitblit.com.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.gitblit.transport.ssh;
17
18 import java.net.SocketAddress;
19
20 import org.apache.mina.transport.socket.SocketSessionConfig;
9ba6bc 21 import org.apache.sshd.common.future.CloseFuture;
DO 22 import org.apache.sshd.common.future.SshFutureListener;
23 import org.apache.sshd.common.io.IoSession;
24 import org.apache.sshd.common.io.mina.MinaSession;
25 import org.apache.sshd.common.session.AbstractSession;
924c9b 26 import org.apache.sshd.server.session.SessionFactory;
JM 27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  *
32  * @author James Moger
33  *
34  */
61b32f 35 public class SshServerSessionFactory extends SessionFactory {
924c9b 36
JM 37     private final Logger log = LoggerFactory.getLogger(getClass());
38
61b32f 39     public SshServerSessionFactory() {
924c9b 40     }
JM 41
42     @Override
61b32f 43     protected AbstractSession createSession(final IoSession io) throws Exception {
521cb6 44         log.info("creating ssh session from {}", io.getRemoteAddress());
924c9b 45
9ba6bc 46         if (io instanceof MinaSession) {
DO 47             if (((MinaSession) io).getSession().getConfig() instanceof SocketSessionConfig) {
61b32f 48                 ((SocketSessionConfig) ((MinaSession) io).getSession().getConfig()).setKeepAlive(true);
9ba6bc 49             }
924c9b 50         }
JM 51
61b32f 52         final SshServerSession session = (SshServerSession) super.createSession(io);
924c9b 53         SocketAddress peer = io.getRemoteAddress();
a8dd37 54         SshDaemonClient client = new SshDaemonClient(peer);
JM 55         session.setAttribute(SshDaemonClient.KEY, client);
924c9b 56
9ba6bc 57         // TODO(davido): Log a session close without authentication as a
DO 58         // failure.
a8dd37 59         session.addCloseSessionListener(new SshFutureListener<CloseFuture>() {
924c9b 60             @Override
9ba6bc 61             public void operationComplete(CloseFuture future) {
521cb6 62                 log.info("closed ssh session from {}", io.getRemoteAddress());
924c9b 63             }
JM 64         });
a8dd37 65         return session;
924c9b 66     }
9ba6bc 67
DO 68     @Override
61b32f 69     protected AbstractSession doCreateSession(IoSession ioSession) throws Exception {
d41034 70         return new SshServerSession(getServer(), ioSession);
9ba6bc 71     }
924c9b 72 }