package org.opendaylight.netconf.ssh;
-import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
+import java.nio.charset.StandardCharsets;
import org.apache.sshd.common.io.IoInputStream;
import org.apache.sshd.common.io.IoOutputStream;
import org.apache.sshd.server.ExitCallback;
+import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader;
import org.opendaylight.netconf.nettyutil.handler.ssh.client.AsyncSshHandlerReader;
import org.opendaylight.netconf.nettyutil.handler.ssh.client.AsyncSshHandlerWriter;
-import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
- * Netty handler that reads SSH from remote client and writes to delegate server and reads from delegate server and writes to remote client
+ * Netty handler that reads SSH from remote client and writes to delegate server
+ * and reads from delegate server and writes to remote client.
*/
final class SshProxyClientHandler extends ChannelInboundHandlerAdapter {
private final NetconfHelloMessageAdditionalHeader netconfHelloMessageAdditionalHeader;
private final ExitCallback callback;
- public SshProxyClientHandler(final IoInputStream in, final IoOutputStream out,
- final NetconfHelloMessageAdditionalHeader netconfHelloMessageAdditionalHeader,
- final ExitCallback callback) {
+ SshProxyClientHandler(final IoInputStream in, final IoOutputStream out,
+ final NetconfHelloMessageAdditionalHeader netconfHelloMessageAdditionalHeader,
+ final ExitCallback callback) {
this.in = in;
this.out = out;
this.netconfHelloMessageAdditionalHeader = netconfHelloMessageAdditionalHeader;
writeAdditionalHeader(ctx);
asyncSshHandlerWriter = new AsyncSshHandlerWriter(out);
- asyncSshHandlerReader = new AsyncSshHandlerReader(new AutoCloseable() {
- @Override
- public void close() throws Exception {
- // Close both sessions (delegate server and remote client)
- ctx.fireChannelInactive();
- ctx.disconnect();
- ctx.close();
- asyncSshHandlerReader.close();
- asyncSshHandlerWriter.close();
- }
- }, new AsyncSshHandlerReader.ReadMsgHandler() {
- @Override
- public void onMessageRead(final ByteBuf msg) {
- if(LOG.isTraceEnabled()) {
- LOG.trace("Forwarding message for client: {} on channel: {}, message: {}",
- netconfHelloMessageAdditionalHeader.getAddress(), ctx.channel(), AsyncSshHandlerWriter.byteBufToString(msg));
- }
- // Just forward to delegate
- ctx.writeAndFlush(msg);
+ asyncSshHandlerReader = new AsyncSshHandlerReader(() -> {
+ // Close both sessions (delegate server and remote client)
+ ctx.fireChannelInactive();
+ ctx.disconnect();
+ ctx.close();
+ asyncSshHandlerReader.close();
+ asyncSshHandlerWriter.close();
+ }, msg -> {
+ if (LOG.isTraceEnabled()) {
+ LOG.trace("Forwarding message for client: {} on channel: {}, message: {}",
+ netconfHelloMessageAdditionalHeader.getAddress(), ctx.channel(),
+ AsyncSshHandlerWriter.byteBufToString(msg));
}
+ // Just forward to delegate
+ ctx.writeAndFlush(msg);
}, "ssh" + netconfHelloMessageAdditionalHeader.getAddress(), in);
}
private void writeAdditionalHeader(final ChannelHandlerContext ctx) {
- ctx.writeAndFlush(Unpooled.copiedBuffer(netconfHelloMessageAdditionalHeader.toFormattedString().getBytes()));
+ ctx.writeAndFlush(Unpooled.copiedBuffer(netconfHelloMessageAdditionalHeader.toFormattedString()
+ .getBytes(StandardCharsets.UTF_8)));
}
@Override
- public void channelRead(final ChannelHandlerContext ctx, final Object msg) throws Exception {
+ public void channelRead(final ChannelHandlerContext ctx, final Object msg) {
asyncSshHandlerWriter.write(ctx, msg, ctx.newPromise());
}
@Override
public void channelInactive(final ChannelHandlerContext ctx) throws Exception {
- LOG.debug("Internal connection to netconf server was dropped for client: {} on channel: ",
+ LOG.debug("Internal connection to netconf server was dropped for client: {} on channel: {}",
netconfHelloMessageAdditionalHeader.getAddress(), ctx.channel());
- callback.onExit(1, "Internal connection to netconf server was dropped for client: " +
- netconfHelloMessageAdditionalHeader.getAddress() + " on channel: " + ctx.channel());
+ callback.onExit(1, "Internal connection to netconf server was dropped for client: "
+ + netconfHelloMessageAdditionalHeader.getAddress() + " on channel: " + ctx.channel());
super.channelInactive(ctx);
}
-
-
}