2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.netconf.ssh;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.annotations.VisibleForTesting;
14 import com.google.common.collect.ImmutableList;
15 import io.netty.channel.EventLoopGroup;
16 import java.io.IOException;
17 import java.nio.channels.AsynchronousChannelGroup;
18 import java.util.List;
19 import java.util.concurrent.ExecutorService;
20 import java.util.concurrent.ScheduledExecutorService;
21 import java.util.concurrent.TimeUnit;
22 import org.opendaylight.netconf.shaded.sshd.common.FactoryManager;
23 import org.opendaylight.netconf.shaded.sshd.common.NamedFactory;
24 import org.opendaylight.netconf.shaded.sshd.common.RuntimeSshException;
25 import org.opendaylight.netconf.shaded.sshd.common.cipher.BuiltinCiphers;
26 import org.opendaylight.netconf.shaded.sshd.common.cipher.Cipher;
27 import org.opendaylight.netconf.shaded.sshd.common.io.IoAcceptor;
28 import org.opendaylight.netconf.shaded.sshd.common.io.IoConnector;
29 import org.opendaylight.netconf.shaded.sshd.common.io.IoHandler;
30 import org.opendaylight.netconf.shaded.sshd.common.io.IoServiceEventListener;
31 import org.opendaylight.netconf.shaded.sshd.common.io.IoServiceFactory;
32 import org.opendaylight.netconf.shaded.sshd.common.io.IoServiceFactoryFactory;
33 import org.opendaylight.netconf.shaded.sshd.common.io.nio2.Nio2Acceptor;
34 import org.opendaylight.netconf.shaded.sshd.common.io.nio2.Nio2Connector;
35 import org.opendaylight.netconf.shaded.sshd.common.io.nio2.Nio2ServiceFactoryFactory;
36 import org.opendaylight.netconf.shaded.sshd.common.session.SessionHeartbeatController.HeartbeatType;
37 import org.opendaylight.netconf.shaded.sshd.common.util.closeable.AbstractCloseable;
38 import org.opendaylight.netconf.shaded.sshd.server.SshServer;
41 * Proxy SSH server that just delegates decrypted content to a delegate server within same VM.
42 * Implemented using Apache Mina SSH lib.
44 public class SshProxyServer implements AutoCloseable {
45 private final SshServer sshServer;
46 private final ScheduledExecutorService minaTimerExecutor;
47 private final EventLoopGroup clientGroup;
48 private final IoServiceFactoryFactory nioServiceWithPoolFactoryFactory;
50 private SshProxyServer(final ScheduledExecutorService minaTimerExecutor, final EventLoopGroup clientGroup,
51 final IoServiceFactoryFactory serviceFactory) {
52 this.minaTimerExecutor = minaTimerExecutor;
53 this.clientGroup = clientGroup;
54 this.nioServiceWithPoolFactoryFactory = serviceFactory;
55 this.sshServer = SshServer.setUpDefaultServer();
58 public SshProxyServer(final ScheduledExecutorService minaTimerExecutor,
59 final EventLoopGroup clientGroup, final ExecutorService nioExecutor) {
60 this(minaTimerExecutor, clientGroup, new NioServiceWithPoolFactoryFactory(nioExecutor));
64 * Create a server with a shared {@link AsynchronousChannelGroup}. Unlike the other constructor, this does
65 * not create a dedicated thread group, which is useful when you need to start a large number of servers and do
66 * not want to have a thread group (and hence an anonyous thread) for each of them.
69 public SshProxyServer(final ScheduledExecutorService minaTimerExecutor, final EventLoopGroup clientGroup,
70 final AsynchronousChannelGroup group) {
71 this(minaTimerExecutor, clientGroup, new SharedNioServiceFactoryFactory(group));
74 public void bind(final SshProxyServerConfiguration sshProxyServerConfiguration) throws IOException {
75 sshServer.setHost(sshProxyServerConfiguration.getBindingAddress().getHostString());
76 sshServer.setPort(sshProxyServerConfiguration.getBindingAddress().getPort());
79 final List<NamedFactory<Cipher>> cipherFactories = sshServer.getCipherFactories();
80 cipherFactories.removeIf(factory -> factory.getName().contains(BuiltinCiphers.arcfour128.getName())
81 || factory.getName().contains(BuiltinCiphers.arcfour256.getName()));
82 sshServer.setPasswordAuthenticator(
83 (username, password, session)
84 -> sshProxyServerConfiguration.getAuthenticator().authenticated(username, password));
86 sshProxyServerConfiguration.getPublickeyAuthenticator().ifPresent(sshServer::setPublickeyAuthenticator);
88 sshServer.setKeyPairProvider(sshProxyServerConfiguration.getKeyPairProvider());
90 sshServer.setIoServiceFactoryFactory(nioServiceWithPoolFactoryFactory);
91 sshServer.setScheduledExecutorService(minaTimerExecutor);
93 final int idleTimeout = sshProxyServerConfiguration.getIdleTimeout();
94 sshServer.getProperties().put(FactoryManager.IDLE_TIMEOUT, String.valueOf(idleTimeout));
95 final String nioReadTimeout;
96 if (idleTimeout > 0) {
97 final long heartBeat = idleTimeout * 333333L;
98 sshServer.setSessionHeartbeat(HeartbeatType.IGNORE, TimeUnit.NANOSECONDS, heartBeat);
99 nioReadTimeout = String.valueOf(idleTimeout + TimeUnit.SECONDS.toMillis(15L));
101 nioReadTimeout = "0";
103 sshServer.getProperties().put(FactoryManager.NIO2_READ_TIMEOUT, nioReadTimeout);
104 sshServer.getProperties().put(FactoryManager.AUTH_TIMEOUT, String.valueOf(idleTimeout));
106 final RemoteNetconfCommand.NetconfCommandFactory netconfCommandFactory =
107 new RemoteNetconfCommand.NetconfCommandFactory(clientGroup,
108 sshProxyServerConfiguration.getLocalAddress());
109 sshServer.setSubsystemFactories(ImmutableList.of(netconfCommandFactory));
114 public void close() throws IOException {
116 sshServer.stop(true);
118 sshServer.close(true);
122 private abstract static class AbstractNioServiceFactory extends AbstractCloseable implements IoServiceFactory {
123 private final FactoryManager manager;
124 private final AsynchronousChannelGroup group;
126 private IoServiceEventListener eventListener;
128 AbstractNioServiceFactory(final FactoryManager manager, final AsynchronousChannelGroup group) {
129 this.manager = requireNonNull(manager);
130 this.group = requireNonNull(group);
133 final AsynchronousChannelGroup group() {
138 public final IoConnector createConnector(final IoHandler handler) {
139 return new Nio2Connector(manager, handler, group);
143 public final IoAcceptor createAcceptor(final IoHandler handler) {
144 return new Nio2Acceptor(manager, handler, group);
148 public final IoServiceEventListener getIoServiceEventListener() {
149 return eventListener;
153 public final void setIoServiceEventListener(final IoServiceEventListener listener) {
154 eventListener = listener;
159 * Based on Nio2ServiceFactory with one addition: injectable executor.
161 private static final class NioServiceWithPoolFactory extends AbstractNioServiceFactory {
162 NioServiceWithPoolFactory(final FactoryManager manager, final AsynchronousChannelGroup group) {
163 super(manager, group);
167 protected void doCloseImmediately() {
169 group().shutdownNow();
170 group().awaitTermination(5, TimeUnit.SECONDS);
171 } catch (final IOException | InterruptedException e) {
172 log.debug("Exception caught while closing channel group", e);
174 super.doCloseImmediately();
179 private static final class NioServiceWithPoolFactoryFactory extends Nio2ServiceFactoryFactory {
180 private final ExecutorService nioExecutor;
182 NioServiceWithPoolFactoryFactory(final ExecutorService nioExecutor) {
183 this.nioExecutor = nioExecutor;
187 public IoServiceFactory create(final FactoryManager manager) {
189 return new NioServiceWithPoolFactory(manager, AsynchronousChannelGroup.withThreadPool(nioExecutor));
190 } catch (final IOException e) {
191 throw new RuntimeSshException("Failed to create channel group", e);
196 private static final class SharedNioServiceFactory extends AbstractNioServiceFactory {
197 SharedNioServiceFactory(final FactoryManager manager, final AsynchronousChannelGroup group) {
198 super(manager, group);
202 private static final class SharedNioServiceFactoryFactory extends Nio2ServiceFactoryFactory {
203 private final AsynchronousChannelGroup group;
205 SharedNioServiceFactoryFactory(final AsynchronousChannelGroup group) {
206 this.group = requireNonNull(group);
210 public IoServiceFactory create(final FactoryManager manager) {
211 return new SharedNioServiceFactory(manager, group);