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
8 package org.opendaylight.netconf.ssh;
10 import io.netty.bootstrap.Bootstrap;
11 import io.netty.buffer.Unpooled;
12 import io.netty.channel.Channel;
13 import io.netty.channel.ChannelFuture;
14 import io.netty.channel.ChannelHandlerContext;
15 import io.netty.channel.ChannelInboundHandlerAdapter;
16 import io.netty.channel.ChannelInitializer;
17 import io.netty.channel.EventLoopGroup;
18 import io.netty.channel.local.LocalAddress;
19 import io.netty.channel.local.LocalChannel;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 public class ProxyServerHandler extends ChannelInboundHandlerAdapter {
24 private static final Logger LOG = LoggerFactory.getLogger(ProxyServerHandler.class);
25 private final Bootstrap clientBootstrap;
26 private final LocalAddress localAddress;
29 private Channel clientChannel;
31 public ProxyServerHandler(final EventLoopGroup bossGroup, final LocalAddress localAddress) {
32 clientBootstrap = new Bootstrap();
33 clientBootstrap.group(bossGroup).channel(LocalChannel.class);
34 this.localAddress = localAddress;
38 public void channelActive(final ChannelHandlerContext remoteCtx) {
39 final ProxyClientHandler clientHandler = new ProxyClientHandler(remoteCtx);
40 clientBootstrap.handler(new ChannelInitializer<LocalChannel>() {
42 public void initChannel(final LocalChannel ch) {
43 ch.pipeline().addLast(clientHandler);
46 ChannelFuture clientChannelFuture = clientBootstrap.connect(localAddress).awaitUninterruptibly();
47 clientChannel = clientChannelFuture.channel();
48 clientChannel.writeAndFlush(Unpooled.copiedBuffer("connected\n".getBytes()));
52 public void channelInactive(final ChannelHandlerContext ctx) {
53 LOG.info("channelInactive - closing client connection");
54 clientChannel.close();
58 public void channelRead(final ChannelHandlerContext ctx, final Object msg) {
59 LOG.debug("Writing to client {}", msg);
60 clientChannel.write(msg);
64 public void channelReadComplete(final ChannelHandlerContext ctx) {
65 LOG.debug("flushing");
66 clientChannel.flush();
70 public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable cause) {
71 // Close the connection when an exception is raised.
72 LOG.warn("Unexpected exception from downstream.", cause);