James Moger
2014-03-14 8982e6e0738c6991b9a4b864423bd4f75383c7f4
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  *
33  * @author James Moger
34  *
35  */
36 public class SshSessionFactory extends SessionFactory {
37
38     private final Logger log = LoggerFactory.getLogger(getClass());
39
0984f6 40     public SshSessionFactory() {
924c9b 41     }
JM 42
43     @Override
9ba6bc 44     protected AbstractSession createSession(final IoSession io)
DO 45             throws Exception {
924c9b 46         log.info("connection accepted on " + io);
JM 47
9ba6bc 48         if (io instanceof MinaSession) {
DO 49             if (((MinaSession) io).getSession().getConfig() instanceof SocketSessionConfig) {
50                 ((SocketSessionConfig) ((MinaSession) io).getSession()
51                         .getConfig()).setKeepAlive(true);
52             }
924c9b 53         }
JM 54
a8dd37 55         final GitblitServerSession session = (GitblitServerSession) super
9ba6bc 56                 .createSession(io);
924c9b 57         SocketAddress peer = io.getRemoteAddress();
a8dd37 58         SshDaemonClient client = new SshDaemonClient(peer);
JM 59         session.setAttribute(SshDaemonClient.KEY, client);
924c9b 60
9ba6bc 61         // TODO(davido): Log a session close without authentication as a
DO 62         // failure.
a8dd37 63         session.addCloseSessionListener(new SshFutureListener<CloseFuture>() {
924c9b 64             @Override
9ba6bc 65             public void operationComplete(CloseFuture future) {
924c9b 66                 log.info("connection closed on " + io);
JM 67             }
68         });
a8dd37 69         return session;
924c9b 70     }
9ba6bc 71
DO 72     @Override
73     protected AbstractSession doCreateSession(IoSession ioSession)
74             throws Exception {
75         return new GitblitServerSession(server, ioSession);
76     }
924c9b 77 }